3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
37 * form to process actions on the field aspect of Custom
39 class CRM_Custom_Form_Field
extends CRM_Core_Form
{
42 * Constants for number of options for data types of multiple option.
44 const NUM_OPTION
= 11;
47 * The custom group id saved to the session for an update.
54 * The field id, used when editing the field
61 * The default custom data/input types, when editing the field
65 protected $_defaultDataType;
68 * Array of custom field values if update mode.
73 * Array for valid combinations of data_type & html_type
77 private static $_dataTypeValues = NULL;
78 private static $_dataTypeKeys = NULL;
80 private static $_dataToHTML = NULL;
82 private static $_dataToLabels = NULL;
85 * Set variables up before form is built.
89 public function preProcess() {
90 if (!(self
::$_dataTypeKeys)) {
91 self
::$_dataTypeKeys = array_keys(CRM_Core_BAO_CustomField
::dataType());
92 self
::$_dataTypeValues = array_values(CRM_Core_BAO_CustomField
::dataType());
95 if (!self
::$_dataToHTML) {
96 self
::$_dataToHTML = CRM_Core_BAO_CustomField
::dataToHtml();
100 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
102 $this->_values
= array();
103 //get the values form db if update.
105 $params = array('id' => $this->_id
);
106 CRM_Core_BAO_CustomField
::retrieve($params, $this->_values
);
107 // note_length is an alias for the text_length field
108 $this->_values
['note_length'] = CRM_Utils_Array
::value('text_length', $this->_values
);
110 $this->_gid
= $this->_values
['custom_group_id'];
114 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this);
117 if ($isReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_reserved', 'id')) {
118 CRM_Core_Error
::fatal("You cannot add or edit fields in a reserved custom field-set.");
122 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field',
123 "reset=1&action=browse&gid={$this->_gid}"
126 $session = CRM_Core_Session
::singleton();
127 $session->pushUserContext($url);
130 if (self
::$_dataToLabels == NULL) {
131 self
::$_dataToLabels = array(
133 'Text' => ts('Text'),
134 'Select' => ts('Select'),
135 'Radio' => ts('Radio'),
136 'CheckBox' => ts('CheckBox'),
137 'Multi-Select' => ts('Multi-Select'),
138 'Autocomplete-Select' => ts('Autocomplete-Select'),
141 'Text' => ts('Text'),
142 'Select' => ts('Select'),
143 'Radio' => ts('Radio'),
146 'Text' => ts('Text'),
147 'Select' => ts('Select'),
148 'Radio' => ts('Radio'),
151 'Text' => ts('Text'),
152 'Select' => ts('Select'),
153 'Radio' => ts('Radio'),
155 array('TextArea' => ts('TextArea'), 'RichTextEditor' => ts('Rich Text Editor')),
156 array('Date' => ts('Select Date')),
157 array('Radio' => ts('Radio')),
158 array('StateProvince' => ts('Select State/Province'), 'Multi-Select' => ts('Multi-Select State/Province')),
159 array('Country' => ts('Select Country'), 'Multi-Select' => ts('Multi-Select Country')),
160 array('File' => ts('Select File')),
161 array('Link' => ts('Link')),
162 array('ContactReference' => ts('Autocomplete-Select')),
168 * Set default values for the form. Note that in edit/view mode
169 * the default values are retrieved from the database
172 * array of default values
174 public function setDefaultValues() {
175 $defaults = $this->_values
;
178 $this->assign('id', $this->_id
);
179 $this->_gid
= $defaults['custom_group_id'];
181 //get the value for state or country
182 if ($defaults['data_type'] == 'StateProvince' &&
183 $stateId = CRM_Utils_Array
::value('default_value', $defaults)
185 $defaults['default_value'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_StateProvince', $stateId);
187 elseif ($defaults['data_type'] == 'Country' &&
188 $countryId = CRM_Utils_Array
::value('default_value', $defaults)
190 $defaults['default_value'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Country', $countryId);
193 if ($defaults['data_type'] == 'ContactReference' && !empty($defaults['filter'])) {
194 $contactRefFilter = 'Advance';
195 if (strpos($defaults['filter'], 'action=lookup') !== FALSE &&
196 strpos($defaults['filter'], 'group=') !== FALSE
198 $filterParts = explode('&', $defaults['filter']);
200 if (count($filterParts) == 2) {
201 $contactRefFilter = 'Group';
202 foreach ($filterParts as $part) {
203 if (strpos($part, 'group=') === FALSE) {
206 $groups = substr($part, strpos($part, '=') +
1);
207 foreach (explode(',', $groups) as $grp) {
208 if (CRM_Utils_Rule
::positiveInteger($grp)) {
209 $defaults['group_id'][] = $grp;
215 $defaults['filter_selected'] = $contactRefFilter;
218 if (!empty($defaults['data_type'])) {
219 $defaultDataType = array_search($defaults['data_type'],
222 $defaultHTMLType = array_search($defaults['html_type'],
223 self
::$_dataToHTML[$defaultDataType]
225 $defaults['data_type'] = array(
226 '0' => $defaultDataType,
227 '1' => $defaultHTMLType,
229 $this->_defaultDataType
= $defaults['data_type'];
232 $defaults['option_type'] = 2;
234 $this->assign('changeFieldType', CRM_Custom_Form_ChangeFieldType
::fieldTypeTransitions($this->_values
['data_type'], $this->_values
['html_type']));
237 $defaults['is_active'] = 1;
238 $defaults['option_type'] = 1;
239 $defaults['is_search_range'] = 1;
242 // set defaults for weight.
243 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
244 $defaults['option_status[' . $i . ']'] = 1;
245 $defaults['option_weight[' . $i . ']'] = $i;
246 $defaults['option_value[' . $i . ']'] = $i;
249 if ($this->_action
& CRM_Core_Action
::ADD
) {
250 $fieldValues = array('custom_group_id' => $this->_gid
);
251 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_CustomField', $fieldValues);
253 $defaults['text_length'] = 255;
254 $defaults['note_columns'] = 60;
255 $defaults['note_rows'] = 4;
256 $defaults['is_view'] = 0;
259 if (!empty($defaults['html_type'])) {
260 $dontShowLink = substr($defaults['html_type'], -14) == 'State/Province' ||
substr($defaults['html_type'], -7) == 'Country' ?
1 : 0;
263 if (isset($dontShowLink)) {
264 $this->assign('dontShowLink', $dontShowLink);
266 if ($this->_action
& CRM_Core_Action
::ADD
&&
267 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_multiple', 'id')
269 $defaults['in_selector'] = 1;
276 * Build the form object.
280 public function buildQuickForm() {
282 $this->_title
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'title');
283 CRM_Utils_System
::setTitle($this->_title
. ' - ' . ($this->_id ?
ts('Edit Field') : ts('New Field')));
284 $this->assign('gid', $this->_gid
);
287 $this->assign('dataTypeKeys', self
::$_dataTypeKeys);
289 // lets trim all the whitespace
290 $this->applyFilter('__ALL__', 'trim');
292 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_CustomField');
298 $attributes['label'],
302 $dt = &self
::$_dataTypeValues;
304 foreach ($dt as $key => $value) {
305 $it[$key] = self
::$_dataToLabels[$key];
307 $sel = &$this->addElement('hierselect',
309 ts('Data and Input Field Type'),
312 $sel->setOptions(array($dt, $it));
314 if (CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_multiple')) {
315 $this->add('checkbox', 'in_selector', ts('Display in Table?'));
318 $optionGroupParams = [
321 'options' => ['limit' => 0, 'sort' => "title ASC"],
322 'return' => ['title'],
325 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
326 $this->freeze('data_type');
327 if (!empty($this->_values
['option_group_id'])) {
328 // Before dev/core#155 we didn't set the is_reserved flag properly, which should be handled by the upgrade script...
329 // but it is still possible that existing installs may have optiongroups linked to custom fields that are marked reserved.
330 $optionGroupParams['id'] = $this->_values
['option_group_id'];
331 $optionGroupParams['options']['or'] = [["is_reserved", "id"]];
335 // Retrieve optiongroups for selection list
336 $optionGroupMetadata = civicrm_api3('OptionGroup', 'get', $optionGroupParams);
338 // OptionGroup selection
339 $optionTypes = array('1' => ts('Create a new set of options'));
341 if (!empty($optionGroupMetadata['values'])) {
342 $emptyOptGroup = FALSE;
343 $optionGroups = CRM_Utils_Array
::collect('title', $optionGroupMetadata['values']);
344 $optionTypes['2'] = ts('Reuse an existing set');
348 ts('Multiple Choice Option Sets'),
350 '' => ts('- select -'),
355 // No custom (non-reserved) option groups
356 $emptyOptGroup = TRUE;
359 $element = &$this->addRadio('option_type',
363 'onclick' => "showOptionSelect();",
366 // if empty option group freeze the option type.
367 if ($emptyOptGroup) {
371 $contactGroups = CRM_Core_PseudoConstant
::group();
372 asort($contactGroups);
376 ts('Limit List to Group'),
379 array('multiple' => 'multiple', 'class' => 'crm-select2')
384 ts('Advanced Filter'),
385 $attributes['filter']
388 $this->add('hidden', 'filter_selected', 'Group', array('id' => 'filter_selected'));
390 // form fields of Custom Option rows
391 $defaultOption = array();
392 $_showHide = new CRM_Core_ShowHideBlocks('', '');
393 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
395 //the show hide blocks
396 $showBlocks = 'optionField_' . $i;
398 $_showHide->addHide($showBlocks);
399 if ($i == self
::NUM_OPTION
) {
400 $_showHide->addHide('additionalOption');
404 $_showHide->addShow($showBlocks);
407 $optionAttributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue');
409 $this->add('text', 'option_label[' . $i . ']', ts('Label'),
410 $optionAttributes['label']
414 $this->add('text', 'option_value[' . $i . ']', ts('Value'),
415 $optionAttributes['value']
419 $this->add('number', "option_weight[$i]", ts('Order'),
420 $optionAttributes['weight']
424 $this->add('checkbox', "option_status[$i]", ts('Active?'));
426 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
428 //for checkbox handling of default option
429 $this->add('checkbox', "default_checkbox_option[$i]", NULL);
432 //default option selection
433 $this->addGroup($defaultOption, 'default_option');
435 $_showHide->addToTemplate();
437 // text length for alpha numeric data types
440 ts('Database field length'),
441 $attributes['text_length'] +
array('min' => 1),
444 $this->addRule('text_length', ts('Value should be a positive number'), 'integer');
448 ts('Dates may be up to'),
449 $attributes['start_date_years'],
454 ts('Dates may be up to'),
455 $attributes['end_date_years'],
459 $this->addRule('start_date_years', ts('Value should be a positive number'), 'integer');
460 $this->addRule('end_date_years', ts('Value should be a positive number'), 'integer');
462 $this->add('select', 'date_format', ts('Date Format'),
463 array('' => ts('- select -')) + CRM_Core_SelectValues
::getDatePluginInputFormats()
466 $this->add('select', 'time_format', ts('Time'),
467 array('' => ts('- none -')) + CRM_Core_SelectValues
::getTimeFormats()
473 ts('Width (columns)') . ' ',
474 $attributes['note_columns'],
479 ts('Height (rows)') . ' ',
480 $attributes['note_rows'],
485 ts('Maximum length') . ' ',
486 $attributes['text_length'], // note_length is an alias for the text-length field
490 $this->addRule('note_columns', ts('Value should be a positive number'), 'positiveInteger');
491 $this->addRule('note_rows', ts('Value should be a positive number'), 'positiveInteger');
492 $this->addRule('note_length', ts('Value should be a positive number'), 'positiveInteger');
495 $this->add('number', 'weight', ts('Order'),
496 $attributes['weight'],
499 $this->addRule('weight', ts('is a numeric field'), 'numeric');
502 $this->add('advcheckbox', 'is_required', ts('Required?'));
504 // checkbox / radio options per line
505 $this->add('number', 'options_per_line', ts('Options Per Line'), array('min' => 0));
506 $this->addRule('options_per_line', ts('must be a numeric value'), 'numeric');
508 // default value, help pre, help post, mask, attributes, javascript ?
509 $this->add('text', 'default_value', ts('Default Value'),
510 $attributes['default_value']
512 $this->add('textarea', 'help_pre', ts('Field Pre Help'),
513 $attributes['help_pre']
515 $this->add('textarea', 'help_post', ts('Field Post Help'),
516 $attributes['help_post']
518 $this->add('text', 'mask', ts('Mask'),
523 $this->add('advcheckbox', 'is_active', ts('Active?'));
526 $this->add('advcheckbox', 'is_view', ts('View Only?'));
529 $this->addElement('advcheckbox',
531 ts('Is this Field Searchable?')
534 // is searchable by range?
535 $searchRange = array();
536 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
537 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
539 $this->addGroup($searchRange, 'is_search_range', ts('Search by Range?'));
542 $this->addButtons(array(
545 'name' => ts('Save'),
550 'name' => ts('Save and New'),
555 'name' => ts('Cancel'),
560 // add a form rule to check default value
561 $this->addFormRule(array('CRM_Custom_Form_Field', 'formRule'), $this);
563 // if view mode pls freeze it with the done button.
564 if ($this->_action
& CRM_Core_Action
::VIEW
) {
566 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid
);
567 $this->addElement('button',
570 array('onclick' => "location.href='$url'")
576 * Global validation rules for the form.
578 * @param array $fields
579 * Posted values of the form.
585 * if errors then list of errors to be posted back to the form,
588 public static function formRule($fields, $files, $self) {
589 $default = CRM_Utils_Array
::value('default_value', $fields);
593 self
::clearEmptyOptions($fields);
595 //validate field label as well as name.
596 $title = $fields['label'];
597 $name = CRM_Utils_String
::munge($title, '_', 64);
598 $gId = $self->_gid
; // CRM-7564
599 $query = 'select count(*) from civicrm_custom_field where ( name like %1 OR label like %2 ) and id != %3 and custom_group_id = %4';
600 $fldCnt = CRM_Core_DAO
::singleValueQuery($query, array(
601 1 => array($name, 'String'),
602 2 => array($title, 'String'),
603 3 => array((int) $self->_id
, 'Integer'),
604 4 => array($gId, 'Integer'),
607 $errors['label'] = ts('Custom field \'%1\' already exists in Database.', array(1 => $title));
610 //checks the given custom field name doesnot start with digit
611 if (!empty($title)) {
612 // gives the ascii value
613 $asciiValue = ord($title{0});
614 if ($asciiValue >= 48 && $asciiValue <= 57) {
615 $errors['label'] = ts("Name cannot not start with a digit");
619 // ensure that the label is not 'id'
620 if (strtolower($title) == 'id') {
621 $errors['label'] = ts("You cannot use 'id' as a field label.");
624 if (!isset($fields['data_type'][0]) ||
!isset($fields['data_type'][1])) {
625 $errors['_qf_default'] = ts('Please enter valid - Data and Input Field Type.');
628 $dataType = self
::$_dataTypeKeys[$fields['data_type'][0]];
630 if ($default ||
$dataType == 'ContactReference') {
633 if (!CRM_Utils_Rule
::integer($default)) {
634 $errors['default_value'] = ts('Please enter a valid integer.');
639 if (!CRM_Utils_Rule
::numeric($default)) {
640 $errors['default_value'] = ts('Please enter a valid number.');
645 if (!CRM_Utils_Rule
::money($default)) {
646 $errors['default_value'] = ts('Please enter a valid number.');
651 if (!CRM_Utils_Rule
::url($default)) {
652 $errors['default_value'] = ts('Please enter a valid link.');
657 if (!CRM_Utils_Rule
::date($default)) {
658 $errors['default_value'] = ts('Please enter a valid date as default value using YYYY-MM-DD format. Example: 2004-12-31.');
663 if ($default != '1' && $default != '0') {
664 $errors['default_value'] = ts('Please enter 1 (for Yes) or 0 (for No) if you want to set a default value.');
669 if (!empty($default)) {
670 $query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1";
671 $params = array(1 => array($fields['default_value'], 'String'));
672 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
673 $errors['default_value'] = ts('Invalid default value for country.');
678 case 'StateProvince':
679 if (!empty($default)) {
682 FROM civicrm_state_province
684 OR abbreviation = %1";
685 $params = array(1 => array($fields['default_value'], 'String'));
686 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
687 $errors['default_value'] = ts('The invalid default value for State/Province data type');
692 case 'ContactReference':
693 if ($fields['filter_selected'] == 'Advance' && !empty($fields['filter'])) {
694 if (strpos($fields['filter'], 'entity=') !== FALSE) {
695 $errors['filter'] = ts("Please do not include entity parameter (entity is always 'contact')");
697 elseif (strpos($fields['filter'], 'action=get') === FALSE) {
698 $errors['filter'] = ts("Only 'get' action is supported.");
701 $self->setDefaults(array('filter_selected', $fields['filter_selected']));
706 if (self
::$_dataTypeKeys[$fields['data_type'][0]] == 'Date') {
707 if (!$fields['date_format']) {
708 $errors['date_format'] = ts('Please select a date format.');
712 /** Check the option values entered
713 * Appropriate values are required for the selected datatype
714 * Incomplete row checking is also required.
716 $_flagOption = $_rowError = 0;
717 $_showHide = new CRM_Core_ShowHideBlocks('', '');
718 $dataType = self
::$_dataTypeKeys[$fields['data_type'][0]];
719 if (isset($fields['data_type'][1])) {
720 $dataField = $fields['data_type'][1];
722 $optionFields = array('Select', 'Multi-Select', 'CheckBox', 'Radio');
724 if (isset($fields['option_type']) && $fields['option_type'] == 1) {
725 //capture duplicate Custom option values
726 if (!empty($fields['option_value'])) {
727 $countValue = count($fields['option_value']);
728 $uniqueCount = count(array_unique($fields['option_value']));
730 if ($countValue > $uniqueCount) {
733 while ($start < self
::NUM_OPTION
) {
734 $nextIndex = $start +
1;
735 while ($nextIndex <= self
::NUM_OPTION
) {
736 if ($fields['option_value'][$start] == $fields['option_value'][$nextIndex] &&
737 strlen($fields['option_value'][$nextIndex])
739 $errors['option_value[' . $start . ']'] = ts('Duplicate Option values');
740 $errors['option_value[' . $nextIndex . ']'] = ts('Duplicate Option values');
750 //capture duplicate Custom Option label
751 if (!empty($fields['option_label'])) {
752 $countValue = count($fields['option_label']);
753 $uniqueCount = count(array_unique($fields['option_label']));
755 if ($countValue > $uniqueCount) {
757 while ($start < self
::NUM_OPTION
) {
758 $nextIndex = $start +
1;
759 while ($nextIndex <= self
::NUM_OPTION
) {
760 if ($fields['option_label'][$start] == $fields['option_label'][$nextIndex] &&
761 !empty($fields['option_label'][$nextIndex])
763 $errors['option_label[' . $start . ']'] = ts('Duplicate Option label');
764 $errors['option_label[' . $nextIndex . ']'] = ts('Duplicate Option label');
774 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
775 if (!$fields['option_label'][$i]) {
776 if ($fields['option_value'][$i]) {
777 $errors['option_label[' . $i . ']'] = ts('Option label cannot be empty');
785 if (!strlen(trim($fields['option_value'][$i]))) {
786 if (!$fields['option_value'][$i]) {
787 $errors['option_value[' . $i . ']'] = ts('Option value cannot be empty');
793 if ($fields['option_value'][$i] && $dataType != 'String') {
794 if ($dataType == 'Int') {
795 if (!CRM_Utils_Rule
::integer($fields['option_value'][$i])) {
797 $errors['option_value[' . $i . ']'] = ts('Please enter a valid integer.');
800 elseif ($dataType == 'Money') {
801 if (!CRM_Utils_Rule
::money($fields['option_value'][$i])) {
803 $errors['option_value[' . $i . ']'] = ts('Please enter a valid money value.');
807 if (!CRM_Utils_Rule
::numeric($fields['option_value'][$i])) {
809 $errors['option_value[' . $i . ']'] = ts('Please enter a valid number.');
814 $showBlocks = 'optionField_' . $i;
816 $_showHide->addShow($showBlocks);
820 if (!empty($_emptyRow)) {
821 $_showHide->addHide($showBlocks);
824 $_showHide->addShow($showBlocks);
826 if ($i == self
::NUM_OPTION
) {
827 $hideBlock = 'additionalOption';
828 $_showHide->addHide($hideBlock);
831 $_flagOption = $_emptyRow = 0;
834 elseif (isset($dataField) &&
835 in_array($dataField, $optionFields) &&
836 !in_array($dataType, array('Boolean', 'Country', 'StateProvince'))
838 if (!$fields['option_group_id']) {
839 $errors['option_group_id'] = ts('You must select a Multiple Choice Option set if you chose Reuse an existing set.');
844 FROM civicrm_custom_field
845 WHERE data_type != %1
846 AND option_group_id = %2";
849 self
::$_dataTypeKeys[$fields['data_type'][0]],
852 2 => array($fields['option_group_id'], 'Integer'),
854 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
856 $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.');
861 $assignError = new CRM_Core_Page();
863 $_showHide->addToTemplate();
864 $assignError->assign('optionRowError', $_rowError);
867 if (isset($fields['data_type'][1])) {
868 switch (self
::$_dataToHTML[$fields['data_type'][0]][$fields['data_type'][1]]) {
871 $assignError->assign('fieldError', $_fieldError);
876 $assignError->assign('fieldError', $_fieldError);
881 $assignError->assign('fieldError', $_fieldError);
886 $assignError->assign('fieldError', $_fieldError);
890 for ($idx = 1; $idx <= self
::NUM_OPTION
; $idx++
) {
891 $showBlocks = 'optionField_' . $idx;
892 if (!empty($fields['option_label'][$idx])) {
893 $_showHide->addShow($showBlocks);
896 $_showHide->addHide($showBlocks);
899 $_showHide->addToTemplate();
902 // we can not set require and view at the same time.
903 if (!empty($fields['is_required']) && !empty($fields['is_view'])) {
904 $errors['is_view'] = ts('Can not set this field Required and View Only at the same time.');
907 return empty($errors) ?
TRUE : $errors;
915 public function postProcess() {
916 // store the submitted values in an array
917 $params = $this->controller
->exportValues($this->_name
);
918 self
::clearEmptyOptions($params);
919 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
920 $dataTypeKey = $this->_defaultDataType
[0];
921 $params['data_type'] = self
::$_dataTypeKeys[$this->_defaultDataType
[0]];
922 $params['html_type'] = self
::$_dataToHTML[$this->_defaultDataType
[0]][$this->_defaultDataType
[1]];
925 $dataTypeKey = $params['data_type'][0];
926 $params['html_type'] = self
::$_dataToHTML[$params['data_type'][0]][$params['data_type'][1]];
927 $params['data_type'] = self
::$_dataTypeKeys[$params['data_type'][0]];
930 //fix for 'is_search_range' field.
931 if (in_array($dataTypeKey, array(
937 if (empty($params['is_searchable'])) {
938 $params['is_search_range'] = 0;
942 $params['is_search_range'] = 0;
946 if ($dataTypeKey == 11 && !empty($params['filter_selected'])) {
947 if ($params['filter_selected'] == 'Advance' && trim(CRM_Utils_Array
::value('filter', $params))) {
948 $filter = trim($params['filter']);
950 elseif ($params['filter_selected'] == 'Group' && !empty($params['group_id'])) {
952 $filter = 'action=lookup&group=' . implode(',', $params['group_id']);
955 $params['filter'] = $filter;
959 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
960 $fieldValues = array('custom_group_id' => $this->_gid
);
962 $oldWeight = $this->_values
['weight'];
964 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_CustomField', $oldWeight, $params['weight'], $fieldValues);
967 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
969 //store the primary key for State/Province or Country as default value.
970 if (strlen(trim($params['default_value']))) {
971 switch ($params['data_type']) {
972 case 'StateProvince':
973 $fieldStateProvince = $strtolower($params['default_value']);
975 // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
978 FROM civicrm_state_province
979 WHERE LOWER(name) = '$fieldStateProvince'
980 OR abbreviation = '$fieldStateProvince'";
981 $dao = CRM_Core_DAO
::executeQuery($query);
983 $params['default_value'] = $dao->id
;
988 $fieldCountry = $strtolower($params['default_value']);
990 // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
994 WHERE LOWER(name) = '$fieldCountry'
995 OR iso_code = '$fieldCountry'";
996 $dao = CRM_Core_DAO
::executeQuery($query);
998 $params['default_value'] = $dao->id
;
1004 // The text_length attribute for Memo fields is in a different input as there
1005 // are different label, help text and default value than for other type fields
1006 if ($params['data_type'] == "Memo") {
1007 $params['text_length'] = $params['note_length'];
1010 // need the FKEY - custom group id
1011 $params['custom_group_id'] = $this->_gid
;
1013 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
1014 $params['id'] = $this->_id
;
1016 $customField = CRM_Core_BAO_CustomField
::create($params);
1017 $this->_id
= $customField->id
;
1020 CRM_Core_BAO_Cache
::deleteGroup('contact fields');
1022 $msg = '<p>' . ts("Custom field '%1' has been saved.", array(1 => $customField->label
)) . '</p>';
1024 $buttonName = $this->controller
->getButtonName();
1025 $session = CRM_Core_Session
::singleton();
1026 if ($buttonName == $this->getButtonName('next', 'new')) {
1027 $msg .= '<p>' . ts("Ready to add another.") . '</p>';
1028 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/custom/group/field/add',
1029 'reset=1&action=add&gid=' . $this->_gid
1033 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/custom/group/field',
1034 'reset=1&action=browse&gid=' . $this->_gid
1037 $session->setStatus($msg, ts('Saved'), 'success');
1039 // Add data when in ajax contect
1040 $this->ajaxResponse
['customField'] = $customField->toArray();
1044 * Removes value from fields with no label.
1046 * This allows default values to be set in the form, but ignored in post-processing.
1048 * @param array $fields
1050 public static function clearEmptyOptions(&$fields) {
1051 foreach ($fields['option_label'] as $i => $label) {
1052 if (!strlen(trim($label))) {
1053 $fields['option_value'][$i] = '';