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