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