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