3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * form to process actions on the field aspect of Custom
23 class CRM_Custom_Form_Option
extends CRM_Core_Form
{
26 * The custom field id saved to the session for an update
33 * The custom group id saved to the session for an update
43 protected $_optionGroupID = NULL;
46 * The Option id, used when editing the Option
53 * Set variables up before form is built.
57 public function preProcess() {
58 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive', $this);
60 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this);
62 if (!isset($this->_gid
) && $this->_fid
) {
63 $this->_gid
= CRM_Core_DAO
::getFieldValue(
64 'CRM_Core_DAO_CustomField',
71 $this->_optionGroupID
= CRM_Core_DAO
::getFieldValue(
72 'CRM_Core_DAO_CustomField',
78 if ($isReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_reserved', 'id')) {
79 CRM_Core_Error
::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
82 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
86 * Set default values for the form. Note that in edit/view mode
87 * the default values are retrieved from the database
90 * array of default values
92 public function setDefaultValues() {
93 $defaults = $fieldDefaults = [];
94 if (isset($this->_id
)) {
95 $params = ['id' => $this->_id
];
96 CRM_Core_BAO_CustomOption
::retrieve($params, $defaults);
98 $paramsField = ['id' => $this->_fid
];
99 CRM_Core_BAO_CustomField
::retrieve($paramsField, $fieldDefaults);
101 if ($fieldDefaults['html_type'] == 'CheckBox'
102 ||
$fieldDefaults['html_type'] == 'Multi-Select'
104 if (!empty($fieldDefaults['default_value'])) {
105 $defaultCheckValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
106 substr($fieldDefaults['default_value'], 1, -1)
108 if (in_array($defaults['value'], $defaultCheckValues)) {
109 $defaults['default_value'] = 1;
114 if (CRM_Utils_Array
::value('default_value', $fieldDefaults) == CRM_Utils_Array
::value('value', $defaults)) {
115 $defaults['default_value'] = 1;
120 $defaults['is_active'] = 1;
123 if ($this->_action
& CRM_Core_Action
::ADD
) {
124 $fieldValues = ['option_group_id' => $this->_optionGroupID
];
125 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
132 * Build the form object.
136 public function buildQuickForm() {
137 if ($this->_action
== CRM_Core_Action
::DELETE
) {
138 $option = civicrm_api3('option_value', 'getsingle', ['id' => $this->_id
]);
139 $this->assign('label', $option['label']);
143 'name' => ts('Delete'),
148 'name' => ts('Cancel'),
153 // lets trim all the whitespace
154 $this->applyFilter('__ALL__', 'trim');
156 // hidden Option Id for validation use
157 $this->add('hidden', 'optionId', $this->_id
);
159 //hidden field ID for validation use
160 $this->add('hidden', 'fieldId', $this->_fid
);
163 $this->add('text', 'label', ts('Option Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'), TRUE);
165 $this->add('text', 'value', ts('Option Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'), TRUE);
167 $this->add('textarea', 'description', ts('Description'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'description'));
169 $this->add('number', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'weight'), TRUE);
170 $this->addRule('weight', ts('is a numeric field'), 'numeric');
173 $this->add('checkbox', 'is_active', ts('Active?'));
175 // Set the default value for Custom Field
176 $this->add('checkbox', 'default_value', ts('Default'));
178 // add a custom form rule
179 $this->addFormRule(['CRM_Custom_Form_Option', 'formRule'], $this);
185 'name' => ts('Save'),
190 'name' => ts('Save and New'),
195 'name' => ts('Cancel'),
199 // if view mode pls freeze it with the done button.
200 if ($this->_action
& CRM_Core_Action
::VIEW
) {
202 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field/option',
203 'reset=1&action=browse&fid=' . $this->_fid
. '&gid=' . $this->_gid
,
206 $this->addElement('button',
209 ['onclick' => "location.href='$url'", 'class' => 'crm-form-submit cancel', 'crm-icon' => 'fa-times']
213 $this->assign('id', $this->_id
);
217 * Global validation rules for the form.
219 * @param array $fields
220 * Posted values of the form.
223 * @param CRM_Core_Form $form
226 * list of errors to be posted back to the form
228 public static function formRule($fields, $files, $form) {
229 $optionLabel = $fields['label'];
230 $optionValue = $fields['value'];
231 $fieldId = $form->_fid
;
232 $optionGroupId = $form->_optionGroupID
;
235 if (empty($form->_id
)) {
238 FROM civicrm_option_value
239 WHERE option_group_id = %1
242 1 => [$optionGroupId, 'Integer'],
243 2 => [$optionLabel, 'String'],
245 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
246 $errors['label'] = ts('There is an entry with the same label.');
251 FROM civicrm_option_value
252 WHERE option_group_id = %1
255 1 => [$optionGroupId, 'Integer'],
256 2 => [$optionValue, 'String'],
258 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
259 $errors['value'] = ts('There is an entry with the same value.');
263 //capture duplicate entries while updating Custom Options
264 $optionId = CRM_Utils_Type
::escape($fields['optionId'], 'Integer');
266 //check label duplicates within a custom field
269 FROM civicrm_option_value
270 WHERE option_group_id = %1
274 1 => [$optionGroupId, 'Integer'],
275 2 => [$optionId, 'Integer'],
276 3 => [$optionLabel, 'String'],
278 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
279 $errors['label'] = ts('There is an entry with the same label.');
282 //check value 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 => [$optionValue, 'String'],
294 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
295 $errors['value'] = ts('There is an entry with the same value.');
301 FROM civicrm_custom_field
303 $params = [1 => [$fieldId, 'Integer']];
304 $dao = CRM_Core_DAO
::executeQuery($query, $params);
306 switch ($dao->data_type
) {
308 if (!CRM_Utils_Rule
::integer($fields["value"])) {
309 $errors['value'] = ts('Please enter a valid integer value.');
315 if (!CRM_Utils_Rule
::numeric($fields["value"])) {
316 $errors['value'] = ts('Please enter a valid number.');
321 if (!CRM_Utils_Rule
::money($fields["value"])) {
322 $errors['value'] = ts('Please enter a valid value.');
327 if (!CRM_Utils_Rule
::date($fields["value"])) {
328 $errors['value'] = ts('Please enter a valid date using YYYY-MM-DD format. Example: 2004-12-31.');
333 if (!CRM_Utils_Rule
::integer($fields["value"]) &&
334 ($fields["value"] != '1' ||
$fields["value"] != '0')
336 $errors['value'] = ts('Please enter 1 or 0 as value.');
341 if (!empty($fields["value"])) {
342 $params = [1 => [$fields['value'], 'String']];
343 $query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1";
344 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
345 $errors['value'] = ts('Invalid default value for country.');
350 case 'StateProvince':
351 if (!empty($fields["value"])) {
352 $params = [1 => [$fields['value'], 'String']];
355 FROM civicrm_state_province
357 OR abbreviation = %1";
358 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
359 $errors['value'] = ts('The invalid value for State/Province data type');
366 return empty($errors) ?
TRUE : $errors;
374 public function postProcess() {
375 // store the submitted values in an array
376 $params = $this->controller
->exportValues('Option');
378 if ($this->_action
== CRM_Core_Action
::DELETE
) {
379 $option = civicrm_api3('option_value', 'getsingle', ['id' => $this->_id
]);
380 $fieldValues = ['option_group_id' => $this->_optionGroupID
];
381 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $this->_id
, $fieldValues);
382 CRM_Core_BAO_CustomOption
::del($this->_id
);
383 CRM_Core_Session
::setStatus(ts('Option "%1" has been deleted.', [1 => $option['label']]), ts('Deleted'), 'success');
387 // set values for custom field properties and save
388 $customOption = new CRM_Core_DAO_OptionValue();
389 $customOption->label
= $params['label'];
390 $customOption->name
= CRM_Utils_String
::titleToVar($params['label']);
391 $customOption->weight
= $params['weight'];
392 $customOption->description
= $params['description'];
393 $customOption->value
= $params['value'];
394 $customOption->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);
398 $customOption->id
= $this->_id
;
399 CRM_Core_BAO_CustomOption
::updateCustomValues($params);
400 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'weight', 'id');
403 $fieldValues = ['option_group_id' => $this->_optionGroupID
];
404 $customOption->weight
405 = CRM_Utils_Weight
::updateOtherWeights(
406 'CRM_Core_DAO_OptionValue',
411 $customOption->option_group_id
= $this->_optionGroupID
;
413 $customField = new CRM_Core_DAO_CustomField();
414 $customField->id
= $this->_fid
;
416 $customField->find(TRUE) &&
418 $customField->html_type
== 'CheckBox' ||
419 $customField->html_type
== 'Multi-Select'
423 CRM_Core_DAO
::VALUE_SEPARATOR
,
424 substr($customField->default_value
, 1, -1)
426 if (!empty($params['default_value'])) {
427 if (!in_array($customOption->value
, $defVal)) {
428 if (empty($defVal[0])) {
429 $defVal = [$customOption->value
];
432 $defVal[] = $customOption->value
;
434 $customField->default_value
435 = CRM_Core_DAO
::VALUE_SEPARATOR
.
436 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defVal) .
437 CRM_Core_DAO
::VALUE_SEPARATOR
;
438 $customField->save();
441 elseif (in_array($customOption->value
, $defVal)) {
443 foreach ($defVal as $v) {
444 if ($v != $customOption->value
) {
449 $customField->default_value
450 = CRM_Core_DAO
::VALUE_SEPARATOR
.
451 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $tempVal) .
452 CRM_Core_DAO
::VALUE_SEPARATOR
;
453 $customField->save();
457 switch ($customField->data_type
) {
459 $customOption->value
= CRM_Utils_Rule
::cleanMoney($customOption->value
);
463 $customOption->value
= intval($customOption->value
);
467 $customOption->value
= floatval($customOption->value
);
471 if (!empty($params['default_value'])) {
472 $customField->default_value
= $customOption->value
;
473 $customField->save();
475 elseif ($customField->find(TRUE) && $customField->default_value
== $customOption->value
) {
476 // this is the case where this option is the current default value and we have been reset
477 $customField->default_value
= 'null';
478 $customField->save();
482 $customOption->save();
484 $msg = ts('Your multiple choice option \'%1\' has been saved', [1 => $customOption->label
]);
485 CRM_Core_Session
::setStatus($msg, '', 'success');
486 $buttonName = $this->controller
->getButtonName();
487 $session = CRM_Core_Session
::singleton();
488 if ($buttonName == $this->getButtonName('next', 'new')) {
489 CRM_Core_Session
::setStatus(ts('You can add another option.'), '', 'info');
490 $session->replaceUserContext(
491 CRM_Utils_System
::url(
492 'civicrm/admin/custom/group/field/option',
493 'reset=1&action=add&fid=' . $this->_fid
. '&gid=' . $this->_gid