3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class is to build the form for adding Group.
21 class CRM_Group_Form_Edit
extends CRM_Core_Form
{
23 use CRM_Core_Form_EntityFormTrait
;
26 * The group object, if an id is present
33 * The title of the group being deleted
40 * Store the group values
44 protected $_groupValues;
47 * What blocks should we show and hide.
49 * @var CRM_Core_ShowHideBlocks
54 * The civicrm_group_organization table id
58 protected $_groupOrganizationID;
61 * Set entity fields to be assigned to the form.
63 protected function setEntityFields() {
64 $this->entityFields
= [
69 'description' => ['name' => 'description'],
70 'frontend_title' => ['name' => 'frontend_title'],
71 'frontend_description' => ['name' => 'frontend_description'],
76 * Set the delete message.
78 * We do this from the constructor in order to do a translation.
80 public function setDeleteMessage() {
81 $this->deleteMessage
= '';
85 * Explicitly declare the entity api name.
87 public function getDefaultEntity() {
92 * Set up variables to build the form.
94 public function preProcess() {
95 $this->_id
= $this->get('id');
99 'title' => ts('Manage Groups'),
100 'url' => CRM_Utils_System
::url('civicrm/group',
105 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
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'];
113 $this->assign('action', $this->_action
);
114 $this->assign('showBlockJS', TRUE);
116 if ($this->_action
== CRM_Core_Action
::DELETE
) {
117 if (isset($this->_id
)) {
118 $this->assign('title', $this->_title
);
120 $this->assign('count', CRM_Contact_BAO_Group
::memberCount($this->_id
));
122 catch (CRM_Core_Exception
$e) {
123 // If the group is borked the query might fail but delete should be possible.
125 $this->setTitle(ts('Confirm Group Delete'));
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."));
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."));
135 if (isset($this->_id
)) {
136 $groupValues = array(
138 'title' => $this->_title
,
139 'saved_search_id' => $this->_groupValues
['saved_search_id'] ??
'',
141 if (isset($this->_groupValues
['saved_search_id'])) {
142 $this->assign('editSmartGroupURL', CRM_Contact_BAO_SavedSearch
::getEditSearchUrl($this->_groupValues
['saved_search_id']));
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');
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');
152 $this->assign_by_ref('group', $groupValues);
154 $this->setTitle(ts('Group Settings: %1', array(1 => $this->_title
)));
156 $session = CRM_Core_Session
::singleton();
157 $session->pushUserContext(CRM_Utils_System
::url('civicrm/group', 'reset=1'));
161 CRM_Custom_Form_CustomData
::preProcess($this, NULL, NULL, 1, 'Group', $this->_id
);
165 * Set default values for the form.
169 public function setDefaultValues() {
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)
177 $defaults['group_type'] = [];
178 foreach ($types as $type) {
179 $defaults['group_type'][$type] = 1;
183 if (CRM_Core_Permission
::check('administer Multiple Organizations') && CRM_Core_Permission
::isMultisiteEnabled()) {
184 CRM_Contact_BAO_GroupOrganization
::retrieve($this->_id
, $defaults);
188 $defaults['is_active'] = 1;
191 if (!((CRM_Core_Permission
::check('access CiviMail')) ||
192 (CRM_Mailing_Info
::workflowEnabled() &&
193 CRM_Core_Permission
::check('create mailings')
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']);
202 $this->assign('hideMailingList', $groupTypes['Mailing List']);
206 if (empty($defaults['parents'])) {
207 $defaults['parents'] = CRM_Core_BAO_Domain
::getGroupId();
210 // custom data set defaults
211 $defaults +
= CRM_Custom_Form_CustomData
::setDefaultValues($this);
216 * Build the form object.
218 public function buildQuickForm() {
219 self
::buildQuickEntityForm();
220 if ($this->_action
& CRM_Core_Action
::DELETE
) {
224 // We want the "new group" form to redirect the user
225 if ($this->_action
== CRM_Core_Action
::ADD
) {
226 $this->preventAjaxSubmit();
229 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
231 if (isset($this->_id
) && !empty($this->_groupValues
['saved_search_id'])) {
232 unset($groupTypes['Access Control']);
235 if (!empty($groupTypes)) {
236 $this->addCheckBox('group_type',
239 NULL, NULL, NULL, NULL, ' '
243 $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues
::groupVisibility(), TRUE);
246 $parentGroups = self
::buildParentGroups($this);
247 self
::buildGroupOrganizations($this);
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');
254 $this->addElement('checkbox', 'is_active', ts('Is active?'));
257 CRM_Custom_Form_CustomData
::buildQuickForm($this);
259 $doParentCheck = FALSE;
260 if (CRM_Core_Permission
::isMultisiteEnabled()) {
261 $doParentCheck = !($this->_id
&& CRM_Core_BAO_Domain
::isDomainGroup($this->_id
));
266 'parentGroups' => $parentGroups,
267 'doParentCheck' => $doParentCheck,
269 $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options);
273 * Global validation rules for the form.
275 * @param array $fields
276 * Posted values of the form.
277 * @param array $fileParams
278 * @param array $options
281 * list of errors to be posted back to the form
283 public static function formRule($fields, $fileParams, $options) {
286 $doParentCheck = $options['doParentCheck'];
287 $self = &$options['selfObj'];
289 if ($doParentCheck) {
290 $parentGroups = $options['parentGroups'];
293 foreach ($fields as $key => $val) {
294 if (substr($key, 0, 20) == 'remove_parent_group_') {
300 if (!empty($fields['parents'])) {
304 if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) {
305 $errors['parents'] = ts('Make sure at least one parent group is set.');
309 // do check for both name and title uniqueness
310 if (!empty($fields['title'])) {
311 $title = trim($fields['title']);
317 $params = array(1 => array($title, 'String'));
320 $query .= "AND id <> %2";
321 $params[2] = array($self->_id
, 'Integer');
324 $grpCnt = CRM_Core_DAO
::singleValueQuery($query, $params);
326 $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title']));
330 return empty($errors) ?
TRUE : $errors;
334 * Process the form when submitted.
336 public function postProcess() {
337 CRM_Utils_System
::flushCache();
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;
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
;
352 if ($this->_action
& CRM_Core_Action
::UPDATE
&& isset($this->_groupOrganizationID
)) {
353 $params['group_organization'] = $this->_groupOrganizationID
;
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'] = [];
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,
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;
382 CRM_Core_Session
::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title
)), ts('Group Saved'), 'success');
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
);
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
));
394 // update the nesting cache
395 if ($updateNestingCache) {
396 CRM_Contact_BAO_GroupNestingCache
::update();
401 * Build parent groups form elements.
403 * @param CRM_Core_Form $form
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]
423 $form->assign_by_ref('parent_groups', $parentGroupElements);
425 if (isset($form->_id
)) {
426 $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache
::getPotentialCandidates($form->_id
, $groupNames);
429 $potentialParentGroupIds = array_keys($groupNames);
432 $parentGroupSelectValues = [];
433 foreach ($potentialParentGroupIds as $potentialParentGroupId) {
434 if (array_key_exists($potentialParentGroupId, $groupNames)) {
435 $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId];
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);
446 $form->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2', 'multiple' => TRUE));
449 return $parentGroups;
453 * Add the group organization checkbox to the form.
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.
458 * @param CRM_Core_Form $form
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);