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