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