Merge pull request #1765 from lynndanzig/master
[civicrm-core.git] / CRM / Group / Form / Edit.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is to build the form for adding Group
38 */
39 class CRM_Group_Form_Edit extends CRM_Core_Form {
40
41 /**
42 * the group id, used when editing a group
43 *
44 * @var int
45 */
46 protected $_id;
47
48 /**
49 * the group object, if an id is present
50 *
51 * @var object
52 */
53 protected $_group;
54
55 /**
56 * The title of the group being deleted
57 *
58 * @var string
59 */
60 protected $_title;
61
62 /**
63 * Store the group values
64 *
65 * @var array
66 */
67 protected $_groupValues;
68
69 /**
70 * what blocks should we show and hide.
71 *
72 * @var CRM_Core_ShowHideBlocks
73 */
74 protected $_showHide;
75
76 /**
77 * the civicrm_group_organization table id
78 *
79 * @var int
80 */
81 protected $_groupOrganizationID;
82
83 /**
84 * set up variables to build the form
85 *
86 * @return void
87 * @acess protected
88 */
89 function preProcess() {
90 $this->_id = $this->get('id');
91 if ($this->_id) {
92 $breadCrumb = array(array('title' => ts('Manage Groups'),
93 'url' => CRM_Utils_System::url('civicrm/group',
94 'reset=1'
95 ),
96 ));
97 CRM_Utils_System::appendBreadCrumb($breadCrumb);
98
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'];
103 }
104
105 $this->assign('action', $this->_action);
106 $this->assign('showBlockJS', TRUE);
107
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'));
113 }
114 }
115 else {
116 if (isset($this->_id)) {
117 $groupValues = array(
118 'id' => $this->_id,
119 'title' => $this->_title,
120 'saved_search_id' =>
121 isset($this->_groupValues['saved_search_id']) ?
122 $this->_groupValues['saved_search_id'] : '',
123 );
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'],
127 'mapping_id'
128 );
129 $groupValues['search_custom_id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch',
130 $this->_groupValues['saved_search_id'],
131 'search_custom_id'
132 );
133 }
134 if (CRM_Utils_Array::value('created_id', $this->_groupValues))
135 $groupValues['created_by'] =
136 CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['created_id'] , 'sort_name', 'id');
137
138 if (CRM_Utils_Array::value('modified_id', $this->_groupValues)) {
139 $groupValues['modified_by'] =
140 CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['modified_id'] , 'sort_name', 'id');
141 }
142
143 $this->assign_by_ref('group', $groupValues);
144
145 CRM_Utils_System::setTitle(ts('Group Settings: %1', array(1 => $this->_title)));
146 }
147 $session = CRM_Core_Session::singleton();
148 $session->pushUserContext(CRM_Utils_System::url('civicrm/group', 'reset=1'));
149 }
150
151 //build custom data
152 CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'Group', $this->_id);
153 }
154
155 /*
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
158 *
159 * @access public
160 * @return None
161 */
162 function setDefaultValues() {
163 $defaults = array();
164
165 if (isset($this->_id)) {
166 $defaults = $this->_groupValues;
167 if (CRM_Utils_Array::value('group_type', $defaults)) {
168 $types = explode(CRM_Core_DAO::VALUE_SEPARATOR,
169 substr($defaults['group_type'], 1, -1)
170 );
171 $defaults['group_type'] = array();
172 foreach ($types as $type) {
173 $defaults['group_type'][$type] = 1;
174 }
175 }
176
177 if (CRM_Core_Permission::check('administer Multiple Organizations') &&
178 CRM_Core_Permission::isMultisiteEnabled()
179 ) {
180 CRM_Contact_BAO_GroupOrganization::retrieve($this->_id, $defaults);
181
182 if (CRM_Utils_Array::value('group_organization', $defaults)) {
183 //used in edit mode
184 $this->_groupOrganizationID = $defaults['group_organization'];
185 }
186
187 $this->assign('organizationID', CRM_Utils_Array::value('organization_id',$defaults));
188 }
189 }
190
191 if (!((CRM_Core_Permission::check('access CiviMail')) ||
192 (CRM_Mailing_Info::workflowEnabled() &&
193 CRM_Core_Permission::check('create mailings')
194 )
195 )) {
196 $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
197 if ($defaults['group_type'][$groupTypes['Mailing List']] == 1) {
198 $this->assign('freezeMailignList', $groupTypes['Mailing List']);
199 }
200 else {
201 $this->assign('hideMailignList', $groupTypes['Mailing List']);
202 }
203 }
204
205 if (!CRM_Utils_Array::value('parents', $defaults)) {
206 $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId();
207 }
208
209 // custom data set defaults
210 $defaults += CRM_Custom_Form_CustomData::setDefaultValues($this);
211 return $defaults;
212 }
213
214 /**
215 * Function to actually build the form
216 *
217 * @return None
218 * @access public
219 */
220 public function buildQuickForm() {
221 if ($this->_action == CRM_Core_Action::DELETE) {
222 $this->addButtons(array(
223 array(
224 'type' => 'next',
225 'name' => ts('Delete Group'),
226 'isDefault' => TRUE,
227 ),
228 array(
229 'type' => 'cancel',
230 'name' => ts('Cancel'),
231 ),
232 )
233 );
234 return;
235 }
236
237 $this->applyFilter('__ALL__', 'trim');
238 $this->add('text', 'title', ts('Name') . ' ',
239 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
240 );
241
242 $this->add('textarea', 'description', ts('Description') . ' ',
243 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')
244 );
245
246 $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
247 $config = CRM_Core_Config::singleton();
248 if (isset($this->_id) &&
249 CRM_Utils_Array::value('saved_search_id', $this->_groupValues)
250 ) {
251 unset($groupTypes['Access Control']);
252 }
253
254 if (!empty($groupTypes)) {
255 $this->addCheckBox('group_type',
256 ts('Group Type'),
257 $groupTypes,
258 NULL, NULL, NULL, NULL, '&nbsp;&nbsp;&nbsp;'
259 );
260 }
261
262 $this->add('select', 'visibility', ts('Visibility'),
263 CRM_Core_SelectValues::ufVisibility(TRUE), TRUE
264 );
265
266 $groupNames = CRM_Core_PseudoConstant::group();
267
268 $parentGroups = $parentGroupElements = array();
269 if (isset($this->_id) &&
270 CRM_Utils_Array::value('parents', $this->_groupValues)
271 ) {
272 $parentGroupIds = explode(',', $this->_groupValues['parents']);
273 foreach ($parentGroupIds as $parentGroupId) {
274 $parentGroups[$parentGroupId] = $groupNames[$parentGroupId];
275 if (array_key_exists($parentGroupId, $groupNames)) {
276 $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId];
277 $this->addElement('checkbox', "remove_parent_group_$parentGroupId",
278 $groupNames[$parentGroupId]
279 );
280 }
281 }
282 }
283 $this->assign_by_ref('parent_groups', $parentGroupElements);
284
285 if (isset($this->_id)) {
286 $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($this->_id,
287 $groupNames
288 );
289 }
290 else {
291 $potentialParentGroupIds = array_keys($groupNames);
292 }
293
294 $parentGroupSelectValues = array('' => '- ' . ts('select') . ' -');
295 foreach ($potentialParentGroupIds as $potentialParentGroupId) {
296 if (array_key_exists($potentialParentGroupId, $groupNames)) {
297 $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId];
298 }
299 }
300
301 if (count($parentGroupSelectValues) > 1) {
302 if (CRM_Core_Permission::isMultisiteEnabled()) {
303 $required = empty($parentGroups) ? TRUE : FALSE;
304 $required = (($this->_id && CRM_Core_BAO_Domain::isDomainGroup($this->_id)) ||
305 !isset($this->_id)
306 ) ? FALSE : $required;
307 }
308 else {
309 $required = FALSE;
310 }
311 $this->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required);
312 }
313 if (CRM_Core_Permission::check('administer Multiple Organizations') &&
314 CRM_Core_Permission::isMultisiteEnabled()
315 ) {
316 //group organization Element
317 $groupOrgDataURL = CRM_Utils_System::url('civicrm/ajax/search', 'org=1', FALSE, NULL, FALSE);
318 $this->assign('groupOrgDataURL', $groupOrgDataURL);
319
320 $this->addElement('text', 'organization', ts('Organization'), '');
321 $this->addElement('hidden', 'organization_id', '', array('id' => 'organization_id'));
322 }
323
324 // is_reserved property CRM-9936
325 $this->addElement('checkbox', 'is_reserved', ts('Reserved Group?'));
326 if (!CRM_Core_Permission::check('administer reserved groups')) {
327 $this->freeze('is_reserved');
328 }
329
330 //build custom data
331 CRM_Custom_Form_CustomData::buildQuickForm($this);
332
333 $this->addButtons(array(
334 array(
335 'type' => 'upload',
336 'name' =>
337 ($this->_action == CRM_Core_Action::ADD) ?
338 ts('Continue') : ts('Save'),
339 'isDefault' => TRUE,
340 ),
341 array(
342 'type' => 'cancel',
343 'name' => ts('Cancel'),
344 ),
345 )
346 );
347
348 $doParentCheck = FALSE;
349 if (CRM_Core_Permission::isMultisiteEnabled()) {
350 $doParentCheck = ($this->_id && CRM_Core_BAO_Domain::isDomainGroup($this->_id)) ? FALSE : TRUE;
351 }
352
353 $options = array(
354 'selfObj' => $this,
355 'parentGroups' => $parentGroups,
356 'doParentCheck' => $doParentCheck,
357 );
358 $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options);
359 }
360
361 /**
362 * global validation rules for the form
363 *
364 * @param array $fields posted values of the form
365 *
366 * @return array list of errors to be posted back to the form
367 * @static
368 * @access public
369 */
370 static function formRule($fields, $fileParams, $options) {
371 $errors = array();
372
373 $doParentCheck = $options['doParentCheck'];
374 $self = &$options['selfObj'];
375
376 if ($doParentCheck) {
377 $parentGroups = $options['parentGroups'];
378
379 $grpRemove = 0;
380 foreach ($fields as $key => $val) {
381 if (substr($key, 0, 20) == 'remove_parent_group_') {
382 $grpRemove++;
383 }
384 }
385
386 $grpAdd = 0;
387 if (CRM_Utils_Array::value('parents', $fields)) {
388 $grpAdd++;
389 }
390
391 if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) {
392 $errors['parents'] = ts('Make sure at least one parent group is set.');
393 }
394 }
395
396 // do check for both name and title uniqueness
397 if (CRM_Utils_Array::value('title', $fields)) {
398 $title = trim($fields['title']);
399 $query = "
400 SELECT count(*)
401 FROM civicrm_group
402 WHERE title = %1
403 ";
404 $params = array(1 => array($title, 'String'));
405
406 if ($self->_id) {
407 $query .= "AND id <> %2";
408 $params[2] = array($self->_id, 'Integer');
409 }
410
411 $grpCnt = CRM_Core_DAO::singleValueQuery($query, $params);
412 if ($grpCnt) {
413 $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title']));
414 }
415 }
416
417 return empty($errors) ? TRUE : $errors;
418 }
419
420 /**
421 * Process the form when submitted
422 *
423 * @return void
424 * @access public
425 */
426 public function postProcess() {
427 CRM_Utils_System::flushCache('CRM_Core_DAO_Group');
428
429 $updateNestingCache = FALSE;
430 if ($this->_action & CRM_Core_Action::DELETE) {
431 CRM_Contact_BAO_Group::discard($this->_id);
432 CRM_Core_Session::setStatus(ts("The Group '%1' has been deleted.", array(1 => $this->_title)), ts('Group Deleted'), 'success');
433 $updateNestingCache = TRUE;
434 }
435 else {
436 // store the submitted values in an array
437 $params = $this->controller->exportValues($this->_name);
438
439 $params['is_active'] = CRM_Utils_Array::value('is_active', $this->_groupValues, 1);
440
441 if ($this->_action & CRM_Core_Action::UPDATE) {
442 $params['id'] = $this->_id;
443 }
444
445 if ($this->_action & CRM_Core_Action::UPDATE && isset($this->_groupOrganizationID)) {
446 $params['group_organization'] = $this->_groupOrganizationID;
447 }
448
449 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
450
451 $customFields = CRM_Core_BAO_CustomField::getFields('Group');
452 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
453 $customFields,
454 $this->_id,
455 'Group'
456 );
457
458 $group = CRM_Contact_BAO_Group::create($params);
459
460 /*
461 * Remove any parent groups requested to be removed
462 */
463
464 if (CRM_Utils_Array::value('parents', $this->_groupValues)) {
465 $parentGroupIds = explode(',', $this->_groupValues['parents']);
466 foreach ($parentGroupIds as $parentGroupId) {
467 if (isset($params["remove_parent_group_$parentGroupId"])) {
468 CRM_Contact_BAO_GroupNesting::remove($parentGroupId, $group->id);
469 $updateNestingCache = TRUE;
470 }
471 }
472 }
473
474 CRM_Core_Session::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title)), ts('Group Saved'), 'success');
475
476 /*
477 * Add context to the session, in case we are adding members to the group
478 */
479
480 if ($this->_action & CRM_Core_Action::ADD) {
481 $this->set('context', 'amtg');
482 $this->set('amtgID', $group->id);
483
484 $session = CRM_Core_Session::singleton();
485 $session->pushUserContext(CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id));
486 }
487 }
488
489 // update the nesting cache
490 if ($updateNestingCache) {
491 CRM_Contact_BAO_GroupNestingCache::update();
492 }
493 }
494 }
495