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 | * form to process actions on the field aspect of Custom | |
38 | */ | |
39 | class CRM_Custom_Form_Field extends CRM_Core_Form { | |
40 | ||
41 | /** | |
42 | * Constants for number of options for data types of multiple option. | |
43 | */ | |
7da04cde | 44 | const NUM_OPTION = 11; |
6a488035 TO |
45 | |
46 | /** | |
100fef9d | 47 | * The custom group id saved to the session for an update |
6a488035 TO |
48 | * |
49 | * @var int | |
6a488035 TO |
50 | */ |
51 | protected $_gid; | |
52 | ||
53 | /** | |
54 | * The field id, used when editing the field | |
55 | * | |
56 | * @var int | |
6a488035 TO |
57 | */ |
58 | protected $_id; | |
59 | ||
60 | /** | |
61 | * The default custom data/input types, when editing the field | |
62 | * | |
63 | * @var array | |
6a488035 TO |
64 | */ |
65 | protected $_defaultDataType; | |
66 | ||
67 | /** | |
100fef9d | 68 | * Array of custom field values if update mode |
6a488035 TO |
69 | */ |
70 | protected $_values; | |
71 | ||
72 | /** | |
73 | * Array for valid combinations of data_type & html_type | |
74 | * | |
75 | * @var array | |
76 | * @static | |
77 | */ | |
78 | private static $_dataTypeValues = NULL; | |
79 | private static $_dataTypeKeys = NULL; | |
80 | ||
81 | private static $_dataToHTML = NULL; | |
82 | ||
83 | private static $_dataToLabels = NULL; | |
84 | ||
85 | /** | |
100fef9d | 86 | * Set variables up before form is built |
6a488035 TO |
87 | * |
88 | * @param null | |
89 | * | |
90 | * @return void | |
6a488035 TO |
91 | */ |
92 | public function preProcess() { | |
93 | if (!(self::$_dataTypeKeys)) { | |
94 | self::$_dataTypeKeys = array_keys(CRM_Core_BAO_CustomField::dataType()); | |
95 | self::$_dataTypeValues = array_values(CRM_Core_BAO_CustomField::dataType()); | |
96 | } | |
97 | ||
98 | if (!self::$_dataToHTML) { | |
99 | self::$_dataToHTML = CRM_Core_BAO_CustomField::dataToHtml(); | |
100 | } | |
101 | ||
2b83aa0d CW |
102 | //custom field id |
103 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
104 | ||
105 | $this->_values = array(); | |
106 | //get the values form db if update. | |
107 | if ($this->_id) { | |
108 | $params = array('id' => $this->_id); | |
109 | CRM_Core_BAO_CustomField::retrieve($params, $this->_values); | |
110 | // note_length is an alias for the text_length field | |
111 | $this->_values['note_length'] = CRM_Utils_Array::value('text_length', $this->_values); | |
112 | // custom group id | |
113 | $this->_gid = $this->_values['custom_group_id']; | |
114 | } | |
115 | else { | |
116 | // custom group id | |
117 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this); | |
118 | } | |
6a488035 | 119 | |
d06700a7 | 120 | if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) { |
e89941dc | 121 | CRM_Core_Error::fatal("You cannot add or edit fields in a reserved custom field-set."); |
d06700a7 RN |
122 | } |
123 | ||
6a488035 TO |
124 | if ($this->_gid) { |
125 | $url = CRM_Utils_System::url('civicrm/admin/custom/group/field', | |
126 | "reset=1&action=browse&gid={$this->_gid}" | |
127 | ); | |
128 | ||
129 | $session = CRM_Core_Session::singleton(); | |
130 | $session->pushUserContext($url); | |
131 | } | |
132 | ||
6a488035 TO |
133 | if (self::$_dataToLabels == NULL) { |
134 | self::$_dataToLabels = array( | |
e8646905 TO |
135 | array( |
136 | 'Text' => ts('Text'), | |
137 | 'Select' => ts('Select'), | |
138 | 'Radio' => ts('Radio'), | |
139 | 'CheckBox' => ts('CheckBox'), | |
140 | 'Multi-Select' => ts('Multi-Select'), | |
10ede6e5 | 141 | 'AdvMulti-Select' => ts('Adv Multi-Select (obsolete)'), |
79dc94e4 | 142 | 'Autocomplete-Select' => ts('Autocomplete-Select'), |
6a488035 | 143 | ), |
e8646905 TO |
144 | array( |
145 | 'Text' => ts('Text'), | |
146 | 'Select' => ts('Select'), | |
6a488035 TO |
147 | 'Radio' => ts('Radio'), |
148 | ), | |
e8646905 TO |
149 | array( |
150 | 'Text' => ts('Text'), | |
151 | 'Select' => ts('Select'), | |
6a488035 TO |
152 | 'Radio' => ts('Radio'), |
153 | ), | |
e8646905 TO |
154 | array( |
155 | 'Text' => ts('Text'), | |
156 | 'Select' => ts('Select'), | |
6a488035 TO |
157 | 'Radio' => ts('Radio'), |
158 | ), | |
438c4f5d | 159 | array('TextArea' => ts('TextArea'), 'RichTextEditor' => ts('Rich Text Editor')), |
6a488035 TO |
160 | array('Date' => ts('Select Date')), |
161 | array('Radio' => ts('Radio')), | |
162 | array('StateProvince' => ts('Select State/Province'), 'Multi-Select' => ts('Multi-Select State/Province')), | |
c9b2a585 | 163 | array('Country' => ts('Select Country'), 'Multi-Select' => ts('Multi-Select Country')), |
6a488035 TO |
164 | array('File' => ts('Select File')), |
165 | array('Link' => ts('Link')), | |
79dc94e4 | 166 | array('ContactReference' => ts('Autocomplete-Select')), |
6a488035 TO |
167 | ); |
168 | } | |
169 | } | |
170 | ||
171 | /** | |
c490a46a | 172 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
173 | * the default values are retrieved from the database |
174 | * | |
175 | * @param null | |
176 | * | |
177 | * @return array array of default values | |
6a488035 | 178 | */ |
00be9182 | 179 | public function setDefaultValues() { |
6a488035 TO |
180 | $defaults = $this->_values; |
181 | ||
182 | if ($this->_id) { | |
183 | $this->assign('id', $this->_id); | |
184 | $this->_gid = $defaults['custom_group_id']; | |
185 | ||
186 | //get the value for state or country | |
187 | if ($defaults['data_type'] == 'StateProvince' && | |
188 | $stateId = CRM_Utils_Array::value('default_value', $defaults) | |
189 | ) { | |
190 | $defaults['default_value'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $stateId); | |
191 | } | |
192 | elseif ($defaults['data_type'] == 'Country' && | |
193 | $countryId = CRM_Utils_Array::value('default_value', $defaults) | |
194 | ) { | |
195 | $defaults['default_value'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Country', $countryId); | |
196 | } | |
197 | ||
8cc574cf | 198 | if ($defaults['data_type'] == 'ContactReference' && !empty($defaults['filter'])) { |
6a488035 TO |
199 | $contactRefFilter = 'Advance'; |
200 | if (strpos($defaults['filter'], 'action=lookup') !== FALSE && | |
201 | strpos($defaults['filter'], 'group=') !== FALSE | |
202 | ) { | |
203 | $filterParts = explode('&', $defaults['filter']); | |
204 | ||
205 | if (count($filterParts) == 2) { | |
206 | $contactRefFilter = 'Group'; | |
207 | foreach ($filterParts as $part) { | |
208 | if (strpos($part, 'group=') === FALSE) { | |
209 | continue; | |
210 | } | |
211 | $groups = substr($part, strpos($part, '=') + 1); | |
212 | foreach (explode(',', $groups) as $grp) { | |
213 | if (CRM_Utils_Rule::positiveInteger($grp)) { | |
214 | $defaults['group_id'][] = $grp; | |
215 | } | |
216 | } | |
217 | } | |
218 | } | |
219 | } | |
220 | $defaults['filter_selected'] = $contactRefFilter; | |
221 | } | |
222 | ||
a7488080 | 223 | if (!empty($defaults['data_type'])) { |
6a488035 TO |
224 | $defaultDataType = array_search($defaults['data_type'], |
225 | self::$_dataTypeKeys | |
226 | ); | |
227 | $defaultHTMLType = array_search($defaults['html_type'], | |
228 | self::$_dataToHTML[$defaultDataType] | |
229 | ); | |
230 | $defaults['data_type'] = array( | |
231 | '0' => $defaultDataType, | |
232 | '1' => $defaultHTMLType, | |
233 | ); | |
234 | $this->_defaultDataType = $defaults['data_type']; | |
235 | } | |
236 | ||
237 | $defaults['option_type'] = 2; | |
238 | ||
239 | $this->assign('changeFieldType', CRM_Custom_Form_ChangeFieldType::fieldTypeTransitions($this->_values['data_type'], $this->_values['html_type'])); | |
240 | } | |
241 | else { | |
242 | $defaults['is_active'] = 1; | |
243 | $defaults['option_type'] = 1; | |
244 | } | |
245 | ||
246 | // set defaults for weight. | |
247 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
248 | $defaults['option_status[' . $i . ']'] = 1; | |
249 | $defaults['option_weight[' . $i . ']'] = $i; | |
250 | } | |
251 | ||
252 | if ($this->_action & CRM_Core_Action::ADD) { | |
253 | $fieldValues = array('custom_group_id' => $this->_gid); | |
254 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_CustomField', $fieldValues); | |
255 | ||
256 | $defaults['text_length'] = 255; | |
257 | $defaults['note_columns'] = 60; | |
258 | $defaults['note_rows'] = 4; | |
259 | $defaults['is_view'] = 0; | |
260 | } | |
261 | ||
a7488080 | 262 | if (!empty($defaults['html_type'])) { |
6a488035 TO |
263 | $dontShowLink = substr($defaults['html_type'], -14) == 'State/Province' || substr($defaults['html_type'], -7) == 'Country' ? 1 : 0; |
264 | } | |
265 | ||
266 | if (isset($dontShowLink)) { | |
267 | $this->assign('dontShowLink', $dontShowLink); | |
268 | } | |
e41f4660 PJ |
269 | if ($this->_action & CRM_Core_Action::ADD && |
270 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_multiple', 'id')) { | |
5a205b89 PJ |
271 | $defaults['in_selector'] = 1; |
272 | } | |
273 | ||
6a488035 TO |
274 | return $defaults; |
275 | } | |
276 | ||
277 | /** | |
c490a46a | 278 | * Build the form object |
6a488035 TO |
279 | * |
280 | * @param null | |
281 | * | |
282 | * @return void | |
6a488035 TO |
283 | */ |
284 | public function buildQuickForm() { | |
285 | if ($this->_gid) { | |
286 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'title'); | |
e2046b33 | 287 | CRM_Utils_System::setTitle($this->_title . ' - ' . ($this->_id ? ts('Edit Field') : ts('New Field'))); |
fa3a5fe2 | 288 | $this->assign('gid', $this->_gid); |
6a488035 TO |
289 | } |
290 | ||
291 | // lets trim all the whitespace | |
292 | $this->applyFilter('__ALL__', 'trim'); | |
293 | ||
294 | $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_CustomField'); | |
295 | ||
296 | // label | |
297 | $this->add('text', | |
298 | 'label', | |
299 | ts('Field Label'), | |
300 | $attributes['label'], | |
301 | TRUE | |
302 | ); | |
303 | ||
304 | $dt = &self::$_dataTypeValues; | |
305 | $it = array(); | |
306 | foreach ($dt as $key => $value) { | |
307 | $it[$key] = self::$_dataToLabels[$key]; | |
308 | } | |
309 | $sel = &$this->addElement('hierselect', | |
310 | 'data_type', | |
311 | ts('Data and Input Field Type'), | |
312 | 'onclick="clearSearchBoxes();custom_option_html_type(this.form)"; onBlur="custom_option_html_type(this.form)";', | |
313 | ' ' | |
314 | ); | |
315 | $sel->setOptions(array($dt, $it)); | |
5a205b89 PJ |
316 | |
317 | if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_multiple')) { | |
318 | $this->add('checkbox', 'in_selector', ts('Display in Table?')); | |
319 | } | |
320 | ||
6a488035 TO |
321 | if ($this->_action == CRM_Core_Action::UPDATE) { |
322 | $this->freeze('data_type'); | |
323 | } | |
324 | $includeFieldIds = NULL; | |
325 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
326 | $includeFieldIds = $this->_values['id']; | |
327 | } | |
328 | $optionGroups = CRM_Core_BAO_CustomField::customOptionGroup($includeFieldIds); | |
329 | $emptyOptGroup = FALSE; | |
330 | if (empty($optionGroups)) { | |
331 | $emptyOptGroup = TRUE; | |
332 | $optionTypes = array('1' => ts('Create a new set of options')); | |
333 | } | |
334 | else { | |
e8646905 TO |
335 | $optionTypes = array( |
336 | '1' => ts('Create a new set of options'), | |
6a488035 TO |
337 | '2' => ts('Reuse an existing set'), |
338 | ); | |
339 | ||
340 | $this->add('select', | |
341 | 'option_group_id', | |
342 | ts('Multiple Choice Option Sets'), | |
343 | array( | |
344 | '' => ts('- select -')) + $optionGroups | |
345 | ); | |
346 | } | |
347 | ||
348 | $element = &$this->addRadio('option_type', | |
349 | ts('Option Type'), | |
350 | $optionTypes, | |
351 | array( | |
352 | 'onclick' => "showOptionSelect();"), '<br/>' | |
353 | ); | |
354 | ||
6a488035 TO |
355 | $contactGroups = CRM_Core_PseudoConstant::group(); |
356 | asort($contactGroups); | |
357 | ||
358 | $this->add('select', | |
359 | 'group_id', | |
360 | ts('Limit List to Group'), | |
361 | $contactGroups, | |
362 | FALSE, | |
363 | array('multiple' => 'multiple') | |
364 | ); | |
365 | ||
366 | $this->add('text', | |
367 | 'filter', | |
368 | ts('Advanced Filter'), | |
369 | $attributes['filter'] | |
370 | ); | |
371 | ||
372 | $this->add('hidden', 'filter_selected', 'Group', array('id' => 'filter_selected')); | |
373 | ||
374 | //if empty option group freeze the option type. | |
375 | if ($emptyOptGroup) { | |
376 | $element->freeze(); | |
377 | } | |
378 | ||
379 | // form fields of Custom Option rows | |
380 | $defaultOption = array(); | |
381 | $_showHide = new CRM_Core_ShowHideBlocks('', ''); | |
382 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
383 | ||
384 | //the show hide blocks | |
385 | $showBlocks = 'optionField_' . $i; | |
386 | if ($i > 2) { | |
387 | $_showHide->addHide($showBlocks); | |
388 | if ($i == self::NUM_OPTION) { | |
389 | $_showHide->addHide('additionalOption'); | |
390 | } | |
391 | } | |
392 | else { | |
393 | $_showHide->addShow($showBlocks); | |
394 | } | |
395 | ||
396 | $optionAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue'); | |
397 | // label | |
398 | $this->add('text', 'option_label[' . $i . ']', ts('Label'), | |
399 | $optionAttributes['label'] | |
400 | ); | |
401 | ||
402 | // value | |
403 | $this->add('text', 'option_value[' . $i . ']', ts('Value'), | |
404 | $optionAttributes['value'] | |
405 | ); | |
406 | ||
407 | // weight | |
408 | $this->add('text', "option_weight[$i]", ts('Order'), | |
409 | $optionAttributes['weight'] | |
410 | ); | |
411 | ||
412 | // is active ? | |
413 | $this->add('checkbox', "option_status[$i]", ts('Active?')); | |
414 | ||
415 | $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i); | |
416 | ||
417 | //for checkbox handling of default option | |
418 | $this->add('checkbox', "default_checkbox_option[$i]", NULL); | |
419 | } | |
420 | ||
421 | //default option selection | |
422 | $this->addGroup($defaultOption, 'default_option'); | |
423 | ||
424 | $_showHide->addToTemplate(); | |
425 | ||
426 | // text length for alpha numeric data types | |
427 | $this->add('text', | |
428 | 'text_length', | |
429 | ts('Database field length'), | |
430 | $attributes['text_length'], | |
431 | FALSE | |
432 | ); | |
433 | $this->addRule('text_length', ts('Value should be a positive number'), 'integer'); | |
434 | ||
435 | $this->add('text', | |
436 | 'start_date_years', | |
437 | ts('Dates may be up to'), | |
438 | $attributes['start_date_years'], | |
439 | FALSE | |
440 | ); | |
441 | $this->add('text', | |
442 | 'end_date_years', | |
443 | ts('Dates may be up to'), | |
444 | $attributes['end_date_years'], | |
445 | FALSE | |
446 | ); | |
447 | ||
448 | $this->addRule('start_date_years', ts('Value should be a positive number'), 'integer'); | |
449 | $this->addRule('end_date_years', ts('Value should be a positive number'), 'integer'); | |
450 | ||
451 | $this->add('select', 'date_format', ts('Date Format'), | |
452 | array('' => ts('- select -')) + CRM_Core_SelectValues::getDatePluginInputFormats() | |
453 | ); | |
454 | ||
455 | $this->add('select', 'time_format', ts('Time'), | |
456 | array('' => ts('- none -')) + CRM_Core_SelectValues::getTimeFormats() | |
457 | ); | |
458 | ||
459 | // for Note field | |
460 | $this->add('text', | |
461 | 'note_columns', | |
462 | ts('Width (columns)') . ' ', | |
463 | $attributes['note_columns'], | |
464 | FALSE | |
465 | ); | |
466 | $this->add('text', | |
467 | 'note_rows', | |
468 | ts('Height (rows)') . ' ', | |
469 | $attributes['note_rows'], | |
470 | FALSE | |
471 | ); | |
2f940a36 NG |
472 | $this->add('text', |
473 | 'note_length', | |
474 | ts('Maximum length') . ' ', | |
475 | $attributes['text_length'], // note_length is an alias for the text-length field | |
476 | FALSE | |
477 | ); | |
6a488035 TO |
478 | |
479 | $this->addRule('note_columns', ts('Value should be a positive number'), 'positiveInteger'); | |
480 | $this->addRule('note_rows', ts('Value should be a positive number'), 'positiveInteger'); | |
2f940a36 | 481 | $this->addRule('note_length', ts('Value should be a positive number'), 'positiveInteger'); |
6a488035 TO |
482 | |
483 | // weight | |
484 | $this->add('text', 'weight', ts('Order'), | |
485 | $attributes['weight'], | |
486 | TRUE | |
487 | ); | |
488 | $this->addRule('weight', ts('is a numeric field'), 'numeric'); | |
489 | ||
490 | // is required ? | |
491 | $this->add('checkbox', 'is_required', ts('Required?')); | |
492 | ||
493 | // checkbox / radio options per line | |
494 | $this->add('text', 'options_per_line', ts('Options Per Line')); | |
495 | $this->addRule('options_per_line', ts('must be a numeric value'), 'numeric'); | |
496 | ||
497 | // default value, help pre, help post, mask, attributes, javascript ? | |
498 | $this->add('text', 'default_value', ts('Default Value'), | |
499 | $attributes['default_value'] | |
500 | ); | |
501 | $this->add('textarea', 'help_pre', ts('Field Pre Help'), | |
502 | $attributes['help_pre'] | |
503 | ); | |
504 | $this->add('textarea', 'help_post', ts('Field Post Help'), | |
505 | $attributes['help_post'] | |
506 | ); | |
507 | $this->add('text', 'mask', ts('Mask'), | |
508 | $attributes['mask'] | |
509 | ); | |
510 | ||
511 | // is active ? | |
512 | $this->add('checkbox', 'is_active', ts('Active?')); | |
513 | ||
514 | // is active ? | |
515 | $this->add('checkbox', 'is_view', ts('View Only?')); | |
516 | ||
517 | // is searchable ? | |
518 | $this->addElement('checkbox', | |
519 | 'is_searchable', | |
520 | ts('Is this Field Searchable?'), | |
521 | NULL, array('onclick' => "showSearchRange(this)") | |
522 | ); | |
523 | ||
524 | // is searchable by range? | |
525 | $searchRange = array(); | |
526 | $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1'); | |
527 | $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0'); | |
528 | ||
529 | $this->addGroup($searchRange, 'is_search_range', ts('Search by Range?')); | |
530 | ||
531 | // add buttons | |
532 | $this->addButtons(array( | |
533 | array( | |
81913453 | 534 | 'type' => 'done', |
6a488035 TO |
535 | 'name' => ts('Save'), |
536 | 'isDefault' => TRUE, | |
537 | ), | |
538 | array( | |
539 | 'type' => 'next', | |
540 | 'name' => ts('Save and New'), | |
541 | 'subName' => 'new', | |
542 | ), | |
543 | array( | |
544 | 'type' => 'cancel', | |
545 | 'name' => ts('Cancel'), | |
546 | ), | |
547 | ) | |
548 | ); | |
549 | ||
550 | // add a form rule to check default value | |
551 | $this->addFormRule(array('CRM_Custom_Form_Field', 'formRule'), $this); | |
552 | ||
553 | // if view mode pls freeze it with the done button. | |
554 | if ($this->_action & CRM_Core_Action::VIEW) { | |
555 | $this->freeze(); | |
556 | $url = CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid); | |
557 | $this->addElement('button', | |
558 | 'done', | |
559 | ts('Done'), | |
560 | array('onclick' => "location.href='$url'") | |
561 | ); | |
562 | } | |
563 | } | |
564 | ||
565 | /** | |
100fef9d | 566 | * Global validation rules for the form |
6a488035 | 567 | * |
c4ca4892 TO |
568 | * @param array $fields |
569 | * Posted values of the form. | |
77b97be7 EM |
570 | * |
571 | * @param $files | |
572 | * @param $self | |
6a488035 TO |
573 | * |
574 | * @return array if errors then list of errors to be posted back to the form, | |
575 | * true otherwise | |
576 | * @static | |
6a488035 | 577 | */ |
00be9182 | 578 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
579 | $default = CRM_Utils_Array::value('default_value', $fields); |
580 | ||
581 | $errors = array(); | |
582 | ||
583 | //validate field label as well as name. | |
584 | $title = $fields['label']; | |
585 | $name = CRM_Utils_String::munge($title, '_', 64); | |
e1069955 | 586 | $gId = $self->_gid; // CRM-7564 |
8ef12e64 | 587 | $query = 'select count(*) from civicrm_custom_field where ( name like %1 OR label like %2 ) and id != %3 and custom_group_id = %4'; |
e8646905 TO |
588 | $fldCnt = CRM_Core_DAO::singleValueQuery($query, array( |
589 | 1 => array($name, 'String'), | |
6a488035 | 590 | 2 => array($title, 'String'), |
e8646905 | 591 | 3 => array((int) $self->_id, 'Integer'), |
e1069955 | 592 | 4 => array($gId, 'Integer'), |
6a488035 TO |
593 | )); |
594 | if ($fldCnt) { | |
595 | $errors['label'] = ts('Custom field \'%1\' already exists in Database.', array(1 => $title)); | |
596 | } | |
597 | ||
598 | //checks the given custom field name doesnot start with digit | |
599 | if (!empty($title)) { | |
600 | // gives the ascii value | |
601 | $asciiValue = ord($title{0}); | |
602 | if ($asciiValue >= 48 && $asciiValue <= 57) { | |
5ab78070 | 603 | $errors['label'] = ts("Name cannot not start with a digit"); |
6a488035 TO |
604 | } |
605 | } | |
606 | ||
607 | // ensure that the label is not 'id' | |
608 | if (strtolower($title) == 'id') { | |
609 | $errors['label'] = ts("You cannot use 'id' as a field label."); | |
610 | } | |
611 | ||
612 | if (!isset($fields['data_type'][0]) || !isset($fields['data_type'][1])) { | |
613 | $errors['_qf_default'] = ts('Please enter valid - Data and Input Field Type.'); | |
614 | } | |
615 | ||
616 | $dataType = self::$_dataTypeKeys[$fields['data_type'][0]]; | |
617 | ||
618 | if ($default || $dataType == 'ContactReference') { | |
619 | switch ($dataType) { | |
620 | case 'Int': | |
621 | if (!CRM_Utils_Rule::integer($default)) { | |
5ab78070 | 622 | $errors['default_value'] = ts('Please enter a valid integer.'); |
6a488035 TO |
623 | } |
624 | break; | |
625 | ||
626 | case 'Float': | |
627 | if (!CRM_Utils_Rule::numeric($default)) { | |
5ab78070 | 628 | $errors['default_value'] = ts('Please enter a valid number.'); |
6a488035 TO |
629 | } |
630 | break; | |
631 | ||
632 | case 'Money': | |
633 | if (!CRM_Utils_Rule::money($default)) { | |
5ab78070 | 634 | $errors['default_value'] = ts('Please enter a valid number.'); |
6a488035 TO |
635 | } |
636 | break; | |
637 | ||
638 | case 'Link': | |
639 | if (!CRM_Utils_Rule::url($default)) { | |
640 | $errors['default_value'] = ts('Please enter a valid link.'); | |
641 | } | |
642 | break; | |
643 | ||
644 | case 'Date': | |
645 | if (!CRM_Utils_Rule::date($default)) { | |
646 | $errors['default_value'] = ts('Please enter a valid date as default value using YYYY-MM-DD format. Example: 2004-12-31.'); | |
647 | } | |
648 | break; | |
649 | ||
650 | case 'Boolean': | |
651 | if ($default != '1' && $default != '0') { | |
652 | $errors['default_value'] = ts('Please enter 1 (for Yes) or 0 (for No) if you want to set a default value.'); | |
653 | } | |
654 | break; | |
655 | ||
656 | case 'Country': | |
657 | if (!empty($default)) { | |
658 | $query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1"; | |
659 | $params = array(1 => array($fields['default_value'], 'String')); | |
660 | if (CRM_Core_DAO::singleValueQuery($query, $params) <= 0) { | |
661 | $errors['default_value'] = ts('Invalid default value for country.'); | |
662 | } | |
663 | } | |
664 | break; | |
665 | ||
666 | case 'StateProvince': | |
667 | if (!empty($default)) { | |
668 | $query = " | |
8ef12e64 | 669 | SELECT count(*) |
6a488035 TO |
670 | FROM civicrm_state_province |
671 | WHERE name = %1 | |
672 | OR abbreviation = %1"; | |
673 | $params = array(1 => array($fields['default_value'], 'String')); | |
674 | if (CRM_Core_DAO::singleValueQuery($query, $params) <= 0) { | |
675 | $errors['default_value'] = ts('The invalid default value for State/Province data type'); | |
676 | } | |
677 | } | |
678 | break; | |
679 | ||
680 | case 'ContactReference': | |
8cc574cf | 681 | if ($fields['filter_selected'] == 'Advance' && !empty($fields['filter'])) { |
6a488035 TO |
682 | if (strpos($fields['filter'], 'entity=') !== FALSE) { |
683 | $errors['filter'] = ts("Please do not include entity parameter (entity is always 'contact')"); | |
684 | } | |
06508628 CW |
685 | elseif (strpos($fields['filter'], 'action=get') === FALSE) { |
686 | $errors['filter'] = ts("Only 'get' action is supported."); | |
6a488035 TO |
687 | } |
688 | } | |
689 | $self->setDefaults(array('filter_selected', $fields['filter_selected'])); | |
690 | break; | |
691 | } | |
692 | } | |
693 | ||
694 | if (self::$_dataTypeKeys[$fields['data_type'][0]] == 'Date') { | |
695 | if (!$fields['date_format']) { | |
696 | $errors['date_format'] = ts('Please select a date format.'); | |
697 | } | |
698 | } | |
699 | ||
700 | /** Check the option values entered | |
701 | * Appropriate values are required for the selected datatype | |
702 | * Incomplete row checking is also required. | |
703 | */ | |
704 | $_flagOption = $_rowError = 0; | |
705 | $_showHide = new CRM_Core_ShowHideBlocks('', ''); | |
706 | $dataType = self::$_dataTypeKeys[$fields['data_type'][0]]; | |
707 | if (isset($fields['data_type'][1])) { | |
708 | $dataField = $fields['data_type'][1]; | |
709 | } | |
710 | $optionFields = array('Select', 'Multi-Select', 'CheckBox', 'Radio', 'AdvMulti-Select'); | |
711 | ||
36d1ff7f | 712 | if (isset($fields['option_type']) && $fields['option_type'] == 1) { |
6a488035 TO |
713 | //capture duplicate Custom option values |
714 | if (!empty($fields['option_value'])) { | |
715 | $countValue = count($fields['option_value']); | |
716 | $uniqueCount = count(array_unique($fields['option_value'])); | |
717 | ||
718 | if ($countValue > $uniqueCount) { | |
719 | ||
720 | $start = 1; | |
721 | while ($start < self::NUM_OPTION) { | |
722 | $nextIndex = $start + 1; | |
723 | while ($nextIndex <= self::NUM_OPTION) { | |
724 | if ($fields['option_value'][$start] == $fields['option_value'][$nextIndex] && | |
725 | !empty($fields['option_value'][$nextIndex]) | |
726 | ) { | |
727 | $errors['option_value[' . $start . ']'] = ts('Duplicate Option values'); | |
728 | $errors['option_value[' . $nextIndex . ']'] = ts('Duplicate Option values'); | |
729 | $_flagOption = 1; | |
730 | } | |
731 | $nextIndex++; | |
732 | } | |
733 | $start++; | |
734 | } | |
735 | } | |
736 | } | |
737 | ||
738 | //capture duplicate Custom Option label | |
739 | if (!empty($fields['option_label'])) { | |
740 | $countValue = count($fields['option_label']); | |
741 | $uniqueCount = count(array_unique($fields['option_label'])); | |
742 | ||
743 | if ($countValue > $uniqueCount) { | |
744 | $start = 1; | |
745 | while ($start < self::NUM_OPTION) { | |
746 | $nextIndex = $start + 1; | |
747 | while ($nextIndex <= self::NUM_OPTION) { | |
748 | if ($fields['option_label'][$start] == $fields['option_label'][$nextIndex] && | |
749 | !empty($fields['option_label'][$nextIndex]) | |
750 | ) { | |
751 | $errors['option_label[' . $start . ']'] = ts('Duplicate Option label'); | |
752 | $errors['option_label[' . $nextIndex . ']'] = ts('Duplicate Option label'); | |
753 | $_flagOption = 1; | |
754 | } | |
755 | $nextIndex++; | |
756 | } | |
757 | $start++; | |
758 | } | |
759 | } | |
760 | } | |
761 | ||
762 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
763 | if (!$fields['option_label'][$i]) { | |
764 | if ($fields['option_value'][$i]) { | |
765 | $errors['option_label[' . $i . ']'] = ts('Option label cannot be empty'); | |
766 | $_flagOption = 1; | |
767 | } | |
768 | else { | |
769 | $_emptyRow = 1; | |
770 | } | |
771 | } | |
772 | else { | |
773 | if (!strlen(trim($fields['option_value'][$i]))) { | |
774 | if (!$fields['option_value'][$i]) { | |
775 | $errors['option_value[' . $i . ']'] = ts('Option value cannot be empty'); | |
776 | $_flagOption = 1; | |
777 | } | |
778 | } | |
779 | } | |
780 | ||
781 | if ($fields['option_value'][$i] && $dataType != 'String') { | |
782 | if ($dataType == 'Int') { | |
783 | if (!CRM_Utils_Rule::integer($fields['option_value'][$i])) { | |
784 | $_flagOption = 1; | |
785 | $errors['option_value[' . $i . ']'] = ts('Please enter a valid integer.'); | |
786 | } | |
787 | } | |
788 | elseif ($dataType == 'Money') { | |
789 | if (!CRM_Utils_Rule::money($fields['option_value'][$i])) { | |
790 | $_flagOption = 1; | |
791 | $errors['option_value[' . $i . ']'] = ts('Please enter a valid money value.'); | |
792 | } | |
793 | } | |
794 | else { | |
795 | if (!CRM_Utils_Rule::numeric($fields['option_value'][$i])) { | |
796 | $_flagOption = 1; | |
797 | $errors['option_value[' . $i . ']'] = ts('Please enter a valid number.'); | |
798 | } | |
799 | } | |
800 | } | |
801 | ||
802 | $showBlocks = 'optionField_' . $i; | |
803 | if ($_flagOption) { | |
804 | $_showHide->addShow($showBlocks); | |
805 | $_rowError = 1; | |
806 | } | |
807 | ||
808 | if (!empty($_emptyRow)) { | |
809 | $_showHide->addHide($showBlocks); | |
810 | } | |
811 | else { | |
812 | $_showHide->addShow($showBlocks); | |
813 | } | |
814 | if ($i == self::NUM_OPTION) { | |
815 | $hideBlock = 'additionalOption'; | |
816 | $_showHide->addHide($hideBlock); | |
817 | } | |
818 | ||
819 | $_flagOption = $_emptyRow = 0; | |
820 | } | |
821 | } | |
822 | elseif (isset($dataField) && | |
823 | in_array($dataField, $optionFields) && | |
824 | !in_array($dataType, array('Boolean', 'Country', 'StateProvince')) | |
825 | ) { | |
826 | if (!$fields['option_group_id']) { | |
827 | $errors['option_group_id'] = ts('You must select a Multiple Choice Option set if you chose Reuse an existing set.'); | |
828 | } | |
829 | else { | |
830 | $query = " | |
831 | SELECT count(*) | |
832 | FROM civicrm_custom_field | |
833 | WHERE data_type != %1 | |
834 | AND option_group_id = %2"; | |
835 | $params = array( | |
836 | 1 => array(self::$_dataTypeKeys[$fields['data_type'][0]], | |
837 | 'String', | |
838 | ), | |
839 | 2 => array($fields['option_group_id'], 'Integer'), | |
840 | ); | |
841 | $count = CRM_Core_DAO::singleValueQuery($query, $params); | |
842 | if ($count > 0) { | |
843 | $errors['option_group_id'] = ts('The data type of the multiple choice option set you\'ve selected does not match the data type assigned to this field.'); | |
844 | } | |
845 | } | |
846 | } | |
847 | ||
848 | $assignError = new CRM_Core_Page(); | |
849 | if ($_rowError) { | |
850 | $_showHide->addToTemplate(); | |
851 | $assignError->assign('optionRowError', $_rowError); | |
852 | } | |
853 | else { | |
854 | if (isset($fields['data_type'][1])) { | |
855 | switch (self::$_dataToHTML[$fields['data_type'][0]][$fields['data_type'][1]]) { | |
856 | case 'Radio': | |
857 | $_fieldError = 1; | |
858 | $assignError->assign('fieldError', $_fieldError); | |
859 | break; | |
860 | ||
861 | case 'Checkbox': | |
862 | $_fieldError = 1; | |
863 | $assignError->assign('fieldError', $_fieldError); | |
864 | break; | |
865 | ||
866 | case 'Select': | |
867 | $_fieldError = 1; | |
868 | $assignError->assign('fieldError', $_fieldError); | |
869 | break; | |
870 | ||
871 | default: | |
872 | $_fieldError = 0; | |
873 | $assignError->assign('fieldError', $_fieldError); | |
874 | } | |
875 | } | |
876 | ||
877 | for ($idx = 1; $idx <= self::NUM_OPTION; $idx++) { | |
878 | $showBlocks = 'optionField_' . $idx; | |
879 | if (!empty($fields['option_label'][$idx])) { | |
880 | $_showHide->addShow($showBlocks); | |
881 | } | |
882 | else { | |
883 | $_showHide->addHide($showBlocks); | |
884 | } | |
885 | } | |
886 | $_showHide->addToTemplate(); | |
887 | } | |
888 | ||
889 | // we can not set require and view at the same time. | |
8cc574cf | 890 | if (!empty($fields['is_required']) && !empty($fields['is_view'])) { |
6a488035 TO |
891 | $errors['is_view'] = ts('Can not set this field Required and View Only at the same time.'); |
892 | } | |
893 | ||
894 | return empty($errors) ? TRUE : $errors; | |
895 | } | |
896 | ||
897 | /** | |
898 | * Process the form | |
899 | * | |
900 | * @param null | |
901 | * | |
902 | * @return void | |
6a488035 TO |
903 | */ |
904 | public function postProcess() { | |
905 | // store the submitted values in an array | |
906 | $params = $this->controller->exportValues($this->_name); | |
907 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
908 | $dataTypeKey = $this->_defaultDataType[0]; | |
909 | $params['data_type'] = self::$_dataTypeKeys[$this->_defaultDataType[0]]; | |
910 | $params['html_type'] = self::$_dataToHTML[$this->_defaultDataType[0]][$this->_defaultDataType[1]]; | |
911 | } | |
912 | else { | |
913 | $dataTypeKey = $params['data_type'][0]; | |
914 | $params['html_type'] = self::$_dataToHTML[$params['data_type'][0]][$params['data_type'][1]]; | |
915 | $params['data_type'] = self::$_dataTypeKeys[$params['data_type'][0]]; | |
916 | } | |
917 | ||
918 | //fix for 'is_search_range' field. | |
919 | if (in_array($dataTypeKey, array( | |
920 | 1, 2, 3, 5))) { | |
a7488080 | 921 | if (empty($params['is_searchable'])) { |
6a488035 TO |
922 | $params['is_search_range'] = 0; |
923 | } | |
924 | } | |
925 | else { | |
926 | $params['is_search_range'] = 0; | |
927 | } | |
928 | ||
929 | $filter = 'null'; | |
8cc574cf | 930 | if ($dataTypeKey == 11 && !empty($params['filter_selected'])) { |
6a488035 TO |
931 | if ($params['filter_selected'] == 'Advance' && trim(CRM_Utils_Array::value('filter', $params))) { |
932 | $filter = trim($params['filter']); | |
933 | } | |
8cc574cf | 934 | elseif ($params['filter_selected'] == 'Group' && !empty($params['group_id'])) { |
6a488035 TO |
935 | |
936 | $filter = 'action=lookup&group=' . implode(',', $params['group_id']); | |
937 | } | |
938 | } | |
939 | $params['filter'] = $filter; | |
940 | ||
941 | // fix for CRM-316 | |
942 | $oldWeight = NULL; | |
943 | if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) { | |
944 | $fieldValues = array('custom_group_id' => $this->_gid); | |
945 | if ($this->_id) { | |
946 | $oldWeight = $this->_values['weight']; | |
947 | } | |
948 | $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_CustomField', $oldWeight, $params['weight'], $fieldValues); | |
949 | } | |
950 | ||
951 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
952 | ||
953 | //store the primary key for State/Province or Country as default value. | |
954 | if (strlen(trim($params['default_value']))) { | |
955 | switch ($params['data_type']) { | |
956 | case 'StateProvince': | |
957 | $fieldStateProvince = $strtolower($params['default_value']); | |
958 | $query = " | |
959 | SELECT id | |
8ef12e64 | 960 | FROM civicrm_state_province |
961 | WHERE LOWER(name) = '$fieldStateProvince' | |
6a488035 TO |
962 | OR abbreviation = '$fieldStateProvince'"; |
963 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
964 | if ($dao->fetch()) { | |
965 | $params['default_value'] = $dao->id; | |
966 | } | |
967 | break; | |
968 | ||
969 | case 'Country': | |
970 | $fieldCountry = $strtolower($params['default_value']); | |
971 | $query = " | |
972 | SELECT id | |
973 | FROM civicrm_country | |
8ef12e64 | 974 | WHERE LOWER(name) = '$fieldCountry' |
6a488035 TO |
975 | OR iso_code = '$fieldCountry'"; |
976 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
977 | if ($dao->fetch()) { | |
978 | $params['default_value'] = $dao->id; | |
979 | } | |
980 | break; | |
981 | } | |
982 | } | |
983 | ||
2f940a36 NG |
984 | // The text_length attribute for Memo fields is in a different input as there |
985 | // are different label, help text and default value than for other type fields | |
986 | if ($params['data_type'] == "Memo") { | |
987 | $params['text_length'] = $params['note_length']; | |
988 | } | |
989 | ||
6a488035 TO |
990 | // need the FKEY - custom group id |
991 | $params['custom_group_id'] = $this->_gid; | |
992 | ||
993 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
994 | $params['id'] = $this->_id; | |
995 | } | |
6a488035 | 996 | $customField = CRM_Core_BAO_CustomField::create($params); |
f84151fd | 997 | $this->_id = $customField->id; |
6a488035 TO |
998 | |
999 | // reset the cache | |
1000 | CRM_Core_BAO_Cache::deleteGroup('contact fields'); | |
1001 | ||
0b1bae4a | 1002 | $msg = '<p>' . ts("Custom field '%1' has been saved.", array(1 => $customField->label)) . '</p>'; |
6a488035 TO |
1003 | |
1004 | $buttonName = $this->controller->getButtonName(); | |
1005 | $session = CRM_Core_Session::singleton(); | |
1006 | if ($buttonName == $this->getButtonName('next', 'new')) { | |
40084681 | 1007 | $msg .= '<p>' . ts("Ready to add another.") . '</p>'; |
6a488035 TO |
1008 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field/add', |
1009 | 'reset=1&action=add&gid=' . $this->_gid | |
1010 | )); | |
1011 | } | |
1012 | else { | |
1013 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', | |
1014 | 'reset=1&action=browse&gid=' . $this->_gid | |
1015 | )); | |
1016 | } | |
0b1bae4a CW |
1017 | $session->setStatus($msg, ts('Saved'), 'success'); |
1018 | ||
1019 | // Add data when in ajax contect | |
1020 | $this->ajaxResponse['customField'] = $customField->toArray(); | |
6a488035 TO |
1021 | } |
1022 | } |