CRM-12942#comment-51519 fix
[civicrm-core.git] / CRM / Custom / Form / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 * form to process actions on the set aspect of Custom Data
38 */
39 class CRM_Custom_Form_Group extends CRM_Core_Form {
40
41 /**
42 * the set id saved to the session for an update
43 *
44 * @var int
45 * @access protected
46 */
47 protected $_id;
48
49 /**
50 * set is empty or not
51 *
52 * @var bool
53 * @access protected
54 */
55 protected $_isGroupEmpty = TRUE;
56
57 /**
58 * array of existing subtypes set for a custom set
59 *
60 * @var array
61 * @access protected
62 */
63 protected $_subtypes = array();
64
65 /**
66 * array of default params
67 *
68 * @var array
69 * @access protected
70 */
71 protected $_defaults = array();
72
73 /**
74 * Function to set variables up before form is built
75 *
76 * @param null
77 *
78 * @return void
79 * @access public
80 */
81 public function preProcess() {
82 // current set id
83 $this->_id = $this->get('id');
84
85 // setting title for html page
86 if ($this->_action == CRM_Core_Action::UPDATE) {
87 $title = CRM_Core_BAO_CustomGroup::getTitle($this->_id);
88 CRM_Utils_System::setTitle(ts('Edit %1', array(1 => $title)));
89 }
90 elseif ($this->_action == CRM_Core_Action::VIEW) {
91 $title = CRM_Core_BAO_CustomGroup::getTitle($this->_id);
92 CRM_Utils_System::setTitle(ts('Preview %1', array(1 => $title)));
93 }
94 else {
95 CRM_Utils_System::setTitle(ts('New Custom Field Set'));
96 }
97
98 if (isset($this->_id)) {
99 $params = array('id' => $this->_id);
100 CRM_Core_BAO_CustomGroup::retrieve($params, $this->_defaults);
101
102 $subExtends = CRM_Utils_Array::value('extends_entity_column_value', $this->_defaults);
103 if (!empty($subExtends)) {
104 $this->_subtypes = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($subExtends, 1, -1));
105 }
106 }
107 }
108
109 /**
110 * global form rule
111 *
112 * @param array $fields the input form values
113 * @param array $files the uploaded files if any
114 * @param array $options additional user data
115 *
116 * @return true if no errors, else array of errors
117 * @access public
118 * @static
119 */
120 static function formRule($fields, $files, $self) {
121 $errors = array();
122
123 //validate group title as well as name.
124 $title = $fields['title'];
125 $name = CRM_Utils_String::munge($title, '_', 64);
126 $query = 'select count(*) from civicrm_custom_group where ( name like %1 OR title like %2 ) and id != %3';
127 $grpCnt = CRM_Core_DAO::singleValueQuery($query, array(1 => array($name, 'String'),
128 2 => array($title, 'String'),
129 3 => array((int)$self->_id, 'Integer'),
130 ));
131 if ($grpCnt) {
132 $errors['title'] = ts('Custom group \'%1\' already exists in Database.', array(1 => $title));
133 }
134
135 if (CRM_Utils_Array::value(1, $fields['extends'])) {
136 if (in_array('', $fields['extends'][1]) && count($fields['extends'][1]) > 1) {
137 $errors['extends'] = ts("Cannot combine other option with 'Any'.");
138 }
139 }
140
141 if (empty($fields['extends'][0])) {
142 $errors['extends'] = ts("You need to select the type of record that this set of custom fields is applicable for.");
143 }
144
145 $extends = array('Activity', 'Relationship', 'Group', 'Contribution', 'Membership', 'Event', 'Participant');
146 if (in_array($fields['extends'][0], $extends) && $fields['style'] == 'Tab') {
147 $errors['style'] = ts("Display Style should be Inline for this Class");
148 $self->assign('showStyle', TRUE);
149 }
150
151 if (CRM_Utils_Array::value('is_multiple', $fields)) {
152 $self->assign('showMultiple', TRUE);
153 }
154
155 //checks the given custom set doesnot start with digit
156 $title = $fields['title'];
157 if (!empty($title)) {
158 // gives the ascii value
159 $asciiValue = ord($title{0});
160 if ($asciiValue >= 48 && $asciiValue <= 57) {
161 $errors['title'] = ts("Set's Name should not start with digit");
162 }
163 }
164
165 return empty($errors) ? TRUE : $errors;
166 }
167
168 /**
169 * This function is used to add the rules (mainly global rules) for form.
170 * All local rules are added near the element
171 *
172 * @param null
173 *
174 * @return void
175 * @access public
176 * @see valid_date
177 */
178 function addRules() {
179 $this->addFormRule(array('CRM_Custom_Form_Group', 'formRule'), $this);
180 }
181
182 /**
183 * Function to actually build the form
184 *
185 * @param null
186 *
187 * @return void
188 * @access public
189 */
190 public function buildQuickForm() {
191 $this->applyFilter('__ALL__', 'trim');
192
193 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_CustomGroup');
194
195 //title
196 $this->add('text', 'title', ts('Set Name'), $attributes['title'], TRUE);
197
198 //Fix for code alignment, CRM-3058
199 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
200 $this->assign('contactTypes', json_encode($contactTypes));
201
202 $sel1 = array("" => "- select -") + CRM_Core_SelectValues::customGroupExtends();
203 $sel2 = array();
204 $activityType = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
205
206 $eventType = CRM_Core_OptionGroup::values('event_type');
207 $grantType = CRM_Core_OptionGroup::values('grant_type');
208 $campaignTypes = CRM_Campaign_PseudoConstant::campaignType();
209 $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
210 $participantRole = CRM_Core_OptionGroup::values('participant_role');
211 $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Individual');
212 $relTypeOrg = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Organization');
213 $relTypeHou = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Household');
214
215 ksort($sel1);
216 asort($activityType);
217 asort($eventType);
218 asort($grantType);
219 asort($membershipType);
220 asort($participantRole);
221 $allRelationshipType = array();
222 $allRelationshipType = array_merge($relTypeInd, $relTypeOrg);
223 $allRelationshipType = array_merge($allRelationshipType, $relTypeHou);
224
225 //adding subtype specific relationships CRM-5256
226 $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo();
227
228 foreach ($subTypes as $subType => $val) {
229 $subTypeRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $val['parent'],
230 FALSE, 'label', TRUE, $subType
231 );
232 $allRelationshipType = array_merge($allRelationshipType, $subTypeRelationshipTypes);
233 }
234
235 $sel2['Event'] = $eventType;
236 $sel2['Grant'] = $grantType;
237 $sel2['Activity'] = $activityType;
238 $sel2['Campaign'] = $campaignTypes;
239 $sel2['Membership'] = $membershipType;
240 $sel2['ParticipantRole'] = $participantRole;
241 $sel2['ParticipantEventName'] = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
242 $sel2['ParticipantEventType'] = $eventType;
243 $sel2['Contribution'] = CRM_Contribute_PseudoConstant::financialType( );
244 $sel2['Relationship'] = $allRelationshipType;
245
246 $sel2['Individual'] = CRM_Contact_BAO_ContactType::subTypePairs('Individual', FALSE, NULL);
247 $sel2['Household'] = CRM_Contact_BAO_ContactType::subTypePairs('Household', FALSE, NULL);
248 $sel2['Organization'] = CRM_Contact_BAO_ContactType::subTypePairs('Organization', FALSE, NULL);
249
250 CRM_Core_BAO_CustomGroup::getExtendedObjectTypes($sel2);
251
252 foreach ($sel2 as $main => $sub) {
253 if (!empty($sel2[$main])) {
254 if ($main == 'Relationship') {
255 $relName = self::getFormattedList($sel2[$main]);
256 $sel2[$main] = array(
257 '' => ts("- Any -")) + $relName;
258 }
259 else {
260 $sel2[$main] = array(
261 '' => ts("- Any -")) + $sel2[$main];
262 }
263 }
264 }
265
266 $cSubTypes = CRM_Core_Component::contactSubTypes();
267
268 if (!empty($cSubTypes)) {
269 $contactSubTypes = array();
270 foreach ($cSubTypes as $key => $value) {
271 $contactSubTypes[$key] = $key;
272 }
273 $sel2['Contact'] = array(
274 "" => "-- Any --") + $contactSubTypes;
275 }
276 else {
277 if (!isset($this->_id)) {
278 $formName = 'document.forms.' . $this->_name;
279
280 $js = "<script type='text/javascript'>\n";
281 $js .= "{$formName}['extends_1'].style.display = 'none';\n";
282 $js .= "</script>";
283 $this->assign('initHideBlocks', $js);
284 }
285 }
286
287 $sel = &$this->add('hierselect',
288 'extends',
289 ts('Used For'),
290 array(
291 'name' => 'extends[0]',
292 'style' => 'vertical-align: top;'
293 ),
294 TRUE
295 );
296 $sel->setOptions(array($sel1, $sel2));
297 if (is_a($sel->_elements[1], 'HTML_QuickForm_select')) {
298 // make second selector a multi-select -
299 $sel->_elements[1]->setMultiple(TRUE);
300 $sel->_elements[1]->setSize(5);
301 }
302 if ($this->_action == CRM_Core_Action::UPDATE) {
303 $subName = CRM_Utils_Array::value('extends_entity_column_id', $this->_defaults);
304 if ($this->_defaults['extends'] == 'Participant') {
305 if ($subName == 1) {
306 $this->_defaults['extends'] = 'ParticipantRole';
307 }
308 elseif ($subName == 2) {
309 $this->_defaults['extends'] = 'ParticipantEventName';
310 }
311 elseif ($subName == 3) {
312 $this->_defaults['extends'] = 'ParticipantEventType';
313 }
314 }
315
316 //allow to edit settings if custom set is empty CRM-5258
317 $this->_isGroupEmpty = CRM_Core_BAO_CustomGroup::isGroupEmpty($this->_id);
318 if (!$this->_isGroupEmpty) {
319 if (!empty($this->_subtypes)) {
320 // we want to allow adding / updating subtypes for this case,
321 // and therefore freeze the first selector only.
322 $sel->_elements[0]->freeze();
323 }
324 else {
325 // freeze both the selectors
326 $sel->freeze();
327 }
328 }
329 $this->assign('isCustomGroupEmpty', $this->_isGroupEmpty);
330 $this->assign('gid', $this->_id);
331 }
332 $this->assign('defaultSubtypes', json_encode($this->_subtypes));
333
334 // help text
335 $this->addWysiwyg('help_pre', ts('Pre-form Help'), $attributes['help_pre']);
336 $this->addWysiwyg('help_post', ts('Post-form Help'), $attributes['help_post']);
337
338 // weight
339 $this->add('text', 'weight', ts('Order'), $attributes['weight'], TRUE);
340 $this->addRule('weight', ts('is a numeric field'), 'numeric');
341
342 // display style
343 $this->add('select', 'style', ts('Display Style'), CRM_Core_SelectValues::customGroupStyle());
344
345 // is this set collapsed or expanded ?
346 $this->addElement('checkbox', 'collapse_display', ts('Collapse this set on initial display'));
347
348 // is this set collapsed or expanded ? in advanced search
349 $this->addElement('checkbox', 'collapse_adv_display', ts('Collapse this set in Advanced Search'));
350
351 // is this set active ?
352 $this->addElement('checkbox', 'is_active', ts('Is this Custom Data Set active?'));
353
354 // does this set have multiple record?
355 $multiple = $this->addElement('checkbox',
356 'is_multiple',
357 ts('Does this Custom Field Set allow multiple records?'),
358 NULL,
359 array('onclick' => "showRange();")
360 );
361
362 // $min_multiple = $this->add('text', 'min_multiple', ts('Minimum number of multiple records'), $attributes['min_multiple'] );
363 // $this->addRule('min_multiple', ts('is a numeric field') , 'numeric');
364
365 $max_multiple = $this->add('text', 'max_multiple', ts('Maximum number of multiple records'), $attributes['max_multiple']);
366 $this->addRule('max_multiple', ts('is a numeric field'), 'numeric');
367
368 //allow to edit settings if custom set is empty CRM-5258
369 $this->assign('isGroupEmpty', $this->_isGroupEmpty);
370 if (!$this->_isGroupEmpty) {
371 $multiple->freeze();
372 //$min_multiple->freeze();
373 $max_multiple->freeze();
374 }
375
376 $this->assign('showStyle', FALSE);
377 $this->assign('showMultiple', FALSE);
378 $buttons = array(
379 array(
380 'type' => 'next',
381 'name' => ts('Save'),
382 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
383 'isDefault' => TRUE,
384 ),
385 array(
386 'type' => 'cancel',
387 'name' => ts('Cancel'),
388 ),
389 );
390 if (!$this->_isGroupEmpty && !empty($this->_subtypes)) {
391 $buttons[0]['js'] = array('onclick' => "return warnDataLoss()");
392 }
393 $this->addButtons($buttons);
394
395 // views are implemented as frozen form
396 if ($this->_action & CRM_Core_Action::VIEW) {
397 $this->freeze();
398 $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'"));
399 }
400 }
401
402 /**
403 * This function sets the default values for the form. Note that in edit/view mode
404 * the default values are retrieved from the database
405 *
406 * @param null
407 *
408 * @return array array of default values
409 * @access public
410 */
411 function setDefaultValues() {
412 $defaults = &$this->_defaults;
413 $this->assign('showMaxMultiple', TRUE);
414 if ($this->_action == CRM_Core_Action::ADD) {
415 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_CustomGroup');
416
417 $defaults['is_multiple'] = $defaults['min_multiple'] = 0;
418 $defaults['is_active'] = $defaults['collapse_display'] = 1;
419 $defaults['style'] = 'Inline';
420 }
421 elseif (!CRM_Utils_Array::value('max_multiple', $defaults) && !$this->_isGroupEmpty) {
422 $this->assign('showMaxMultiple', FALSE);
423 }
424
425 if (isset($defaults['extends'])) {
426 $extends = $defaults['extends'];
427 unset($defaults['extends']);
428
429 $defaults['extends'][0] = $extends;
430
431 if (!empty($this->_subtypes)) {
432 $defaults['extends'][1] = $this->_subtypes;
433 }
434 else {
435 $defaults['extends'][1] = array(0 => '');
436 }
437
438
439 $subName = CRM_Utils_Array::value('extends_entity_column_id', $defaults);
440
441 if ($extends == 'Relationship' && !empty($this->_subtypes)) {
442 $relationshipDefaults = array();
443 foreach ($defaults['extends'][1] as $donCare => $rel_type_id) {
444 $relationshipDefaults[] = $rel_type_id;
445 }
446
447 $defaults['extends'][1] = $relationshipDefaults;
448 }
449 }
450
451 return $defaults;
452 }
453
454 /**
455 * Process the form
456 *
457 * @param null
458 *
459 * @return void
460 * @access public
461 */
462 public function postProcess() {
463 // get the submitted form values.
464 $params = $this->controller->exportValues('Group');
465 $params['overrideFKConstraint'] = 0;
466 if ($this->_action & CRM_Core_Action::UPDATE) {
467 $params['id'] = $this->_id;
468 if ($this->_defaults['extends'][0] != $params['extends'][0]) {
469 $params['overrideFKConstraint'] = 1;
470 }
471
472 if (!empty($this->_subtypes)) {
473 $subtypesToBeRemoved = array_diff($this->_subtypes, array_intersect($this->_subtypes, $params['extends'][1]));
474 CRM_Contact_BAO_ContactType::deleteCustomRowsOfSubtype($this->_id, $subtypesToBeRemoved);
475 }
476 }
477 elseif ($this->_action & CRM_Core_Action::ADD) {
478 //new custom set , so lets set the created_id
479 $session = CRM_Core_Session::singleton();
480 $params['created_id'] = $session->get('userID');
481 $params['created_date'] = date('YmdHis');
482 }
483
484 $group = CRM_Core_BAO_CustomGroup::create($params);
485
486 // reset the cache
487 CRM_Core_BAO_Cache::deleteGroup('contact fields');
488
489 if ($this->_action & CRM_Core_Action::UPDATE) {
490 CRM_Core_Session::setStatus(ts('Your custom field set \'%1 \' has been saved.', array(1 => $group->title)), ts('Saved'), 'success');
491 }
492 else {
493 $url = CRM_Utils_System::url('civicrm/admin/custom/group/field/add', 'reset=1&action=add&gid=' . $group->id);
494 CRM_Core_Session::setStatus(ts("Your custom field set '%1' has been added. You can add custom fields now.",
495 array(1 => $group->title)
496 ), ts('Saved'), 'success');
497 $session = CRM_Core_Session::singleton();
498 $session->replaceUserContext($url);
499 }
500
501 // prompt Drupal Views users to update $db_prefix in settings.php, if necessary
502 global $db_prefix;
503 $config = CRM_Core_Config::singleton();
504 if (is_array($db_prefix) && $config->userSystem->is_drupal && module_exists('views')) {
505 // get table_name for each custom group
506 $tables = array();
507 $sql = "SELECT table_name FROM civicrm_custom_group WHERE is_active = 1";
508 $result = CRM_Core_DAO::executeQuery($sql);
509 while ($result->fetch()) {
510 $tables[$result->table_name] = $result->table_name;
511 }
512
513 // find out which tables are missing from the $db_prefix array
514 $missingTableNames = array_diff_key($tables, $db_prefix);
515
516 if (!empty($missingTableNames)) {
517 CRM_Core_Session::setStatus(ts("To ensure that all of your custom data groups are available to Views, you may need to add the following key(s) to the db_prefix array in your settings.php file: '%1'.",
518 array(1 => implode(', ', $missingTableNames))
519 ), ts('Note'), 'info');
520 }
521 }
522 }
523
524 /*
525 * Function to return a formatted list of relationship name.
526 * @param $list array array of relationship name.
527 * @static
528 * return array array of relationship name.
529 */
530 static function getFormattedList(&$list) {
531 $relName = array();
532
533 foreach ($list as $k => $v) {
534 $key = substr($k, 0, strpos($k, '_'));
535 if (isset($list["{$key}_b_a"])) {
536 if ($list["{$key}_a_b"] != $list["{$key}_b_a"]) {
537 $relName["$key"] = $list["{$key}_a_b"] . ' / ' . $list["{$key}_b_a"];
538 }
539 unset($list["{$key}_b_a"]);
540 }
541 else {
542 $relName["{$key}"] = $list["{$key}_a_b"];
543 }
544 }
545 return $relName;
546 }
547 }
548