ManagedEntities - Fix permission error during uninstallation (regression-fix)
[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 object, if an id is present
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 /**
47 * What blocks should we show and hide.
48 *
49 * @var CRM_Core_ShowHideBlocks
50 */
51 protected $_showHide;
52
53 /**
54 * The civicrm_group_organization table id
55 *
56 * @var int
57 */
58 protected $_groupOrganizationID;
59
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'],
70 'frontend_title' => ['name' => 'frontend_title'],
71 'frontend_description' => ['name' => 'frontend_description'],
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
91 /**
92 * Set up variables to build the form.
93 */
94 public function preProcess() {
95 $this->_id = $this->get('id');
96 if ($this->_id) {
97 $breadCrumb = array(
98 array(
99 'title' => ts('Manage Groups'),
100 'url' => CRM_Utils_System::url('civicrm/group',
101 'reset=1'
102 ),
103 ),
104 );
105 CRM_Utils_System::appendBreadCrumb($breadCrumb);
106
107 $this->_groupValues = [];
108 $params = array('id' => $this->_id);
109 $this->_group = CRM_Contact_BAO_Group::retrieve($params, $this->_groupValues);
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);
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 }
125 $this->setTitle(ts('Confirm Group Delete'));
126 }
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 }
130 }
131 else {
132 if ($this->_id && $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 change settings for this reserved group."));
134 }
135 if (isset($this->_id)) {
136 $groupValues = array(
137 'id' => $this->_id,
138 'title' => $this->_title,
139 'saved_search_id' => $this->_groupValues['saved_search_id'] ?? '',
140 );
141 if (isset($this->_groupValues['saved_search_id'])) {
142 $this->assign('editSmartGroupURL', CRM_Contact_BAO_SavedSearch::getEditSearchUrl($this->_groupValues['saved_search_id']));
143 }
144 if (!empty($this->_groupValues['created_id'])) {
145 $groupValues['created_by'] = CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['created_id'], 'sort_name', 'id');
146 }
147
148 if (!empty($this->_groupValues['modified_id'])) {
149 $groupValues['modified_by'] = CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['modified_id'], 'sort_name', 'id');
150 }
151
152 $this->assign_by_ref('group', $groupValues);
153
154 $this->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
164 /**
165 * Set default values for the form.
166 *
167 * @return array
168 */
169 public function setDefaultValues() {
170 $defaults = [];
171 if (isset($this->_id)) {
172 $defaults = $this->_groupValues;
173 if (!empty($defaults['group_type'])) {
174 $types = explode(CRM_Core_DAO::VALUE_SEPARATOR,
175 substr($defaults['group_type'], 1, -1)
176 );
177 $defaults['group_type'] = [];
178 foreach ($types as $type) {
179 $defaults['group_type'][$type] = 1;
180 }
181 }
182
183 if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) {
184 CRM_Contact_BAO_GroupOrganization::retrieve($this->_id, $defaults);
185 }
186 }
187 else {
188 $defaults['is_active'] = 1;
189 }
190
191 if (!((CRM_Core_Permission::check('access CiviMail')) ||
192 (CRM_Mailing_Info::workflowEnabled() &&
193 CRM_Core_Permission::check('create mailings')
194 )
195 )
196 ) {
197 $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
198 if ($defaults['group_type'][$groupTypes['Mailing List']] == 1) {
199 $this->assign('freezeMailingList', $groupTypes['Mailing List']);
200 }
201 else {
202 $this->assign('hideMailingList', $groupTypes['Mailing List']);
203 }
204 }
205
206 if (empty($defaults['parents'])) {
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 /**
216 * Build the form object.
217 */
218 public function buildQuickForm() {
219 self::buildQuickEntityForm();
220 if ($this->_action & CRM_Core_Action::DELETE) {
221 return;
222 }
223
224 // We want the "new group" form to redirect the user
225 if ($this->_action == CRM_Core_Action::ADD) {
226 $this->preventAjaxSubmit();
227 }
228
229 $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
230
231 if (isset($this->_id) && !empty($this->_groupValues['saved_search_id'])) {
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, '&nbsp;&nbsp;&nbsp;'
240 );
241 }
242
243 $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues::groupVisibility(), TRUE);
244
245 //CRM-14190
246 $parentGroups = self::buildParentGroups($this);
247 self::buildGroupOrganizations($this);
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 }
254 $this->addElement('checkbox', 'is_active', ts('Is active?'));
255
256 //build custom data
257 CRM_Custom_Form_CustomData::buildQuickForm($this);
258
259 $doParentCheck = FALSE;
260 if (CRM_Core_Permission::isMultisiteEnabled()) {
261 $doParentCheck = !($this->_id && CRM_Core_BAO_Domain::isDomainGroup($this->_id));
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 /**
273 * Global validation rules for the form.
274 *
275 * @param array $fields
276 * Posted values of the form.
277 * @param array $fileParams
278 * @param array $options
279 *
280 * @return array
281 * list of errors to be posted back to the form
282 */
283 public static function formRule($fields, $fileParams, $options) {
284 $errors = [];
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;
300 if (!empty($fields['parents'])) {
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
310 if (!empty($fields['title'])) {
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 /**
334 * Process the form when submitted.
335 */
336 public function postProcess() {
337 CRM_Utils_System::flushCache();
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);
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
356 // CRM-21431 If all group_type are unchecked, the change will not be saved otherwise.
357 if (!isset($params['group_type'])) {
358 $params['group_type'] = [];
359 }
360
361 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
362 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
363 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
364 $this->_id,
365 'Group'
366 );
367
368 $group = CRM_Contact_BAO_Group::create($params);
369 // Set the entity id so it is available to postProcess hook consumers
370 $this->setEntityId($group->id);
371 //Remove any parent groups requested to be removed
372 if (!empty($this->_groupValues['parents'])) {
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
384 // Add context to the session, in case we are adding members to the group
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 }
399
400 /**
401 * Build parent groups form elements.
402 *
403 * @param CRM_Core_Form $form
404 *
405 * @return array
406 * parent groups
407 */
408 public static function buildParentGroups(&$form) {
409 $groupNames = CRM_Core_PseudoConstant::group();
410 $parentGroups = $parentGroupElements = [];
411 if (isset($form->_id) && !empty($form->_groupValues['parents'])) {
412 $parentGroupIds = explode(',', $form->_groupValues['parents']);
413 foreach ($parentGroupIds as $parentGroupId) {
414 $parentGroups[$parentGroupId] = $groupNames[$parentGroupId];
415 if (array_key_exists($parentGroupId, $groupNames)) {
416 $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId];
417 $form->addElement('checkbox', "remove_parent_group_$parentGroupId",
418 $groupNames[$parentGroupId]
419 );
420 }
421 }
422 }
423 $form->assign_by_ref('parent_groups', $parentGroupElements);
424
425 if (isset($form->_id)) {
426 $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($form->_id, $groupNames);
427 }
428 else {
429 $potentialParentGroupIds = array_keys($groupNames);
430 }
431
432 $parentGroupSelectValues = [];
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()) {
441 $required = !isset($form->_id) || ($form->_id && CRM_Core_BAO_Domain::isDomainGroup($form->_id)) ? FALSE : empty($parentGroups);
442 }
443 else {
444 $required = FALSE;
445 }
446 $form->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2', 'multiple' => TRUE));
447 }
448
449 return $parentGroups;
450 }
451
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
468 }