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