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