Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * form to process actions on the field aspect of Custom | |
38 | */ | |
39 | class CRM_Custom_Form_Option extends CRM_Core_Form { | |
40 | ||
41 | /** | |
42 | * the custom field id saved to the session for an update | |
43 | * | |
44 | * @var int | |
45 | * @access protected | |
46 | */ | |
47 | protected $_fid; | |
48 | ||
49 | /** | |
50 | * the custom group id saved to the session for an update | |
51 | * | |
52 | * @var int | |
53 | * @access protected | |
54 | */ | |
55 | protected $_gid; | |
56 | ||
57 | /** | |
58 | * The option group ID | |
59 | */ | |
60 | protected $_optionGroupID = NULL; | |
61 | ||
62 | /** | |
63 | * The Option id, used when editing the Option | |
64 | * | |
65 | * @var int | |
66 | * @access protected | |
67 | */ | |
68 | protected $_id; | |
69 | ||
70 | /** | |
71 | * Function to set variables up before form is built | |
72 | * | |
73 | * @param null | |
74 | * | |
75 | * @return void | |
76 | * @access public | |
77 | */ | |
78 | public function preProcess() { | |
79 | $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this); | |
80 | ||
81 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this); | |
d06700a7 | 82 | |
6a488035 | 83 | if (!isset($this->_gid) && $this->_fid) { |
7688c6be DL |
84 | $this->_gid = CRM_Core_DAO::getFieldValue( |
85 | 'CRM_Core_DAO_CustomField', | |
6a488035 TO |
86 | $this->_fid, |
87 | 'custom_group_id' | |
88 | ); | |
89 | } | |
7688c6be | 90 | |
6a488035 | 91 | if ($this->_fid) { |
7688c6be DL |
92 | $this->_optionGroupID = CRM_Core_DAO::getFieldValue( |
93 | 'CRM_Core_DAO_CustomField', | |
6a488035 TO |
94 | $this->_fid, |
95 | 'option_group_id' | |
96 | ); | |
97 | } | |
98 | ||
7688c6be DL |
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."); | |
101 | } | |
102 | ||
6a488035 TO |
103 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
104 | } | |
105 | ||
106 | /** | |
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 | |
109 | * | |
110 | * @param null | |
111 | * | |
112 | * @return array array of default values | |
113 | * @access public | |
114 | */ | |
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); | |
120 | ||
121 | $paramsField = array('id' => $this->_fid); | |
122 | CRM_Core_BAO_CustomField::retrieve($paramsField, $fieldDefaults); | |
123 | ||
124 | if ($fieldDefaults['html_type'] == 'CheckBox' | |
125 | || $fieldDefaults['html_type'] == 'Multi-Select' | |
126 | || $fieldDefaults['html_type'] == 'AdvMulti-Select' | |
127 | ) { | |
a7488080 | 128 | if (!empty($fieldDefaults['default_value'])) { |
6a488035 TO |
129 | $defaultCheckValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
130 | substr($fieldDefaults['default_value'], 1, -1) | |
131 | ); | |
132 | if (in_array($defaults['value'], $defaultCheckValues)) { | |
133 | $defaults['default_value'] = 1; | |
134 | } | |
135 | } | |
136 | } | |
137 | else { | |
138 | if (CRM_Utils_Array::value('default_value', $fieldDefaults) == CRM_Utils_Array::value('value', $defaults)) { | |
139 | $defaults['default_value'] = 1; | |
140 | } | |
141 | } | |
142 | } | |
143 | else { | |
144 | $defaults['is_active'] = 1; | |
145 | } | |
146 | ||
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); | |
150 | } | |
151 | ||
152 | return $defaults; | |
153 | } | |
154 | ||
155 | /** | |
156 | * Function to actually build the form | |
157 | * | |
158 | * @param null | |
159 | * | |
160 | * @return void | |
161 | * @access public | |
162 | */ | |
163 | public function buildQuickForm() { | |
164 | if ($this->_action == CRM_Core_Action::DELETE) { | |
c31db174 CW |
165 | $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id)); |
166 | $this->assign('label', $option['label']); | |
6a488035 TO |
167 | $this->addButtons(array( |
168 | array( | |
169 | 'type' => 'next', | |
170 | 'name' => ts('Delete'), | |
171 | 'isDefault' => TRUE, | |
172 | ), | |
173 | array( | |
174 | 'type' => 'cancel', | |
175 | 'name' => ts('Cancel'), | |
176 | ), | |
177 | ) | |
178 | ); | |
179 | } | |
180 | else { | |
181 | // lets trim all the whitespace | |
182 | $this->applyFilter('__ALL__', 'trim'); | |
183 | ||
184 | // hidden Option Id for validation use | |
185 | $this->add('hidden', 'optionId', $this->_id); | |
186 | ||
187 | //hidden field ID for validation use | |
188 | $this->add('hidden', 'fieldId', $this->_fid); | |
189 | ||
190 | // label | |
191 | $this->add('text', 'label', ts('Option Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'), TRUE); | |
192 | ||
193 | $this->add('text', 'value', ts('Option Value'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'value'), TRUE); | |
194 | ||
195 | // weight | |
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'); | |
198 | ||
199 | // is active ? | |
200 | $this->add('checkbox', 'is_active', ts('Active?')); | |
201 | ||
202 | // Set the default value for Custom Field | |
203 | $this->add('checkbox', 'default_value', ts('Default')); | |
204 | ||
205 | // add a custom form rule | |
206 | $this->addFormRule(array('CRM_Custom_Form_Option', 'formRule'), $this); | |
207 | ||
208 | // add buttons | |
209 | $this->addButtons(array( | |
210 | array( | |
211 | 'type' => 'next', | |
212 | 'name' => ts('Save'), | |
213 | 'isDefault' => TRUE, | |
214 | ), | |
215 | array( | |
216 | 'type' => 'next', | |
217 | 'name' => ts('Save and New'), | |
218 | 'subName' => 'new', | |
219 | ), | |
220 | array( | |
221 | 'type' => 'cancel', | |
222 | 'name' => ts('Cancel'), | |
223 | ), | |
224 | ) | |
225 | ); | |
226 | ||
227 | ||
228 | // if view mode pls freeze it with the done button. | |
229 | if ($this->_action & CRM_Core_Action::VIEW) { | |
230 | $this->freeze(); | |
231 | $url = CRM_Utils_System::url('civicrm/admin/custom/group/field/option', | |
232 | 'reset=1&action=browse&fid=' . $this->_fid . '&gid=' . $this->_gid, | |
233 | TRUE, NULL, FALSE | |
234 | ); | |
235 | $this->addElement('button', | |
236 | 'done', | |
237 | ts('Done'), | |
238 | array('onclick' => "location.href='$url'", 'class' => 'form-submit') | |
239 | ); | |
240 | } | |
241 | } | |
242 | $this->assign('id', $this->_id); | |
243 | } | |
244 | ||
245 | /** | |
246 | * global validation rules for the form | |
247 | * | |
248 | * @param array $fields posted values of the form | |
249 | * | |
250 | * @return array list of errors to be posted back to the form | |
251 | * @static | |
252 | * @access public | |
253 | */ | |
254 | static function formRule($fields, $files, $form) { | |
3df236f6 ML |
255 | $optionLabel = $fields['label']; |
256 | $optionValue = $fields['value']; | |
6a488035 TO |
257 | $fieldId = $form->_fid; |
258 | $optionGroupId = $form->_optionGroupID; | |
259 | ||
260 | $temp = array(); | |
261 | if (empty($form->_id)) { | |
262 | $query = " | |
361fb6c0 | 263 | SELECT count(*) |
6a488035 TO |
264 | FROM civicrm_option_value |
265 | WHERE option_group_id = %1 | |
266 | AND label = %2"; | |
3df236f6 ML |
267 | $params = array( |
268 | 1 => array($optionGroupId, 'Integer'), | |
6a488035 TO |
269 | 2 => array($optionLabel, 'String'), |
270 | ); | |
271 | if (CRM_Core_DAO::singleValueQuery($query, $params) > 0) { | |
272 | $errors['label'] = ts('There is an entry with the same label.'); | |
273 | } | |
274 | ||
275 | $query = " | |
361fb6c0 | 276 | SELECT count(*) |
6a488035 TO |
277 | FROM civicrm_option_value |
278 | WHERE option_group_id = %1 | |
279 | AND value = %2"; | |
3df236f6 ML |
280 | $params = array( |
281 | 1 => array($optionGroupId, 'Integer'), | |
6a488035 TO |
282 | 2 => array($optionValue, 'String'), |
283 | ); | |
284 | if (CRM_Core_DAO::singleValueQuery($query, $params) > 0) { | |
285 | $errors['value'] = ts('There is an entry with the same value.'); | |
286 | } | |
287 | } | |
288 | else { | |
289 | //capture duplicate entries while updating Custom Options | |
290 | $optionId = CRM_Utils_Type::escape($fields['optionId'], 'Integer'); | |
291 | ||
292 | //check label duplicates within a custom field | |
293 | $query = " | |
361fb6c0 | 294 | SELECT count(*) |
6a488035 TO |
295 | FROM civicrm_option_value |
296 | WHERE option_group_id = %1 | |
297 | AND id != %2 | |
298 | AND label = %3"; | |
3df236f6 ML |
299 | $params = array( |
300 | 1 => array($optionGroupId, 'Integer'), | |
6a488035 TO |
301 | 2 => array($optionId, 'Integer'), |
302 | 3 => array($optionLabel, 'String'), | |
303 | ); | |
304 | if (CRM_Core_DAO::singleValueQuery($query, $params) > 0) { | |
305 | $errors['label'] = ts('There is an entry with the same label.'); | |
306 | } | |
307 | ||
308 | //check value duplicates within a custom field | |
309 | $query = " | |
361fb6c0 | 310 | SELECT count(*) |
6a488035 TO |
311 | FROM civicrm_option_value |
312 | WHERE option_group_id = %1 | |
313 | AND id != %2 | |
314 | AND value = %3"; | |
3df236f6 ML |
315 | $params = array( |
316 | 1 => array($optionGroupId, 'Integer'), | |
6a488035 TO |
317 | 2 => array($optionId, 'Integer'), |
318 | 3 => array($optionValue, 'String'), | |
319 | ); | |
320 | if (CRM_Core_DAO::singleValueQuery($query, $params) > 0) { | |
321 | $errors['value'] = ts('There is an entry with the same value.'); | |
322 | } | |
323 | } | |
324 | ||
325 | $query = " | |
361fb6c0 | 326 | SELECT data_type |
6a488035 TO |
327 | FROM civicrm_custom_field |
328 | WHERE id = %1"; | |
329 | $params = array(1 => array($fieldId, 'Integer')); | |
330 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
331 | if ($dao->fetch()) { | |
332 | switch ($dao->data_type) { | |
333 | case 'Int': | |
334 | if (!CRM_Utils_Rule::integer($fields["value"])) { | |
335 | $errors['value'] = ts('Please enter a valid integer value.'); | |
336 | } | |
337 | break; | |
338 | ||
339 | case 'Float': | |
340 | // case 'Money': | |
341 | if (!CRM_Utils_Rule::numeric($fields["value"])) { | |
342 | $errors['value'] = ts('Please enter a valid number value.'); | |
343 | } | |
344 | break; | |
345 | ||
346 | case 'Money': | |
347 | if (!CRM_Utils_Rule::money($fields["value"])) { | |
348 | $errors['value'] = ts('Please enter a valid value.'); | |
349 | } | |
350 | break; | |
351 | ||
352 | case 'Date': | |
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.'); | |
355 | } | |
356 | break; | |
357 | ||
358 | case 'Boolean': | |
359 | if (!CRM_Utils_Rule::integer($fields["value"]) && | |
360 | ($fields["value"] != '1' || $fields["value"] != '0') | |
361 | ) { | |
362 | $errors['value'] = ts('Please enter 1 or 0 as value.'); | |
363 | } | |
364 | break; | |
365 | ||
366 | case 'Country': | |
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.'); | |
372 | } | |
373 | } | |
374 | break; | |
375 | ||
376 | case 'StateProvince': | |
377 | if (!empty($fields["value"])) { | |
378 | $params = array(1 => array($fields['value'], 'String')); | |
379 | $query = " | |
361fb6c0 | 380 | SELECT count(*) |
6a488035 TO |
381 | FROM civicrm_state_province |
382 | WHERE name = %1 | |
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'); | |
386 | } | |
387 | } | |
388 | break; | |
389 | } | |
390 | } | |
391 | ||
392 | return empty($errors) ? TRUE : $errors; | |
393 | } | |
394 | ||
395 | /** | |
396 | * Process the form | |
397 | * | |
398 | * @param null | |
399 | * | |
400 | * @return void | |
401 | * @access public | |
402 | */ | |
403 | public function postProcess() { | |
404 | // store the submitted values in an array | |
405 | $params = $this->controller->exportValues('Option'); | |
406 | ||
407 | if ($this->_action == CRM_Core_Action::DELETE) { | |
c31db174 | 408 | $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id)); |
6a488035 | 409 | $fieldValues = array('option_group_id' => $this->_optionGroupID); |
c31db174 | 410 | CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues); |
6a488035 | 411 | CRM_Core_BAO_CustomOption::del($this->_id); |
c31db174 | 412 | CRM_Core_Session::setStatus(ts('Option "%1" has been deleted.', array(1 => $option['label'])), ts('Deleted'), 'success'); |
6a488035 TO |
413 | return; |
414 | } | |
415 | ||
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); | |
423 | ||
424 | $oldWeight = NULL; | |
425 | if ($this->_id) { | |
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'); | |
429 | } | |
430 | ||
431 | $fieldValues = array('option_group_id' => $this->_optionGroupID); | |
361fb6c0 DL |
432 | $customOption->weight = |
433 | CRM_Utils_Weight::updateOtherWeights( | |
434 | 'CRM_Core_DAO_OptionValue', | |
435 | $oldWeight, | |
436 | $params['weight'], | |
437 | $fieldValues); | |
6a488035 TO |
438 | |
439 | $customOption->option_group_id = $this->_optionGroupID; | |
440 | ||
441 | $customField = new CRM_Core_DAO_CustomField(); | |
442 | $customField->id = $this->_fid; | |
361fb6c0 DL |
443 | if ( |
444 | $customField->find(TRUE) && | |
445 | ( | |
446 | $customField->html_type == 'CheckBox' || | |
6a488035 TO |
447 | $customField->html_type == 'AdvMulti-Select' || |
448 | $customField->html_type == 'Multi-Select' | |
449 | ) | |
450 | ) { | |
361fb6c0 DL |
451 | $defVal = explode( |
452 | CRM_Core_DAO::VALUE_SEPARATOR, | |
6a488035 TO |
453 | substr($customField->default_value, 1, -1) |
454 | ); | |
a7488080 | 455 | if (!empty($params['default_value'])) { |
6a488035 TO |
456 | if (!in_array($customOption->value, $defVal)) { |
457 | if (empty($defVal[0])) { | |
458 | $defVal = array($customOption->value); | |
459 | } | |
460 | else { | |
461 | $defVal[] = $customOption->value; | |
462 | } | |
361fb6c0 DL |
463 | $customField->default_value = |
464 | CRM_Core_DAO::VALUE_SEPARATOR . | |
465 | implode(CRM_Core_DAO::VALUE_SEPARATOR, $defVal) . | |
466 | CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
467 | $customField->save(); |
468 | } | |
469 | } | |
470 | elseif (in_array($customOption->value, $defVal)) { | |
471 | $tempVal = array(); | |
472 | foreach ($defVal as $v) { | |
473 | if ($v != $customOption->value) { | |
474 | $tempVal[] = $v; | |
475 | } | |
476 | } | |
477 | ||
361fb6c0 DL |
478 | $customField->default_value = |
479 | CRM_Core_DAO::VALUE_SEPARATOR . | |
480 | implode(CRM_Core_DAO::VALUE_SEPARATOR, $tempVal) . | |
481 | CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
482 | $customField->save(); |
483 | } | |
484 | } | |
485 | else { | |
486 | switch ($customField->data_type) { | |
487 | case 'Money': | |
488 | $customOption->value = CRM_Utils_Rule::cleanMoney($customOption->value); | |
489 | break; | |
490 | ||
491 | case 'Int': | |
492 | $customOption->value = intval($customOption->value); | |
493 | break; | |
494 | ||
495 | case 'Float': | |
496 | $customOption->value = floatval($customOption->value); | |
497 | break; | |
498 | } | |
499 | ||
a7488080 | 500 | if (!empty($params['default_value'])) { |
6a488035 TO |
501 | $customField->default_value = $customOption->value; |
502 | $customField->save(); | |
503 | } | |
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(); | |
508 | } | |
509 | } | |
510 | ||
511 | $customOption->save(); | |
512 | ||
361fb6c0 DL |
513 | $msg = ts('Your multiple choice option \'%1\' has been saved', array(1 => $customOption->label)); |
514 | CRM_Core_Session::setStatus($msg, '', 'success'); | |
6a488035 TO |
515 | $buttonName = $this->controller->getButtonName(); |
516 | $session = CRM_Core_Session::singleton(); | |
517 | if ($buttonName == $this->getButtonName('next', 'new')) { | |
361fb6c0 DL |
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', | |
6a488035 | 522 | 'reset=1&action=add&fid=' . $this->_fid . '&gid=' . $this->_gid |
361fb6c0 DL |
523 | ) |
524 | ); | |
6a488035 TO |
525 | } |
526 | } | |
527 | } | |
528 |