Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | /** | |
100fef9d | 42 | * The group id, used when editing a group |
6a488035 TO |
43 | * |
44 | * @var int | |
45 | */ | |
46 | protected $_id; | |
47 | ||
48 | /** | |
100fef9d | 49 | * The group object, if an id is present |
6a488035 TO |
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 | /** | |
100fef9d | 70 | * What blocks should we show and hide. |
6a488035 TO |
71 | * |
72 | * @var CRM_Core_ShowHideBlocks | |
73 | */ | |
74 | protected $_showHide; | |
75 | ||
76 | /** | |
100fef9d | 77 | * The civicrm_group_organization table id |
6a488035 TO |
78 | * |
79 | * @var int | |
80 | */ | |
81 | protected $_groupOrganizationID; | |
82 | ||
83 | /** | |
100fef9d | 84 | * Set up variables to build the form |
6a488035 TO |
85 | * |
86 | * @return void | |
87 | * @acess protected | |
88 | */ | |
00be9182 | 89 | public function preProcess() { |
6a488035 | 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 | ||
c490a46a CW |
161 | /** |
162 | * Set default values for the form. LocationType that in edit/view mode | |
6a488035 TO |
163 | * the default values are retrieved from the database |
164 | * | |
e0ef6999 EM |
165 | * @return array |
166 | */ | |
00be9182 | 167 | public function setDefaultValues() { |
6a488035 TO |
168 | $defaults = array(); |
169 | ||
170 | if (isset($this->_id)) { | |
171 | $defaults = $this->_groupValues; | |
a7488080 | 172 | if (!empty($defaults['group_type'])) { |
6a488035 TO |
173 | $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
174 | substr($defaults['group_type'], 1, -1) | |
175 | ); | |
176 | $defaults['group_type'] = array(); | |
177 | foreach ($types as $type) { | |
178 | $defaults['group_type'][$type] = 1; | |
179 | } | |
180 | } | |
181 | ||
532e9275 | 182 | if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { |
6a488035 | 183 | CRM_Contact_BAO_GroupOrganization::retrieve($this->_id, $defaults); |
6a488035 TO |
184 | } |
185 | } | |
186 | ||
187 | if (!((CRM_Core_Permission::check('access CiviMail')) || | |
188 | (CRM_Mailing_Info::workflowEnabled() && | |
189 | CRM_Core_Permission::check('create mailings') | |
190 | ) | |
191 | )) { | |
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']); | |
195 | } | |
196 | else { | |
197 | $this->assign('hideMailignList', $groupTypes['Mailing List']); | |
198 | } | |
199 | } | |
200 | ||
a7488080 | 201 | if (empty($defaults['parents'])) { |
6a488035 TO |
202 | $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId(); |
203 | } | |
204 | ||
205 | // custom data set defaults | |
206 | $defaults += CRM_Custom_Form_CustomData::setDefaultValues($this); | |
207 | return $defaults; | |
208 | } | |
209 | ||
210 | /** | |
c490a46a | 211 | * Build the form object |
6a488035 | 212 | * |
355ba699 | 213 | * @return void |
6a488035 TO |
214 | */ |
215 | public function buildQuickForm() { | |
216 | if ($this->_action == CRM_Core_Action::DELETE) { | |
217 | $this->addButtons(array( | |
218 | array( | |
219 | 'type' => 'next', | |
220 | 'name' => ts('Delete Group'), | |
221 | 'isDefault' => TRUE, | |
222 | ), | |
223 | array( | |
224 | 'type' => 'cancel', | |
225 | 'name' => ts('Cancel'), | |
226 | ), | |
227 | ) | |
228 | ); | |
229 | return; | |
230 | } | |
231 | ||
cae80d9f CW |
232 | // We want the "new group" form to redirect the user |
233 | if ($this->_action == CRM_Core_Action::ADD) { | |
234 | $this->preventAjaxSubmit(); | |
235 | } | |
236 | ||
6a488035 TO |
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(); | |
8cc574cf | 248 | if (isset($this->_id) && !empty($this->_groupValues['saved_search_id'])) { |
6a488035 TO |
249 | unset($groupTypes['Access Control']); |
250 | } | |
251 | ||
252 | if (!empty($groupTypes)) { | |
253 | $this->addCheckBox('group_type', | |
254 | ts('Group Type'), | |
255 | $groupTypes, | |
256 | NULL, NULL, NULL, NULL, ' ' | |
257 | ); | |
258 | } | |
259 | ||
93bfa565 | 260 | $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues::groupVisibility(), TRUE); |
6a488035 | 261 | |
31c31c92 BS |
262 | //CRM-14190 |
263 | $parentGroups = self::buildParentGroups($this); | |
6a488035 | 264 | |
532e9275 | 265 | if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { |
6a488035 | 266 | //group organization Element |
532e9275 CW |
267 | $props = array('api' => array('params' => array('contact_type' => 'Organization'))); |
268 | $this->addEntityRef('organization_id', ts('Organization'), $props); | |
6a488035 TO |
269 | } |
270 | ||
271 | // is_reserved property CRM-9936 | |
272 | $this->addElement('checkbox', 'is_reserved', ts('Reserved Group?')); | |
273 | if (!CRM_Core_Permission::check('administer reserved groups')) { | |
274 | $this->freeze('is_reserved'); | |
275 | } | |
276 | ||
277 | //build custom data | |
278 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
279 | ||
280 | $this->addButtons(array( | |
281 | array( | |
282 | 'type' => 'upload', | |
283 | 'name' => | |
284 | ($this->_action == CRM_Core_Action::ADD) ? | |
285 | ts('Continue') : ts('Save'), | |
286 | 'isDefault' => TRUE, | |
287 | ), | |
288 | array( | |
289 | 'type' => 'cancel', | |
290 | 'name' => ts('Cancel'), | |
291 | ), | |
292 | ) | |
293 | ); | |
294 | ||
295 | $doParentCheck = FALSE; | |
296 | if (CRM_Core_Permission::isMultisiteEnabled()) { | |
297 | $doParentCheck = ($this->_id && CRM_Core_BAO_Domain::isDomainGroup($this->_id)) ? FALSE : TRUE; | |
298 | } | |
299 | ||
300 | $options = array( | |
301 | 'selfObj' => $this, | |
302 | 'parentGroups' => $parentGroups, | |
303 | 'doParentCheck' => $doParentCheck, | |
304 | ); | |
305 | $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options); | |
306 | } | |
307 | ||
308 | /** | |
100fef9d | 309 | * Global validation rules for the form |
6a488035 | 310 | * |
ed6aedaa TO |
311 | * @param array $fields |
312 | * Posted values of the form. | |
100fef9d | 313 | * @param array $fileParams |
77b97be7 EM |
314 | * @param $options |
315 | * | |
6a488035 TO |
316 | * @return array list of errors to be posted back to the form |
317 | * @static | |
6a488035 | 318 | */ |
00be9182 | 319 | public static function formRule($fields, $fileParams, $options) { |
6a488035 TO |
320 | $errors = array(); |
321 | ||
322 | $doParentCheck = $options['doParentCheck']; | |
323 | $self = &$options['selfObj']; | |
324 | ||
325 | if ($doParentCheck) { | |
326 | $parentGroups = $options['parentGroups']; | |
327 | ||
328 | $grpRemove = 0; | |
329 | foreach ($fields as $key => $val) { | |
330 | if (substr($key, 0, 20) == 'remove_parent_group_') { | |
331 | $grpRemove++; | |
332 | } | |
333 | } | |
334 | ||
335 | $grpAdd = 0; | |
a7488080 | 336 | if (!empty($fields['parents'])) { |
6a488035 TO |
337 | $grpAdd++; |
338 | } | |
339 | ||
340 | if ((count($parentGroups) >= 1) && (($grpRemove - $grpAdd) >= count($parentGroups))) { | |
341 | $errors['parents'] = ts('Make sure at least one parent group is set.'); | |
342 | } | |
343 | } | |
344 | ||
345 | // do check for both name and title uniqueness | |
a7488080 | 346 | if (!empty($fields['title'])) { |
6a488035 TO |
347 | $title = trim($fields['title']); |
348 | $query = " | |
349 | SELECT count(*) | |
350 | FROM civicrm_group | |
351 | WHERE title = %1 | |
352 | "; | |
353 | $params = array(1 => array($title, 'String')); | |
354 | ||
355 | if ($self->_id) { | |
356 | $query .= "AND id <> %2"; | |
357 | $params[2] = array($self->_id, 'Integer'); | |
358 | } | |
359 | ||
360 | $grpCnt = CRM_Core_DAO::singleValueQuery($query, $params); | |
361 | if ($grpCnt) { | |
362 | $errors['title'] = ts('Group \'%1\' already exists.', array(1 => $fields['title'])); | |
363 | } | |
364 | } | |
365 | ||
366 | return empty($errors) ? TRUE : $errors; | |
367 | } | |
368 | ||
369 | /** | |
370 | * Process the form when submitted | |
371 | * | |
372 | * @return void | |
6a488035 TO |
373 | */ |
374 | public function postProcess() { | |
375 | CRM_Utils_System::flushCache('CRM_Core_DAO_Group'); | |
376 | ||
377 | $updateNestingCache = FALSE; | |
378 | if ($this->_action & CRM_Core_Action::DELETE) { | |
379 | CRM_Contact_BAO_Group::discard($this->_id); | |
380 | CRM_Core_Session::setStatus(ts("The Group '%1' has been deleted.", array(1 => $this->_title)), ts('Group Deleted'), 'success'); | |
381 | $updateNestingCache = TRUE; | |
382 | } | |
383 | else { | |
384 | // store the submitted values in an array | |
385 | $params = $this->controller->exportValues($this->_name); | |
386 | ||
387 | $params['is_active'] = CRM_Utils_Array::value('is_active', $this->_groupValues, 1); | |
388 | ||
389 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
390 | $params['id'] = $this->_id; | |
391 | } | |
392 | ||
393 | if ($this->_action & CRM_Core_Action::UPDATE && isset($this->_groupOrganizationID)) { | |
394 | $params['group_organization'] = $this->_groupOrganizationID; | |
395 | } | |
396 | ||
397 | $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); | |
398 | ||
399 | $customFields = CRM_Core_BAO_CustomField::getFields('Group'); | |
400 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
401 | $customFields, | |
402 | $this->_id, | |
403 | 'Group' | |
404 | ); | |
405 | ||
406 | $group = CRM_Contact_BAO_Group::create($params); | |
407 | ||
c490a46a | 408 | //Remove any parent groups requested to be removed |
a7488080 | 409 | if (!empty($this->_groupValues['parents'])) { |
6a488035 TO |
410 | $parentGroupIds = explode(',', $this->_groupValues['parents']); |
411 | foreach ($parentGroupIds as $parentGroupId) { | |
412 | if (isset($params["remove_parent_group_$parentGroupId"])) { | |
413 | CRM_Contact_BAO_GroupNesting::remove($parentGroupId, $group->id); | |
414 | $updateNestingCache = TRUE; | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
419 | CRM_Core_Session::setStatus(ts('The Group \'%1\' has been saved.', array(1 => $group->title)), ts('Group Saved'), 'success'); | |
420 | ||
c490a46a | 421 | // Add context to the session, in case we are adding members to the group |
6a488035 TO |
422 | if ($this->_action & CRM_Core_Action::ADD) { |
423 | $this->set('context', 'amtg'); | |
424 | $this->set('amtgID', $group->id); | |
425 | ||
426 | $session = CRM_Core_Session::singleton(); | |
427 | $session->pushUserContext(CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id)); | |
428 | } | |
429 | } | |
430 | ||
431 | // update the nesting cache | |
432 | if ($updateNestingCache) { | |
433 | CRM_Contact_BAO_GroupNestingCache::update(); | |
434 | } | |
435 | } | |
31c31c92 | 436 | |
c490a46a | 437 | /** |
31c31c92 BS |
438 | * Build parent groups form elements |
439 | * | |
c490a46a | 440 | * @param CRM_Core_Form $form |
31c31c92 | 441 | * |
c490a46a | 442 | * @return array parent groups |
31c31c92 | 443 | * @static |
31c31c92 | 444 | */ |
00be9182 | 445 | public static function buildParentGroups(&$form) { |
31c31c92 BS |
446 | $groupNames = CRM_Core_PseudoConstant::group(); |
447 | $parentGroups = $parentGroupElements = array(); | |
c490a46a CW |
448 | if (isset($form->_id) && !empty($form->_groupValues['parents'])) { |
449 | $parentGroupIds = explode(',', $form->_groupValues['parents']); | |
31c31c92 BS |
450 | foreach ($parentGroupIds as $parentGroupId) { |
451 | $parentGroups[$parentGroupId] = $groupNames[$parentGroupId]; | |
452 | if (array_key_exists($parentGroupId, $groupNames)) { | |
453 | $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId]; | |
c490a46a | 454 | $form->addElement('checkbox', "remove_parent_group_$parentGroupId", |
31c31c92 BS |
455 | $groupNames[$parentGroupId] |
456 | ); | |
457 | } | |
458 | } | |
459 | } | |
c490a46a | 460 | $form->assign_by_ref('parent_groups', $parentGroupElements); |
31c31c92 | 461 | |
c490a46a CW |
462 | if (isset($form->_id)) { |
463 | $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($form->_id, $groupNames); | |
31c31c92 BS |
464 | } |
465 | else { | |
466 | $potentialParentGroupIds = array_keys($groupNames); | |
467 | } | |
468 | ||
cae80d9f | 469 | $parentGroupSelectValues = array('' => '- ' . ts('select group') . ' -'); |
31c31c92 BS |
470 | foreach ($potentialParentGroupIds as $potentialParentGroupId) { |
471 | if (array_key_exists($potentialParentGroupId, $groupNames)) { | |
472 | $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId]; | |
473 | } | |
474 | } | |
475 | ||
476 | if (count($parentGroupSelectValues) > 1) { | |
477 | if (CRM_Core_Permission::isMultisiteEnabled()) { | |
c490a46a | 478 | $required = !isset($form->_id) || ($form->_id && CRM_Core_BAO_Domain::isDomainGroup($form->_id)) ? FALSE : empty($parentGroups); |
31c31c92 BS |
479 | } |
480 | else { | |
481 | $required = FALSE; | |
482 | } | |
c490a46a | 483 | $form->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2')); |
31c31c92 BS |
484 | } |
485 | ||
486 | return $parentGroups; | |
487 | } | |
6a488035 | 488 | } |