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