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 id, used when editing a group
33 * The group object, if an id is present
40 * The title of the group being deleted
47 * Store the group values
51 protected $_groupValues;
54 * What blocks should we show and hide.
56 * @var CRM_Core_ShowHideBlocks
61 * The civicrm_group_organization table id
65 protected $_groupOrganizationID;
68 * Set entity fields to be assigned to the form.
70 protected function setEntityFields() {
71 $this->entityFields
= [
76 'description' => ['name' => 'description'],
81 * Set the delete message.
83 * We do this from the constructor in order to do a translation.
85 public function setDeleteMessage() {
86 $this->deleteMessage
= '';
90 * Explicitly declare the entity api name.
92 public function getDefaultEntity() {
97 * Set up variables to build the form.
99 public function preProcess() {
100 $this->_id
= $this->get('id');
104 'title' => ts('Manage Groups'),
105 'url' => CRM_Utils_System
::url('civicrm/group',
110 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
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'];
118 $this->assign('action', $this->_action
);
119 $this->assign('showBlockJS', TRUE);
121 if ($this->_action
== CRM_Core_Action
::DELETE
) {
122 if (isset($this->_id
)) {
123 $this->assign('title', $this->_title
);
125 $this->assign('count', CRM_Contact_BAO_Group
::memberCount($this->_id
));
127 catch (CRM_Core_Exception
$e) {
128 // If the group is borked the query might fail but delete should be possible.
130 CRM_Utils_System
::setTitle(ts('Confirm Group Delete'));
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."));
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."));
140 if (isset($this->_id
)) {
141 $groupValues = array(
143 'title' => $this->_title
,
144 'saved_search_id' => isset($this->_groupValues
['saved_search_id']) ?
$this->_groupValues
['saved_search_id'] : '',
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'],
151 $groupValues['search_custom_id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch',
152 $this->_groupValues
['saved_search_id'],
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');
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');
164 $this->assign_by_ref('group', $groupValues);
166 CRM_Utils_System
::setTitle(ts('Group Settings: %1', array(1 => $this->_title
)));
168 $session = CRM_Core_Session
::singleton();
169 $session->pushUserContext(CRM_Utils_System
::url('civicrm/group', 'reset=1'));
173 CRM_Custom_Form_CustomData
::preProcess($this, NULL, NULL, 1, 'Group', $this->_id
);
177 * Set default values for the form.
181 public function setDefaultValues() {
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)
189 $defaults['group_type'] = array();
190 foreach ($types as $type) {
191 $defaults['group_type'][$type] = 1;
195 if (CRM_Core_Permission
::check('administer Multiple Organizations') && CRM_Core_Permission
::isMultisiteEnabled()) {
196 CRM_Contact_BAO_GroupOrganization
::retrieve($this->_id
, $defaults);
200 $defaults['is_active'] = 1;
203 if (!((CRM_Core_Permission
::check('access CiviMail')) ||
204 (CRM_Mailing_Info
::workflowEnabled() &&
205 CRM_Core_Permission
::check('create mailings')
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']);
214 $this->assign('hideMailingList', $groupTypes['Mailing List']);
218 if (empty($defaults['parents'])) {
219 $defaults['parents'] = CRM_Core_BAO_Domain
::getGroupId();
222 // custom data set defaults
223 $defaults +
= CRM_Custom_Form_CustomData
::setDefaultValues($this);
228 * Build the form object.
230 public function buildQuickForm() {
231 self
::buildQuickEntityForm();
232 if ($this->_action
& CRM_Core_Action
::DELETE
) {
236 // We want the "new group" form to redirect the user
237 if ($this->_action
== CRM_Core_Action
::ADD
) {
238 $this->preventAjaxSubmit();
241 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
243 if (isset($this->_id
) && !empty($this->_groupValues
['saved_search_id'])) {
244 unset($groupTypes['Access Control']);
247 if (!empty($groupTypes)) {
248 $this->addCheckBox('group_type',
251 NULL, NULL, NULL, NULL, ' '
255 $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues
::groupVisibility(), TRUE);
258 $parentGroups = self
::buildParentGroups($this);
259 self
::buildGroupOrganizations($this);
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');
266 $this->addElement('checkbox', 'is_active', ts('Is active?'));
269 CRM_Custom_Form_CustomData
::buildQuickForm($this);
271 $doParentCheck = FALSE;
272 if (CRM_Core_Permission
::isMultisiteEnabled()) {
273 $doParentCheck = ($this->_id
&& CRM_Core_BAO_Domain
::isDomainGroup($this->_id
)) ?
FALSE : TRUE;
278 'parentGroups' => $parentGroups,
279 'doParentCheck' => $doParentCheck,
281 $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options);
285 * Global validation rules for the form.
287 * @param array $fields
288 * Posted values of the form.
289 * @param array $fileParams
290 * @param array $options
293 * list of errors to be posted back to the form
295 public static function formRule($fields, $fileParams, $options) {
298 $doParentCheck = $options['doParentCheck'];
299 $self = &$options['selfObj'];
301 if ($doParentCheck) {
302 $parentGroups = $options['parentGroups'];
305 foreach ($fields as $key => $val) {
306 if (substr($key, 0, 20) == 'remove_parent_group_') {
312 if (!empty($fields['parents'])) {
316 if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) {
317 $errors['parents'] = ts('Make sure at least one parent group is set.');
321 // do check for both name and title uniqueness
322 if (!empty($fields['title'])) {
323 $title = trim($fields['title']);
329 $params = array(1 => array($title, 'String'));
332 $query .= "AND id <> %2";
333 $params[2] = array($self->_id
, 'Integer');
336 $grpCnt = CRM_Core_DAO
::singleValueQuery($query, $params);
338 $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title']));
342 return empty($errors) ?
TRUE : $errors;
346 * Process the form when submitted.
348 public function postProcess() {
349 CRM_Utils_System
::flushCache();
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;
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
;
364 if ($this->_action
& CRM_Core_Action
::UPDATE
&& isset($this->_groupOrganizationID
)) {
365 $params['group_organization'] = $this->_groupOrganizationID
;
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();
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,
380 $group = CRM_Contact_BAO_Group
::create($params);
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;
393 CRM_Core_Session
::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title
)), ts('Group Saved'), 'success');
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
);
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
));
405 // update the nesting cache
406 if ($updateNestingCache) {
407 CRM_Contact_BAO_GroupNestingCache
::update();
412 * Build parent groups form elements.
414 * @param CRM_Core_Form $form
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]
434 $form->assign_by_ref('parent_groups', $parentGroupElements);
436 if (isset($form->_id
)) {
437 $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache
::getPotentialCandidates($form->_id
, $groupNames);
440 $potentialParentGroupIds = array_keys($groupNames);
443 $parentGroupSelectValues = array();
444 foreach ($potentialParentGroupIds as $potentialParentGroupId) {
445 if (array_key_exists($potentialParentGroupId, $groupNames)) {
446 $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId];
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);
457 $form->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2', 'multiple' => TRUE));
460 return $parentGroups;
464 * Add the group organization checkbox to the form.
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.
469 * @param CRM_Core_Form $form
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);