Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class is to build the form for adding Group | |
38 | */ | |
39 | class CRM_Group_Form_Edit extends CRM_Core_Form { | |
40 | ||
41 | /** | |
100fef9d | 42 | * The group id, used when editing a group |
6a488035 TO |
43 | * |
44 | * @var int | |
45 | */ | |
46 | protected $_id; | |
47 | ||
48 | /** | |
100fef9d | 49 | * The group object, if an id is present |
6a488035 TO |
50 | * |
51 | * @var object | |
52 | */ | |
53 | protected $_group; | |
54 | ||
55 | /** | |
56 | * The title of the group being deleted | |
57 | * | |
58 | * @var string | |
59 | */ | |
60 | protected $_title; | |
61 | ||
62 | /** | |
63 | * Store the group values | |
64 | * | |
65 | * @var array | |
66 | */ | |
67 | protected $_groupValues; | |
68 | ||
69 | /** | |
100fef9d | 70 | * What blocks should we show and hide. |
6a488035 TO |
71 | * |
72 | * @var CRM_Core_ShowHideBlocks | |
73 | */ | |
74 | protected $_showHide; | |
75 | ||
76 | /** | |
100fef9d | 77 | * The civicrm_group_organization table id |
6a488035 TO |
78 | * |
79 | * @var int | |
80 | */ | |
81 | protected $_groupOrganizationID; | |
82 | ||
83 | /** | |
100fef9d | 84 | * Set up variables to build the form |
6a488035 TO |
85 | * |
86 | * @return void | |
87 | * @acess protected | |
88 | */ | |
00be9182 | 89 | public function preProcess() { |
6a488035 | 90 | $this->_id = $this->get('id'); |
6a488035 | 91 | if ($this->_id) { |
5c61c747 TO |
92 | $breadCrumb = array(array( |
93 | 'title' => ts('Manage Groups'), | |
6a488035 TO |
94 | 'url' => CRM_Utils_System::url('civicrm/group', |
95 | 'reset=1' | |
96 | ), | |
97 | )); | |
98 | CRM_Utils_System::appendBreadCrumb($breadCrumb); | |
99 | ||
100 | $this->_groupValues = array(); | |
101 | $params = array('id' => $this->_id); | |
f828fa2c | 102 | $this->_group = CRM_Contact_BAO_Group::retrieve($params, $this->_groupValues); |
6a488035 TO |
103 | $this->_title = $this->_groupValues['title']; |
104 | } | |
105 | ||
106 | $this->assign('action', $this->_action); | |
107 | $this->assign('showBlockJS', TRUE); | |
108 | ||
109 | if ($this->_action == CRM_Core_Action::DELETE) { | |
110 | if (isset($this->_id)) { | |
111 | $this->assign('title', $this->_title); | |
112 | $this->assign('count', CRM_Contact_BAO_Group::memberCount($this->_id)); | |
113 | CRM_Utils_System::setTitle(ts('Confirm Group Delete')); | |
114 | } | |
bf4b6f8f JP |
115 | if ($this->_groupValues['is_reserved'] == 1 && !CRM_Core_Permission::check('administer reserved groups')) { |
116 | CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to delete this reserved group.")); | |
117 | } | |
6a488035 TO |
118 | } |
119 | else { | |
bf4b6f8f JP |
120 | if ($this->_groupValues['is_reserved'] == 1 && !CRM_Core_Permission::check('administer reserved groups')) { |
121 | CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to change settings for this reserved group.")); | |
122 | } | |
6a488035 TO |
123 | if (isset($this->_id)) { |
124 | $groupValues = array( | |
125 | 'id' => $this->_id, | |
126 | 'title' => $this->_title, | |
127 | 'saved_search_id' => | |
5c61c747 | 128 | isset($this->_groupValues['saved_search_id']) ? $this->_groupValues['saved_search_id'] : '', |
6a488035 TO |
129 | ); |
130 | if (isset($this->_groupValues['saved_search_id'])) { | |
131 | $groupValues['mapping_id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', | |
132 | $this->_groupValues['saved_search_id'], | |
133 | 'mapping_id' | |
134 | ); | |
135 | $groupValues['search_custom_id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', | |
136 | $this->_groupValues['saved_search_id'], | |
137 | 'search_custom_id' | |
138 | ); | |
139 | } | |
5c61c747 | 140 | if (!empty($this->_groupValues['created_id'])) { |
f828fa2c | 141 | $groupValues['created_by'] = |
5c61c747 TO |
142 | CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['created_id'], 'sort_name', 'id'); |
143 | } | |
6a488035 | 144 | |
a7488080 | 145 | if (!empty($this->_groupValues['modified_id'])) { |
d0dfb649 | 146 | $groupValues['modified_by'] = |
5c61c747 | 147 | CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['modified_id'], 'sort_name', 'id'); |
d0dfb649 PJ |
148 | } |
149 | ||
6a488035 TO |
150 | $this->assign_by_ref('group', $groupValues); |
151 | ||
152 | CRM_Utils_System::setTitle(ts('Group Settings: %1', array(1 => $this->_title))); | |
153 | } | |
154 | $session = CRM_Core_Session::singleton(); | |
155 | $session->pushUserContext(CRM_Utils_System::url('civicrm/group', 'reset=1')); | |
156 | } | |
157 | ||
158 | //build custom data | |
159 | CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'Group', $this->_id); | |
160 | } | |
161 | ||
c490a46a CW |
162 | /** |
163 | * Set default values for the form. LocationType that in edit/view mode | |
6a488035 TO |
164 | * the default values are retrieved from the database |
165 | * | |
e0ef6999 EM |
166 | * @return array |
167 | */ | |
00be9182 | 168 | public function setDefaultValues() { |
6a488035 TO |
169 | $defaults = array(); |
170 | ||
171 | if (isset($this->_id)) { | |
172 | $defaults = $this->_groupValues; | |
a7488080 | 173 | if (!empty($defaults['group_type'])) { |
6a488035 TO |
174 | $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
175 | substr($defaults['group_type'], 1, -1) | |
176 | ); | |
177 | $defaults['group_type'] = array(); | |
178 | foreach ($types as $type) { | |
179 | $defaults['group_type'][$type] = 1; | |
180 | } | |
181 | } | |
182 | ||
532e9275 | 183 | if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { |
6a488035 | 184 | CRM_Contact_BAO_GroupOrganization::retrieve($this->_id, $defaults); |
6a488035 TO |
185 | } |
186 | } | |
187 | ||
188 | if (!((CRM_Core_Permission::check('access CiviMail')) || | |
189 | (CRM_Mailing_Info::workflowEnabled() && | |
190 | CRM_Core_Permission::check('create mailings') | |
191 | ) | |
192 | )) { | |
193 | $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE); | |
194 | if ($defaults['group_type'][$groupTypes['Mailing List']] == 1) { | |
195 | $this->assign('freezeMailignList', $groupTypes['Mailing List']); | |
196 | } | |
197 | else { | |
198 | $this->assign('hideMailignList', $groupTypes['Mailing List']); | |
199 | } | |
200 | } | |
201 | ||
a7488080 | 202 | if (empty($defaults['parents'])) { |
6a488035 TO |
203 | $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId(); |
204 | } | |
205 | ||
206 | // custom data set defaults | |
207 | $defaults += CRM_Custom_Form_CustomData::setDefaultValues($this); | |
208 | return $defaults; | |
209 | } | |
210 | ||
211 | /** | |
c490a46a | 212 | * Build the form object |
6a488035 | 213 | * |
355ba699 | 214 | * @return void |
6a488035 TO |
215 | */ |
216 | public function buildQuickForm() { | |
217 | if ($this->_action == CRM_Core_Action::DELETE) { | |
218 | $this->addButtons(array( | |
219 | array( | |
220 | 'type' => 'next', | |
221 | 'name' => ts('Delete Group'), | |
222 | 'isDefault' => TRUE, | |
223 | ), | |
224 | array( | |
225 | 'type' => 'cancel', | |
226 | 'name' => ts('Cancel'), | |
227 | ), | |
228 | ) | |
229 | ); | |
230 | return; | |
231 | } | |
232 | ||
cae80d9f CW |
233 | // We want the "new group" form to redirect the user |
234 | if ($this->_action == CRM_Core_Action::ADD) { | |
235 | $this->preventAjaxSubmit(); | |
236 | } | |
237 | ||
6a488035 TO |
238 | $this->applyFilter('__ALL__', 'trim'); |
239 | $this->add('text', 'title', ts('Name') . ' ', | |
240 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE | |
241 | ); | |
242 | ||
243 | $this->add('textarea', 'description', ts('Description') . ' ', | |
244 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description') | |
245 | ); | |
246 | ||
247 | $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE); | |
248 | $config = CRM_Core_Config::singleton(); | |
8cc574cf | 249 | if (isset($this->_id) && !empty($this->_groupValues['saved_search_id'])) { |
6a488035 TO |
250 | unset($groupTypes['Access Control']); |
251 | } | |
252 | ||
253 | if (!empty($groupTypes)) { | |
254 | $this->addCheckBox('group_type', | |
255 | ts('Group Type'), | |
256 | $groupTypes, | |
257 | NULL, NULL, NULL, NULL, ' ' | |
258 | ); | |
259 | } | |
260 | ||
93bfa565 | 261 | $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues::groupVisibility(), TRUE); |
6a488035 | 262 | |
31c31c92 BS |
263 | //CRM-14190 |
264 | $parentGroups = self::buildParentGroups($this); | |
6a488035 | 265 | |
532e9275 | 266 | if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { |
6a488035 | 267 | //group organization Element |
532e9275 CW |
268 | $props = array('api' => array('params' => array('contact_type' => 'Organization'))); |
269 | $this->addEntityRef('organization_id', ts('Organization'), $props); | |
6a488035 TO |
270 | } |
271 | ||
272 | // is_reserved property CRM-9936 | |
273 | $this->addElement('checkbox', 'is_reserved', ts('Reserved Group?')); | |
274 | if (!CRM_Core_Permission::check('administer reserved groups')) { | |
275 | $this->freeze('is_reserved'); | |
276 | } | |
277 | ||
278 | //build custom data | |
279 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
280 | ||
281 | $this->addButtons(array( | |
282 | array( | |
283 | 'type' => 'upload', | |
284 | 'name' => | |
5c61c747 | 285 | ($this->_action == CRM_Core_Action::ADD) ? ts('Continue') : ts('Save'), |
6a488035 TO |
286 | 'isDefault' => TRUE, |
287 | ), | |
288 | array( | |
289 | 'type' => 'cancel', | |
290 | 'name' => ts('Cancel'), | |
291 | ), | |
292 | ) | |
293 | ); | |
294 | ||
295 | $doParentCheck = FALSE; | |
296 | if (CRM_Core_Permission::isMultisiteEnabled()) { | |
297 | $doParentCheck = ($this->_id && CRM_Core_BAO_Domain::isDomainGroup($this->_id)) ? FALSE : TRUE; | |
298 | } | |
299 | ||
300 | $options = array( | |
301 | 'selfObj' => $this, | |
302 | 'parentGroups' => $parentGroups, | |
303 | 'doParentCheck' => $doParentCheck, | |
304 | ); | |
305 | $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options); | |
306 | } | |
307 | ||
308 | /** | |
100fef9d | 309 | * Global validation rules for the form |
6a488035 | 310 | * |
ed6aedaa TO |
311 | * @param array $fields |
312 | * Posted values of the form. | |
100fef9d | 313 | * @param array $fileParams |
77b97be7 EM |
314 | * @param $options |
315 | * | |
a6c01b45 CW |
316 | * @return array |
317 | * list of errors to be posted back to the form | |
6a488035 | 318 | * @static |
6a488035 | 319 | */ |
00be9182 | 320 | public static function formRule($fields, $fileParams, $options) { |
6a488035 TO |
321 | $errors = array(); |
322 | ||
323 | $doParentCheck = $options['doParentCheck']; | |
324 | $self = &$options['selfObj']; | |
325 | ||
326 | if ($doParentCheck) { | |
327 | $parentGroups = $options['parentGroups']; | |
328 | ||
329 | $grpRemove = 0; | |
330 | foreach ($fields as $key => $val) { | |
331 | if (substr($key, 0, 20) == 'remove_parent_group_') { | |
332 | $grpRemove++; | |
333 | } | |
334 | } | |
335 | ||
336 | $grpAdd = 0; | |
a7488080 | 337 | if (!empty($fields['parents'])) { |
6a488035 TO |
338 | $grpAdd++; |
339 | } | |
340 | ||
341 | if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) { | |
342 | $errors['parents'] = ts('Make sure at least one parent group is set.'); | |
343 | } | |
344 | } | |
345 | ||
346 | // do check for both name and title uniqueness | |
a7488080 | 347 | if (!empty($fields['title'])) { |
6a488035 TO |
348 | $title = trim($fields['title']); |
349 | $query = " | |
350 | SELECT count(*) | |
351 | FROM civicrm_group | |
352 | WHERE title = %1 | |
353 | "; | |
354 | $params = array(1 => array($title, 'String')); | |
355 | ||
356 | if ($self->_id) { | |
357 | $query .= "AND id <> %2"; | |
358 | $params[2] = array($self->_id, 'Integer'); | |
359 | } | |
360 | ||
361 | $grpCnt = CRM_Core_DAO::singleValueQuery($query, $params); | |
362 | if ($grpCnt) { | |
363 | $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title'])); | |
364 | } | |
365 | } | |
366 | ||
367 | return empty($errors) ? TRUE : $errors; | |
368 | } | |
369 | ||
370 | /** | |
371 | * Process the form when submitted | |
372 | * | |
373 | * @return void | |
6a488035 TO |
374 | */ |
375 | public function postProcess() { | |
376 | CRM_Utils_System::flushCache('CRM_Core_DAO_Group'); | |
377 | ||
378 | $updateNestingCache = FALSE; | |
379 | if ($this->_action & CRM_Core_Action::DELETE) { | |
380 | CRM_Contact_BAO_Group::discard($this->_id); | |
381 | CRM_Core_Session::setStatus(ts("The Group '%1' has been deleted.", array(1 => $this->_title)), ts('Group Deleted'), 'success'); | |
382 | $updateNestingCache = TRUE; | |
383 | } | |
384 | else { | |
385 | // store the submitted values in an array | |
386 | $params = $this->controller->exportValues($this->_name); | |
387 | ||
388 | $params['is_active'] = CRM_Utils_Array::value('is_active', $this->_groupValues, 1); | |
389 | ||
390 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
391 | $params['id'] = $this->_id; | |
392 | } | |
393 | ||
394 | if ($this->_action & CRM_Core_Action::UPDATE && isset($this->_groupOrganizationID)) { | |
395 | $params['group_organization'] = $this->_groupOrganizationID; | |
396 | } | |
397 | ||
398 | $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); | |
399 | ||
400 | $customFields = CRM_Core_BAO_CustomField::getFields('Group'); | |
401 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
402 | $customFields, | |
403 | $this->_id, | |
404 | 'Group' | |
405 | ); | |
406 | ||
407 | $group = CRM_Contact_BAO_Group::create($params); | |
408 | ||
c490a46a | 409 | //Remove any parent groups requested to be removed |
a7488080 | 410 | if (!empty($this->_groupValues['parents'])) { |
6a488035 TO |
411 | $parentGroupIds = explode(',', $this->_groupValues['parents']); |
412 | foreach ($parentGroupIds as $parentGroupId) { | |
413 | if (isset($params["remove_parent_group_$parentGroupId"])) { | |
414 | CRM_Contact_BAO_GroupNesting::remove($parentGroupId, $group->id); | |
415 | $updateNestingCache = TRUE; | |
416 | } | |
417 | } | |
418 | } | |
419 | ||
420 | CRM_Core_Session::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title)), ts('Group Saved'), 'success'); | |
421 | ||
c490a46a | 422 | // Add context to the session, in case we are adding members to the group |
6a488035 TO |
423 | if ($this->_action & CRM_Core_Action::ADD) { |
424 | $this->set('context', 'amtg'); | |
425 | $this->set('amtgID', $group->id); | |
426 | ||
427 | $session = CRM_Core_Session::singleton(); | |
428 | $session->pushUserContext(CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id)); | |
429 | } | |
430 | } | |
431 | ||
432 | // update the nesting cache | |
433 | if ($updateNestingCache) { | |
434 | CRM_Contact_BAO_GroupNestingCache::update(); | |
435 | } | |
436 | } | |
31c31c92 | 437 | |
c490a46a | 438 | /** |
31c31c92 BS |
439 | * Build parent groups form elements |
440 | * | |
c490a46a | 441 | * @param CRM_Core_Form $form |
31c31c92 | 442 | * |
a6c01b45 CW |
443 | * @return array |
444 | * parent groups | |
31c31c92 | 445 | * @static |
31c31c92 | 446 | */ |
00be9182 | 447 | public static function buildParentGroups(&$form) { |
31c31c92 BS |
448 | $groupNames = CRM_Core_PseudoConstant::group(); |
449 | $parentGroups = $parentGroupElements = array(); | |
c490a46a CW |
450 | if (isset($form->_id) && !empty($form->_groupValues['parents'])) { |
451 | $parentGroupIds = explode(',', $form->_groupValues['parents']); | |
31c31c92 BS |
452 | foreach ($parentGroupIds as $parentGroupId) { |
453 | $parentGroups[$parentGroupId] = $groupNames[$parentGroupId]; | |
454 | if (array_key_exists($parentGroupId, $groupNames)) { | |
455 | $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId]; | |
c490a46a | 456 | $form->addElement('checkbox', "remove_parent_group_$parentGroupId", |
31c31c92 BS |
457 | $groupNames[$parentGroupId] |
458 | ); | |
459 | } | |
460 | } | |
461 | } | |
c490a46a | 462 | $form->assign_by_ref('parent_groups', $parentGroupElements); |
31c31c92 | 463 | |
c490a46a CW |
464 | if (isset($form->_id)) { |
465 | $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($form->_id, $groupNames); | |
31c31c92 BS |
466 | } |
467 | else { | |
468 | $potentialParentGroupIds = array_keys($groupNames); | |
469 | } | |
470 | ||
cae80d9f | 471 | $parentGroupSelectValues = array('' => '- ' . ts('select group') . ' -'); |
31c31c92 BS |
472 | foreach ($potentialParentGroupIds as $potentialParentGroupId) { |
473 | if (array_key_exists($potentialParentGroupId, $groupNames)) { | |
474 | $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId]; | |
475 | } | |
476 | } | |
477 | ||
478 | if (count($parentGroupSelectValues) > 1) { | |
479 | if (CRM_Core_Permission::isMultisiteEnabled()) { | |
c490a46a | 480 | $required = !isset($form->_id) || ($form->_id && CRM_Core_BAO_Domain::isDomainGroup($form->_id)) ? FALSE : empty($parentGroups); |
31c31c92 BS |
481 | } |
482 | else { | |
483 | $required = FALSE; | |
484 | } | |
c490a46a | 485 | $form->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2')); |
31c31c92 BS |
486 | } |
487 | ||
488 | return $parentGroups; | |
489 | } | |
6a488035 | 490 | } |