3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2020
35 * This class is to build the form for adding Group.
37 class CRM_Group_Form_Edit
extends CRM_Core_Form
{
39 use CRM_Core_Form_EntityFormTrait
;
42 * The group id, used when editing a group
49 * The group object, if an id is present
56 * The title of the group being deleted
63 * Store the group values
67 protected $_groupValues;
70 * What blocks should we show and hide.
72 * @var CRM_Core_ShowHideBlocks
77 * The civicrm_group_organization table id
81 protected $_groupOrganizationID;
84 * Set entity fields to be assigned to the form.
86 protected function setEntityFields() {
87 $this->entityFields
= [
92 'description' => ['name' => 'description'],
97 * Set the delete message.
99 * We do this from the constructor in order to do a translation.
101 public function setDeleteMessage() {
102 $this->deleteMessage
= '';
106 * Explicitly declare the entity api name.
108 public function getDefaultEntity() {
113 * Set up variables to build the form.
115 public function preProcess() {
116 $this->_id
= $this->get('id');
120 'title' => ts('Manage Groups'),
121 'url' => CRM_Utils_System
::url('civicrm/group',
126 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
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'];
134 $this->assign('action', $this->_action
);
135 $this->assign('showBlockJS', TRUE);
137 if ($this->_action
== CRM_Core_Action
::DELETE
) {
138 if (isset($this->_id
)) {
139 $this->assign('title', $this->_title
);
141 $this->assign('count', CRM_Contact_BAO_Group
::memberCount($this->_id
));
143 catch (CRM_Core_Exception
$e) {
144 // If the group is borked the query might fail but delete should be possible.
146 CRM_Utils_System
::setTitle(ts('Confirm Group Delete'));
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."));
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."));
156 if (isset($this->_id
)) {
157 $groupValues = array(
159 'title' => $this->_title
,
160 'saved_search_id' => isset($this->_groupValues
['saved_search_id']) ?
$this->_groupValues
['saved_search_id'] : '',
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'],
167 $groupValues['search_custom_id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch',
168 $this->_groupValues
['saved_search_id'],
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');
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');
180 $this->assign_by_ref('group', $groupValues);
182 CRM_Utils_System
::setTitle(ts('Group Settings: %1', array(1 => $this->_title
)));
184 $session = CRM_Core_Session
::singleton();
185 $session->pushUserContext(CRM_Utils_System
::url('civicrm/group', 'reset=1'));
189 CRM_Custom_Form_CustomData
::preProcess($this, NULL, NULL, 1, 'Group', $this->_id
);
193 * Set default values for the form.
197 public function setDefaultValues() {
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)
205 $defaults['group_type'] = array();
206 foreach ($types as $type) {
207 $defaults['group_type'][$type] = 1;
211 if (CRM_Core_Permission
::check('administer Multiple Organizations') && CRM_Core_Permission
::isMultisiteEnabled()) {
212 CRM_Contact_BAO_GroupOrganization
::retrieve($this->_id
, $defaults);
216 $defaults['is_active'] = 1;
219 if (!((CRM_Core_Permission
::check('access CiviMail')) ||
220 (CRM_Mailing_Info
::workflowEnabled() &&
221 CRM_Core_Permission
::check('create mailings')
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']);
230 $this->assign('hideMailignList', $groupTypes['Mailing List']);
234 if (empty($defaults['parents'])) {
235 $defaults['parents'] = CRM_Core_BAO_Domain
::getGroupId();
238 // custom data set defaults
239 $defaults +
= CRM_Custom_Form_CustomData
::setDefaultValues($this);
244 * Build the form object.
246 public function buildQuickForm() {
247 self
::buildQuickEntityForm();
248 if ($this->_action
& CRM_Core_Action
::DELETE
) {
252 // We want the "new group" form to redirect the user
253 if ($this->_action
== CRM_Core_Action
::ADD
) {
254 $this->preventAjaxSubmit();
257 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
259 if (isset($this->_id
) && !empty($this->_groupValues
['saved_search_id'])) {
260 unset($groupTypes['Access Control']);
263 if (!empty($groupTypes)) {
264 $this->addCheckBox('group_type',
267 NULL, NULL, NULL, NULL, ' '
271 $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues
::groupVisibility(), TRUE);
274 $parentGroups = self
::buildParentGroups($this);
275 self
::buildGroupOrganizations($this);
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');
282 $this->addElement('checkbox', 'is_active', ts('Is active?'));
285 CRM_Custom_Form_CustomData
::buildQuickForm($this);
287 $doParentCheck = FALSE;
288 if (CRM_Core_Permission
::isMultisiteEnabled()) {
289 $doParentCheck = ($this->_id
&& CRM_Core_BAO_Domain
::isDomainGroup($this->_id
)) ?
FALSE : TRUE;
294 'parentGroups' => $parentGroups,
295 'doParentCheck' => $doParentCheck,
297 $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options);
301 * Global validation rules for the form.
303 * @param array $fields
304 * Posted values of the form.
305 * @param array $fileParams
306 * @param array $options
309 * list of errors to be posted back to the form
311 public static function formRule($fields, $fileParams, $options) {
314 $doParentCheck = $options['doParentCheck'];
315 $self = &$options['selfObj'];
317 if ($doParentCheck) {
318 $parentGroups = $options['parentGroups'];
321 foreach ($fields as $key => $val) {
322 if (substr($key, 0, 20) == 'remove_parent_group_') {
328 if (!empty($fields['parents'])) {
332 if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) {
333 $errors['parents'] = ts('Make sure at least one parent group is set.');
337 // do check for both name and title uniqueness
338 if (!empty($fields['title'])) {
339 $title = trim($fields['title']);
345 $params = array(1 => array($title, 'String'));
348 $query .= "AND id <> %2";
349 $params[2] = array($self->_id
, 'Integer');
352 $grpCnt = CRM_Core_DAO
::singleValueQuery($query, $params);
354 $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title']));
358 return empty($errors) ?
TRUE : $errors;
362 * Process the form when submitted.
364 public function postProcess() {
365 CRM_Utils_System
::flushCache();
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;
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
;
380 if ($this->_action
& CRM_Core_Action
::UPDATE
&& isset($this->_groupOrganizationID
)) {
381 $params['group_organization'] = $this->_groupOrganizationID
;
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();
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,
396 $group = CRM_Contact_BAO_Group
::create($params);
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;
409 CRM_Core_Session
::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title
)), ts('Group Saved'), 'success');
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
);
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
));
421 // update the nesting cache
422 if ($updateNestingCache) {
423 CRM_Contact_BAO_GroupNestingCache
::update();
428 * Build parent groups form elements.
430 * @param CRM_Core_Form $form
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]
450 $form->assign_by_ref('parent_groups', $parentGroupElements);
452 if (isset($form->_id
)) {
453 $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache
::getPotentialCandidates($form->_id
, $groupNames);
456 $potentialParentGroupIds = array_keys($groupNames);
459 $parentGroupSelectValues = array();
460 foreach ($potentialParentGroupIds as $potentialParentGroupId) {
461 if (array_key_exists($potentialParentGroupId, $groupNames)) {
462 $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId];
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);
473 $form->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2', 'multiple' => TRUE));
476 return $parentGroups;
480 * Add the group organization checkbox to the form.
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.
485 * @param CRM_Core_Form $form
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);