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');
93 $breadCrumb = array(array('title' => ts('Manage Groups'),
94 'url' => CRM_Utils_System
::url('civicrm/group',
98 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
100 $this->_groupValues
= array();
101 $params = array('id' => $this->_id
);
102 $this->_group
= CRM_Contact_BAO_Group
::retrieve($params, $this->_groupValues
);
103 $this->_title
= $this->_groupValues
['title'];
106 $this->assign('action', $this->_action
);
107 $this->assign('showBlockJS', TRUE);
109 if ($this->_action
== CRM_Core_Action
::DELETE
) {
110 if (isset($this->_id
)) {
111 $this->assign('title', $this->_title
);
112 $this->assign('count', CRM_Contact_BAO_Group
::memberCount($this->_id
));
113 CRM_Utils_System
::setTitle(ts('Confirm Group Delete'));
117 if (isset($this->_id
)) {
118 $groupValues = array(
120 'title' => $this->_title
,
122 isset($this->_groupValues
['saved_search_id']) ?
123 $this->_groupValues
['saved_search_id'] : '',
125 if (isset($this->_groupValues
['saved_search_id'])) {
126 $groupValues['mapping_id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch',
127 $this->_groupValues
['saved_search_id'],
130 $groupValues['search_custom_id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch',
131 $this->_groupValues
['saved_search_id'],
135 if (CRM_Utils_Array
::value('created_id', $this->_groupValues
))
136 $groupValues['created_by'] =
137 CRM_Core_DAO
::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues
['created_id'] , 'sort_name', 'id');
139 $this->assign_by_ref('group', $groupValues);
141 CRM_Utils_System
::setTitle(ts('Group Settings: %1', array(1 => $this->_title
)));
143 $session = CRM_Core_Session
::singleton();
144 $session->pushUserContext(CRM_Utils_System
::url('civicrm/group', 'reset=1'));
148 CRM_Custom_Form_CustomData
::preProcess($this, NULL, NULL, 1, 'Group', $this->_id
);
152 * This function sets the default values for the form. LocationType that in edit/view mode
153 * the default values are retrieved from the database
158 function setDefaultValues() {
161 if (isset($this->_id
)) {
162 $defaults = $this->_groupValues
;
163 if (CRM_Utils_Array
::value('group_type', $defaults)) {
164 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
165 substr($defaults['group_type'], 1, -1)
167 $defaults['group_type'] = array();
168 foreach ($types as $type) {
169 $defaults['group_type'][$type] = 1;
173 if (CRM_Core_Permission
::check('administer Multiple Organizations') &&
174 CRM_Core_Permission
::isMultisiteEnabled()
176 CRM_Contact_BAO_GroupOrganization
::retrieve($this->_id
, $defaults);
178 if (CRM_Utils_Array
::value('group_organization', $defaults)) {
180 $this->_groupOrganizationID
= $defaults['group_organization'];
183 $this->assign('organizationID', CRM_Utils_Array
::value('organization_id',$defaults));
187 if (!((CRM_Core_Permission
::check('access CiviMail')) ||
188 (CRM_Mailing_Info
::workflowEnabled() &&
189 CRM_Core_Permission
::check('create mailings')
192 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
193 if ($defaults['group_type'][$groupTypes['Mailing List']] == 1) {
194 $this->assign('freezeMailignList', $groupTypes['Mailing List']);
197 $this->assign('hideMailignList', $groupTypes['Mailing List']);
201 if (!CRM_Utils_Array
::value('parents', $defaults)) {
202 $defaults['parents'] = CRM_Core_BAO_Domain
::getGroupId();
205 // custom data set defaults
206 $defaults +
= CRM_Custom_Form_CustomData
::setDefaultValues($this);
211 * Function to actually build the form
216 public function buildQuickForm() {
217 if ($this->_action
== CRM_Core_Action
::DELETE
) {
218 $this->addButtons(array(
221 'name' => ts('Delete Group'),
226 'name' => ts('Cancel'),
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
) &&
245 CRM_Utils_Array
::value('saved_search_id', $this->_groupValues
)
247 unset($groupTypes['Access Control']);
250 if (!empty($groupTypes)) {
251 $this->addCheckBox('group_type',
254 NULL, NULL, NULL, NULL, ' '
258 $this->add('select', 'visibility', ts('Visibility'),
259 CRM_Core_SelectValues
::ufVisibility(TRUE), TRUE
262 $groupNames = CRM_Core_PseudoConstant
::group();
264 $parentGroups = $parentGroupElements = array();
265 if (isset($this->_id
) &&
266 CRM_Utils_Array
::value('parents', $this->_groupValues
)
268 $parentGroupIds = explode(',', $this->_groupValues
['parents']);
269 foreach ($parentGroupIds as $parentGroupId) {
270 $parentGroups[$parentGroupId] = $groupNames[$parentGroupId];
271 if (array_key_exists($parentGroupId, $groupNames)) {
272 $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId];
273 $this->addElement('checkbox', "remove_parent_group_$parentGroupId",
274 $groupNames[$parentGroupId]
279 $this->assign_by_ref('parent_groups', $parentGroupElements);
281 if (isset($this->_id
)) {
282 $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache
::getPotentialCandidates($this->_id
,
287 $potentialParentGroupIds = array_keys($groupNames);
290 $parentGroupSelectValues = array('' => '- ' . ts('select') . ' -');
291 foreach ($potentialParentGroupIds as $potentialParentGroupId) {
292 if (array_key_exists($potentialParentGroupId, $groupNames)) {
293 $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId];
297 if (count($parentGroupSelectValues) > 1) {
298 if (CRM_Core_Permission
::isMultisiteEnabled()) {
299 $required = empty($parentGroups) ?
TRUE : FALSE;
300 $required = (($this->_id
&& CRM_Core_BAO_Domain
::isDomainGroup($this->_id
)) ||
302 ) ?
FALSE : $required;
307 $this->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required);
309 if (CRM_Core_Permission
::check('administer Multiple Organizations') &&
310 CRM_Core_Permission
::isMultisiteEnabled()
312 //group organization Element
313 $groupOrgDataURL = CRM_Utils_System
::url('civicrm/ajax/search', 'org=1', FALSE, NULL, FALSE);
314 $this->assign('groupOrgDataURL', $groupOrgDataURL);
316 $this->addElement('text', 'organization', ts('Organization'), '');
317 $this->addElement('hidden', 'organization_id', '', array('id' => 'organization_id'));
320 // is_reserved property CRM-9936
321 $this->addElement('checkbox', 'is_reserved', ts('Reserved Group?'));
322 if (!CRM_Core_Permission
::check('administer reserved groups')) {
323 $this->freeze('is_reserved');
327 CRM_Custom_Form_CustomData
::buildQuickForm($this);
329 $this->addButtons(array(
333 ($this->_action
== CRM_Core_Action
::ADD
) ?
334 ts('Continue') : ts('Save'),
339 'name' => ts('Cancel'),
344 $doParentCheck = FALSE;
345 if (CRM_Core_Permission
::isMultisiteEnabled()) {
346 $doParentCheck = ($this->_id
&& CRM_Core_BAO_Domain
::isDomainGroup($this->_id
)) ?
FALSE : TRUE;
351 'parentGroups' => $parentGroups,
352 'doParentCheck' => $doParentCheck,
354 $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options);
358 * global validation rules for the form
360 * @param array $fields posted values of the form
362 * @return array list of errors to be posted back to the form
366 static function formRule($fields, $fileParams, $options) {
369 $doParentCheck = $options['doParentCheck'];
370 $self = &$options['selfObj'];
372 if ($doParentCheck) {
373 $parentGroups = $options['parentGroups'];
376 foreach ($fields as $key => $val) {
377 if (substr($key, 0, 20) == 'remove_parent_group_') {
383 if (CRM_Utils_Array
::value('parents', $fields)) {
387 if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) {
388 $errors['parents'] = ts('Make sure at least one parent group is set.');
392 // do check for both name and title uniqueness
393 if (CRM_Utils_Array
::value('title', $fields)) {
394 $title = trim($fields['title']);
400 $params = array(1 => array($title, 'String'));
403 $query .= "AND id <> %2";
404 $params[2] = array($self->_id
, 'Integer');
407 $grpCnt = CRM_Core_DAO
::singleValueQuery($query, $params);
409 $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title']));
413 return empty($errors) ?
TRUE : $errors;
417 * Process the form when submitted
422 public function postProcess() {
423 CRM_Utils_System
::flushCache('CRM_Core_DAO_Group');
425 $updateNestingCache = FALSE;
426 if ($this->_action
& CRM_Core_Action
::DELETE
) {
427 CRM_Contact_BAO_Group
::discard($this->_id
);
428 CRM_Core_Session
::setStatus(ts("The Group '%1' has been deleted.", array(1 => $this->_title
)), ts('Group Deleted'), 'success');
429 $updateNestingCache = TRUE;
432 // store the submitted values in an array
433 $params = $this->controller
->exportValues($this->_name
);
435 $params['is_active'] = CRM_Utils_Array
::value('is_active', $this->_groupValues
, 1);
437 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
438 $params['id'] = $this->_id
;
441 if ($this->_action
& CRM_Core_Action
::UPDATE
&& isset($this->_groupOrganizationID
)) {
442 $params['group_organization'] = $this->_groupOrganizationID
;
445 $params['is_reserved'] = CRM_Utils_Array
::value('is_reserved', $params, FALSE);
447 $customFields = CRM_Core_BAO_CustomField
::getFields('Group');
448 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
454 $group = CRM_Contact_BAO_Group
::create($params);
457 * Remove any parent groups requested to be removed
460 if (CRM_Utils_Array
::value('parents', $this->_groupValues
)) {
461 $parentGroupIds = explode(',', $this->_groupValues
['parents']);
462 foreach ($parentGroupIds as $parentGroupId) {
463 if (isset($params["remove_parent_group_$parentGroupId"])) {
464 CRM_Contact_BAO_GroupNesting
::remove($parentGroupId, $group->id
);
465 $updateNestingCache = TRUE;
470 CRM_Core_Session
::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title
)), ts('Group Saved'), 'success');
473 * Add context to the session, in case we are adding members to the group
476 if ($this->_action
& CRM_Core_Action
::ADD
) {
477 $this->set('context', 'amtg');
478 $this->set('amtgID', $group->id
);
480 $session = CRM_Core_Session
::singleton();
481 $session->pushUserContext(CRM_Utils_System
::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id
));
485 // update the nesting cache
486 if ($updateNestingCache) {
487 CRM_Contact_BAO_GroupNestingCache
::update();