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 | } | |
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 | } | |
a7488080 | 134 | if (!empty($this->_groupValues['created_id'])) |
f828fa2c | 135 | $groupValues['created_by'] = |
6a488035 TO |
136 | CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['created_id'] , 'sort_name', 'id'); |
137 | ||
a7488080 | 138 | if (!empty($this->_groupValues['modified_id'])) { |
d0dfb649 PJ |
139 | $groupValues['modified_by'] = |
140 | CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['modified_id'] , 'sort_name', 'id'); | |
141 | } | |
142 | ||
6a488035 TO |
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 | |
355ba699 | 160 | * @return void |
6a488035 TO |
161 | */ |
162 | function setDefaultValues() { | |
163 | $defaults = array(); | |
164 | ||
165 | if (isset($this->_id)) { | |
166 | $defaults = $this->_groupValues; | |
a7488080 | 167 | if (!empty($defaults['group_type'])) { |
6a488035 TO |
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 | ||
532e9275 | 177 | if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { |
6a488035 | 178 | CRM_Contact_BAO_GroupOrganization::retrieve($this->_id, $defaults); |
6a488035 TO |
179 | } |
180 | } | |
181 | ||
182 | if (!((CRM_Core_Permission::check('access CiviMail')) || | |
183 | (CRM_Mailing_Info::workflowEnabled() && | |
184 | CRM_Core_Permission::check('create mailings') | |
185 | ) | |
186 | )) { | |
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 | /** | |
206 | * Function to actually build the form | |
207 | * | |
355ba699 | 208 | * @return void |
6a488035 TO |
209 | * @access public |
210 | */ | |
211 | public function buildQuickForm() { | |
212 | if ($this->_action == CRM_Core_Action::DELETE) { | |
213 | $this->addButtons(array( | |
214 | array( | |
215 | 'type' => 'next', | |
216 | 'name' => ts('Delete Group'), | |
217 | 'isDefault' => TRUE, | |
218 | ), | |
219 | array( | |
220 | 'type' => 'cancel', | |
221 | 'name' => ts('Cancel'), | |
222 | ), | |
223 | ) | |
224 | ); | |
225 | return; | |
226 | } | |
227 | ||
cae80d9f CW |
228 | // We want the "new group" form to redirect the user |
229 | if ($this->_action == CRM_Core_Action::ADD) { | |
230 | $this->preventAjaxSubmit(); | |
231 | } | |
232 | ||
6a488035 TO |
233 | $this->applyFilter('__ALL__', 'trim'); |
234 | $this->add('text', 'title', ts('Name') . ' ', | |
235 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE | |
236 | ); | |
237 | ||
238 | $this->add('textarea', 'description', ts('Description') . ' ', | |
239 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description') | |
240 | ); | |
241 | ||
242 | $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE); | |
243 | $config = CRM_Core_Config::singleton(); | |
8cc574cf | 244 | if (isset($this->_id) && !empty($this->_groupValues['saved_search_id'])) { |
6a488035 TO |
245 | unset($groupTypes['Access Control']); |
246 | } | |
247 | ||
248 | if (!empty($groupTypes)) { | |
249 | $this->addCheckBox('group_type', | |
250 | ts('Group Type'), | |
251 | $groupTypes, | |
252 | NULL, NULL, NULL, NULL, ' ' | |
253 | ); | |
254 | } | |
255 | ||
93bfa565 | 256 | $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues::groupVisibility(), TRUE); |
6a488035 | 257 | |
31c31c92 BS |
258 | //CRM-14190 |
259 | $parentGroups = self::buildParentGroups($this); | |
6a488035 | 260 | |
532e9275 | 261 | if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { |
6a488035 | 262 | //group organization Element |
532e9275 CW |
263 | $props = array('api' => array('params' => array('contact_type' => 'Organization'))); |
264 | $this->addEntityRef('organization_id', ts('Organization'), $props); | |
6a488035 TO |
265 | } |
266 | ||
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'); | |
271 | } | |
272 | ||
273 | //build custom data | |
274 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
275 | ||
276 | $this->addButtons(array( | |
277 | array( | |
278 | 'type' => 'upload', | |
279 | 'name' => | |
280 | ($this->_action == CRM_Core_Action::ADD) ? | |
281 | ts('Continue') : ts('Save'), | |
282 | 'isDefault' => TRUE, | |
283 | ), | |
284 | array( | |
285 | 'type' => 'cancel', | |
286 | 'name' => ts('Cancel'), | |
287 | ), | |
288 | ) | |
289 | ); | |
290 | ||
291 | $doParentCheck = FALSE; | |
292 | if (CRM_Core_Permission::isMultisiteEnabled()) { | |
293 | $doParentCheck = ($this->_id && CRM_Core_BAO_Domain::isDomainGroup($this->_id)) ? FALSE : TRUE; | |
294 | } | |
295 | ||
296 | $options = array( | |
297 | 'selfObj' => $this, | |
298 | 'parentGroups' => $parentGroups, | |
299 | 'doParentCheck' => $doParentCheck, | |
300 | ); | |
301 | $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options); | |
302 | } | |
303 | ||
304 | /** | |
305 | * global validation rules for the form | |
306 | * | |
307 | * @param array $fields posted values of the form | |
308 | * | |
77b97be7 EM |
309 | * @param $fileParams |
310 | * @param $options | |
311 | * | |
6a488035 TO |
312 | * @return array list of errors to be posted back to the form |
313 | * @static | |
314 | * @access public | |
315 | */ | |
316 | static function formRule($fields, $fileParams, $options) { | |
317 | $errors = array(); | |
318 | ||
319 | $doParentCheck = $options['doParentCheck']; | |
320 | $self = &$options['selfObj']; | |
321 | ||
322 | if ($doParentCheck) { | |
323 | $parentGroups = $options['parentGroups']; | |
324 | ||
325 | $grpRemove = 0; | |
326 | foreach ($fields as $key => $val) { | |
327 | if (substr($key, 0, 20) == 'remove_parent_group_') { | |
328 | $grpRemove++; | |
329 | } | |
330 | } | |
331 | ||
332 | $grpAdd = 0; | |
a7488080 | 333 | if (!empty($fields['parents'])) { |
6a488035 TO |
334 | $grpAdd++; |
335 | } | |
336 | ||
337 | if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) { | |
338 | $errors['parents'] = ts('Make sure at least one parent group is set.'); | |
339 | } | |
340 | } | |
341 | ||
342 | // do check for both name and title uniqueness | |
a7488080 | 343 | if (!empty($fields['title'])) { |
6a488035 TO |
344 | $title = trim($fields['title']); |
345 | $query = " | |
346 | SELECT count(*) | |
347 | FROM civicrm_group | |
348 | WHERE title = %1 | |
349 | "; | |
350 | $params = array(1 => array($title, 'String')); | |
351 | ||
352 | if ($self->_id) { | |
353 | $query .= "AND id <> %2"; | |
354 | $params[2] = array($self->_id, 'Integer'); | |
355 | } | |
356 | ||
357 | $grpCnt = CRM_Core_DAO::singleValueQuery($query, $params); | |
358 | if ($grpCnt) { | |
359 | $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title'])); | |
360 | } | |
361 | } | |
362 | ||
363 | return empty($errors) ? TRUE : $errors; | |
364 | } | |
365 | ||
366 | /** | |
367 | * Process the form when submitted | |
368 | * | |
369 | * @return void | |
370 | * @access public | |
371 | */ | |
372 | public function postProcess() { | |
373 | CRM_Utils_System::flushCache('CRM_Core_DAO_Group'); | |
374 | ||
375 | $updateNestingCache = FALSE; | |
376 | if ($this->_action & CRM_Core_Action::DELETE) { | |
377 | CRM_Contact_BAO_Group::discard($this->_id); | |
378 | CRM_Core_Session::setStatus(ts("The Group '%1' has been deleted.", array(1 => $this->_title)), ts('Group Deleted'), 'success'); | |
379 | $updateNestingCache = TRUE; | |
380 | } | |
381 | else { | |
382 | // store the submitted values in an array | |
383 | $params = $this->controller->exportValues($this->_name); | |
384 | ||
385 | $params['is_active'] = CRM_Utils_Array::value('is_active', $this->_groupValues, 1); | |
386 | ||
387 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
388 | $params['id'] = $this->_id; | |
389 | } | |
390 | ||
391 | if ($this->_action & CRM_Core_Action::UPDATE && isset($this->_groupOrganizationID)) { | |
392 | $params['group_organization'] = $this->_groupOrganizationID; | |
393 | } | |
394 | ||
395 | $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); | |
396 | ||
397 | $customFields = CRM_Core_BAO_CustomField::getFields('Group'); | |
398 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
399 | $customFields, | |
400 | $this->_id, | |
401 | 'Group' | |
402 | ); | |
403 | ||
404 | $group = CRM_Contact_BAO_Group::create($params); | |
405 | ||
406 | /* | |
31c31c92 BS |
407 | * Remove any parent groups requested to be removed |
408 | */ | |
6a488035 | 409 | |
a7488080 | 410 | if (!empty($this->_groupValues['parents'])) { |
6a488035 TO |
411 | $parentGroupIds = explode(',', $this->_groupValues['parents']); |
412 | foreach ($parentGroupIds as $parentGroupId) { | |
413 | if (isset($params["remove_parent_group_$parentGroupId"])) { | |
414 | CRM_Contact_BAO_GroupNesting::remove($parentGroupId, $group->id); | |
415 | $updateNestingCache = TRUE; | |
416 | } | |
417 | } | |
418 | } | |
419 | ||
420 | CRM_Core_Session::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title)), ts('Group Saved'), 'success'); | |
421 | ||
422 | /* | |
423 | * Add context to the session, in case we are adding members to the group | |
424 | */ | |
425 | ||
426 | if ($this->_action & CRM_Core_Action::ADD) { | |
427 | $this->set('context', 'amtg'); | |
428 | $this->set('amtgID', $group->id); | |
429 | ||
430 | $session = CRM_Core_Session::singleton(); | |
431 | $session->pushUserContext(CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id)); | |
432 | } | |
433 | } | |
434 | ||
435 | // update the nesting cache | |
436 | if ($updateNestingCache) { | |
437 | CRM_Contact_BAO_GroupNestingCache::update(); | |
438 | } | |
439 | } | |
31c31c92 BS |
440 | |
441 | /* | |
442 | * Build parent groups form elements | |
443 | * | |
444 | * @obj form object passed by reference | |
445 | * | |
446 | * @return parent groups array | |
447 | * @static | |
448 | * @access public | |
449 | */ | |
450 | static function buildParentGroups( &$obj ) { | |
451 | $groupNames = CRM_Core_PseudoConstant::group(); | |
452 | $parentGroups = $parentGroupElements = array(); | |
453 | if (isset($obj->_id) && | |
454 | CRM_Utils_Array::value('parents', $obj->_groupValues) | |
455 | ) { | |
456 | $parentGroupIds = explode(',', $obj->_groupValues['parents']); | |
457 | foreach ($parentGroupIds as $parentGroupId) { | |
458 | $parentGroups[$parentGroupId] = $groupNames[$parentGroupId]; | |
459 | if (array_key_exists($parentGroupId, $groupNames)) { | |
460 | $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId]; | |
461 | $obj->addElement('checkbox', "remove_parent_group_$parentGroupId", | |
462 | $groupNames[$parentGroupId] | |
463 | ); | |
464 | } | |
465 | } | |
466 | } | |
467 | $obj->assign_by_ref('parent_groups', $parentGroupElements); | |
468 | ||
469 | if (isset($obj->_id)) { | |
470 | $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($obj->_id, | |
471 | $groupNames | |
472 | ); | |
473 | } | |
474 | else { | |
475 | $potentialParentGroupIds = array_keys($groupNames); | |
476 | } | |
477 | ||
cae80d9f | 478 | $parentGroupSelectValues = array('' => '- ' . ts('select group') . ' -'); |
31c31c92 BS |
479 | foreach ($potentialParentGroupIds as $potentialParentGroupId) { |
480 | if (array_key_exists($potentialParentGroupId, $groupNames)) { | |
481 | $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId]; | |
482 | } | |
483 | } | |
484 | ||
485 | if (count($parentGroupSelectValues) > 1) { | |
486 | if (CRM_Core_Permission::isMultisiteEnabled()) { | |
487 | $required = empty($parentGroups) ? TRUE : FALSE; | |
488 | $required = (($obj->_id && CRM_Core_BAO_Domain::isDomainGroup($obj->_id)) || | |
489 | !isset($obj->_id) | |
490 | ) ? FALSE : $required; | |
491 | } | |
492 | else { | |
493 | $required = FALSE; | |
494 | } | |
cae80d9f | 495 | $obj->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2')); |
31c31c92 BS |
496 | } |
497 | ||
498 | return $parentGroups; | |
499 | } | |
6a488035 TO |
500 | } |
501 |