3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 * This class is to build the form for adding Group.
37 class CRM_Group_Form_Edit
extends CRM_Core_Form
{
40 * The group id, used when editing a group
47 * The group object, if an id is present
54 * The title of the group being deleted
61 * Store the group values
65 protected $_groupValues;
68 * What blocks should we show and hide.
70 * @var CRM_Core_ShowHideBlocks
75 * The civicrm_group_organization table id
79 protected $_groupOrganizationID;
82 * Set up variables to build the form.
84 public function preProcess() {
85 $this->_id
= $this->get('id');
89 'title' => ts('Manage Groups'),
90 'url' => CRM_Utils_System
::url('civicrm/group',
95 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
97 $this->_groupValues
= array();
98 $params = array('id' => $this->_id
);
99 $this->_group
= CRM_Contact_BAO_Group
::retrieve($params, $this->_groupValues
);
100 $this->_title
= $this->_groupValues
['title'];
103 $this->assign('action', $this->_action
);
104 $this->assign('showBlockJS', TRUE);
106 if ($this->_action
== CRM_Core_Action
::DELETE
) {
107 if (isset($this->_id
)) {
108 $this->assign('title', $this->_title
);
109 $this->assign('count', CRM_Contact_BAO_Group
::memberCount($this->_id
));
110 CRM_Utils_System
::setTitle(ts('Confirm Group Delete'));
112 if ($this->_groupValues
['is_reserved'] == 1 && !CRM_Core_Permission
::check('administer reserved groups')) {
113 CRM_Core_Error
::statusBounce(ts("You do not have sufficient permission to delete this reserved group."));
117 if ($this->_groupValues
['is_reserved'] == 1 && !CRM_Core_Permission
::check('administer reserved groups')) {
118 CRM_Core_Error
::statusBounce(ts("You do not have sufficient permission to change settings for this reserved group."));
120 if (isset($this->_id
)) {
121 $groupValues = array(
123 'title' => $this->_title
,
124 'saved_search_id' => isset($this->_groupValues
['saved_search_id']) ?
$this->_groupValues
['saved_search_id'] : '',
126 if (isset($this->_groupValues
['saved_search_id'])) {
127 $groupValues['mapping_id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch',
128 $this->_groupValues
['saved_search_id'],
131 $groupValues['search_custom_id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch',
132 $this->_groupValues
['saved_search_id'],
136 if (!empty($this->_groupValues
['created_id'])) {
137 $groupValues['created_by'] = CRM_Core_DAO
::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues
['created_id'], 'sort_name', 'id');
140 if (!empty($this->_groupValues
['modified_id'])) {
141 $groupValues['modified_by'] = CRM_Core_DAO
::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues
['modified_id'], 'sort_name', 'id');
144 $this->assign_by_ref('group', $groupValues);
146 CRM_Utils_System
::setTitle(ts('Group Settings: %1', array(1 => $this->_title
)));
148 $session = CRM_Core_Session
::singleton();
149 $session->pushUserContext(CRM_Utils_System
::url('civicrm/group', 'reset=1'));
153 CRM_Custom_Form_CustomData
::preProcess($this, NULL, NULL, 1, 'Group', $this->_id
);
157 * Set default values for the form.
161 public function setDefaultValues() {
164 if (isset($this->_id
)) {
165 $defaults = $this->_groupValues
;
166 if (!empty($defaults['group_type'])) {
167 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
168 substr($defaults['group_type'], 1, -1)
170 $defaults['group_type'] = array();
171 foreach ($types as $type) {
172 $defaults['group_type'][$type] = 1;
176 if (CRM_Core_Permission
::check('administer Multiple Organizations') && CRM_Core_Permission
::isMultisiteEnabled()) {
177 CRM_Contact_BAO_GroupOrganization
::retrieve($this->_id
, $defaults);
181 if (!((CRM_Core_Permission
::check('access CiviMail')) ||
182 (CRM_Mailing_Info
::workflowEnabled() &&
183 CRM_Core_Permission
::check('create mailings')
187 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
188 if ($defaults['group_type'][$groupTypes['Mailing List']] == 1) {
189 $this->assign('freezeMailignList', $groupTypes['Mailing List']);
192 $this->assign('hideMailignList', $groupTypes['Mailing List']);
196 if (empty($defaults['parents'])) {
197 $defaults['parents'] = CRM_Core_BAO_Domain
::getGroupId();
200 // custom data set defaults
201 $defaults +
= CRM_Custom_Form_CustomData
::setDefaultValues($this);
206 * Build the form object.
208 public function buildQuickForm() {
209 if ($this->_action
== CRM_Core_Action
::DELETE
) {
210 $this->addButtons(array(
213 'name' => ts('Delete Group'),
218 'name' => ts('Cancel'),
225 // We want the "new group" form to redirect the user
226 if ($this->_action
== CRM_Core_Action
::ADD
) {
227 $this->preventAjaxSubmit();
230 $this->applyFilter('__ALL__', 'trim');
231 $this->add('text', 'title', ts('Name') . ' ',
232 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
235 $this->add('textarea', 'description', ts('Description') . ' ',
236 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'description')
239 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
241 if (isset($this->_id
) && !empty($this->_groupValues
['saved_search_id'])) {
242 unset($groupTypes['Access Control']);
245 if (!empty($groupTypes)) {
246 $this->addCheckBox('group_type',
249 NULL, NULL, NULL, NULL, ' '
253 $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues
::groupVisibility(), TRUE);
256 $parentGroups = self
::buildParentGroups($this);
257 self
::buildGroupOrganizations($this);
259 // is_reserved property CRM-9936
260 $this->addElement('checkbox', 'is_reserved', ts('Reserved Group?'));
261 if (!CRM_Core_Permission
::check('administer reserved groups')) {
262 $this->freeze('is_reserved');
266 CRM_Custom_Form_CustomData
::buildQuickForm($this);
268 $this->addButtons(array(
271 'name' => ($this->_action
== CRM_Core_Action
::ADD
) ?
ts('Continue') : ts('Save'),
276 'name' => ts('Cancel'),
281 $doParentCheck = FALSE;
282 if (CRM_Core_Permission
::isMultisiteEnabled()) {
283 $doParentCheck = ($this->_id
&& CRM_Core_BAO_Domain
::isDomainGroup($this->_id
)) ?
FALSE : TRUE;
288 'parentGroups' => $parentGroups,
289 'doParentCheck' => $doParentCheck,
291 $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options);
295 * Global validation rules for the form.
297 * @param array $fields
298 * Posted values of the form.
299 * @param array $fileParams
300 * @param array $options
303 * list of errors to be posted back to the form
305 public static function formRule($fields, $fileParams, $options) {
308 $doParentCheck = $options['doParentCheck'];
309 $self = &$options['selfObj'];
311 if ($doParentCheck) {
312 $parentGroups = $options['parentGroups'];
315 foreach ($fields as $key => $val) {
316 if (substr($key, 0, 20) == 'remove_parent_group_') {
322 if (!empty($fields['parents'])) {
326 if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) {
327 $errors['parents'] = ts('Make sure at least one parent group is set.');
331 // do check for both name and title uniqueness
332 if (!empty($fields['title'])) {
333 $title = trim($fields['title']);
339 $params = array(1 => array($title, 'String'));
342 $query .= "AND id <> %2";
343 $params[2] = array($self->_id
, 'Integer');
346 $grpCnt = CRM_Core_DAO
::singleValueQuery($query, $params);
348 $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title']));
352 return empty($errors) ?
TRUE : $errors;
356 * Process the form when submitted.
358 public function postProcess() {
359 CRM_Utils_System
::flushCache('CRM_Core_DAO_Group');
361 $updateNestingCache = FALSE;
362 if ($this->_action
& CRM_Core_Action
::DELETE
) {
363 CRM_Contact_BAO_Group
::discard($this->_id
);
364 CRM_Core_Session
::setStatus(ts("The Group '%1' has been deleted.", array(1 => $this->_title
)), ts('Group Deleted'), 'success');
365 $updateNestingCache = TRUE;
368 // store the submitted values in an array
369 $params = $this->controller
->exportValues($this->_name
);
371 $params['is_active'] = CRM_Utils_Array
::value('is_active', $this->_groupValues
, 1);
373 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
374 $params['id'] = $this->_id
;
377 if ($this->_action
& CRM_Core_Action
::UPDATE
&& isset($this->_groupOrganizationID
)) {
378 $params['group_organization'] = $this->_groupOrganizationID
;
381 $params['is_reserved'] = CRM_Utils_Array
::value('is_reserved', $params, FALSE);
383 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
388 $group = CRM_Contact_BAO_Group
::create($params);
390 //Remove any parent groups requested to be removed
391 if (!empty($this->_groupValues
['parents'])) {
392 $parentGroupIds = explode(',', $this->_groupValues
['parents']);
393 foreach ($parentGroupIds as $parentGroupId) {
394 if (isset($params["remove_parent_group_$parentGroupId"])) {
395 CRM_Contact_BAO_GroupNesting
::remove($parentGroupId, $group->id
);
396 $updateNestingCache = TRUE;
401 CRM_Core_Session
::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title
)), ts('Group Saved'), 'success');
403 // Add context to the session, in case we are adding members to the group
404 if ($this->_action
& CRM_Core_Action
::ADD
) {
405 $this->set('context', 'amtg');
406 $this->set('amtgID', $group->id
);
408 $session = CRM_Core_Session
::singleton();
409 $session->pushUserContext(CRM_Utils_System
::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id
));
413 // update the nesting cache
414 if ($updateNestingCache) {
415 CRM_Contact_BAO_GroupNestingCache
::update();
420 * Build parent groups form elements.
422 * @param CRM_Core_Form $form
427 public static function buildParentGroups(&$form) {
428 $groupNames = CRM_Core_PseudoConstant
::group();
429 $parentGroups = $parentGroupElements = array();
430 if (isset($form->_id
) && !empty($form->_groupValues
['parents'])) {
431 $parentGroupIds = explode(',', $form->_groupValues
['parents']);
432 foreach ($parentGroupIds as $parentGroupId) {
433 $parentGroups[$parentGroupId] = $groupNames[$parentGroupId];
434 if (array_key_exists($parentGroupId, $groupNames)) {
435 $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId];
436 $form->addElement('checkbox', "remove_parent_group_$parentGroupId",
437 $groupNames[$parentGroupId]
442 $form->assign_by_ref('parent_groups', $parentGroupElements);
444 if (isset($form->_id
)) {
445 $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache
::getPotentialCandidates($form->_id
, $groupNames);
448 $potentialParentGroupIds = array_keys($groupNames);
451 $parentGroupSelectValues = array('' => '- ' . ts('select group') . ' -');
452 foreach ($potentialParentGroupIds as $potentialParentGroupId) {
453 if (array_key_exists($potentialParentGroupId, $groupNames)) {
454 $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId];
458 if (count($parentGroupSelectValues) > 1) {
459 if (CRM_Core_Permission
::isMultisiteEnabled()) {
460 $required = !isset($form->_id
) ||
($form->_id
&& CRM_Core_BAO_Domain
::isDomainGroup($form->_id
)) ?
FALSE : empty($parentGroups);
465 $form->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2'));
468 return $parentGroups;
472 * Add the group organization checkbox to the form.
474 * Note this was traditionally a multisite thing - there is no particular reason why it is not available
475 * as a general field - it's historical use-case driven.
477 * @param CRM_Core_Form $form
479 public static function buildGroupOrganizations(&$form) {
480 if (CRM_Core_Permission
::check('administer Multiple Organizations') && CRM_Core_Permission
::isMultisiteEnabled()) {
481 //group organization Element
482 $props = array('api' => array('params' => array('contact_type' => 'Organization')));
483 $form->addEntityRef('organization_id', ts('Organization'), $props);