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