3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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
55 * The field id, used when editing the field
63 * The default custom data/input types, when editing the field
68 protected $_defaultDataType;
71 * array of custom field values if update mode
76 * Array for valid combinations of data_type & html_type
81 private static $_dataTypeValues = NULL;
82 private static $_dataTypeKeys = NULL;
84 private static $_dataToHTML = NULL;
86 private static $_dataToLabels = NULL;
89 * Function to set variables up before form is built
96 public function preProcess() {
97 if (!(self
::$_dataTypeKeys)) {
98 self
::$_dataTypeKeys = array_keys(CRM_Core_BAO_CustomField
::dataType());
99 self
::$_dataTypeValues = array_values(CRM_Core_BAO_CustomField
::dataType());
102 if (!self
::$_dataToHTML) {
103 self
::$_dataToHTML = CRM_Core_BAO_CustomField
::dataToHtml();
107 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
109 $this->_values
= array();
110 //get the values form db if update.
112 $params = array('id' => $this->_id
);
113 CRM_Core_BAO_CustomField
::retrieve($params, $this->_values
);
114 // note_length is an alias for the text_length field
115 $this->_values
['note_length'] = CRM_Utils_Array
::value('text_length', $this->_values
);
117 $this->_gid
= $this->_values
['custom_group_id'];
121 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this);
124 if ($isReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_reserved', 'id')) {
125 CRM_Core_Error
::fatal("You cannot add or edit fields in a reserved custom field-set.");
129 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field',
130 "reset=1&action=browse&gid={$this->_gid}"
133 $session = CRM_Core_Session
::singleton();
134 $session->pushUserContext($url);
137 if (self
::$_dataToLabels == NULL) {
138 self
::$_dataToLabels = array(
139 array('Text' => ts('Text'), 'Select' => ts('Select'),
140 'Radio' => ts('Radio'), 'CheckBox' => ts('CheckBox'), 'Multi-Select' => ts('Multi-Select'),
141 'AdvMulti-Select' => ts('Advanced Multi-Select'),
142 'Autocomplete-Select' => ts('Autocomplete Select'),
144 array('Text' => ts('Text'), 'Select' => ts('Select'),
145 'Radio' => ts('Radio'),
147 array('Text' => ts('Text'), 'Select' => ts('Select'),
148 'Radio' => ts('Radio'),
150 array('Text' => ts('Text'), 'Select' => ts('Select'),
151 'Radio' => ts('Radio'),
153 array('TextArea' => ts('TextArea'), 'RichTextEditor' => 'RichTextEditor'),
154 array('Date' => ts('Select Date')),
155 array('Radio' => ts('Radio')),
156 array('StateProvince' => ts('Select State/Province'), 'Multi-Select' => ts('Multi-Select State/Province')),
157 array('Country' => ts('Select Country'), 'Multi-Select' => ts('Multi-Select Country ')),
158 array('File' => ts('Select File')),
159 array('Link' => ts('Link')),
160 array('ContactReference' => ts('Autocomplete Select')),
166 * This function sets the default values for the form. Note that in edit/view mode
167 * the default values are retrieved from the database
171 * @return array array of default values
174 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;
241 // set defaults for weight.
242 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
243 $defaults['option_status[' . $i . ']'] = 1;
244 $defaults['option_weight[' . $i . ']'] = $i;
247 if ($this->_action
& CRM_Core_Action
::ADD
) {
248 $fieldValues = array('custom_group_id' => $this->_gid
);
249 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_CustomField', $fieldValues);
251 $defaults['text_length'] = 255;
252 $defaults['note_columns'] = 60;
253 $defaults['note_rows'] = 4;
254 $defaults['is_view'] = 0;
257 if (!empty($defaults['html_type'])) {
258 $dontShowLink = substr($defaults['html_type'], -14) == 'State/Province' ||
substr($defaults['html_type'], -7) == 'Country' ?
1 : 0;
261 if (isset($dontShowLink)) {
262 $this->assign('dontShowLink', $dontShowLink);
264 if ($this->_action
& CRM_Core_Action
::ADD
&&
265 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_multiple', 'id')) {
266 $defaults['in_selector'] = 1;
273 * Function to actually build the form
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('Add Field')));
284 $this->assign('gid', $this->_gid
);
287 // lets trim all the whitespace
288 $this->applyFilter('__ALL__', 'trim');
290 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_CustomField');
296 $attributes['label'],
300 $dt = &self
::$_dataTypeValues;
302 foreach ($dt as $key => $value) {
303 $it[$key] = self
::$_dataToLabels[$key];
305 $sel = &$this->addElement('hierselect',
307 ts('Data and Input Field Type'),
308 'onclick="clearSearchBoxes();custom_option_html_type(this.form)"; onBlur="custom_option_html_type(this.form)";',
311 $sel->setOptions(array($dt, $it));
313 if (CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_multiple')) {
314 $this->add('checkbox', 'in_selector', ts('Display in Table?'));
317 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
318 $this->freeze('data_type');
320 $includeFieldIds = NULL;
321 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
322 $includeFieldIds = $this->_values
['id'];
324 $optionGroups = CRM_Core_BAO_CustomField
::customOptionGroup($includeFieldIds);
325 $emptyOptGroup = FALSE;
326 if (empty($optionGroups)) {
327 $emptyOptGroup = TRUE;
328 $optionTypes = array('1' => ts('Create a new set of options'));
331 $optionTypes = array('1' => ts('Create a new set of options'),
332 '2' => ts('Reuse an existing set'),
337 ts('Multiple Choice Option Sets'),
339 '' => ts('- select -')) +
$optionGroups
343 $element = &$this->addRadio('option_type',
347 'onclick' => "showOptionSelect();"), '<br/>'
351 $contactGroups = CRM_Core_PseudoConstant
::group();
352 asort($contactGroups);
356 ts('Limit List to Group'),
359 array('multiple' => 'multiple')
364 ts('Advanced Filter'),
365 $attributes['filter']
368 $this->add('hidden', 'filter_selected', 'Group', array('id' => 'filter_selected'));
370 //if empty option group freeze the option type.
371 if ($emptyOptGroup) {
375 // form fields of Custom Option rows
376 $defaultOption = array();
377 $_showHide = new CRM_Core_ShowHideBlocks('', '');
378 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
380 //the show hide blocks
381 $showBlocks = 'optionField_' . $i;
383 $_showHide->addHide($showBlocks);
384 if ($i == self
::NUM_OPTION
) {
385 $_showHide->addHide('additionalOption');
389 $_showHide->addShow($showBlocks);
392 $optionAttributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue');
394 $this->add('text', 'option_label[' . $i . ']', ts('Label'),
395 $optionAttributes['label']
399 $this->add('text', 'option_value[' . $i . ']', ts('Value'),
400 $optionAttributes['value']
404 $this->add('text', "option_weight[$i]", ts('Order'),
405 $optionAttributes['weight']
409 $this->add('checkbox', "option_status[$i]", ts('Active?'));
411 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
413 //for checkbox handling of default option
414 $this->add('checkbox', "default_checkbox_option[$i]", NULL);
417 //default option selection
418 $this->addGroup($defaultOption, 'default_option');
420 $_showHide->addToTemplate();
422 // text length for alpha numeric data types
425 ts('Database field length'),
426 $attributes['text_length'],
429 $this->addRule('text_length', ts('Value should be a positive number'), 'integer');
433 ts('Dates may be up to'),
434 $attributes['start_date_years'],
439 ts('Dates may be up to'),
440 $attributes['end_date_years'],
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');
447 $this->add('select', 'date_format', ts('Date Format'),
448 array('' => ts('- select -')) + CRM_Core_SelectValues
::getDatePluginInputFormats()
451 $this->add('select', 'time_format', ts('Time'),
452 array('' => ts('- none -')) + CRM_Core_SelectValues
::getTimeFormats()
458 ts('Width (columns)') . ' ',
459 $attributes['note_columns'],
464 ts('Height (rows)') . ' ',
465 $attributes['note_rows'],
470 ts('Maximum length') . ' ',
471 $attributes['text_length'], // note_length is an alias for the text-length field
475 $this->addRule('note_columns', ts('Value should be a positive number'), 'positiveInteger');
476 $this->addRule('note_rows', ts('Value should be a positive number'), 'positiveInteger');
477 $this->addRule('note_length', ts('Value should be a positive number'), 'positiveInteger');
480 $this->add('text', 'weight', ts('Order'),
481 $attributes['weight'],
484 $this->addRule('weight', ts('is a numeric field'), 'numeric');
487 $this->add('checkbox', 'is_required', ts('Required?'));
489 // checkbox / radio options per line
490 $this->add('text', 'options_per_line', ts('Options Per Line'));
491 $this->addRule('options_per_line', ts('must be a numeric value'), 'numeric');
493 // default value, help pre, help post, mask, attributes, javascript ?
494 $this->add('text', 'default_value', ts('Default Value'),
495 $attributes['default_value']
497 $this->add('textarea', 'help_pre', ts('Field Pre Help'),
498 $attributes['help_pre']
500 $this->add('textarea', 'help_post', ts('Field Post Help'),
501 $attributes['help_post']
503 $this->add('text', 'mask', ts('Mask'),
508 $this->add('checkbox', 'is_active', ts('Active?'));
511 $this->add('checkbox', 'is_view', ts('View Only?'));
514 $this->addElement('checkbox',
516 ts('Is this Field Searchable?'),
517 NULL, array('onclick' => "showSearchRange(this)")
520 // is searchable by range?
521 $searchRange = array();
522 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
523 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
525 $this->addGroup($searchRange, 'is_search_range', ts('Search by Range?'));
528 $this->addButtons(array(
531 'name' => ts('Save'),
536 'name' => ts('Save and New'),
541 'name' => ts('Cancel'),
546 // add a form rule to check default value
547 $this->addFormRule(array('CRM_Custom_Form_Field', 'formRule'), $this);
549 // if view mode pls freeze it with the done button.
550 if ($this->_action
& CRM_Core_Action
::VIEW
) {
552 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid
);
553 $this->addElement('button',
556 array('onclick' => "location.href='$url'")
562 * global validation rules for the form
564 * @param array $fields (referance) posted values of the form
566 * @return array if errors then list of errors to be posted back to the form,
571 static function formRule($fields, $files, $self) {
572 $default = CRM_Utils_Array
::value('default_value', $fields);
576 //validate field label as well as name.
577 $title = $fields['label'];
578 $name = CRM_Utils_String
::munge($title, '_', 64);
579 $gId = $self->_gid
; // CRM-7564
580 $query = 'select count(*) from civicrm_custom_field where ( name like %1 OR label like %2 ) and id != %3 and custom_group_id = %4';
581 $fldCnt = CRM_Core_DAO
::singleValueQuery($query, array(1 => array($name, 'String'),
582 2 => array($title, 'String'),
583 3 => array((int)$self->_id
, 'Integer'),
584 4 => array($gId, 'Integer'),
587 $errors['label'] = ts('Custom field \'%1\' already exists in Database.', array(1 => $title));
590 //checks the given custom field name doesnot start with digit
591 if (!empty($title)) {
592 // gives the ascii value
593 $asciiValue = ord($title{0});
594 if ($asciiValue >= 48 && $asciiValue <= 57) {
595 $errors['label'] = ts("Field's Name should not start with digit");
599 // ensure that the label is not 'id'
600 if (strtolower($title) == 'id') {
601 $errors['label'] = ts("You cannot use 'id' as a field label.");
604 if (!isset($fields['data_type'][0]) ||
!isset($fields['data_type'][1])) {
605 $errors['_qf_default'] = ts('Please enter valid - Data and Input Field Type.');
608 $dataType = self
::$_dataTypeKeys[$fields['data_type'][0]];
610 if ($default ||
$dataType == 'ContactReference') {
613 if (!CRM_Utils_Rule
::integer($default)) {
614 $errors['default_value'] = ts('Please enter a valid integer as default value.');
619 if (!CRM_Utils_Rule
::numeric($default)) {
620 $errors['default_value'] = ts('Please enter a valid number as default value.');
625 if (!CRM_Utils_Rule
::money($default)) {
626 $errors['default_value'] = ts('Please enter a valid number value.');
631 if (!CRM_Utils_Rule
::url($default)) {
632 $errors['default_value'] = ts('Please enter a valid link.');
637 if (!CRM_Utils_Rule
::date($default)) {
638 $errors['default_value'] = ts('Please enter a valid date as default value using YYYY-MM-DD format. Example: 2004-12-31.');
643 if ($default != '1' && $default != '0') {
644 $errors['default_value'] = ts('Please enter 1 (for Yes) or 0 (for No) if you want to set a default value.');
649 if (!empty($default)) {
650 $query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1";
651 $params = array(1 => array($fields['default_value'], 'String'));
652 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
653 $errors['default_value'] = ts('Invalid default value for country.');
658 case 'StateProvince':
659 if (!empty($default)) {
662 FROM civicrm_state_province
664 OR abbreviation = %1";
665 $params = array(1 => array($fields['default_value'], 'String'));
666 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
667 $errors['default_value'] = ts('The invalid default value for State/Province data type');
672 case 'ContactReference':
673 if ($fields['filter_selected'] == 'Advance' && !empty($fields['filter'])) {
674 if (strpos($fields['filter'], 'entity=') !== FALSE) {
675 $errors['filter'] = ts("Please do not include entity parameter (entity is always 'contact')");
677 elseif (strpos($fields['filter'], 'action=') === FALSE) {
678 $errors['filter'] = ts("Please specify 'action' parameter, it should be 'lookup' or 'get'");
680 elseif (strpos($fields['filter'], 'action=get') === FALSE &&
681 strpos($fields['filter'], 'action=lookup') === FALSE
683 $errors['filter'] = ts("Only 'get' and 'lookup' actions are supported.");
686 $self->setDefaults(array('filter_selected', $fields['filter_selected']));
691 if (self
::$_dataTypeKeys[$fields['data_type'][0]] == 'Date') {
692 if (!$fields['date_format']) {
693 $errors['date_format'] = ts('Please select a date format.');
697 /** Check the option values entered
698 * Appropriate values are required for the selected datatype
699 * Incomplete row checking is also required.
701 $_flagOption = $_rowError = 0;
702 $_showHide = new CRM_Core_ShowHideBlocks('', '');
703 $dataType = self
::$_dataTypeKeys[$fields['data_type'][0]];
704 if (isset($fields['data_type'][1])) {
705 $dataField = $fields['data_type'][1];
707 $optionFields = array('Select', 'Multi-Select', 'CheckBox', 'Radio', 'AdvMulti-Select');
709 if (isset($fields['option_type']) && $fields['option_type'] == 1) {
710 //capture duplicate Custom option values
711 if (!empty($fields['option_value'])) {
712 $countValue = count($fields['option_value']);
713 $uniqueCount = count(array_unique($fields['option_value']));
715 if ($countValue > $uniqueCount) {
718 while ($start < self
::NUM_OPTION
) {
719 $nextIndex = $start +
1;
720 while ($nextIndex <= self
::NUM_OPTION
) {
721 if ($fields['option_value'][$start] == $fields['option_value'][$nextIndex] &&
722 !empty($fields['option_value'][$nextIndex])
724 $errors['option_value[' . $start . ']'] = ts('Duplicate Option values');
725 $errors['option_value[' . $nextIndex . ']'] = ts('Duplicate Option values');
735 //capture duplicate Custom Option label
736 if (!empty($fields['option_label'])) {
737 $countValue = count($fields['option_label']);
738 $uniqueCount = count(array_unique($fields['option_label']));
740 if ($countValue > $uniqueCount) {
742 while ($start < self
::NUM_OPTION
) {
743 $nextIndex = $start +
1;
744 while ($nextIndex <= self
::NUM_OPTION
) {
745 if ($fields['option_label'][$start] == $fields['option_label'][$nextIndex] &&
746 !empty($fields['option_label'][$nextIndex])
748 $errors['option_label[' . $start . ']'] = ts('Duplicate Option label');
749 $errors['option_label[' . $nextIndex . ']'] = ts('Duplicate Option label');
759 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
760 if (!$fields['option_label'][$i]) {
761 if ($fields['option_value'][$i]) {
762 $errors['option_label[' . $i . ']'] = ts('Option label cannot be empty');
770 if (!strlen(trim($fields['option_value'][$i]))) {
771 if (!$fields['option_value'][$i]) {
772 $errors['option_value[' . $i . ']'] = ts('Option value cannot be empty');
778 if ($fields['option_value'][$i] && $dataType != 'String') {
779 if ($dataType == 'Int') {
780 if (!CRM_Utils_Rule
::integer($fields['option_value'][$i])) {
782 $errors['option_value[' . $i . ']'] = ts('Please enter a valid integer.');
785 elseif ($dataType == 'Money') {
786 if (!CRM_Utils_Rule
::money($fields['option_value'][$i])) {
788 $errors['option_value[' . $i . ']'] = ts('Please enter a valid money value.');
792 if (!CRM_Utils_Rule
::numeric($fields['option_value'][$i])) {
794 $errors['option_value[' . $i . ']'] = ts('Please enter a valid number.');
799 $showBlocks = 'optionField_' . $i;
801 $_showHide->addShow($showBlocks);
805 if (!empty($_emptyRow)) {
806 $_showHide->addHide($showBlocks);
809 $_showHide->addShow($showBlocks);
811 if ($i == self
::NUM_OPTION
) {
812 $hideBlock = 'additionalOption';
813 $_showHide->addHide($hideBlock);
816 $_flagOption = $_emptyRow = 0;
819 elseif (isset($dataField) &&
820 in_array($dataField, $optionFields) &&
821 !in_array($dataType, array('Boolean', 'Country', 'StateProvince'))
823 if (!$fields['option_group_id']) {
824 $errors['option_group_id'] = ts('You must select a Multiple Choice Option set if you chose Reuse an existing set.');
829 FROM civicrm_custom_field
830 WHERE data_type != %1
831 AND option_group_id = %2";
833 1 => array(self
::$_dataTypeKeys[$fields['data_type'][0]],
836 2 => array($fields['option_group_id'], 'Integer'),
838 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
840 $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.');
845 $assignError = new CRM_Core_Page();
847 $_showHide->addToTemplate();
848 $assignError->assign('optionRowError', $_rowError);
851 if (isset($fields['data_type'][1])) {
852 switch (self
::$_dataToHTML[$fields['data_type'][0]][$fields['data_type'][1]]) {
855 $assignError->assign('fieldError', $_fieldError);
860 $assignError->assign('fieldError', $_fieldError);
865 $assignError->assign('fieldError', $_fieldError);
870 $assignError->assign('fieldError', $_fieldError);
874 for ($idx = 1; $idx <= self
::NUM_OPTION
; $idx++
) {
875 $showBlocks = 'optionField_' . $idx;
876 if (!empty($fields['option_label'][$idx])) {
877 $_showHide->addShow($showBlocks);
880 $_showHide->addHide($showBlocks);
883 $_showHide->addToTemplate();
886 // we can not set require and view at the same time.
887 if (!empty($fields['is_required']) && !empty($fields['is_view'])) {
888 $errors['is_view'] = ts('Can not set this field Required and View Only at the same time.');
891 return empty($errors) ?
TRUE : $errors;
902 public function postProcess() {
903 // store the submitted values in an array
904 $params = $this->controller
->exportValues($this->_name
);
905 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
906 $dataTypeKey = $this->_defaultDataType
[0];
907 $params['data_type'] = self
::$_dataTypeKeys[$this->_defaultDataType
[0]];
908 $params['html_type'] = self
::$_dataToHTML[$this->_defaultDataType
[0]][$this->_defaultDataType
[1]];
911 $dataTypeKey = $params['data_type'][0];
912 $params['html_type'] = self
::$_dataToHTML[$params['data_type'][0]][$params['data_type'][1]];
913 $params['data_type'] = self
::$_dataTypeKeys[$params['data_type'][0]];
916 //fix for 'is_search_range' field.
917 if (in_array($dataTypeKey, array(
919 if (empty($params['is_searchable'])) {
920 $params['is_search_range'] = 0;
924 $params['is_search_range'] = 0;
928 if ($dataTypeKey == 11 && !empty($params['filter_selected'])) {
929 if ($params['filter_selected'] == 'Advance' && trim(CRM_Utils_Array
::value('filter', $params))) {
930 $filter = trim($params['filter']);
932 elseif ($params['filter_selected'] == 'Group' && !empty($params['group_id'])) {
934 $filter = 'action=lookup&group=' . implode(',', $params['group_id']);
937 $params['filter'] = $filter;
941 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
942 $fieldValues = array('custom_group_id' => $this->_gid
);
944 $oldWeight = $this->_values
['weight'];
946 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_CustomField', $oldWeight, $params['weight'], $fieldValues);
949 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
951 //store the primary key for State/Province or Country as default value.
952 if (strlen(trim($params['default_value']))) {
953 switch ($params['data_type']) {
954 case 'StateProvince':
955 $fieldStateProvince = $strtolower($params['default_value']);
958 FROM civicrm_state_province
959 WHERE LOWER(name) = '$fieldStateProvince'
960 OR abbreviation = '$fieldStateProvince'";
961 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
963 $params['default_value'] = $dao->id
;
968 $fieldCountry = $strtolower($params['default_value']);
972 WHERE LOWER(name) = '$fieldCountry'
973 OR iso_code = '$fieldCountry'";
974 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
976 $params['default_value'] = $dao->id
;
982 // The text_length attribute for Memo fields is in a different input as there
983 // are different label, help text and default value than for other type fields
984 if ($params['data_type'] == "Memo") {
985 $params['text_length'] = $params['note_length'];
988 // need the FKEY - custom group id
989 $params['custom_group_id'] = $this->_gid
;
991 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
992 $params['id'] = $this->_id
;
994 $customField = CRM_Core_BAO_CustomField
::create($params);
995 $this->_id
= $customField->id
;
998 CRM_Core_BAO_Cache
::deleteGroup('contact fields');
1000 $msg = '<p>' . ts("Custom field '%1' has been saved.", array(1 => $customField->label
)) . '</p>';
1002 $buttonName = $this->controller
->getButtonName();
1003 $session = CRM_Core_Session
::singleton();
1004 if ($buttonName == $this->getButtonName('next', 'new')) {
1005 $msg +
= '<p>' . ts("Ready to add another.") . '</p>';
1006 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/custom/group/field/add',
1007 'reset=1&action=add&gid=' . $this->_gid
1011 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/custom/group/field',
1012 'reset=1&action=browse&gid=' . $this->_gid
1015 $session->setStatus($msg, ts('Saved'), 'success');
1017 // Add data when in ajax contect
1018 $this->ajaxResponse
['customField'] = $customField->toArray();