Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Business objects for managing price fields. | |
38 | * | |
39 | */ | |
40 | class CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field extends CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field { | |
41 | ||
42 | protected $_options; | |
43 | ||
44 | /** | |
45 | * takes an associative array and creates a price field object | |
46 | * | |
47 | * the function extract all the params it needs to initialize the create a | |
48 | * price field object. the params array could contain additional unused name/value | |
49 | * pairs | |
50 | * | |
51 | * @param array $params (reference ) an assoc array of name/value pairs | |
52 | * @param array $ids the array that holds all the db ids | |
53 | * | |
54 | * @return object CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field object | |
55 | * @access public | |
56 | * @static | |
57 | */ | |
58 | static function &add(&$params) { | |
59 | $priceFieldBAO = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field(); | |
60 | ||
61 | $priceFieldBAO->copyValues($params); | |
62 | ||
63 | if ($id = CRM_Utils_Array::value('id', $params)) { | |
64 | $priceFieldBAO->id = $id; | |
65 | } | |
66 | ||
67 | $priceFieldBAO->save(); | |
68 | return $priceFieldBAO; | |
69 | } | |
70 | ||
71 | /** | |
72 | * takes an associative array and creates a price field object | |
73 | * | |
74 | * This function is invoked from within the web form layer and also from the api layer | |
75 | * | |
76 | * @param array $params (reference) an assoc array of name/value pairs | |
77 | * | |
78 | * @return object CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field object | |
79 | * @access public | |
80 | * @static | |
81 | */ | |
82 | static function create(&$params) { | |
83 | ||
84 | $transaction = new CRM_Core_Transaction(); | |
85 | ||
86 | $priceField = self::add($params); | |
87 | ||
88 | if (is_a($priceField, 'CRM_Core_Error')) { | |
89 | $transaction->rollback(); | |
90 | return $priceField; | |
91 | } | |
92 | ||
93 | $options = $optionsIds = array(); | |
94 | $maxIndex = CRM_Price_Form_Field::NUM_OPTION; | |
95 | ||
96 | if ($priceField->html_type == 'Text') { | |
97 | $maxIndex = 1; | |
98 | ||
99 | $fieldValue = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue(); | |
100 | $fieldValue->price_field_id = $priceField->id; | |
101 | ||
102 | // update previous field values( if any ) | |
103 | if ($fieldValue->find(TRUE)) { | |
104 | $optionsIds['id'] = $fieldValue->id; | |
105 | } | |
106 | } | |
107 | $defaultArray = array(); | |
108 | if ($params['html_type'] == 'CheckBox' && isset($params['default_checkbox_option'])) { | |
109 | $tempArray = array_keys($params['default_checkbox_option']); | |
110 | foreach ($tempArray as $v) { | |
111 | if ($params['option_amount'][$v]) { | |
112 | $defaultArray[$v] = 1; | |
113 | } | |
114 | } | |
115 | } | |
116 | else { | |
117 | if (CRM_Utils_Array::value('default_option', $params) | |
118 | ) { | |
119 | $defaultArray[$params['default_option']] = 1; | |
120 | } | |
121 | } | |
122 | ||
123 | for ($index = 1; $index <= $maxIndex; $index++) { | |
124 | ||
10824d34 | 125 | if (array_key_exists('option_amount', $params) && |
126 | array_key_exists($index, $params['option_amount']) && | |
127 | (CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_label', $params)) || | |
6a488035 TO |
128 | CRM_Utils_Array::value('is_quick_config', $params)) && |
129 | !CRM_Utils_System::isNull($params['option_amount'][$index]) | |
130 | ) { | |
131 | $options = array( | |
132 | 'price_field_id' => $priceField->id, | |
133 | 'label' => trim($params['option_label'][$index]), | |
134 | 'name' => CRM_Utils_String::munge($params['option_label'][$index], '_', 64), | |
135 | 'amount' => CRM_Utils_Rule::cleanMoney(trim($params['option_amount'][$index])), | |
136 | 'count' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_count', $params), NULL), | |
137 | 'max_value' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_max_value', $params), NULL), | |
138 | 'description' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_description', $params), NULL), | |
139 | 'membership_type_id' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('membership_type_id', $params), NULL), | |
140 | 'weight' => $params['option_weight'][$index], | |
141 | 'is_active' => 1, | |
142 | 'is_default' => CRM_Utils_Array::value($params['option_weight'][$index], $defaultArray) ? $defaultArray[$params['option_weight'][$index]] : 0, | |
143 | ); | |
144 | ||
145 | if ($opIds = CRM_Utils_Array::value('option_id', $params)) { | |
146 | if ($opId = CRM_Utils_Array::value($index, $opIds)) { | |
147 | $optionsIds['id'] = $opId; | |
148 | } | |
149 | else $optionsIds['id'] = NULL; | |
150 | } | |
151 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::create($options, $optionsIds); | |
152 | } | |
153 | } | |
154 | ||
155 | $transaction->commit(); | |
156 | return $priceField; | |
157 | } | |
158 | ||
159 | /** | |
160 | * Takes a bunch of params that are needed to match certain criteria and | |
161 | * retrieves the relevant objects. Typically the valid params are only | |
162 | * contact_id. We'll tweak this function to be more full featured over a period | |
163 | * of time. This is the inverse function of create. It also stores all the retrieved | |
164 | * values in the default array | |
165 | * | |
166 | * @param array $params (reference ) an assoc array of name/value pairs | |
167 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
168 | * | |
169 | * @return object CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field object | |
170 | * @access public | |
171 | * @static | |
172 | */ | |
173 | static function retrieve(&$params, &$defaults) { | |
174 | return CRM_Core_DAO::commonRetrieve('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $params, $defaults); | |
175 | } | |
176 | ||
177 | /** | |
178 | * update the is_active flag in the db | |
179 | * | |
180 | * @param int $id Id of the database record | |
181 | * @param boolean $is_active Value we want to set the is_active field | |
182 | * | |
183 | * @return Object DAO object on sucess, null otherwise | |
184 | * | |
185 | * @access public | |
186 | * @static | |
187 | */ | |
188 | static function setIsActive($id, $is_active) { | |
189 | return CRM_Core_DAO::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $id, 'is_active', $is_active); | |
190 | } | |
191 | ||
192 | /** | |
193 | * Get the field title. | |
194 | * | |
195 | * @param int $id id of field. | |
196 | * | |
197 | * @return string name | |
198 | * | |
199 | * @access public | |
200 | * @static | |
201 | * | |
202 | */ | |
203 | public static function getTitle($id) { | |
204 | return CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $id, 'label'); | |
205 | } | |
206 | ||
207 | /** | |
208 | * This function for building custom fields | |
209 | * | |
210 | * @param object $qf form object (reference) | |
211 | * @param string $elementName name of the custom field | |
212 | * @param boolean $inactiveNeeded | |
213 | * @param boolean $useRequired true if required else false | |
214 | * @param boolean $search true if used for search else false | |
215 | * @param string $label label for custom field | |
216 | * | |
217 | * @access public | |
218 | * @static | |
219 | */ | |
220 | public static function addQuickFormElement(&$qf, | |
221 | $elementName, | |
222 | $fieldId, | |
223 | $inactiveNeeded, | |
224 | $useRequired = TRUE, | |
225 | $label = NULL, | |
226 | $fieldOptions = NULL, | |
227 | $feezeOptions = array() | |
228 | ) { | |
229 | ||
230 | $field = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field(); | |
231 | $field->id = $fieldId; | |
232 | if (!$field->find(TRUE)) { | |
233 | /* FIXME: failure! */ | |
234 | ||
235 | return NULL; | |
236 | } | |
237 | ||
238 | $otherAmount = $qf->get('values'); | |
239 | $config = CRM_Core_Config::singleton(); | |
240 | $qf->assign('currencySymbol', CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Currency', $config->defaultCurrency, 'symbol', 'name')); | |
241 | // get currency name for price field and option attributes | |
242 | $currencyName = $config->defaultCurrency; | |
243 | ||
244 | if (!isset($label)) { | |
245 | $label = (property_exists($qf,'_membershipBlock') && CRM_Utils_Array::value('is_separate_payment', $qf->_membershipBlock) && $field->name == 'contribution_amount' && !CRM_Utils_Array::value('is_allow_other_amount', $otherAmount)) ? ts('Additional Contribution') : $field->label; | |
246 | } | |
247 | ||
248 | if ($field->name == 'contribution_amount') { | |
249 | $qf->_contributionAmount = 1; | |
250 | } | |
251 | ||
252 | if (isset($qf->_online) && $qf->_online) { | |
253 | $useRequired = FALSE; | |
254 | } | |
255 | ||
256 | $customOption = $fieldOptions; | |
257 | if (!is_array($customOption)) { | |
258 | $customOption = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::getOptions($field->id, $inactiveNeeded); | |
259 | } | |
260 | ||
261 | //use value field. | |
262 | $valueFieldName = 'amount'; | |
263 | $seperator = '|'; | |
264 | switch ($field->html_type) { | |
265 | case 'Text': | |
266 | $optionKey = key($customOption); | |
267 | $count = CRM_Utils_Array::value('count', $customOption[$optionKey], ''); | |
268 | $max_value = CRM_Utils_Array::value('max_value', $customOption[$optionKey], ''); | |
269 | $priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName], $count, $max_value)); | |
270 | ||
271 | $extra = array(); | |
272 | if (property_exists($qf,'_quickConfig') && $qf->_quickConfig && property_exists($qf,'_contributionAmount') && $qf->_contributionAmount) { | |
273 | $qf->assign('priceset', $elementName); | |
274 | $extra = array('onclick' => 'useAmountOther();'); | |
275 | } | |
276 | ||
277 | // if seperate membership payment is used with quick config priceset then change the other amount label | |
278 | if (property_exists($qf,'_membershipBlock') && CRM_Utils_Array::value('is_separate_payment', $qf->_membershipBlock) && $qf->_quickConfig && $field->name == 'other_amount' && !property_exists($qf,'_contributionAmount')) { | |
279 | $label = ts('Additional Contribution'); | |
280 | $useRequired = 0; | |
281 | } elseif (CRM_Utils_Array::value('label', $fieldOptions[$optionKey])) { //check for label. | |
282 | $label = $fieldOptions[$optionKey]['label']; | |
283 | } | |
284 | ||
285 | if ($field->is_display_amounts) { | |
286 | $label .= ' - '; | |
287 | $label .= CRM_Utils_Money::format(CRM_Utils_Array::value($valueFieldName, $customOption[$optionKey])); | |
288 | } | |
289 | ||
290 | $element = &$qf->add('text', $elementName, $label, | |
291 | array_merge($extra, | |
292 | array('price' => json_encode(array($optionKey, $priceVal)), | |
293 | 'size' => '4', | |
294 | ) | |
295 | ), | |
296 | $useRequired && $field->is_required | |
297 | ); | |
298 | ||
299 | // CRM-6902 | |
300 | if (in_array($optionKey, $feezeOptions)) { | |
301 | $element->freeze(); | |
302 | } | |
303 | ||
304 | //CRM-10117 | |
305 | if (property_exists($qf, '_quickConfig') && $qf->_quickConfig) { | |
306 | $message = ts("Please enter a valid amount."); | |
307 | $type = "money"; | |
308 | } else { | |
309 | $message = ts('%1 must be an integer (whole number).', array(1 => $label)); | |
310 | $type = "positiveInteger"; | |
311 | } | |
312 | // integers will have numeric rule applied to them. | |
313 | $qf->addRule($elementName, $message, $type); | |
314 | break; | |
315 | ||
316 | case 'Radio': | |
317 | $choice = array(); | |
318 | ||
319 | if (property_exists($qf, '_quickConfig') && $qf->_quickConfig && property_exists($qf,'_contributionAmount') && $qf->_contributionAmount) { | |
320 | $qf->assign('contriPriceset', $elementName); | |
321 | } | |
322 | ||
323 | foreach ($customOption as $opId => $opt) { | |
324 | if ($field->is_display_amounts) { | |
325 | $opt['label'] = CRM_Utils_Array::value('label', $opt) ? $opt['label'] . ' - ' : ''; | |
326 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); | |
327 | } | |
328 | $count = CRM_Utils_Array::value('count', $opt, ''); | |
329 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); | |
330 | $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); | |
331 | $extra = array('price' => json_encode(array($elementName, $priceVal)), | |
332 | 'data-amount' => $opt[$valueFieldName], | |
333 | 'data-currency' => $currencyName, | |
334 | ); | |
335 | if (property_exists($qf, '_quickConfig') && $qf->_quickConfig && $field->name == 'contribution_amount') { | |
336 | $extra += array('onclick' => 'clearAmountOther();'); | |
337 | } elseif (property_exists($qf, '_quickConfig') && $qf->_quickConfig && $field->name == 'membership_amount') { | |
338 | $extra += array('onclick' => "return showHideAutoRenew({$opt['membership_type_id']});", | |
339 | 'membership-type' => $opt['membership_type_id'], | |
340 | ); | |
341 | $qf->assign('membershipFieldID',$field->id); | |
342 | } | |
343 | $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra); | |
344 | ||
345 | // CRM-6902 | |
346 | if (in_array($opId, $feezeOptions)) { | |
347 | $choice[$opId]->freeze(); | |
348 | } | |
349 | } | |
350 | ||
351 | if (property_exists($qf, '_membershipBlock') && CRM_Utils_Array::value('is_separate_payment', $qf->_membershipBlock) && $field->name == 'contribution_amount') { | |
352 | $choice[] = $qf->createElement('radio', NULL, '', 'No thank you', '-1', | |
353 | array( | |
354 | 'onclick' => 'clearAmountOther();', | |
355 | ) | |
356 | ); | |
357 | } | |
358 | ||
359 | if (!$field->is_required) { | |
360 | // add "none" option | |
361 | if (CRM_Utils_Array::value('is_allow_other_amount', $otherAmount) && $field->name == 'contribution_amount') { | |
362 | $none = ts('Other Amount'); | |
363 | } elseif (property_exists($qf, '_membershipBlock') && !CRM_Utils_Array::value('is_required', $qf->_membershipBlock) && $field->name == 'membership_amount') { | |
364 | $none = ts('No thank you'); | |
365 | } else { | |
366 | $none = ts('-none-'); | |
367 | } | |
368 | ||
369 | $choice[] = $qf->createElement('radio', NULL, '', $none, '0', | |
370 | array('price' => json_encode(array($elementName, "0"))) | |
371 | ); | |
372 | } | |
373 | ||
374 | $element = &$qf->addGroup($choice, $elementName, $label); | |
375 | ||
376 | // make contribution field required for quick config when membership block is enabled | |
377 | if (($field->name == 'contribution_amount' || $field->name == 'membership_amount') && property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock) && !$field->is_required) { | |
378 | $useRequired = $field->is_required = TRUE; | |
379 | } | |
380 | ||
381 | if ($useRequired && $field->is_required) { | |
382 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
383 | } | |
384 | break; | |
385 | ||
386 | case 'Select': | |
387 | $selectOption = $allowedOptions = $priceVal = array(); | |
388 | ||
389 | foreach ($customOption as $opt) { | |
390 | $count = CRM_Utils_Array::value('count', $opt, ''); | |
391 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); | |
392 | $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); | |
393 | ||
394 | if ($field->is_display_amounts) { | |
395 | $opt['label'] .= ' - '; | |
396 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); | |
397 | } | |
398 | $selectOption[$opt['id']] = $opt['label']; | |
399 | ||
400 | if (!in_array($opt['id'], $feezeOptions)) { | |
401 | $allowedOptions[] = $opt['id']; | |
402 | } | |
403 | } | |
404 | $element = &$qf->add('select', $elementName, $label, | |
405 | array( | |
406 | '' => ts('- select -')) + $selectOption, | |
407 | $useRequired && $field->is_required, | |
408 | array('price' => json_encode($priceVal)) | |
409 | ); | |
410 | ||
411 | // CRM-6902 | |
412 | $button = substr($qf->controller->getButtonName(), -4); | |
413 | if (!empty($feezeOptions) && $button != 'skip') { | |
414 | $qf->addRule($elementName, ts('Sorry, this option is currently sold out.'), 'regex', "/" . implode('|', $allowedOptions) . "/"); | |
415 | } | |
416 | break; | |
417 | ||
418 | case 'CheckBox': | |
419 | ||
420 | $check = array(); | |
421 | foreach ($customOption as $opId => $opt) { | |
422 | $count = CRM_Utils_Array::value('count', $opt, ''); | |
423 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); | |
424 | $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); | |
425 | ||
426 | if ($field->is_display_amounts) { | |
427 | $opt['label'] .= ' - '; | |
428 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); | |
429 | } | |
430 | $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'], | |
431 | array('price' => json_encode(array($opt['id'], $priceVal)), | |
432 | 'data-amount' => $opt[$valueFieldName], | |
433 | 'data-currency' => $currencyName, | |
434 | ) | |
435 | ); | |
436 | ||
437 | // CRM-6902 | |
438 | if (in_array($opId, $feezeOptions)) { | |
439 | $check[$opId]->freeze(); | |
440 | } | |
441 | } | |
442 | $element = &$qf->addGroup($check, $elementName, $label); | |
443 | if ($useRequired && $field->is_required) { | |
444 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
445 | } | |
446 | break; | |
447 | } | |
448 | if (isset($qf->_online) && $qf->_online) { | |
449 | $element->freeze(); | |
450 | } | |
451 | } | |
452 | ||
453 | /** | |
454 | * Retrieve a list of options for the specified field | |
455 | * | |
456 | * @param int $fieldId price field ID | |
457 | * @param bool $inactiveNeeded include inactive options | |
458 | * @param bool $reset ignore stored values\ | |
459 | * | |
460 | * @return array array of options | |
461 | */ | |
462 | public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE) { | |
463 | static $options = array(); | |
464 | ||
465 | if ($reset || empty($options[$fieldId])) { | |
466 | $values = array(); | |
467 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::getValues($fieldId, $values, 'weight', !$inactiveNeeded); | |
468 | $options[$fieldId] = $values; | |
469 | } | |
470 | ||
471 | return $options[$fieldId]; | |
472 | } | |
473 | ||
474 | public static function getOptionId($optionLabel, $fid) { | |
475 | if (!$optionLabel || !$fid) { | |
476 | return; | |
477 | } | |
478 | ||
479 | $optionGroupName = "civicrm_price_field.amount.{$fid}"; | |
480 | ||
481 | $query = " | |
482 | SELECT | |
483 | option_value.id as id | |
484 | FROM | |
485 | civicrm_option_value option_value, | |
486 | civicrm_option_group option_group | |
487 | WHERE | |
488 | option_group.name = %1 | |
489 | AND option_group.id = option_value.option_group_id | |
490 | AND option_value.label = %2"; | |
491 | ||
492 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($optionGroupName, 'String'), 2 => array($optionLabel, 'String'))); | |
493 | ||
494 | while ($dao->fetch()) { | |
495 | return $dao->id; | |
496 | } | |
497 | } | |
498 | ||
499 | /** | |
500 | * Delete the price set field. | |
501 | * | |
502 | * @param int $id Field Id | |
503 | * | |
504 | * @return boolean | |
505 | * | |
506 | * @access public | |
507 | * @static | |
508 | * | |
509 | */ | |
510 | public static function deleteField($id) { | |
511 | $field = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field(); | |
512 | $field->id = $id; | |
513 | ||
514 | if ($field->find(TRUE)) { | |
515 | // delete the options for this field | |
516 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::deleteValues($id); | |
517 | ||
518 | // reorder the weight before delete | |
519 | $fieldValues = array('price_set_id' => $field->price_set_id); | |
520 | ||
521 | CRM_Utils_Weight::delWeight('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $field->id, $fieldValues); | |
522 | ||
523 | // now delete the field | |
524 | return $field->delete(); | |
525 | } | |
526 | ||
527 | return NULL; | |
528 | } | |
529 | ||
530 | static function &htmlTypes() { | |
531 | static $htmlTypes = NULL; | |
532 | if (!$htmlTypes) { | |
533 | $htmlTypes = array( | |
534 | 'Text' => ts('Text / Numeric Quantity'), | |
535 | 'Select' => ts('Select'), | |
536 | 'Radio' => ts('Radio'), | |
537 | 'CheckBox' => ts('CheckBox'), | |
538 | ); | |
539 | } | |
540 | return $htmlTypes; | |
541 | } | |
542 | ||
543 | /** | |
544 | * Validate the priceset | |
545 | * | |
546 | * @param int $priceSetId, array $fields | |
547 | * | |
548 | * retrun the error string | |
549 | * | |
550 | * @access public | |
551 | * @static | |
552 | * | |
553 | */ | |
554 | ||
555 | public static function priceSetValidation($priceSetId, $fields, &$error) { | |
556 | // check for at least one positive | |
557 | // amount price field should be selected. | |
558 | $priceField = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field(); | |
559 | $priceField->price_set_id = $priceSetId; | |
560 | $priceField->find(); | |
561 | ||
562 | $priceFields = array(); | |
563 | ||
564 | while ($priceField->fetch()) { | |
565 | $key = "price_{$priceField->id}"; | |
566 | if (CRM_Utils_Array::value($key, $fields)) { | |
567 | $priceFields[$priceField->id] = $fields[$key]; | |
568 | } | |
569 | } | |
570 | ||
571 | if (!empty($priceFields)) { | |
572 | // we should has to have positive amount. | |
573 | $sql = " | |
574 | SELECT id, html_type | |
575 | FROM civicrm_price_field | |
576 | WHERE id IN (" . implode(',', array_keys($priceFields)) . ')'; | |
577 | $fieldDAO = CRM_Core_DAO::executeQuery($sql); | |
578 | $htmlTypes = array(); | |
579 | while ($fieldDAO->fetch()) { | |
580 | $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type; | |
581 | } | |
582 | ||
583 | $selectedAmounts = array(); | |
584 | ||
585 | foreach ($htmlTypes as $fieldId => $type) { | |
586 | $options = array(); | |
587 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::getValues($fieldId, $options); | |
588 | ||
589 | if (empty($options)) { | |
590 | ||
591 | continue; | |
592 | ||
593 | } | |
594 | ||
595 | if ($type == 'Text') { | |
596 | foreach ($options as $opId => $option) { | |
597 | $selectedAmounts[$opId] = $priceFields[$fieldId] * $option['amount']; | |
598 | break; | |
599 | } | |
600 | } | |
601 | elseif (is_array($fields["price_{$fieldId}"])) { | |
602 | foreach (array_keys($fields["price_{$fieldId}"]) as $opId) { | |
603 | $selectedAmounts[$opId] = $options[$opId]['amount']; | |
604 | } | |
605 | } | |
606 | elseif (in_array($fields["price_{$fieldId}"], array_keys($options))) { | |
607 | $selectedAmounts[$fields["price_{$fieldId}"]] = $options[$fields["price_{$fieldId}"]]['amount']; | |
608 | } | |
609 | } | |
610 | ||
611 | list($componentName) = explode(':', $fields['_qf_default']); | |
612 | // now we have all selected amount in hand. | |
613 | $totalAmount = array_sum($selectedAmounts); | |
614 | if ($totalAmount < 0) { | |
615 | $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', array(1 => $componentName)); | |
616 | } | |
617 | } | |
618 | else { | |
619 | $error['_qf_default'] = ts("Please select at least one option from price set."); | |
620 | } | |
621 | } | |
622 | } | |
623 |