3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
50 * the custom group id saved to the session for an update
60 protected $_optionGroupID = NULL;
63 * The Option id, used when editing the Option
71 * Function to set variables up before form is built
78 public function preProcess() {
79 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive', $this);
81 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this);
83 if (!isset($this->_gid
) && $this->_fid
) {
84 $this->_gid
= CRM_Core_DAO
::getFieldValue(
85 'CRM_Core_DAO_CustomField',
92 $this->_optionGroupID
= CRM_Core_DAO
::getFieldValue(
93 'CRM_Core_DAO_CustomField',
99 if ($isReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_reserved', 'id')) {
100 CRM_Core_Error
::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
103 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
107 * This function sets the default values for the form. Note that in edit/view mode
108 * the default values are retrieved from the database
112 * @return array array of default values
115 function setDefaultValues() {
116 $defaults = $fieldDefaults = array();
117 if (isset($this->_id
)) {
118 $params = array('id' => $this->_id
);
119 CRM_Core_BAO_CustomOption
::retrieve($params, $defaults);
121 $paramsField = array('id' => $this->_fid
);
122 CRM_Core_BAO_CustomField
::retrieve($paramsField, $fieldDefaults);
124 if ($fieldDefaults['html_type'] == 'CheckBox'
125 ||
$fieldDefaults['html_type'] == 'Multi-Select'
126 ||
$fieldDefaults['html_type'] == 'AdvMulti-Select'
128 if (!empty($fieldDefaults['default_value'])) {
129 $defaultCheckValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
130 substr($fieldDefaults['default_value'], 1, -1)
132 if (in_array($defaults['value'], $defaultCheckValues)) {
133 $defaults['default_value'] = 1;
138 if (CRM_Utils_Array
::value('default_value', $fieldDefaults) == CRM_Utils_Array
::value('value', $defaults)) {
139 $defaults['default_value'] = 1;
144 $defaults['is_active'] = 1;
147 if ($this->_action
& CRM_Core_Action
::ADD
) {
148 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
149 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
156 * Function to actually build the form
163 public function buildQuickForm() {
164 if ($this->_action
== CRM_Core_Action
::DELETE
) {
165 $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id
));
166 $this->assign('label', $option['label']);
167 $this->addButtons(array(
170 'name' => ts('Delete'),
175 'name' => ts('Cancel'),
181 // lets trim all the whitespace
182 $this->applyFilter('__ALL__', 'trim');
184 // hidden Option Id for validation use
185 $this->add('hidden', 'optionId', $this->_id
);
187 //hidden field ID for validation use
188 $this->add('hidden', 'fieldId', $this->_fid
);
191 $this->add('text', 'label', ts('Option Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'), TRUE);
193 $this->add('text', 'value', ts('Option Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'), TRUE);
196 $this->add('text', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'weight'), TRUE);
197 $this->addRule('weight', ts('is a numeric field'), 'numeric');
200 $this->add('checkbox', 'is_active', ts('Active?'));
202 // Set the default value for Custom Field
203 $this->add('checkbox', 'default_value', ts('Default'));
205 // add a custom form rule
206 $this->addFormRule(array('CRM_Custom_Form_Option', 'formRule'), $this);
209 $this->addButtons(array(
212 'name' => ts('Save'),
217 'name' => ts('Save and New'),
222 'name' => ts('Cancel'),
228 // if view mode pls freeze it with the done button.
229 if ($this->_action
& CRM_Core_Action
::VIEW
) {
231 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field/option',
232 'reset=1&action=browse&fid=' . $this->_fid
. '&gid=' . $this->_gid
,
235 $this->addElement('button',
238 array('onclick' => "location.href='$url'", 'class' => 'crm-form-submit')
242 $this->assign('id', $this->_id
);
246 * global validation rules for the form
248 * @param array $fields posted values of the form
253 * @return array list of errors to be posted back to the form
257 static function formRule($fields, $files, $form) {
258 $optionLabel = $fields['label'];
259 $optionValue = $fields['value'];
260 $fieldId = $form->_fid
;
261 $optionGroupId = $form->_optionGroupID
;
264 if (empty($form->_id
)) {
267 FROM civicrm_option_value
268 WHERE option_group_id = %1
271 1 => array($optionGroupId, 'Integer'),
272 2 => array($optionLabel, 'String'),
274 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
275 $errors['label'] = ts('There is an entry with the same label.');
280 FROM civicrm_option_value
281 WHERE option_group_id = %1
284 1 => array($optionGroupId, 'Integer'),
285 2 => array($optionValue, 'String'),
287 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
288 $errors['value'] = ts('There is an entry with the same value.');
292 //capture duplicate entries while updating Custom Options
293 $optionId = CRM_Utils_Type
::escape($fields['optionId'], 'Integer');
295 //check label duplicates within a custom field
298 FROM civicrm_option_value
299 WHERE option_group_id = %1
303 1 => array($optionGroupId, 'Integer'),
304 2 => array($optionId, 'Integer'),
305 3 => array($optionLabel, 'String'),
307 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
308 $errors['label'] = ts('There is an entry with the same label.');
311 //check value duplicates within a custom field
314 FROM civicrm_option_value
315 WHERE option_group_id = %1
319 1 => array($optionGroupId, 'Integer'),
320 2 => array($optionId, 'Integer'),
321 3 => array($optionValue, 'String'),
323 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
324 $errors['value'] = ts('There is an entry with the same value.');
330 FROM civicrm_custom_field
332 $params = array(1 => array($fieldId, 'Integer'));
333 $dao = CRM_Core_DAO
::executeQuery($query, $params);
335 switch ($dao->data_type
) {
337 if (!CRM_Utils_Rule
::integer($fields["value"])) {
338 $errors['value'] = ts('Please enter a valid integer value.');
344 if (!CRM_Utils_Rule
::numeric($fields["value"])) {
345 $errors['value'] = ts('Please enter a valid number value.');
350 if (!CRM_Utils_Rule
::money($fields["value"])) {
351 $errors['value'] = ts('Please enter a valid value.');
356 if (!CRM_Utils_Rule
::date($fields["value"])) {
357 $errors['value'] = ts('Please enter a valid date using YYYY-MM-DD format. Example: 2004-12-31.');
362 if (!CRM_Utils_Rule
::integer($fields["value"]) &&
363 ($fields["value"] != '1' ||
$fields["value"] != '0')
365 $errors['value'] = ts('Please enter 1 or 0 as value.');
370 if (!empty($fields["value"])) {
371 $params = array(1 => array($fields['value'], 'String'));
372 $query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1";
373 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
374 $errors['value'] = ts('Invalid default value for country.');
379 case 'StateProvince':
380 if (!empty($fields["value"])) {
381 $params = array(1 => array($fields['value'], 'String'));
384 FROM civicrm_state_province
386 OR abbreviation = %1";
387 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
388 $errors['value'] = ts('The invalid value for State/Province data type');
395 return empty($errors) ?
TRUE : $errors;
406 public function postProcess() {
407 // store the submitted values in an array
408 $params = $this->controller
->exportValues('Option');
410 if ($this->_action
== CRM_Core_Action
::DELETE
) {
411 $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id
));
412 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
413 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $this->_id
, $fieldValues);
414 CRM_Core_BAO_CustomOption
::del($this->_id
);
415 CRM_Core_Session
::setStatus(ts('Option "%1" has been deleted.', array(1 => $option['label'])), ts('Deleted'), 'success');
419 // set values for custom field properties and save
420 $customOption = new CRM_Core_DAO_OptionValue();
421 $customOption->label
= $params['label'];
422 $customOption->name
= CRM_Utils_String
::titleToVar($params['label']);
423 $customOption->weight
= $params['weight'];
424 $customOption->value
= $params['value'];
425 $customOption->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);
429 $customOption->id
= $this->_id
;
430 CRM_Core_BAO_CustomOption
::updateCustomValues($params);
431 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'weight', 'id');
434 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
435 $customOption->weight
=
436 CRM_Utils_Weight
::updateOtherWeights(
437 'CRM_Core_DAO_OptionValue',
442 $customOption->option_group_id
= $this->_optionGroupID
;
444 $customField = new CRM_Core_DAO_CustomField();
445 $customField->id
= $this->_fid
;
447 $customField->find(TRUE) &&
449 $customField->html_type
== 'CheckBox' ||
450 $customField->html_type
== 'AdvMulti-Select' ||
451 $customField->html_type
== 'Multi-Select'
455 CRM_Core_DAO
::VALUE_SEPARATOR
,
456 substr($customField->default_value
, 1, -1)
458 if (!empty($params['default_value'])) {
459 if (!in_array($customOption->value
, $defVal)) {
460 if (empty($defVal[0])) {
461 $defVal = array($customOption->value
);
464 $defVal[] = $customOption->value
;
466 $customField->default_value
=
467 CRM_Core_DAO
::VALUE_SEPARATOR
.
468 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defVal) .
469 CRM_Core_DAO
::VALUE_SEPARATOR
;
470 $customField->save();
473 elseif (in_array($customOption->value
, $defVal)) {
475 foreach ($defVal as $v) {
476 if ($v != $customOption->value
) {
481 $customField->default_value
=
482 CRM_Core_DAO
::VALUE_SEPARATOR
.
483 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $tempVal) .
484 CRM_Core_DAO
::VALUE_SEPARATOR
;
485 $customField->save();
489 switch ($customField->data_type
) {
491 $customOption->value
= CRM_Utils_Rule
::cleanMoney($customOption->value
);
495 $customOption->value
= intval($customOption->value
);
499 $customOption->value
= floatval($customOption->value
);
503 if (!empty($params['default_value'])) {
504 $customField->default_value
= $customOption->value
;
505 $customField->save();
507 elseif ($customField->find(TRUE) && $customField->default_value
== $customOption->value
) {
508 // this is the case where this option is the current default value and we have been reset
509 $customField->default_value
= 'null';
510 $customField->save();
514 $customOption->save();
516 $msg = ts('Your multiple choice option \'%1\' has been saved', array(1 => $customOption->label
));
517 CRM_Core_Session
::setStatus($msg, '', 'success');
518 $buttonName = $this->controller
->getButtonName();
519 $session = CRM_Core_Session
::singleton();
520 if ($buttonName == $this->getButtonName('next', 'new')) {
521 CRM_Core_Session
::setStatus(ts('You can add another option.'), '', 'info');
522 $session->replaceUserContext(
523 CRM_Utils_System
::url(
524 'civicrm/admin/custom/group/field/option',
525 'reset=1&action=add&fid=' . $this->_fid
. '&gid=' . $this->_gid