3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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);
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' => 'form-submit')
242 $this->assign('id', $this->_id
);
246 * global validation rules for the form
248 * @param array $fields posted values of the form
250 * @return array list of errors to be posted back to the form
254 static function formRule($fields, $files, $form) {
255 $optionLabel = $fields['label'];
256 $optionValue = $fields['value'];
257 $fieldId = $form->_fid
;
258 $optionGroupId = $form->_optionGroupID
;
261 if (empty($form->_id
)) {
264 FROM civicrm_option_value
265 WHERE option_group_id = %1
268 1 => array($optionGroupId, 'Integer'),
269 2 => array($optionLabel, 'String'),
271 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
272 $errors['label'] = ts('There is an entry with the same label.');
277 FROM civicrm_option_value
278 WHERE option_group_id = %1
281 1 => array($optionGroupId, 'Integer'),
282 2 => array($optionValue, 'String'),
284 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
285 $errors['value'] = ts('There is an entry with the same value.');
289 //capture duplicate entries while updating Custom Options
290 $optionId = CRM_Utils_Type
::escape($fields['optionId'], 'Integer');
292 //check label duplicates within a custom field
295 FROM civicrm_option_value
296 WHERE option_group_id = %1
300 1 => array($optionGroupId, 'Integer'),
301 2 => array($optionId, 'Integer'),
302 3 => array($optionLabel, 'String'),
304 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
305 $errors['label'] = ts('There is an entry with the same label.');
308 //check value duplicates within a custom field
311 FROM civicrm_option_value
312 WHERE option_group_id = %1
316 1 => array($optionGroupId, 'Integer'),
317 2 => array($optionId, 'Integer'),
318 3 => array($optionValue, 'String'),
320 if (CRM_Core_DAO
::singleValueQuery($query, $params) > 0) {
321 $errors['value'] = ts('There is an entry with the same value.');
327 FROM civicrm_custom_field
329 $params = array(1 => array($fieldId, 'Integer'));
330 $dao = CRM_Core_DAO
::executeQuery($query, $params);
332 switch ($dao->data_type
) {
334 if (!CRM_Utils_Rule
::integer($fields["value"])) {
335 $errors['value'] = ts('Please enter a valid integer value.');
341 if (!CRM_Utils_Rule
::numeric($fields["value"])) {
342 $errors['value'] = ts('Please enter a valid number value.');
347 if (!CRM_Utils_Rule
::money($fields["value"])) {
348 $errors['value'] = ts('Please enter a valid value.');
353 if (!CRM_Utils_Rule
::date($fields["value"])) {
354 $errors['value'] = ts('Please enter a valid date using YYYY-MM-DD format. Example: 2004-12-31.');
359 if (!CRM_Utils_Rule
::integer($fields["value"]) &&
360 ($fields["value"] != '1' ||
$fields["value"] != '0')
362 $errors['value'] = ts('Please enter 1 or 0 as value.');
367 if (!empty($fields["value"])) {
368 $params = array(1 => array($fields['value'], 'String'));
369 $query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1";
370 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
371 $errors['value'] = ts('Invalid default value for country.');
376 case 'StateProvince':
377 if (!empty($fields["value"])) {
378 $params = array(1 => array($fields['value'], 'String'));
381 FROM civicrm_state_province
383 OR abbreviation = %1";
384 if (CRM_Core_DAO
::singleValueQuery($query, $params) <= 0) {
385 $errors['value'] = ts('The invalid value for State/Province data type');
392 return empty($errors) ?
TRUE : $errors;
403 public function postProcess() {
404 // store the submitted values in an array
405 $params = $this->controller
->exportValues('Option');
407 if ($this->_action
== CRM_Core_Action
::DELETE
) {
408 $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id
));
409 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
410 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $this->_id
, $fieldValues);
411 CRM_Core_BAO_CustomOption
::del($this->_id
);
412 CRM_Core_Session
::setStatus(ts('Option "%1" has been deleted.', array(1 => $option['label'])), ts('Deleted'), 'success');
416 // set values for custom field properties and save
417 $customOption = new CRM_Core_DAO_OptionValue();
418 $customOption->label
= $params['label'];
419 $customOption->name
= CRM_Utils_String
::titleToVar($params['label']);
420 $customOption->weight
= $params['weight'];
421 $customOption->value
= $params['value'];
422 $customOption->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);
426 $customOption->id
= $this->_id
;
427 CRM_Core_BAO_CustomOption
::updateCustomValues($params);
428 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'weight', 'id');
431 $fieldValues = array('option_group_id' => $this->_optionGroupID
);
432 $customOption->weight
=
433 CRM_Utils_Weight
::updateOtherWeights(
434 'CRM_Core_DAO_OptionValue',
439 $customOption->option_group_id
= $this->_optionGroupID
;
441 $customField = new CRM_Core_DAO_CustomField();
442 $customField->id
= $this->_fid
;
444 $customField->find(TRUE) &&
446 $customField->html_type
== 'CheckBox' ||
447 $customField->html_type
== 'AdvMulti-Select' ||
448 $customField->html_type
== 'Multi-Select'
452 CRM_Core_DAO
::VALUE_SEPARATOR
,
453 substr($customField->default_value
, 1, -1)
455 if (!empty($params['default_value'])) {
456 if (!in_array($customOption->value
, $defVal)) {
457 if (empty($defVal[0])) {
458 $defVal = array($customOption->value
);
461 $defVal[] = $customOption->value
;
463 $customField->default_value
=
464 CRM_Core_DAO
::VALUE_SEPARATOR
.
465 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defVal) .
466 CRM_Core_DAO
::VALUE_SEPARATOR
;
467 $customField->save();
470 elseif (in_array($customOption->value
, $defVal)) {
472 foreach ($defVal as $v) {
473 if ($v != $customOption->value
) {
478 $customField->default_value
=
479 CRM_Core_DAO
::VALUE_SEPARATOR
.
480 implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $tempVal) .
481 CRM_Core_DAO
::VALUE_SEPARATOR
;
482 $customField->save();
486 switch ($customField->data_type
) {
488 $customOption->value
= CRM_Utils_Rule
::cleanMoney($customOption->value
);
492 $customOption->value
= intval($customOption->value
);
496 $customOption->value
= floatval($customOption->value
);
500 if (!empty($params['default_value'])) {
501 $customField->default_value
= $customOption->value
;
502 $customField->save();
504 elseif ($customField->find(TRUE) && $customField->default_value
== $customOption->value
) {
505 // this is the case where this option is the current default value and we have been reset
506 $customField->default_value
= 'null';
507 $customField->save();
511 $customOption->save();
513 $msg = ts('Your multiple choice option \'%1\' has been saved', array(1 => $customOption->label
));
514 CRM_Core_Session
::setStatus($msg, '', 'success');
515 $buttonName = $this->controller
->getButtonName();
516 $session = CRM_Core_Session
::singleton();
517 if ($buttonName == $this->getButtonName('next', 'new')) {
518 CRM_Core_Session
::setStatus(ts('You can add another option.'), '', 'info');
519 $session->replaceUserContext(
520 CRM_Utils_System
::url(
521 'civicrm/admin/custom/group/field/option',
522 'reset=1&action=add&fid=' . $this->_fid
. '&gid=' . $this->_gid