3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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_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
74 public function preProcess() {
75 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive', $this);
77 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this);
79 if (!isset($this->_gid
) && $this->_fid
) {
80 $this->_gid
= CRM_Core_DAO
::getFieldValue(
81 'CRM_Core_DAO_CustomField',
88 $this->_optionGroupID
= CRM_Core_DAO
::getFieldValue(
89 'CRM_Core_DAO_CustomField',
95 if ($isReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_reserved', 'id')) {
96 CRM_Core_Error
::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
99 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
103 * Set default values for the form. Note that in edit/view mode
104 * the default values are retrieved from the database
108 * @return array array of default values
110 public function setDefaultValues() {
111 $defaults = $fieldDefaults = array();
112 if (isset($this->_id
)) {
113 $params = array('id' => $this->_id
);
114 CRM_Core_BAO_CustomOption
::retrieve($params, $defaults);
116 $paramsField = array('id' => $this->_fid
);
117 CRM_Core_BAO_CustomField
::retrieve($paramsField, $fieldDefaults);
119 if ($fieldDefaults['html_type'] == 'CheckBox'
120 ||
$fieldDefaults['html_type'] == 'Multi-Select'
121 ||
$fieldDefaults['html_type'] == 'AdvMulti-Select'
123 if (!empty($fieldDefaults['default_value'])) {
124 $defaultCheckValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
125 substr($fieldDefaults['default_value'], 1, -1)
127 if (in_array($defaults['value'], $defaultCheckValues)) {
128 $defaults['default_value'] = 1;
133 if (CRM_Utils_Array
::value('default_value', $fieldDefaults) == CRM_Utils_Array
::value('value', $defaults)) {
134 $defaults['default_value'] = 1;
139 $defaults['is_active'] = 1;
142 if ($this->_action
& CRM_Core_Action
::ADD
) {
143 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
144 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
151 * Build the form object
157 public function buildQuickForm() {
158 if ($this->_action
== CRM_Core_Action
::DELETE
) {
159 $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id
));
160 $this->assign('label', $option['label']);
161 $this->addButtons(array(
164 'name' => ts('Delete'),
169 'name' => ts('Cancel'),
175 // lets trim all the whitespace
176 $this->applyFilter('__ALL__', 'trim');
178 // hidden Option Id for validation use
179 $this->add('hidden', 'optionId', $this->_id
);
181 //hidden field ID for validation use
182 $this->add('hidden', 'fieldId', $this->_fid
);
185 $this->add('text', 'label', ts('Option Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'), TRUE);
187 $this->add('text', 'value', ts('Option Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'), TRUE);
190 $this->add('text', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'weight'), TRUE);
191 $this->addRule('weight', ts('is a numeric field'), 'numeric');
194 $this->add('checkbox', 'is_active', ts('Active?'));
196 // Set the default value for Custom Field
197 $this->add('checkbox', 'default_value', ts('Default'));
199 // add a custom form rule
200 $this->addFormRule(array('CRM_Custom_Form_Option', 'formRule'), $this);
203 $this->addButtons(array(
206 'name' => ts('Save'),
211 'name' => ts('Save and New'),
216 'name' => ts('Cancel'),
222 // if view mode pls freeze it with the done button.
223 if ($this->_action
& CRM_Core_Action
::VIEW
) {
225 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field/option',
226 'reset=1&action=browse&fid=' . $this->_fid
. '&gid=' . $this->_gid
,
229 $this->addElement('button',
232 array('onclick' => "location.href='$url'", 'class' => 'crm-form-submit')
236 $this->assign('id', $this->_id
);
240 * Global validation rules for the form
242 * @param array $fields
243 * Posted values of the form.
246 * @param CRM_Core_Form $form
248 * @return array list of errors to be posted back to the form
251 public static function formRule($fields, $files, $form) {
252 $optionLabel = $fields['label'];
253 $optionValue = $fields['value'];
254 $fieldId = $form->_fid
;
255 $optionGroupId = $form->_optionGroupID
;
258 if (empty($form->_id
)) {
261 FROM civicrm_option_value
262 WHERE option_group_id = %1
265 1 => array($optionGroupId, 'Integer'),
266 2 => array($optionLabel, 'String'),
268 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
269 $errors['label'] = ts('There is an entry with the same label.');
274 FROM civicrm_option_value
275 WHERE option_group_id = %1
278 1 => array($optionGroupId, 'Integer'),
279 2 => array($optionValue, 'String'),
281 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
282 $errors['value'] = ts('There is an entry with the same value.');
286 //capture duplicate entries while updating Custom Options
287 $optionId = CRM_Utils_Type
::escape($fields['optionId'], 'Integer');
289 //check label duplicates within a custom field
292 FROM civicrm_option_value
293 WHERE option_group_id = %1
297 1 => array($optionGroupId, 'Integer'),
298 2 => array($optionId, 'Integer'),
299 3 => array($optionLabel, 'String'),
301 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
302 $errors['label'] = ts('There is an entry with the same label.');
305 //check value duplicates within a custom field
308 FROM civicrm_option_value
309 WHERE option_group_id = %1
313 1 => array($optionGroupId, 'Integer'),
314 2 => array($optionId, 'Integer'),
315 3 => array($optionValue, 'String'),
317 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
318 $errors['value'] = ts('There is an entry with the same value.');
324 FROM civicrm_custom_field
326 $params = array(1 => array($fieldId, 'Integer'));
327 $dao = CRM_Core_DAO
::executeQuery($query, $params);
329 switch ($dao->data_type
) {
331 if (!CRM_Utils_Rule
::integer($fields["value"])) {
332 $errors['value'] = ts('Please enter a valid integer value.');
338 if (!CRM_Utils_Rule
::numeric($fields["value"])) {
339 $errors['value'] = ts('Please enter a valid number.');
344 if (!CRM_Utils_Rule
::money($fields["value"])) {
345 $errors['value'] = ts('Please enter a valid value.');
350 if (!CRM_Utils_Rule
::date($fields["value"])) {
351 $errors['value'] = ts('Please enter a valid date using YYYY-MM-DD format. Example: 2004-12-31.');
356 if (!CRM_Utils_Rule
::integer($fields["value"]) &&
357 ($fields["value"] != '1' ||
$fields["value"] != '0')
359 $errors['value'] = ts('Please enter 1 or 0 as value.');
364 if (!empty($fields["value"])) {
365 $params = array(1 => array($fields['value'], 'String'));
366 $query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1";
367 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
368 $errors['value'] = ts('Invalid default value for country.');
373 case 'StateProvince':
374 if (!empty($fields["value"])) {
375 $params = array(1 => array($fields['value'], 'String'));
378 FROM civicrm_state_province
380 OR abbreviation = %1";
381 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
382 $errors['value'] = ts('The invalid value for State/Province data type');
389 return empty($errors) ?
TRUE : $errors;
399 public function postProcess() {
400 // store the submitted values in an array
401 $params = $this->controller
->exportValues('Option');
403 if ($this->_action
== CRM_Core_Action
::DELETE
) {
404 $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id
));
405 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
406 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $this->_id
, $fieldValues);
407 CRM_Core_BAO_CustomOption
::del($this->_id
);
408 CRM_Core_Session
::setStatus(ts('Option "%1" has been deleted.', array(1 => $option['label'])), ts('Deleted'), 'success');
412 // set values for custom field properties and save
413 $customOption = new CRM_Core_DAO_OptionValue();
414 $customOption->label
= $params['label'];
415 $customOption->name
= CRM_Utils_String
::titleToVar($params['label']);
416 $customOption->weight
= $params['weight'];
417 $customOption->value
= $params['value'];
418 $customOption->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);
422 $customOption->id
= $this->_id
;
423 CRM_Core_BAO_CustomOption
::updateCustomValues($params);
424 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'weight', 'id');
427 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
428 $customOption->weight
=
429 CRM_Utils_Weight
::updateOtherWeights(
430 'CRM_Core_DAO_OptionValue',
435 $customOption->option_group_id
= $this->_optionGroupID
;
437 $customField = new CRM_Core_DAO_CustomField();
438 $customField->id
= $this->_fid
;
440 $customField->find(TRUE) &&
442 $customField->html_type
== 'CheckBox' ||
443 $customField->html_type
== 'AdvMulti-Select' ||
444 $customField->html_type
== 'Multi-Select'
448 CRM_Core_DAO
::VALUE_SEPARATOR
,
449 substr($customField->default_value
, 1, -1)
451 if (!empty($params['default_value'])) {
452 if (!in_array($customOption->value
, $defVal)) {
453 if (empty($defVal[0])) {
454 $defVal = array($customOption->value
);
457 $defVal[] = $customOption->value
;
459 $customField->default_value
=
460 CRM_Core_DAO
::VALUE_SEPARATOR
.
461 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defVal) .
462 CRM_Core_DAO
::VALUE_SEPARATOR
;
463 $customField->save();
466 elseif (in_array($customOption->value
, $defVal)) {
468 foreach ($defVal as $v) {
469 if ($v != $customOption->value
) {
474 $customField->default_value
=
475 CRM_Core_DAO
::VALUE_SEPARATOR
.
476 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $tempVal) .
477 CRM_Core_DAO
::VALUE_SEPARATOR
;
478 $customField->save();
482 switch ($customField->data_type
) {
484 $customOption->value
= CRM_Utils_Rule
::cleanMoney($customOption->value
);
488 $customOption->value
= intval($customOption->value
);
492 $customOption->value
= floatval($customOption->value
);
496 if (!empty($params['default_value'])) {
497 $customField->default_value
= $customOption->value
;
498 $customField->save();
500 elseif ($customField->find(TRUE) && $customField->default_value
== $customOption->value
) {
501 // this is the case where this option is the current default value and we have been reset
502 $customField->default_value
= 'null';
503 $customField->save();
507 $customOption->save();
509 $msg = ts('Your multiple choice option \'%1\' has been saved', array(1 => $customOption->label
));
510 CRM_Core_Session
::setStatus($msg, '', 'success');
511 $buttonName = $this->controller
->getButtonName();
512 $session = CRM_Core_Session
::singleton();
513 if ($buttonName == $this->getButtonName('next', 'new')) {
514 CRM_Core_Session
::setStatus(ts('You can add another option.'), '', 'info');
515 $session->replaceUserContext(
516 CRM_Utils_System
::url(
517 'civicrm/admin/custom/group/field/option',
518 'reset=1&action=add&fid=' . $this->_fid
. '&gid=' . $this->_gid