3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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);
82 if (!isset($this->_gid
) && $this->_fid
) {
83 $this->_gid
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
89 $this->_optionGroupID
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
95 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
99 * This function sets the default values for the form. Note that in edit/view mode
100 * the default values are retrieved from the database
104 * @return array array of default values
107 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'
118 ||
$fieldDefaults['html_type'] == 'AdvMulti-Select'
120 if (CRM_Utils_Array
::value('default_value', $fieldDefaults)) {
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 = array('option_group_id' => $this->_optionGroupID
);
141 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
148 * Function to actually build the form
155 public function buildQuickForm() {
156 if ($this->_action
== CRM_Core_Action
::DELETE
) {
157 $this->addButtons(array(
160 'name' => ts('Delete'),
165 'name' => ts('Cancel'),
171 // lets trim all the whitespace
172 $this->applyFilter('__ALL__', 'trim');
174 // hidden Option Id for validation use
175 $this->add('hidden', 'optionId', $this->_id
);
177 //hidden field ID for validation use
178 $this->add('hidden', 'fieldId', $this->_fid
);
181 $this->add('text', 'label', ts('Option Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'), TRUE);
183 $this->add('text', 'value', ts('Option Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'), TRUE);
186 $this->add('text', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'weight'), TRUE);
187 $this->addRule('weight', ts('is a numeric field'), 'numeric');
190 $this->add('checkbox', 'is_active', ts('Active?'));
192 // Set the default value for Custom Field
193 $this->add('checkbox', 'default_value', ts('Default'));
195 // add a custom form rule
196 $this->addFormRule(array('CRM_Custom_Form_Option', 'formRule'), $this);
199 $this->addButtons(array(
202 'name' => ts('Save'),
207 'name' => ts('Save and New'),
212 'name' => ts('Cancel'),
218 // if view mode pls freeze it with the done button.
219 if ($this->_action
& CRM_Core_Action
::VIEW
) {
221 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field/option',
222 'reset=1&action=browse&fid=' . $this->_fid
. '&gid=' . $this->_gid
,
225 $this->addElement('button',
228 array('onclick' => "location.href='$url'", 'class' => 'form-submit')
232 $this->assign('id', $this->_id
);
236 * global validation rules for the form
238 * @param array $fields posted values of the form
240 * @return array list of errors to be posted back to the form
244 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 => array($optionGroupId, 'Integer'),
259 2 => array($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 => array($optionGroupId, 'Integer'),
272 2 => array($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 => array($optionGroupId, 'Integer'),
291 2 => array($optionId, 'Integer'),
292 3 => array($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 => array($optionGroupId, 'Integer'),
307 2 => array($optionId, 'Integer'),
308 3 => array($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 = array(1 => array($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 value.');
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 = array(1 => array($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 = array(1 => array($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;
393 public function postProcess() {
394 // store the submitted values in an array
395 $params = $this->controller
->exportValues('Option');
397 if ($this->_action
== CRM_Core_Action
::DELETE
) {
398 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
399 $wt = CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $this->_id
, $fieldValues);
400 CRM_Core_BAO_CustomOption
::del($this->_id
);
401 CRM_Core_Session
::setStatus(ts('Your multiple choice option has been deleted'), ts('Deleted'), 'success');
405 // set values for custom field properties and save
406 $customOption = new CRM_Core_DAO_OptionValue();
407 $customOption->label
= $params['label'];
408 $customOption->name
= CRM_Utils_String
::titleToVar($params['label']);
409 $customOption->weight
= $params['weight'];
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
== 'AdvMulti-Select' ||
437 $customField->html_type
== 'Multi-Select'
441 CRM_Core_DAO
::VALUE_SEPARATOR
,
442 substr($customField->default_value
, 1, -1)
444 if (CRM_Utils_Array
::value('default_value', $params)) {
445 if (!in_array($customOption->value
, $defVal)) {
446 if (empty($defVal[0])) {
447 $defVal = array($customOption->value
);
450 $defVal[] = $customOption->value
;
452 $customField->default_value
=
453 CRM_Core_DAO
::VALUE_SEPARATOR
.
454 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defVal) .
455 CRM_Core_DAO
::VALUE_SEPARATOR
;
456 $customField->save();
459 elseif (in_array($customOption->value
, $defVal)) {
461 foreach ($defVal as $v) {
462 if ($v != $customOption->value
) {
467 $customField->default_value
=
468 CRM_Core_DAO
::VALUE_SEPARATOR
.
469 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $tempVal) .
470 CRM_Core_DAO
::VALUE_SEPARATOR
;
471 $customField->save();
475 switch ($customField->data_type
) {
477 $customOption->value
= CRM_Utils_Rule
::cleanMoney($customOption->value
);
481 $customOption->value
= intval($customOption->value
);
485 $customOption->value
= floatval($customOption->value
);
489 if (CRM_Utils_Array
::value('default_value', $params)) {
490 $customField->default_value
= $customOption->value
;
491 $customField->save();
493 elseif ($customField->find(TRUE) && $customField->default_value
== $customOption->value
) {
494 // this is the case where this option is the current default value and we have been reset
495 $customField->default_value
= 'null';
496 $customField->save();
500 $customOption->save();
502 $msg = ts('Your multiple choice option \'%1\' has been saved', array(1 => $customOption->label
));
503 CRM_Core_Session
::setStatus($msg, '', 'success');
504 $buttonName = $this->controller
->getButtonName();
505 $session = CRM_Core_Session
::singleton();
506 if ($buttonName == $this->getButtonName('next', 'new')) {
507 CRM_Core_Session
::setStatus(ts('You can add another option.'), '', 'info');
508 $session->replaceUserContext(
509 CRM_Utils_System
::url(
510 'civicrm/admin/custom/group/field/option',
511 'reset=1&action=add&fid=' . $this->_fid
. '&gid=' . $this->_gid