Merge pull request #16091 from civicrm/5.21
[civicrm-core.git] / CRM / Group / Form / Edit.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class is to build the form for adding Group.
20 */
21 class CRM_Group_Form_Edit extends CRM_Core_Form {
22
23 use CRM_Core_Form_EntityFormTrait;
24
25 /**
26 * The group id, used when editing a group
27 *
28 * @var int
29 */
30 protected $_id;
31
32 /**
33 * The group object, if an id is present
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 /**
54 * What blocks should we show and hide.
55 *
56 * @var CRM_Core_ShowHideBlocks
57 */
58 protected $_showHide;
59
60 /**
61 * The civicrm_group_organization table id
62 *
63 * @var int
64 */
65 protected $_groupOrganizationID;
66
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
96 /**
97 * Set up variables to build the form.
98 */
99 public function preProcess() {
100 $this->_id = $this->get('id');
101 if ($this->_id) {
102 $breadCrumb = array(
103 array(
104 'title' => ts('Manage Groups'),
105 'url' => CRM_Utils_System::url('civicrm/group',
106 'reset=1'
107 ),
108 ),
109 );
110 CRM_Utils_System::appendBreadCrumb($breadCrumb);
111
112 $this->_groupValues = array();
113 $params = array('id' => $this->_id);
114 $this->_group = CRM_Contact_BAO_Group::retrieve($params, $this->_groupValues);
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);
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 }
130 CRM_Utils_System::setTitle(ts('Confirm Group Delete'));
131 }
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 }
135 }
136 else {
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 }
140 if (isset($this->_id)) {
141 $groupValues = array(
142 'id' => $this->_id,
143 'title' => $this->_title,
144 'saved_search_id' => isset($this->_groupValues['saved_search_id']) ? $this->_groupValues['saved_search_id'] : '',
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 }
156 if (!empty($this->_groupValues['created_id'])) {
157 $groupValues['created_by'] = CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['created_id'], 'sort_name', 'id');
158 }
159
160 if (!empty($this->_groupValues['modified_id'])) {
161 $groupValues['modified_by'] = CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['modified_id'], 'sort_name', 'id');
162 }
163
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
176 /**
177 * Set default values for the form.
178 *
179 * @return array
180 */
181 public function setDefaultValues() {
182 $defaults = array();
183 if (isset($this->_id)) {
184 $defaults = $this->_groupValues;
185 if (!empty($defaults['group_type'])) {
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
195 if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) {
196 CRM_Contact_BAO_GroupOrganization::retrieve($this->_id, $defaults);
197 }
198 }
199 else {
200 $defaults['is_active'] = 1;
201 }
202
203 if (!((CRM_Core_Permission::check('access CiviMail')) ||
204 (CRM_Mailing_Info::workflowEnabled() &&
205 CRM_Core_Permission::check('create mailings')
206 )
207 )
208 ) {
209 $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
210 if ($defaults['group_type'][$groupTypes['Mailing List']] == 1) {
211 $this->assign('freezeMailingList', $groupTypes['Mailing List']);
212 }
213 else {
214 $this->assign('hideMailingList', $groupTypes['Mailing List']);
215 }
216 }
217
218 if (empty($defaults['parents'])) {
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 /**
228 * Build the form object.
229 */
230 public function buildQuickForm() {
231 self::buildQuickEntityForm();
232 if ($this->_action & CRM_Core_Action::DELETE) {
233 return;
234 }
235
236 // We want the "new group" form to redirect the user
237 if ($this->_action == CRM_Core_Action::ADD) {
238 $this->preventAjaxSubmit();
239 }
240
241 $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
242
243 if (isset($this->_id) && !empty($this->_groupValues['saved_search_id'])) {
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, '&nbsp;&nbsp;&nbsp;'
252 );
253 }
254
255 $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues::groupVisibility(), TRUE);
256
257 //CRM-14190
258 $parentGroups = self::buildParentGroups($this);
259 self::buildGroupOrganizations($this);
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 }
266 $this->addElement('checkbox', 'is_active', ts('Is active?'));
267
268 //build custom data
269 CRM_Custom_Form_CustomData::buildQuickForm($this);
270
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 /**
285 * Global validation rules for the form.
286 *
287 * @param array $fields
288 * Posted values of the form.
289 * @param array $fileParams
290 * @param array $options
291 *
292 * @return array
293 * list of errors to be posted back to the form
294 */
295 public static function formRule($fields, $fileParams, $options) {
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;
312 if (!empty($fields['parents'])) {
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
322 if (!empty($fields['title'])) {
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 /**
346 * Process the form when submitted.
347 */
348 public function postProcess() {
349 CRM_Utils_System::flushCache();
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);
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
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
373 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
374 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
375 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
376 $this->_id,
377 'Group'
378 );
379
380 $group = CRM_Contact_BAO_Group::create($params);
381
382 //Remove any parent groups requested to be removed
383 if (!empty($this->_groupValues['parents'])) {
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
395 // Add context to the session, in case we are adding members to the group
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 }
410
411 /**
412 * Build parent groups form elements.
413 *
414 * @param CRM_Core_Form $form
415 *
416 * @return array
417 * parent groups
418 */
419 public static function buildParentGroups(&$form) {
420 $groupNames = CRM_Core_PseudoConstant::group();
421 $parentGroups = $parentGroupElements = array();
422 if (isset($form->_id) && !empty($form->_groupValues['parents'])) {
423 $parentGroupIds = explode(',', $form->_groupValues['parents']);
424 foreach ($parentGroupIds as $parentGroupId) {
425 $parentGroups[$parentGroupId] = $groupNames[$parentGroupId];
426 if (array_key_exists($parentGroupId, $groupNames)) {
427 $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId];
428 $form->addElement('checkbox', "remove_parent_group_$parentGroupId",
429 $groupNames[$parentGroupId]
430 );
431 }
432 }
433 }
434 $form->assign_by_ref('parent_groups', $parentGroupElements);
435
436 if (isset($form->_id)) {
437 $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($form->_id, $groupNames);
438 }
439 else {
440 $potentialParentGroupIds = array_keys($groupNames);
441 }
442
443 $parentGroupSelectValues = array();
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()) {
452 $required = !isset($form->_id) || ($form->_id && CRM_Core_BAO_Domain::isDomainGroup($form->_id)) ? FALSE : empty($parentGroups);
453 }
454 else {
455 $required = FALSE;
456 }
457 $form->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2', 'multiple' => TRUE));
458 }
459
460 return $parentGroups;
461 }
462
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
479 }