3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this);
109 if ($isReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_reserved', 'id')) {
110 CRM_Core_Error
::fatal("You cannot add or edit fields in a reserved custom field-set.");
114 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field',
115 "reset=1&action=browse&gid={$this->_gid}"
118 $session = CRM_Core_Session
::singleton();
119 $session->pushUserContext($url);
123 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
125 //get the values form db if update.
126 $this->_values
= array();
128 $params = array('id' => $this->_id
);
129 CRM_Core_BAO_CustomField
::retrieve($params, $this->_values
);
130 // note_length is an alias for the text_length field
131 $this->_values
['note_length'] = CRM_Utils_Array
::value('text_length', $this->_values
);
134 if (self
::$_dataToLabels == NULL) {
135 self
::$_dataToLabels = array(
136 array('Text' => ts('Text'), 'Select' => ts('Select'),
137 'Radio' => ts('Radio'), 'CheckBox' => ts('CheckBox'), 'Multi-Select' => ts('Multi-Select'),
138 'AdvMulti-Select' => ts('Advanced Multi-Select'),
139 'Autocomplete-Select' => ts('Autocomplete Select'),
141 array('Text' => ts('Text'), 'Select' => ts('Select'),
142 'Radio' => ts('Radio'),
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('TextArea' => ts('TextArea'), 'RichTextEditor' => 'RichTextEditor'),
151 array('Date' => ts('Select Date')),
152 array('Radio' => ts('Radio')),
153 array('StateProvince' => ts('Select State/Province'), 'Multi-Select' => ts('Multi-Select State/Province')),
154 array('Country' => ts('Select Country'), 'Multi-Select' => ts('Multi-Select Country ')),
155 array('File' => ts('Select File')),
156 array('Link' => ts('Link')),
157 array('ContactReference' => ts('Autocomplete Select')),
163 * This function sets the default values for the form. Note that in edit/view mode
164 * the default values are retrieved from the database
168 * @return array array of default values
171 function setDefaultValues() {
172 $defaults = $this->_values
;
175 $this->assign('id', $this->_id
);
176 $this->_gid
= $defaults['custom_group_id'];
178 //get the value for state or country
179 if ($defaults['data_type'] == 'StateProvince' &&
180 $stateId = CRM_Utils_Array
::value('default_value', $defaults)
182 $defaults['default_value'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_StateProvince', $stateId);
184 elseif ($defaults['data_type'] == 'Country' &&
185 $countryId = CRM_Utils_Array
::value('default_value', $defaults)
187 $defaults['default_value'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Country', $countryId);
190 if ($defaults['data_type'] == 'ContactReference' && CRM_Utils_Array
::value('filter', $defaults)) {
191 $contactRefFilter = 'Advance';
192 if (strpos($defaults['filter'], 'action=lookup') !== FALSE &&
193 strpos($defaults['filter'], 'group=') !== FALSE
195 $filterParts = explode('&', $defaults['filter']);
197 if (count($filterParts) == 2) {
198 $contactRefFilter = 'Group';
199 foreach ($filterParts as $part) {
200 if (strpos($part, 'group=') === FALSE) {
203 $groups = substr($part, strpos($part, '=') +
1);
204 foreach (explode(',', $groups) as $grp) {
205 if (CRM_Utils_Rule
::positiveInteger($grp)) {
206 $defaults['group_id'][] = $grp;
212 $defaults['filter_selected'] = $contactRefFilter;
215 if (CRM_Utils_Array
::value('data_type', $defaults)) {
216 $defaultDataType = array_search($defaults['data_type'],
219 $defaultHTMLType = array_search($defaults['html_type'],
220 self
::$_dataToHTML[$defaultDataType]
222 $defaults['data_type'] = array(
223 '0' => $defaultDataType,
224 '1' => $defaultHTMLType,
226 $this->_defaultDataType
= $defaults['data_type'];
229 $defaults['option_type'] = 2;
231 $this->assign('changeFieldType', CRM_Custom_Form_ChangeFieldType
::fieldTypeTransitions($this->_values
['data_type'], $this->_values
['html_type']));
234 $defaults['is_active'] = 1;
235 $defaults['option_type'] = 1;
238 // set defaults for weight.
239 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
240 $defaults['option_status[' . $i . ']'] = 1;
241 $defaults['option_weight[' . $i . ']'] = $i;
244 if ($this->_action
& CRM_Core_Action
::ADD
) {
245 $fieldValues = array('custom_group_id' => $this->_gid
);
246 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_CustomField', $fieldValues);
248 $defaults['text_length'] = 255;
249 $defaults['note_columns'] = 60;
250 $defaults['note_rows'] = 4;
251 $defaults['is_view'] = 0;
254 if (CRM_Utils_Array
::value('html_type', $defaults)) {
255 $dontShowLink = substr($defaults['html_type'], -14) == 'State/Province' ||
substr($defaults['html_type'], -7) == 'Country' ?
1 : 0;
258 if (isset($dontShowLink)) {
259 $this->assign('dontShowLink', $dontShowLink);
265 * Function to actually build the form
272 public function buildQuickForm() {
274 $this->_title
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'title');
275 CRM_Utils_System
::setTitle($this->_title
. ' - ' . ts('Custom Fields'));
278 // lets trim all the whitespace
279 $this->applyFilter('__ALL__', 'trim');
281 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_CustomField');
287 $attributes['label'],
291 $dt = &self
::$_dataTypeValues;
293 foreach ($dt as $key => $value) {
294 $it[$key] = self
::$_dataToLabels[$key];
296 $sel = &$this->addElement('hierselect',
298 ts('Data and Input Field Type'),
299 'onclick="clearSearchBoxes();custom_option_html_type(this.form)"; onBlur="custom_option_html_type(this.form)";',
302 $sel->setOptions(array($dt, $it));
303 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
304 $this->freeze('data_type');
306 $includeFieldIds = NULL;
307 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
308 $includeFieldIds = $this->_values
['id'];
310 $optionGroups = CRM_Core_BAO_CustomField
::customOptionGroup($includeFieldIds);
311 $emptyOptGroup = FALSE;
312 if (empty($optionGroups)) {
313 $emptyOptGroup = TRUE;
314 $optionTypes = array('1' => ts('Create a new set of options'));
317 $optionTypes = array('1' => ts('Create a new set of options'),
318 '2' => ts('Reuse an existing set'),
323 ts('Multiple Choice Option Sets'),
325 '' => ts('- select -')) +
$optionGroups
329 $element = &$this->addRadio('option_type',
333 'onclick' => "showOptionSelect();"), '<br/>'
337 $contactGroups = CRM_Core_PseudoConstant
::group();
338 asort($contactGroups);
342 ts('Limit List to Group'),
345 array('multiple' => 'multiple')
350 ts('Advanced Filter'),
351 $attributes['filter']
354 $this->add('hidden', 'filter_selected', 'Group', array('id' => 'filter_selected'));
356 //if empty option group freeze the option type.
357 if ($emptyOptGroup) {
361 // form fields of Custom Option rows
362 $defaultOption = array();
363 $_showHide = new CRM_Core_ShowHideBlocks('', '');
364 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
366 //the show hide blocks
367 $showBlocks = 'optionField_' . $i;
369 $_showHide->addHide($showBlocks);
370 if ($i == self
::NUM_OPTION
) {
371 $_showHide->addHide('additionalOption');
375 $_showHide->addShow($showBlocks);
378 $optionAttributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue');
380 $this->add('text', 'option_label[' . $i . ']', ts('Label'),
381 $optionAttributes['label']
385 $this->add('text', 'option_value[' . $i . ']', ts('Value'),
386 $optionAttributes['value']
390 $this->add('text', "option_weight[$i]", ts('Order'),
391 $optionAttributes['weight']
395 $this->add('checkbox', "option_status[$i]", ts('Active?'));
397 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
399 //for checkbox handling of default option
400 $this->add('checkbox', "default_checkbox_option[$i]", NULL);
403 //default option selection
404 $this->addGroup($defaultOption, 'default_option');
406 $_showHide->addToTemplate();
408 // text length for alpha numeric data types
411 ts('Database field length'),
412 $attributes['text_length'],
415 $this->addRule('text_length', ts('Value should be a positive number'), 'integer');
419 ts('Dates may be up to'),
420 $attributes['start_date_years'],
425 ts('Dates may be up to'),
426 $attributes['end_date_years'],
430 $this->addRule('start_date_years', ts('Value should be a positive number'), 'integer');
431 $this->addRule('end_date_years', ts('Value should be a positive number'), 'integer');
433 $this->add('select', 'date_format', ts('Date Format'),
434 array('' => ts('- select -')) + CRM_Core_SelectValues
::getDatePluginInputFormats()
437 $this->add('select', 'time_format', ts('Time'),
438 array('' => ts('- none -')) + CRM_Core_SelectValues
::getTimeFormats()
444 ts('Width (columns)') . ' ',
445 $attributes['note_columns'],
450 ts('Height (rows)') . ' ',
451 $attributes['note_rows'],
456 ts('Maximum length') . ' ',
457 $attributes['text_length'], // note_length is an alias for the text-length field
461 $this->addRule('note_columns', ts('Value should be a positive number'), 'positiveInteger');
462 $this->addRule('note_rows', ts('Value should be a positive number'), 'positiveInteger');
463 $this->addRule('note_length', ts('Value should be a positive number'), 'positiveInteger');
466 $this->add('text', 'weight', ts('Order'),
467 $attributes['weight'],
470 $this->addRule('weight', ts('is a numeric field'), 'numeric');
473 $this->add('checkbox', 'is_required', ts('Required?'));
475 // checkbox / radio options per line
476 $this->add('text', 'options_per_line', ts('Options Per Line'));
477 $this->addRule('options_per_line', ts('must be a numeric value'), 'numeric');
479 // default value, help pre, help post, mask, attributes, javascript ?
480 $this->add('text', 'default_value', ts('Default Value'),
481 $attributes['default_value']
483 $this->add('textarea', 'help_pre', ts('Field Pre Help'),
484 $attributes['help_pre']
486 $this->add('textarea', 'help_post', ts('Field Post Help'),
487 $attributes['help_post']
489 $this->add('text', 'mask', ts('Mask'),
494 $this->add('checkbox', 'is_active', ts('Active?'));
497 $this->add('checkbox', 'is_view', ts('View Only?'));
500 $this->addElement('checkbox',
502 ts('Is this Field Searchable?'),
503 NULL, array('onclick' => "showSearchRange(this)")
506 // is searchable by range?
507 $searchRange = array();
508 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('Yes'), '1');
509 $searchRange[] = $this->createElement('radio', NULL, NULL, ts('No'), '0');
511 $this->addGroup($searchRange, 'is_search_range', ts('Search by Range?'));
514 $this->addButtons(array(
517 'name' => ts('Save'),
522 'name' => ts('Save and New'),
527 'name' => ts('Cancel'),
532 // add a form rule to check default value
533 $this->addFormRule(array('CRM_Custom_Form_Field', 'formRule'), $this);
535 // if view mode pls freeze it with the done button.
536 if ($this->_action
& CRM_Core_Action
::VIEW
) {
538 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid
);
539 $this->addElement('button',
542 array('onclick' => "location.href='$url'")
548 * global validation rules for the form
550 * @param array $fields (referance) posted values of the form
552 * @return array if errors then list of errors to be posted back to the form,
557 static function formRule($fields, $files, $self) {
558 $default = CRM_Utils_Array
::value('default_value', $fields);
562 //validate field label as well as name.
563 $title = $fields['label'];
564 $name = CRM_Utils_String
::munge($title, '_', 64);
565 $gId = $self->_gid
; // CRM-7564
566 $query = 'select count(*) from civicrm_custom_field where ( name like %1 OR label like %2 ) and id != %3 and custom_group_id = %4';
567 $fldCnt = CRM_Core_DAO
::singleValueQuery($query, array(1 => array($name, 'String'),
568 2 => array($title, 'String'),
569 3 => array((int)$self->_id
, 'Integer'),
570 4 => array($gId, 'Integer'),
573 $errors['label'] = ts('Custom field \'%1\' already exists in Database.', array(1 => $title));
576 //checks the given custom field name doesnot start with digit
577 if (!empty($title)) {
578 // gives the ascii value
579 $asciiValue = ord($title{0});
580 if ($asciiValue >= 48 && $asciiValue <= 57) {
581 $errors['label'] = ts("Field's Name should not start with digit");
585 // ensure that the label is not 'id'
586 if (strtolower($title) == 'id') {
587 $errors['label'] = ts("You cannot use 'id' as a field label.");
590 if (!isset($fields['data_type'][0]) ||
!isset($fields['data_type'][1])) {
591 $errors['_qf_default'] = ts('Please enter valid - Data and Input Field Type.');
594 $dataType = self
::$_dataTypeKeys[$fields['data_type'][0]];
596 if ($default ||
$dataType == 'ContactReference') {
599 if (!CRM_Utils_Rule
::integer($default)) {
600 $errors['default_value'] = ts('Please enter a valid integer as default value.');
605 if (!CRM_Utils_Rule
::numeric($default)) {
606 $errors['default_value'] = ts('Please enter a valid number as default value.');
611 if (!CRM_Utils_Rule
::money($default)) {
612 $errors['default_value'] = ts('Please enter a valid number value.');
617 if (!CRM_Utils_Rule
::url($default)) {
618 $errors['default_value'] = ts('Please enter a valid link.');
623 if (!CRM_Utils_Rule
::date($default)) {
624 $errors['default_value'] = ts('Please enter a valid date as default value using YYYY-MM-DD format. Example: 2004-12-31.');
629 if ($default != '1' && $default != '0') {
630 $errors['default_value'] = ts('Please enter 1 (for Yes) or 0 (for No) if you want to set a default value.');
635 if (!empty($default)) {
636 $query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1";
637 $params = array(1 => array($fields['default_value'], 'String'));
638 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
639 $errors['default_value'] = ts('Invalid default value for country.');
644 case 'StateProvince':
645 if (!empty($default)) {
648 FROM civicrm_state_province
650 OR abbreviation = %1";
651 $params = array(1 => array($fields['default_value'], 'String'));
652 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
653 $errors['default_value'] = ts('The invalid default value for State/Province data type');
658 case 'ContactReference':
659 if ($fields['filter_selected'] == 'Advance' &&
660 CRM_Utils_Array
::value('filter', $fields)
662 if (strpos($fields['filter'], 'entity=') !== FALSE) {
663 $errors['filter'] = ts("Please do not include entity parameter (entity is always 'contact')");
665 elseif (strpos($fields['filter'], 'action=') === FALSE) {
666 $errors['filter'] = ts("Please specify 'action' parameter, it should be 'lookup' or 'get'");
668 elseif (strpos($fields['filter'], 'action=get') === FALSE &&
669 strpos($fields['filter'], 'action=lookup') === FALSE
671 $errors['filter'] = ts("Only 'get' and 'lookup' actions are supported.");
674 $self->setDefaults(array('filter_selected', $fields['filter_selected']));
679 if (self
::$_dataTypeKeys[$fields['data_type'][0]] == 'Date') {
680 if (!$fields['date_format']) {
681 $errors['date_format'] = ts('Please select a date format.');
685 /** Check the option values entered
686 * Appropriate values are required for the selected datatype
687 * Incomplete row checking is also required.
689 $_flagOption = $_rowError = 0;
690 $_showHide = new CRM_Core_ShowHideBlocks('', '');
691 $dataType = self
::$_dataTypeKeys[$fields['data_type'][0]];
692 if (isset($fields['data_type'][1])) {
693 $dataField = $fields['data_type'][1];
695 $optionFields = array('Select', 'Multi-Select', 'CheckBox', 'Radio', 'AdvMulti-Select');
697 if ($fields['option_type'] == 1) {
698 //capture duplicate Custom option values
699 if (!empty($fields['option_value'])) {
700 $countValue = count($fields['option_value']);
701 $uniqueCount = count(array_unique($fields['option_value']));
703 if ($countValue > $uniqueCount) {
706 while ($start < self
::NUM_OPTION
) {
707 $nextIndex = $start +
1;
708 while ($nextIndex <= self
::NUM_OPTION
) {
709 if ($fields['option_value'][$start] == $fields['option_value'][$nextIndex] &&
710 !empty($fields['option_value'][$nextIndex])
712 $errors['option_value[' . $start . ']'] = ts('Duplicate Option values');
713 $errors['option_value[' . $nextIndex . ']'] = ts('Duplicate Option values');
723 //capture duplicate Custom Option label
724 if (!empty($fields['option_label'])) {
725 $countValue = count($fields['option_label']);
726 $uniqueCount = count(array_unique($fields['option_label']));
728 if ($countValue > $uniqueCount) {
730 while ($start < self
::NUM_OPTION
) {
731 $nextIndex = $start +
1;
732 while ($nextIndex <= self
::NUM_OPTION
) {
733 if ($fields['option_label'][$start] == $fields['option_label'][$nextIndex] &&
734 !empty($fields['option_label'][$nextIndex])
736 $errors['option_label[' . $start . ']'] = ts('Duplicate Option label');
737 $errors['option_label[' . $nextIndex . ']'] = ts('Duplicate Option label');
747 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
748 if (!$fields['option_label'][$i]) {
749 if ($fields['option_value'][$i]) {
750 $errors['option_label[' . $i . ']'] = ts('Option label cannot be empty');
758 if (!strlen(trim($fields['option_value'][$i]))) {
759 if (!$fields['option_value'][$i]) {
760 $errors['option_value[' . $i . ']'] = ts('Option value cannot be empty');
766 if ($fields['option_value'][$i] && $dataType != 'String') {
767 if ($dataType == 'Int') {
768 if (!CRM_Utils_Rule
::integer($fields['option_value'][$i])) {
770 $errors['option_value[' . $i . ']'] = ts('Please enter a valid integer.');
773 elseif ($dataType == 'Money') {
774 if (!CRM_Utils_Rule
::money($fields['option_value'][$i])) {
776 $errors['option_value[' . $i . ']'] = ts('Please enter a valid money value.');
780 if (!CRM_Utils_Rule
::numeric($fields['option_value'][$i])) {
782 $errors['option_value[' . $i . ']'] = ts('Please enter a valid number.');
787 $showBlocks = 'optionField_' . $i;
789 $_showHide->addShow($showBlocks);
793 if (!empty($_emptyRow)) {
794 $_showHide->addHide($showBlocks);
797 $_showHide->addShow($showBlocks);
799 if ($i == self
::NUM_OPTION
) {
800 $hideBlock = 'additionalOption';
801 $_showHide->addHide($hideBlock);
804 $_flagOption = $_emptyRow = 0;
807 elseif (isset($dataField) &&
808 in_array($dataField, $optionFields) &&
809 !in_array($dataType, array('Boolean', 'Country', 'StateProvince'))
811 if (!$fields['option_group_id']) {
812 $errors['option_group_id'] = ts('You must select a Multiple Choice Option set if you chose Reuse an existing set.');
817 FROM civicrm_custom_field
818 WHERE data_type != %1
819 AND option_group_id = %2";
821 1 => array(self
::$_dataTypeKeys[$fields['data_type'][0]],
824 2 => array($fields['option_group_id'], 'Integer'),
826 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
828 $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.');
833 $assignError = new CRM_Core_Page();
835 $_showHide->addToTemplate();
836 $assignError->assign('optionRowError', $_rowError);
839 if (isset($fields['data_type'][1])) {
840 switch (self
::$_dataToHTML[$fields['data_type'][0]][$fields['data_type'][1]]) {
843 $assignError->assign('fieldError', $_fieldError);
848 $assignError->assign('fieldError', $_fieldError);
853 $assignError->assign('fieldError', $_fieldError);
858 $assignError->assign('fieldError', $_fieldError);
862 for ($idx = 1; $idx <= self
::NUM_OPTION
; $idx++
) {
863 $showBlocks = 'optionField_' . $idx;
864 if (!empty($fields['option_label'][$idx])) {
865 $_showHide->addShow($showBlocks);
868 $_showHide->addHide($showBlocks);
871 $_showHide->addToTemplate();
874 // we can not set require and view at the same time.
875 if (CRM_Utils_Array
::value('is_required', $fields) &&
876 CRM_Utils_Array
::value('is_view', $fields)
878 $errors['is_view'] = ts('Can not set this field Required and View Only at the same time.');
881 return empty($errors) ?
TRUE : $errors;
892 public function postProcess() {
893 // store the submitted values in an array
894 $params = $this->controller
->exportValues($this->_name
);
895 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
896 $dataTypeKey = $this->_defaultDataType
[0];
897 $params['data_type'] = self
::$_dataTypeKeys[$this->_defaultDataType
[0]];
898 $params['html_type'] = self
::$_dataToHTML[$this->_defaultDataType
[0]][$this->_defaultDataType
[1]];
901 $dataTypeKey = $params['data_type'][0];
902 $params['html_type'] = self
::$_dataToHTML[$params['data_type'][0]][$params['data_type'][1]];
903 $params['data_type'] = self
::$_dataTypeKeys[$params['data_type'][0]];
906 //fix for 'is_search_range' field.
907 if (in_array($dataTypeKey, array(
909 if (!CRM_Utils_Array
::value('is_searchable', $params)) {
910 $params['is_search_range'] = 0;
914 $params['is_search_range'] = 0;
918 if ($dataTypeKey == 11 && CRM_Utils_Array
::value('filter_selected', $params)) {
919 if ($params['filter_selected'] == 'Advance' && trim(CRM_Utils_Array
::value('filter', $params))) {
920 $filter = trim($params['filter']);
922 elseif ($params['filter_selected'] == 'Group' && CRM_Utils_Array
::value('group_id', $params)) {
924 $filter = 'action=lookup&group=' . implode(',', $params['group_id']);
927 $params['filter'] = $filter;
931 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
932 $fieldValues = array('custom_group_id' => $this->_gid
);
934 $oldWeight = $this->_values
['weight'];
936 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_CustomField', $oldWeight, $params['weight'], $fieldValues);
939 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
941 //store the primary key for State/Province or Country as default value.
942 if (strlen(trim($params['default_value']))) {
943 switch ($params['data_type']) {
944 case 'StateProvince':
945 $fieldStateProvince = $strtolower($params['default_value']);
948 FROM civicrm_state_province
949 WHERE LOWER(name) = '$fieldStateProvince'
950 OR abbreviation = '$fieldStateProvince'";
951 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
953 $params['default_value'] = $dao->id
;
958 $fieldCountry = $strtolower($params['default_value']);
962 WHERE LOWER(name) = '$fieldCountry'
963 OR iso_code = '$fieldCountry'";
964 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
966 $params['default_value'] = $dao->id
;
972 // The text_length attribute for Memo fields is in a different input as there
973 // are different label, help text and default value than for other type fields
974 if ($params['data_type'] == "Memo") {
975 $params['text_length'] = $params['note_length'];
978 // need the FKEY - custom group id
979 $params['custom_group_id'] = $this->_gid
;
981 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
982 $params['id'] = $this->_id
;
985 $customField = CRM_Core_BAO_CustomField
::create($params);
988 CRM_Core_BAO_Cache
::deleteGroup('contact fields');
990 CRM_Core_Session
::setStatus(ts('Your custom field \'%1\' has been saved.',
991 array(1 => $customField->label
)
992 ), ts('Saved'), 'success');
994 $buttonName = $this->controller
->getButtonName();
995 $session = CRM_Core_Session
::singleton();
996 if ($buttonName == $this->getButtonName('next', 'new')) {
997 CRM_Core_Session
::setStatus(ts(' You can add another custom field.'), '', 'info');
998 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/custom/group/field/add',
999 'reset=1&action=add&gid=' . $this->_gid
1003 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/custom/group/field',
1004 'reset=1&action=browse&gid=' . $this->_gid