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_Option
extends CRM_Core_Form
{
42 * The custom field id saved to the session for an update
49 * The custom group id saved to the session for an update
59 protected $_optionGroupID = NULL;
62 * The Option id, used when editing the Option
69 * Set variables up before form is built.
73 public function preProcess() {
74 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive', $this);
76 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this);
78 if (!isset($this->_gid
) && $this->_fid
) {
79 $this->_gid
= CRM_Core_DAO
::getFieldValue(
80 'CRM_Core_DAO_CustomField',
87 $this->_optionGroupID
= CRM_Core_DAO
::getFieldValue(
88 'CRM_Core_DAO_CustomField',
94 if ($isReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_reserved', 'id')) {
95 CRM_Core_Error
::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
98 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
102 * Set default values for the form. Note that in edit/view mode
103 * the default values are retrieved from the database
106 * array of default values
108 public function setDefaultValues() {
109 $defaults = $fieldDefaults = [];
110 if (isset($this->_id
)) {
111 $params = ['id' => $this->_id
];
112 CRM_Core_BAO_CustomOption
::retrieve($params, $defaults);
114 $paramsField = ['id' => $this->_fid
];
115 CRM_Core_BAO_CustomField
::retrieve($paramsField, $fieldDefaults);
117 if ($fieldDefaults['html_type'] == 'CheckBox'
118 ||
$fieldDefaults['html_type'] == 'Multi-Select'
120 if (!empty($fieldDefaults['default_value'])) {
121 $defaultCheckValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
122 substr($fieldDefaults['default_value'], 1, -1)
124 if (in_array($defaults['value'], $defaultCheckValues)) {
125 $defaults['default_value'] = 1;
130 if (CRM_Utils_Array
::value('default_value', $fieldDefaults) == CRM_Utils_Array
::value('value', $defaults)) {
131 $defaults['default_value'] = 1;
136 $defaults['is_active'] = 1;
139 if ($this->_action
& CRM_Core_Action
::ADD
) {
140 $fieldValues = ['option_group_id' => $this->_optionGroupID
];
141 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
148 * Build the form object.
152 public function buildQuickForm() {
153 if ($this->_action
== CRM_Core_Action
::DELETE
) {
154 $option = civicrm_api3('option_value', 'getsingle', ['id' => $this->_id
]);
155 $this->assign('label', $option['label']);
159 'name' => ts('Delete'),
164 'name' => ts('Cancel'),
169 // lets trim all the whitespace
170 $this->applyFilter('__ALL__', 'trim');
172 // hidden Option Id for validation use
173 $this->add('hidden', 'optionId', $this->_id
);
175 //hidden field ID for validation use
176 $this->add('hidden', 'fieldId', $this->_fid
);
179 $this->add('text', 'label', ts('Option Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'), TRUE);
181 $this->add('text', 'value', ts('Option Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'), TRUE);
183 $this->add('textarea', 'description', ts('Description'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'description'));
185 $this->add('number', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'weight'), TRUE);
186 $this->addRule('weight', ts('is a numeric field'), 'numeric');
189 $this->add('checkbox', 'is_active', ts('Active?'));
191 // Set the default value for Custom Field
192 $this->add('checkbox', 'default_value', ts('Default'));
194 // add a custom form rule
195 $this->addFormRule(['CRM_Custom_Form_Option', 'formRule'], $this);
201 'name' => ts('Save'),
206 'name' => ts('Save and New'),
211 'name' => ts('Cancel'),
215 // if view mode pls freeze it with the done button.
216 if ($this->_action
& CRM_Core_Action
::VIEW
) {
218 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field/option',
219 'reset=1&action=browse&fid=' . $this->_fid
. '&gid=' . $this->_gid
,
222 $this->addElement('button',
225 ['onclick' => "location.href='$url'", 'class' => 'crm-form-submit cancel', 'crm-icon' => 'fa-times']
229 $this->assign('id', $this->_id
);
233 * Global validation rules for the form.
235 * @param array $fields
236 * Posted values of the form.
239 * @param CRM_Core_Form $form
242 * list of errors to be posted back to the form
244 public static function formRule($fields, $files, $form) {
245 $optionLabel = $fields['label'];
246 $optionValue = $fields['value'];
247 $fieldId = $form->_fid
;
248 $optionGroupId = $form->_optionGroupID
;
251 if (empty($form->_id
)) {
254 FROM civicrm_option_value
255 WHERE option_group_id = %1
258 1 => [$optionGroupId, 'Integer'],
259 2 => [$optionLabel, 'String'],
261 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
262 $errors['label'] = ts('There is an entry with the same label.');
267 FROM civicrm_option_value
268 WHERE option_group_id = %1
271 1 => [$optionGroupId, 'Integer'],
272 2 => [$optionValue, 'String'],
274 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
275 $errors['value'] = ts('There is an entry with the same value.');
279 //capture duplicate entries while updating Custom Options
280 $optionId = CRM_Utils_Type
::escape($fields['optionId'], 'Integer');
282 //check label duplicates within a custom field
285 FROM civicrm_option_value
286 WHERE option_group_id = %1
290 1 => [$optionGroupId, 'Integer'],
291 2 => [$optionId, 'Integer'],
292 3 => [$optionLabel, 'String'],
294 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
295 $errors['label'] = ts('There is an entry with the same label.');
298 //check value duplicates within a custom field
301 FROM civicrm_option_value
302 WHERE option_group_id = %1
306 1 => [$optionGroupId, 'Integer'],
307 2 => [$optionId, 'Integer'],
308 3 => [$optionValue, 'String'],
310 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
311 $errors['value'] = ts('There is an entry with the same value.');
317 FROM civicrm_custom_field
319 $params = [1 => [$fieldId, 'Integer']];
320 $dao = CRM_Core_DAO
::executeQuery($query, $params);
322 switch ($dao->data_type
) {
324 if (!CRM_Utils_Rule
::integer($fields["value"])) {
325 $errors['value'] = ts('Please enter a valid integer value.');
331 if (!CRM_Utils_Rule
::numeric($fields["value"])) {
332 $errors['value'] = ts('Please enter a valid number.');
337 if (!CRM_Utils_Rule
::money($fields["value"])) {
338 $errors['value'] = ts('Please enter a valid value.');
343 if (!CRM_Utils_Rule
::date($fields["value"])) {
344 $errors['value'] = ts('Please enter a valid date using YYYY-MM-DD format. Example: 2004-12-31.');
349 if (!CRM_Utils_Rule
::integer($fields["value"]) &&
350 ($fields["value"] != '1' ||
$fields["value"] != '0')
352 $errors['value'] = ts('Please enter 1 or 0 as value.');
357 if (!empty($fields["value"])) {
358 $params = [1 => [$fields['value'], 'String']];
359 $query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1";
360 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
361 $errors['value'] = ts('Invalid default value for country.');
366 case 'StateProvince':
367 if (!empty($fields["value"])) {
368 $params = [1 => [$fields['value'], 'String']];
371 FROM civicrm_state_province
373 OR abbreviation = %1";
374 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
375 $errors['value'] = ts('The invalid value for State/Province data type');
382 return empty($errors) ?
TRUE : $errors;
390 public function postProcess() {
391 // store the submitted values in an array
392 $params = $this->controller
->exportValues('Option');
394 if ($this->_action
== CRM_Core_Action
::DELETE
) {
395 $option = civicrm_api3('option_value', 'getsingle', ['id' => $this->_id
]);
396 $fieldValues = ['option_group_id' => $this->_optionGroupID
];
397 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $this->_id
, $fieldValues);
398 CRM_Core_BAO_CustomOption
::del($this->_id
);
399 CRM_Core_Session
::setStatus(ts('Option "%1" has been deleted.', [1 => $option['label']]), ts('Deleted'), 'success');
403 // set values for custom field properties and save
404 $customOption = new CRM_Core_DAO_OptionValue();
405 $customOption->label
= $params['label'];
406 $customOption->name
= CRM_Utils_String
::titleToVar($params['label']);
407 $customOption->weight
= $params['weight'];
408 $customOption->description
= $params['description'];
409 $customOption->value
= $params['value'];
410 $customOption->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);
414 $customOption->id
= $this->_id
;
415 CRM_Core_BAO_CustomOption
::updateCustomValues($params);
416 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'weight', 'id');
419 $fieldValues = ['option_group_id' => $this->_optionGroupID
];
420 $customOption->weight
421 = CRM_Utils_Weight
::updateOtherWeights(
422 'CRM_Core_DAO_OptionValue',
427 $customOption->option_group_id
= $this->_optionGroupID
;
429 $customField = new CRM_Core_DAO_CustomField();
430 $customField->id
= $this->_fid
;
432 $customField->find(TRUE) &&
434 $customField->html_type
== 'CheckBox' ||
435 $customField->html_type
== 'Multi-Select'
439 CRM_Core_DAO
::VALUE_SEPARATOR
,
440 substr($customField->default_value
, 1, -1)
442 if (!empty($params['default_value'])) {
443 if (!in_array($customOption->value
, $defVal)) {
444 if (empty($defVal[0])) {
445 $defVal = [$customOption->value
];
448 $defVal[] = $customOption->value
;
450 $customField->default_value
451 = CRM_Core_DAO
::VALUE_SEPARATOR
.
452 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defVal) .
453 CRM_Core_DAO
::VALUE_SEPARATOR
;
454 $customField->save();
457 elseif (in_array($customOption->value
, $defVal)) {
459 foreach ($defVal as $v) {
460 if ($v != $customOption->value
) {
465 $customField->default_value
466 = CRM_Core_DAO
::VALUE_SEPARATOR
.
467 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $tempVal) .
468 CRM_Core_DAO
::VALUE_SEPARATOR
;
469 $customField->save();
473 switch ($customField->data_type
) {
475 $customOption->value
= CRM_Utils_Rule
::cleanMoney($customOption->value
);
479 $customOption->value
= intval($customOption->value
);
483 $customOption->value
= floatval($customOption->value
);
487 if (!empty($params['default_value'])) {
488 $customField->default_value
= $customOption->value
;
489 $customField->save();
491 elseif ($customField->find(TRUE) && $customField->default_value
== $customOption->value
) {
492 // this is the case where this option is the current default value and we have been reset
493 $customField->default_value
= 'null';
494 $customField->save();
498 $customOption->save();
500 $msg = ts('Your multiple choice option \'%1\' has been saved', [1 => $customOption->label
]);
501 CRM_Core_Session
::setStatus($msg, '', 'success');
502 $buttonName = $this->controller
->getButtonName();
503 $session = CRM_Core_Session
::singleton();
504 if ($buttonName == $this->getButtonName('next', 'new')) {
505 CRM_Core_Session
::setStatus(ts('You can add another option.'), '', 'info');
506 $session->replaceUserContext(
507 CRM_Utils_System
::url(
508 'civicrm/admin/custom/group/field/option',
509 'reset=1&action=add&fid=' . $this->_fid
. '&gid=' . $this->_gid