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
58 protected $_optionGroupID = NULL;
61 * The Option id, used when editing the Option
68 * Set variables up before form is built.
72 public function preProcess() {
73 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive', $this);
75 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this);
77 if (!isset($this->_gid
) && $this->_fid
) {
78 $this->_gid
= CRM_Core_DAO
::getFieldValue(
79 'CRM_Core_DAO_CustomField',
86 $this->_optionGroupID
= CRM_Core_DAO
::getFieldValue(
87 'CRM_Core_DAO_CustomField',
93 if ($isReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_reserved', 'id')) {
94 CRM_Core_Error
::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
97 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
101 * Set default values for the form. Note that in edit/view mode
102 * the default values are retrieved from the database
105 * array of default values
107 public function setDefaultValues() {
108 $defaults = $fieldDefaults = array();
109 if (isset($this->_id
)) {
110 $params = array('id' => $this->_id
);
111 CRM_Core_BAO_CustomOption
::retrieve($params, $defaults);
113 $paramsField = array('id' => $this->_fid
);
114 CRM_Core_BAO_CustomField
::retrieve($paramsField, $fieldDefaults);
116 if ($fieldDefaults['html_type'] == 'CheckBox'
117 ||
$fieldDefaults['html_type'] == 'Multi-Select'
119 if (!empty($fieldDefaults['default_value'])) {
120 $defaultCheckValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
121 substr($fieldDefaults['default_value'], 1, -1)
123 if (in_array($defaults['value'], $defaultCheckValues)) {
124 $defaults['default_value'] = 1;
129 if (CRM_Utils_Array
::value('default_value', $fieldDefaults) == CRM_Utils_Array
::value('value', $defaults)) {
130 $defaults['default_value'] = 1;
135 $defaults['is_active'] = 1;
138 if ($this->_action
& CRM_Core_Action
::ADD
) {
139 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
140 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
147 * Build the form object.
151 public function buildQuickForm() {
152 if ($this->_action
== CRM_Core_Action
::DELETE
) {
153 $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id
));
154 $this->assign('label', $option['label']);
155 $this->addButtons(array(
158 'name' => ts('Delete'),
163 '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('text', '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(array('CRM_Custom_Form_Option', 'formRule'), $this);
198 $this->addButtons(array(
201 'name' => ts('Save'),
206 'name' => ts('Save and New'),
211 'name' => ts('Cancel'),
216 // if view mode pls freeze it with the done button.
217 if ($this->_action
& CRM_Core_Action
::VIEW
) {
219 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field/option',
220 'reset=1&action=browse&fid=' . $this->_fid
. '&gid=' . $this->_gid
,
223 $this->addElement('button',
226 array('onclick' => "location.href='$url'", 'class' => 'crm-form-submit cancel', 'crm-icon' => 'fa-times')
230 $this->assign('id', $this->_id
);
234 * Global validation rules for the form.
236 * @param array $fields
237 * Posted values of the form.
240 * @param CRM_Core_Form $form
243 * list of errors to be posted back to the form
245 public static function formRule($fields, $files, $form) {
246 $optionLabel = $fields['label'];
247 $optionValue = $fields['value'];
248 $fieldId = $form->_fid
;
249 $optionGroupId = $form->_optionGroupID
;
252 if (empty($form->_id
)) {
255 FROM civicrm_option_value
256 WHERE option_group_id = %1
259 1 => array($optionGroupId, 'Integer'),
260 2 => array($optionLabel, 'String'),
262 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
263 $errors['label'] = ts('There is an entry with the same label.');
268 FROM civicrm_option_value
269 WHERE option_group_id = %1
272 1 => array($optionGroupId, 'Integer'),
273 2 => array($optionValue, 'String'),
275 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
276 $errors['value'] = ts('There is an entry with the same value.');
280 //capture duplicate entries while updating Custom Options
281 $optionId = CRM_Utils_Type
::escape($fields['optionId'], 'Integer');
283 //check label duplicates within a custom field
286 FROM civicrm_option_value
287 WHERE option_group_id = %1
291 1 => array($optionGroupId, 'Integer'),
292 2 => array($optionId, 'Integer'),
293 3 => array($optionLabel, 'String'),
295 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
296 $errors['label'] = ts('There is an entry with the same label.');
299 //check value duplicates within a custom field
302 FROM civicrm_option_value
303 WHERE option_group_id = %1
307 1 => array($optionGroupId, 'Integer'),
308 2 => array($optionId, 'Integer'),
309 3 => array($optionValue, 'String'),
311 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
312 $errors['value'] = ts('There is an entry with the same value.');
318 FROM civicrm_custom_field
320 $params = array(1 => array($fieldId, 'Integer'));
321 $dao = CRM_Core_DAO
::executeQuery($query, $params);
323 switch ($dao->data_type
) {
325 if (!CRM_Utils_Rule
::integer($fields["value"])) {
326 $errors['value'] = ts('Please enter a valid integer value.');
332 if (!CRM_Utils_Rule
::numeric($fields["value"])) {
333 $errors['value'] = ts('Please enter a valid number.');
338 if (!CRM_Utils_Rule
::money($fields["value"])) {
339 $errors['value'] = ts('Please enter a valid value.');
344 if (!CRM_Utils_Rule
::date($fields["value"])) {
345 $errors['value'] = ts('Please enter a valid date using YYYY-MM-DD format. Example: 2004-12-31.');
350 if (!CRM_Utils_Rule
::integer($fields["value"]) &&
351 ($fields["value"] != '1' ||
$fields["value"] != '0')
353 $errors['value'] = ts('Please enter 1 or 0 as value.');
358 if (!empty($fields["value"])) {
359 $params = array(1 => array($fields['value'], 'String'));
360 $query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1";
361 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
362 $errors['value'] = ts('Invalid default value for country.');
367 case 'StateProvince':
368 if (!empty($fields["value"])) {
369 $params = array(1 => array($fields['value'], 'String'));
372 FROM civicrm_state_province
374 OR abbreviation = %1";
375 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
376 $errors['value'] = ts('The invalid value for State/Province data type');
383 return empty($errors) ?
TRUE : $errors;
391 public function postProcess() {
392 // store the submitted values in an array
393 $params = $this->controller
->exportValues('Option');
395 if ($this->_action
== CRM_Core_Action
::DELETE
) {
396 $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id
));
397 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
398 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $this->_id
, $fieldValues);
399 CRM_Core_BAO_CustomOption
::del($this->_id
);
400 CRM_Core_Session
::setStatus(ts('Option "%1" has been deleted.', array(1 => $option['label'])), ts('Deleted'), 'success');
404 // set values for custom field properties and save
405 $customOption = new CRM_Core_DAO_OptionValue();
406 $customOption->label
= $params['label'];
407 $customOption->name
= CRM_Utils_String
::titleToVar($params['label']);
408 $customOption->weight
= $params['weight'];
409 $customOption->description
= $params['description'];
410 $customOption->value
= $params['value'];
411 $customOption->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);
415 $customOption->id
= $this->_id
;
416 CRM_Core_BAO_CustomOption
::updateCustomValues($params);
417 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'weight', 'id');
420 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
421 $customOption->weight
422 = CRM_Utils_Weight
::updateOtherWeights(
423 'CRM_Core_DAO_OptionValue',
428 $customOption->option_group_id
= $this->_optionGroupID
;
430 $customField = new CRM_Core_DAO_CustomField();
431 $customField->id
= $this->_fid
;
433 $customField->find(TRUE) &&
435 $customField->html_type
== 'CheckBox' ||
436 $customField->html_type
== 'Multi-Select'
440 CRM_Core_DAO
::VALUE_SEPARATOR
,
441 substr($customField->default_value
, 1, -1)
443 if (!empty($params['default_value'])) {
444 if (!in_array($customOption->value
, $defVal)) {
445 if (empty($defVal[0])) {
446 $defVal = array($customOption->value
);
449 $defVal[] = $customOption->value
;
451 $customField->default_value
452 = CRM_Core_DAO
::VALUE_SEPARATOR
.
453 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defVal) .
454 CRM_Core_DAO
::VALUE_SEPARATOR
;
455 $customField->save();
458 elseif (in_array($customOption->value
, $defVal)) {
460 foreach ($defVal as $v) {
461 if ($v != $customOption->value
) {
466 $customField->default_value
467 = CRM_Core_DAO
::VALUE_SEPARATOR
.
468 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $tempVal) .
469 CRM_Core_DAO
::VALUE_SEPARATOR
;
470 $customField->save();
474 switch ($customField->data_type
) {
476 $customOption->value
= CRM_Utils_Rule
::cleanMoney($customOption->value
);
480 $customOption->value
= intval($customOption->value
);
484 $customOption->value
= floatval($customOption->value
);
488 if (!empty($params['default_value'])) {
489 $customField->default_value
= $customOption->value
;
490 $customField->save();
492 elseif ($customField->find(TRUE) && $customField->default_value
== $customOption->value
) {
493 // this is the case where this option is the current default value and we have been reset
494 $customField->default_value
= 'null';
495 $customField->save();
499 $customOption->save();
501 $msg = ts('Your multiple choice option \'%1\' has been saved', array(1 => $customOption->label
));
502 CRM_Core_Session
::setStatus($msg, '', 'success');
503 $buttonName = $this->controller
->getButtonName();
504 $session = CRM_Core_Session
::singleton();
505 if ($buttonName == $this->getButtonName('next', 'new')) {
506 CRM_Core_Session
::setStatus(ts('You can add another option.'), '', 'info');
507 $session->replaceUserContext(
508 CRM_Utils_System
::url(
509 'civicrm/admin/custom/group/field/option',
510 'reset=1&action=add&fid=' . $this->_fid
. '&gid=' . $this->_gid