Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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'); | |
6a488035 TO |
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); | |
f828fa2c | 101 | $this->_group = CRM_Contact_BAO_Group::retrieve($params, $this->_groupValues); |
6a488035 TO |
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 | } | |
bf4b6f8f JP |
114 | if ($this->_groupValues['is_reserved'] == 1 && !CRM_Core_Permission::check('administer reserved groups')) { |
115 | CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to delete this reserved group.")); | |
116 | } | |
6a488035 TO |
117 | } |
118 | else { | |
bf4b6f8f JP |
119 | if ($this->_groupValues['is_reserved'] == 1 && !CRM_Core_Permission::check('administer reserved groups')) { |
120 | CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to change settings for this reserved group.")); | |
121 | } | |
6a488035 TO |
122 | if (isset($this->_id)) { |
123 | $groupValues = array( | |
124 | 'id' => $this->_id, | |
125 | 'title' => $this->_title, | |
126 | 'saved_search_id' => | |
127 | isset($this->_groupValues['saved_search_id']) ? | |
128 | $this->_groupValues['saved_search_id'] : '', | |
129 | ); | |
130 | if (isset($this->_groupValues['saved_search_id'])) { | |
131 | $groupValues['mapping_id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', | |
132 | $this->_groupValues['saved_search_id'], | |
133 | 'mapping_id' | |
134 | ); | |
135 | $groupValues['search_custom_id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', | |
136 | $this->_groupValues['saved_search_id'], | |
137 | 'search_custom_id' | |
138 | ); | |
139 | } | |
a7488080 | 140 | if (!empty($this->_groupValues['created_id'])) |
f828fa2c | 141 | $groupValues['created_by'] = |
6a488035 TO |
142 | CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['created_id'] , 'sort_name', 'id'); |
143 | ||
a7488080 | 144 | if (!empty($this->_groupValues['modified_id'])) { |
d0dfb649 PJ |
145 | $groupValues['modified_by'] = |
146 | CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['modified_id'] , 'sort_name', 'id'); | |
147 | } | |
148 | ||
6a488035 TO |
149 | $this->assign_by_ref('group', $groupValues); |
150 | ||
151 | CRM_Utils_System::setTitle(ts('Group Settings: %1', array(1 => $this->_title))); | |
152 | } | |
153 | $session = CRM_Core_Session::singleton(); | |
154 | $session->pushUserContext(CRM_Utils_System::url('civicrm/group', 'reset=1')); | |
155 | } | |
156 | ||
157 | //build custom data | |
158 | CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'Group', $this->_id); | |
159 | } | |
160 | ||
161 | /* | |
162 | * This function sets the default values for the form. LocationType that in edit/view mode | |
163 | * the default values are retrieved from the database | |
164 | * | |
165 | * @access public | |
355ba699 | 166 | * @return void |
6a488035 | 167 | */ |
e0ef6999 EM |
168 | /** |
169 | * @return array | |
170 | */ | |
6a488035 TO |
171 | function setDefaultValues() { |
172 | $defaults = array(); | |
173 | ||
174 | if (isset($this->_id)) { | |
175 | $defaults = $this->_groupValues; | |
a7488080 | 176 | if (!empty($defaults['group_type'])) { |
6a488035 TO |
177 | $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
178 | substr($defaults['group_type'], 1, -1) | |
179 | ); | |
180 | $defaults['group_type'] = array(); | |
181 | foreach ($types as $type) { | |
182 | $defaults['group_type'][$type] = 1; | |
183 | } | |
184 | } | |
185 | ||
532e9275 | 186 | if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { |
6a488035 | 187 | CRM_Contact_BAO_GroupOrganization::retrieve($this->_id, $defaults); |
6a488035 TO |
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 | ||
a7488080 | 205 | if (empty($defaults['parents'])) { |
6a488035 TO |
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 | * | |
355ba699 | 217 | * @return void |
6a488035 TO |
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 | ||
cae80d9f CW |
237 | // We want the "new group" form to redirect the user |
238 | if ($this->_action == CRM_Core_Action::ADD) { | |
239 | $this->preventAjaxSubmit(); | |
240 | } | |
241 | ||
6a488035 TO |
242 | $this->applyFilter('__ALL__', 'trim'); |
243 | $this->add('text', 'title', ts('Name') . ' ', | |
244 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE | |
245 | ); | |
246 | ||
247 | $this->add('textarea', 'description', ts('Description') . ' ', | |
248 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description') | |
249 | ); | |
250 | ||
251 | $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE); | |
252 | $config = CRM_Core_Config::singleton(); | |
8cc574cf | 253 | if (isset($this->_id) && !empty($this->_groupValues['saved_search_id'])) { |
6a488035 TO |
254 | unset($groupTypes['Access Control']); |
255 | } | |
256 | ||
257 | if (!empty($groupTypes)) { | |
258 | $this->addCheckBox('group_type', | |
259 | ts('Group Type'), | |
260 | $groupTypes, | |
261 | NULL, NULL, NULL, NULL, ' ' | |
262 | ); | |
263 | } | |
264 | ||
93bfa565 | 265 | $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues::groupVisibility(), TRUE); |
6a488035 | 266 | |
31c31c92 BS |
267 | //CRM-14190 |
268 | $parentGroups = self::buildParentGroups($this); | |
6a488035 | 269 | |
532e9275 | 270 | if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { |
6a488035 | 271 | //group organization Element |
532e9275 CW |
272 | $props = array('api' => array('params' => array('contact_type' => 'Organization'))); |
273 | $this->addEntityRef('organization_id', ts('Organization'), $props); | |
6a488035 TO |
274 | } |
275 | ||
276 | // is_reserved property CRM-9936 | |
277 | $this->addElement('checkbox', 'is_reserved', ts('Reserved Group?')); | |
278 | if (!CRM_Core_Permission::check('administer reserved groups')) { | |
279 | $this->freeze('is_reserved'); | |
280 | } | |
281 | ||
282 | //build custom data | |
283 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
284 | ||
285 | $this->addButtons(array( | |
286 | array( | |
287 | 'type' => 'upload', | |
288 | 'name' => | |
289 | ($this->_action == CRM_Core_Action::ADD) ? | |
290 | ts('Continue') : ts('Save'), | |
291 | 'isDefault' => TRUE, | |
292 | ), | |
293 | array( | |
294 | 'type' => 'cancel', | |
295 | 'name' => ts('Cancel'), | |
296 | ), | |
297 | ) | |
298 | ); | |
299 | ||
300 | $doParentCheck = FALSE; | |
301 | if (CRM_Core_Permission::isMultisiteEnabled()) { | |
302 | $doParentCheck = ($this->_id && CRM_Core_BAO_Domain::isDomainGroup($this->_id)) ? FALSE : TRUE; | |
303 | } | |
304 | ||
305 | $options = array( | |
306 | 'selfObj' => $this, | |
307 | 'parentGroups' => $parentGroups, | |
308 | 'doParentCheck' => $doParentCheck, | |
309 | ); | |
310 | $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options); | |
311 | } | |
312 | ||
313 | /** | |
314 | * global validation rules for the form | |
315 | * | |
316 | * @param array $fields posted values of the form | |
317 | * | |
77b97be7 EM |
318 | * @param $fileParams |
319 | * @param $options | |
320 | * | |
6a488035 TO |
321 | * @return array list of errors to be posted back to the form |
322 | * @static | |
323 | * @access public | |
324 | */ | |
325 | static function formRule($fields, $fileParams, $options) { | |
326 | $errors = array(); | |
327 | ||
328 | $doParentCheck = $options['doParentCheck']; | |
329 | $self = &$options['selfObj']; | |
330 | ||
331 | if ($doParentCheck) { | |
332 | $parentGroups = $options['parentGroups']; | |
333 | ||
334 | $grpRemove = 0; | |
335 | foreach ($fields as $key => $val) { | |
336 | if (substr($key, 0, 20) == 'remove_parent_group_') { | |
337 | $grpRemove++; | |
338 | } | |
339 | } | |
340 | ||
341 | $grpAdd = 0; | |
a7488080 | 342 | if (!empty($fields['parents'])) { |
6a488035 TO |
343 | $grpAdd++; |
344 | } | |
345 | ||
346 | if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) { | |
347 | $errors['parents'] = ts('Make sure at least one parent group is set.'); | |
348 | } | |
349 | } | |
350 | ||
351 | // do check for both name and title uniqueness | |
a7488080 | 352 | if (!empty($fields['title'])) { |
6a488035 TO |
353 | $title = trim($fields['title']); |
354 | $query = " | |
355 | SELECT count(*) | |
356 | FROM civicrm_group | |
357 | WHERE title = %1 | |
358 | "; | |
359 | $params = array(1 => array($title, 'String')); | |
360 | ||
361 | if ($self->_id) { | |
362 | $query .= "AND id <> %2"; | |
363 | $params[2] = array($self->_id, 'Integer'); | |
364 | } | |
365 | ||
366 | $grpCnt = CRM_Core_DAO::singleValueQuery($query, $params); | |
367 | if ($grpCnt) { | |
368 | $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title'])); | |
369 | } | |
370 | } | |
371 | ||
372 | return empty($errors) ? TRUE : $errors; | |
373 | } | |
374 | ||
375 | /** | |
376 | * Process the form when submitted | |
377 | * | |
378 | * @return void | |
379 | * @access public | |
380 | */ | |
381 | public function postProcess() { | |
382 | CRM_Utils_System::flushCache('CRM_Core_DAO_Group'); | |
383 | ||
384 | $updateNestingCache = FALSE; | |
385 | if ($this->_action & CRM_Core_Action::DELETE) { | |
386 | CRM_Contact_BAO_Group::discard($this->_id); | |
387 | CRM_Core_Session::setStatus(ts("The Group '%1' has been deleted.", array(1 => $this->_title)), ts('Group Deleted'), 'success'); | |
388 | $updateNestingCache = TRUE; | |
389 | } | |
390 | else { | |
391 | // store the submitted values in an array | |
392 | $params = $this->controller->exportValues($this->_name); | |
393 | ||
394 | $params['is_active'] = CRM_Utils_Array::value('is_active', $this->_groupValues, 1); | |
395 | ||
396 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
397 | $params['id'] = $this->_id; | |
398 | } | |
399 | ||
400 | if ($this->_action & CRM_Core_Action::UPDATE && isset($this->_groupOrganizationID)) { | |
401 | $params['group_organization'] = $this->_groupOrganizationID; | |
402 | } | |
403 | ||
404 | $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); | |
405 | ||
406 | $customFields = CRM_Core_BAO_CustomField::getFields('Group'); | |
407 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
408 | $customFields, | |
409 | $this->_id, | |
410 | 'Group' | |
411 | ); | |
412 | ||
413 | $group = CRM_Contact_BAO_Group::create($params); | |
414 | ||
415 | /* | |
31c31c92 BS |
416 | * Remove any parent groups requested to be removed |
417 | */ | |
6a488035 | 418 | |
a7488080 | 419 | if (!empty($this->_groupValues['parents'])) { |
6a488035 TO |
420 | $parentGroupIds = explode(',', $this->_groupValues['parents']); |
421 | foreach ($parentGroupIds as $parentGroupId) { | |
422 | if (isset($params["remove_parent_group_$parentGroupId"])) { | |
423 | CRM_Contact_BAO_GroupNesting::remove($parentGroupId, $group->id); | |
424 | $updateNestingCache = TRUE; | |
425 | } | |
426 | } | |
427 | } | |
428 | ||
429 | CRM_Core_Session::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title)), ts('Group Saved'), 'success'); | |
430 | ||
431 | /* | |
432 | * Add context to the session, in case we are adding members to the group | |
433 | */ | |
434 | ||
435 | if ($this->_action & CRM_Core_Action::ADD) { | |
436 | $this->set('context', 'amtg'); | |
437 | $this->set('amtgID', $group->id); | |
438 | ||
439 | $session = CRM_Core_Session::singleton(); | |
440 | $session->pushUserContext(CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id)); | |
441 | } | |
442 | } | |
443 | ||
444 | // update the nesting cache | |
445 | if ($updateNestingCache) { | |
446 | CRM_Contact_BAO_GroupNestingCache::update(); | |
447 | } | |
448 | } | |
31c31c92 BS |
449 | |
450 | /* | |
451 | * Build parent groups form elements | |
452 | * | |
453 | * @obj form object passed by reference | |
454 | * | |
455 | * @return parent groups array | |
456 | * @static | |
457 | * @access public | |
458 | */ | |
e0ef6999 EM |
459 | /** |
460 | * @param $obj | |
461 | * | |
462 | * @return array | |
463 | */ | |
31c31c92 BS |
464 | static function buildParentGroups( &$obj ) { |
465 | $groupNames = CRM_Core_PseudoConstant::group(); | |
466 | $parentGroups = $parentGroupElements = array(); | |
467 | if (isset($obj->_id) && | |
468 | CRM_Utils_Array::value('parents', $obj->_groupValues) | |
469 | ) { | |
470 | $parentGroupIds = explode(',', $obj->_groupValues['parents']); | |
471 | foreach ($parentGroupIds as $parentGroupId) { | |
472 | $parentGroups[$parentGroupId] = $groupNames[$parentGroupId]; | |
473 | if (array_key_exists($parentGroupId, $groupNames)) { | |
474 | $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId]; | |
475 | $obj->addElement('checkbox', "remove_parent_group_$parentGroupId", | |
476 | $groupNames[$parentGroupId] | |
477 | ); | |
478 | } | |
479 | } | |
480 | } | |
481 | $obj->assign_by_ref('parent_groups', $parentGroupElements); | |
482 | ||
483 | if (isset($obj->_id)) { | |
484 | $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($obj->_id, | |
485 | $groupNames | |
486 | ); | |
487 | } | |
488 | else { | |
489 | $potentialParentGroupIds = array_keys($groupNames); | |
490 | } | |
491 | ||
cae80d9f | 492 | $parentGroupSelectValues = array('' => '- ' . ts('select group') . ' -'); |
31c31c92 BS |
493 | foreach ($potentialParentGroupIds as $potentialParentGroupId) { |
494 | if (array_key_exists($potentialParentGroupId, $groupNames)) { | |
495 | $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId]; | |
496 | } | |
497 | } | |
498 | ||
499 | if (count($parentGroupSelectValues) > 1) { | |
500 | if (CRM_Core_Permission::isMultisiteEnabled()) { | |
2d994358 | 501 | $required = !isset($obj->_id) || ($obj->_id && CRM_Core_BAO_Domain::isDomainGroup($obj->_id)) ? FALSE : empty($parentGroups); |
31c31c92 BS |
502 | } |
503 | else { | |
504 | $required = FALSE; | |
505 | } | |
cae80d9f | 506 | $obj->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2')); |
31c31c92 BS |
507 | } |
508 | ||
509 | return $parentGroups; | |
510 | } | |
6a488035 TO |
511 | } |
512 |