3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This class is to build the form for adding Group
39 class CRM_Group_Form_Edit
extends CRM_Core_Form
{
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 up variables to build the form
89 function preProcess() {
90 $this->_id
= $this->get('id');
92 $breadCrumb = array(array('title' => ts('Manage Groups'),
93 'url' => CRM_Utils_System
::url('civicrm/group',
97 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
99 $this->_groupValues
= array();
100 $params = array('id' => $this->_id
);
101 $this->_group
= CRM_Contact_BAO_Group
::retrieve($params, $this->_groupValues
);
102 $this->_title
= $this->_groupValues
['title'];
105 $this->assign('action', $this->_action
);
106 $this->assign('showBlockJS', TRUE);
108 if ($this->_action
== CRM_Core_Action
::DELETE
) {
109 if (isset($this->_id
)) {
110 $this->assign('title', $this->_title
);
111 $this->assign('count', CRM_Contact_BAO_Group
::memberCount($this->_id
));
112 CRM_Utils_System
::setTitle(ts('Confirm Group Delete'));
116 if (isset($this->_id
)) {
117 $groupValues = array(
119 'title' => $this->_title
,
121 isset($this->_groupValues
['saved_search_id']) ?
122 $this->_groupValues
['saved_search_id'] : '',
124 if (isset($this->_groupValues
['saved_search_id'])) {
125 $groupValues['mapping_id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch',
126 $this->_groupValues
['saved_search_id'],
129 $groupValues['search_custom_id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch',
130 $this->_groupValues
['saved_search_id'],
134 if (!empty($this->_groupValues
['created_id']))
135 $groupValues['created_by'] =
136 CRM_Core_DAO
::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues
['created_id'] , 'sort_name', 'id');
138 if (!empty($this->_groupValues
['modified_id'])) {
139 $groupValues['modified_by'] =
140 CRM_Core_DAO
::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues
['modified_id'] , 'sort_name', 'id');
143 $this->assign_by_ref('group', $groupValues);
145 CRM_Utils_System
::setTitle(ts('Group Settings: %1', array(1 => $this->_title
)));
147 $session = CRM_Core_Session
::singleton();
148 $session->pushUserContext(CRM_Utils_System
::url('civicrm/group', 'reset=1'));
152 CRM_Custom_Form_CustomData
::preProcess($this, NULL, NULL, 1, 'Group', $this->_id
);
156 * This function sets the default values for the form. LocationType that in edit/view mode
157 * the default values are retrieved from the database
162 function setDefaultValues() {
165 if (isset($this->_id
)) {
166 $defaults = $this->_groupValues
;
167 if (!empty($defaults['group_type'])) {
168 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
169 substr($defaults['group_type'], 1, -1)
171 $defaults['group_type'] = array();
172 foreach ($types as $type) {
173 $defaults['group_type'][$type] = 1;
177 if (CRM_Core_Permission
::check('administer Multiple Organizations') && CRM_Core_Permission
::isMultisiteEnabled()) {
178 CRM_Contact_BAO_GroupOrganization
::retrieve($this->_id
, $defaults);
182 if (!((CRM_Core_Permission
::check('access CiviMail')) ||
183 (CRM_Mailing_Info
::workflowEnabled() &&
184 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 * Function to actually build the form
211 public function buildQuickForm() {
212 if ($this->_action
== CRM_Core_Action
::DELETE
) {
213 $this->addButtons(array(
216 'name' => ts('Delete Group'),
221 'name' => ts('Cancel'),
228 // We want the "new group" form to redirect the user
229 if ($this->_action
== CRM_Core_Action
::ADD
) {
230 $this->preventAjaxSubmit();
233 $this->applyFilter('__ALL__', 'trim');
234 $this->add('text', 'title', ts('Name') . ' ',
235 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
238 $this->add('textarea', 'description', ts('Description') . ' ',
239 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'description')
242 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
243 $config = CRM_Core_Config
::singleton();
244 if (isset($this->_id
) && !empty($this->_groupValues
['saved_search_id'])) {
245 unset($groupTypes['Access Control']);
248 if (!empty($groupTypes)) {
249 $this->addCheckBox('group_type',
252 NULL, NULL, NULL, NULL, ' '
256 $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues
::groupVisibility(), TRUE);
259 $parentGroups = self
::buildParentGroups($this);
261 if (CRM_Core_Permission
::check('administer Multiple Organizations') && CRM_Core_Permission
::isMultisiteEnabled()) {
262 //group organization Element
263 $props = array('api' => array('params' => array('contact_type' => 'Organization')));
264 $this->addEntityRef('organization_id', ts('Organization'), $props);
267 // is_reserved property CRM-9936
268 $this->addElement('checkbox', 'is_reserved', ts('Reserved Group?'));
269 if (!CRM_Core_Permission
::check('administer reserved groups')) {
270 $this->freeze('is_reserved');
274 CRM_Custom_Form_CustomData
::buildQuickForm($this);
276 $this->addButtons(array(
280 ($this->_action
== CRM_Core_Action
::ADD
) ?
281 ts('Continue') : ts('Save'),
286 'name' => ts('Cancel'),
291 $doParentCheck = FALSE;
292 if (CRM_Core_Permission
::isMultisiteEnabled()) {
293 $doParentCheck = ($this->_id
&& CRM_Core_BAO_Domain
::isDomainGroup($this->_id
)) ?
FALSE : TRUE;
298 'parentGroups' => $parentGroups,
299 'doParentCheck' => $doParentCheck,
301 $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options);
305 * global validation rules for the form
307 * @param array $fields posted values of the form
309 * @return array list of errors to be posted back to the form
313 static function formRule($fields, $fileParams, $options) {
316 $doParentCheck = $options['doParentCheck'];
317 $self = &$options['selfObj'];
319 if ($doParentCheck) {
320 $parentGroups = $options['parentGroups'];
323 foreach ($fields as $key => $val) {
324 if (substr($key, 0, 20) == 'remove_parent_group_') {
330 if (!empty($fields['parents'])) {
334 if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) {
335 $errors['parents'] = ts('Make sure at least one parent group is set.');
339 // do check for both name and title uniqueness
340 if (!empty($fields['title'])) {
341 $title = trim($fields['title']);
347 $params = array(1 => array($title, 'String'));
350 $query .= "AND id <> %2";
351 $params[2] = array($self->_id
, 'Integer');
354 $grpCnt = CRM_Core_DAO
::singleValueQuery($query, $params);
356 $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title']));
360 return empty($errors) ?
TRUE : $errors;
364 * Process the form when submitted
369 public function postProcess() {
370 CRM_Utils_System
::flushCache('CRM_Core_DAO_Group');
372 $updateNestingCache = FALSE;
373 if ($this->_action
& CRM_Core_Action
::DELETE
) {
374 CRM_Contact_BAO_Group
::discard($this->_id
);
375 CRM_Core_Session
::setStatus(ts("The Group '%1' has been deleted.", array(1 => $this->_title
)), ts('Group Deleted'), 'success');
376 $updateNestingCache = TRUE;
379 // store the submitted values in an array
380 $params = $this->controller
->exportValues($this->_name
);
382 $params['is_active'] = CRM_Utils_Array
::value('is_active', $this->_groupValues
, 1);
384 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
385 $params['id'] = $this->_id
;
388 if ($this->_action
& CRM_Core_Action
::UPDATE
&& isset($this->_groupOrganizationID
)) {
389 $params['group_organization'] = $this->_groupOrganizationID
;
392 $params['is_reserved'] = CRM_Utils_Array
::value('is_reserved', $params, FALSE);
394 $customFields = CRM_Core_BAO_CustomField
::getFields('Group');
395 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
401 $group = CRM_Contact_BAO_Group
::create($params);
404 * Remove any parent groups requested to be removed
407 if (!empty($this->_groupValues
['parents'])) {
408 $parentGroupIds = explode(',', $this->_groupValues
['parents']);
409 foreach ($parentGroupIds as $parentGroupId) {
410 if (isset($params["remove_parent_group_$parentGroupId"])) {
411 CRM_Contact_BAO_GroupNesting
::remove($parentGroupId, $group->id
);
412 $updateNestingCache = TRUE;
417 CRM_Core_Session
::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title
)), ts('Group Saved'), 'success');
420 * Add context to the session, in case we are adding members to the group
423 if ($this->_action
& CRM_Core_Action
::ADD
) {
424 $this->set('context', 'amtg');
425 $this->set('amtgID', $group->id
);
427 $session = CRM_Core_Session
::singleton();
428 $session->pushUserContext(CRM_Utils_System
::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id
));
432 // update the nesting cache
433 if ($updateNestingCache) {
434 CRM_Contact_BAO_GroupNestingCache
::update();
439 * Build parent groups form elements
441 * @obj form object passed by reference
443 * @return parent groups array
447 static function buildParentGroups( &$obj ) {
448 $groupNames = CRM_Core_PseudoConstant
::group();
449 $parentGroups = $parentGroupElements = array();
450 if (isset($obj->_id
) &&
451 CRM_Utils_Array
::value('parents', $obj->_groupValues
)
453 $parentGroupIds = explode(',', $obj->_groupValues
['parents']);
454 foreach ($parentGroupIds as $parentGroupId) {
455 $parentGroups[$parentGroupId] = $groupNames[$parentGroupId];
456 if (array_key_exists($parentGroupId, $groupNames)) {
457 $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId];
458 $obj->addElement('checkbox', "remove_parent_group_$parentGroupId",
459 $groupNames[$parentGroupId]
464 $obj->assign_by_ref('parent_groups', $parentGroupElements);
466 if (isset($obj->_id
)) {
467 $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache
::getPotentialCandidates($obj->_id
,
472 $potentialParentGroupIds = array_keys($groupNames);
475 $parentGroupSelectValues = array('' => '- ' . ts('select group') . ' -');
476 foreach ($potentialParentGroupIds as $potentialParentGroupId) {
477 if (array_key_exists($potentialParentGroupId, $groupNames)) {
478 $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId];
482 if (count($parentGroupSelectValues) > 1) {
483 if (CRM_Core_Permission
::isMultisiteEnabled()) {
484 $required = empty($parentGroups) ?
TRUE : FALSE;
485 $required = (($obj->_id
&& CRM_Core_BAO_Domain
::isDomainGroup($obj->_id
)) ||
487 ) ?
FALSE : $required;
492 $obj->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2'));
495 return $parentGroups;