More Fixes after merge.
[civicrm-core.git] / CRM / Price / BAO / PriceField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Business objects for managing price fields.
38 *
39 */
40 class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
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 *
53 * @internal param array $ids the array that holds all the db ids
54 *
55 * @return object CRM_Price_BAO_PriceField object
56 * @access public
57 * @static
58 */
59 static function add(&$params) {
60 $priceFieldBAO = new CRM_Price_BAO_PriceField();
61
62 $priceFieldBAO->copyValues($params);
63
64 if ($id = CRM_Utils_Array::value('id', $params)) {
65 $priceFieldBAO->id = $id;
66 }
67
68 $priceFieldBAO->save();
69 return $priceFieldBAO;
70 }
71
72 /**
73 * takes an associative array and creates a price field object
74 *
75 * This function is invoked from within the web form layer and also from the api layer
76 *
77 * @param array $params (reference) an assoc array of name/value pairs
78 *
79 * @return object CRM_Price_DAO_PriceField object
80 * @access public
81 * @static
82 */
83 static function create(&$params) {
84 if(empty($params['id']) && empty($params['name'])) {
85 $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242));
86 }
87 $transaction = new CRM_Core_Transaction();
88
89 $priceField = self::add($params);
90
91 if (is_a($priceField, 'CRM_Core_Error')) {
92 $transaction->rollback();
93 return $priceField;
94 }
95
96 $optionsIds = array();
97 $maxIndex = CRM_Price_Form_Field::NUM_OPTION;
98
99 if ($priceField->html_type == 'Text') {
100 $maxIndex = 1;
101
102 $fieldValue = new CRM_Price_DAO_PriceFieldValue();
103 $fieldValue->price_field_id = $priceField->id;
104
105 // update previous field values( if any )
106 if ($fieldValue->find(TRUE)) {
107 $optionsIds['id'] = $fieldValue->id;
108 }
109 }
110 $defaultArray = array();
111 //html type would be empty in update scenario not sure what would happen ...
112 if (!empty($params['html_type']) && $params['html_type'] == 'CheckBox' && isset($params['default_checkbox_option'])) {
113 $tempArray = array_keys($params['default_checkbox_option']);
114 foreach ($tempArray as $v) {
115 if ($params['option_amount'][$v]) {
116 $defaultArray[$v] = 1;
117 }
118 }
119 }
120 else {
121 if (!empty($params['default_option'])) {
122 $defaultArray[$params['default_option']] = 1;
123 }
124 }
125
126 for ($index = 1; $index <= $maxIndex; $index++) {
127 if (array_key_exists('option_amount', $params) &&
128 array_key_exists($index, $params['option_amount']) &&
129 (CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_label', $params)) || !empty($params['is_quick_config'])) &&
130 !CRM_Utils_System::isNull($params['option_amount'][$index])
131 ) {
132 $options = array(
133 'price_field_id' => $priceField->id,
134 'label' => trim($params['option_label'][$index]),
135 'name' => CRM_Utils_String::munge($params['option_label'][$index], '_', 64),
136 'amount' => CRM_Utils_Rule::cleanMoney(trim($params['option_amount'][$index])),
137 'count' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_count', $params), NULL),
138 'max_value' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_max_value', $params), NULL),
139 'description' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_description', $params), NULL),
140 'membership_type_id' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('membership_type_id', $params), NULL),
141 'weight' => $params['option_weight'][$index],
142 'is_active' => 1,
143 'is_default' => CRM_Utils_Array::value($params['option_weight'][$index], $defaultArray) ? $defaultArray[$params['option_weight'][$index]] : 0,
144 'membership_num_terms' => NULL,
145 );
146
147 if ($options['membership_type_id']) {
148 $options['membership_num_terms'] = CRM_Utils_Array::value($index, CRM_Utils_Array::value('membership_num_terms', $params), 1);
149 }
150
151 if (CRM_Utils_Array::value( $index, CRM_Utils_Array::value('option_financial_type_id', $params))) {
152 $options['financial_type_id'] = $params['option_financial_type_id'][$index];
153 } elseif (!empty($params['financial_type_id'])) {
154 $options['financial_type_id'] = $params['financial_type_id'];
155 }
156
157 if ($opIds = CRM_Utils_Array::value('option_id', $params)) {
158 if ($opId = CRM_Utils_Array::value($index, $opIds)) {
159 $optionsIds['id'] = $opId;
160 } else {
161 $optionsIds['id'] = NULL;
162 }
163 }
164 CRM_Price_BAO_PriceFieldValue::create($options, $optionsIds);
165 }
166 }
167
168 $transaction->commit();
169 return $priceField;
170 }
171
172 /**
173 * Takes a bunch of params that are needed to match certain criteria and
174 * retrieves the relevant objects. Typically the valid params are only
175 * contact_id. We'll tweak this function to be more full featured over a period
176 * of time. This is the inverse function of create. It also stores all the retrieved
177 * values in the default array
178 *
179 * @param array $params (reference ) an assoc array of name/value pairs
180 * @param array $defaults (reference ) an assoc array to hold the flattened values
181 *
182 * @return object CRM_Price_DAO_PriceField object
183 * @access public
184 * @static
185 */
186 static function retrieve(&$params, &$defaults) {
187 return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceField', $params, $defaults);
188 }
189
190 /**
191 * update the is_active flag in the db
192 *
193 * @param int $id Id of the database record
194 * @param boolean $is_active Value we want to set the is_active field
195 *
196 * @return Object DAO object on sucess, null otherwise
197 *
198 * @access public
199 * @static
200 */
201 static function setIsActive($id, $is_active) {
202 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceField', $id, 'is_active', $is_active);
203 }
204
205 /**
206 * Get the field title.
207 *
208 * @param int $id id of field.
209 *
210 * @return string name
211 *
212 * @access public
213 * @static
214 *
215 */
216 public static function getTitle($id) {
217 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $id, 'label');
218 }
219
220 /**
221 * This function for building custom fields
222 *
223 * @param CRM_Core_Form $qf form object (reference)
224 * @param string $elementName name of the custom field
225 * @param $fieldId
226 * @param boolean $inactiveNeeded
227 * @param boolean $useRequired true if required else false
228 * @param string $label label for custom field
229 *
230 * @param null $fieldOptions
231 * @param array $feezeOptions
232 *
233 * @return null
234 * @internal param bool $search true if used for search else false
235 * @access public
236 * @static
237 */
238 public static function addQuickFormElement(&$qf,
239 $elementName,
240 $fieldId,
241 $inactiveNeeded,
242 $useRequired = TRUE,
243 $label = NULL,
244 $fieldOptions = NULL,
245 $feezeOptions = array()
246 ) {
247
248 $field = new CRM_Price_DAO_PriceField();
249 $field->id = $fieldId;
250 if (!$field->find(TRUE)) {
251 /* FIXME: failure! */
252 return NULL;
253 }
254
255 $is_pay_later = 0;
256 if (isset($qf->_mode) && empty($qf->_mode)) {
257 $is_pay_later = 1;
258 }
259 elseif (isset($qf->_values)) {
260 $is_pay_later = CRM_Utils_Array::value( 'is_pay_later', $qf->_values);
261 }
262
263 $otherAmount = $qf->get('values');
264 $config = CRM_Core_Config::singleton();
265 $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency',$config->defaultCurrency,'symbol','name');
266 $qf->assign('currencySymbol', $currencySymbol);
267 // get currency name for price field and option attributes
268 $currencyName = $config->defaultCurrency;
269
270 if (!isset($label)) {
271 $label = (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') ? ts('Additional Contribution') : $field->label;
272 }
273
274 if ($field->name == 'contribution_amount') {
275 $qf->_contributionAmount = 1;
276 }
277
278 if (isset($qf->_online) && $qf->_online) {
279 $useRequired = FALSE;
280 }
281
282 $customOption = $fieldOptions;
283 if (!is_array($customOption)) {
284 $customOption = CRM_Price_BAO_PriceField::getOptions($field->id, $inactiveNeeded);
285 }
286
287 //use value field.
288 $valueFieldName = 'amount';
289 $seperator = '|';
290 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
291 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
292 $displayOpt = CRM_Utils_Array::value('tax_display_settings', $invoiceSettings);
293 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
294 switch ($field->html_type) {
295 case 'Text':
296 $optionKey = key($customOption);
297 $count = CRM_Utils_Array::value('count', $customOption[$optionKey], '');
298 $max_value = CRM_Utils_Array::value('max_value', $customOption[$optionKey], '');
299 $taxAmount = CRM_Utils_Array::value('tax_amount', $customOption[$optionKey]);
300 if (isset($taxAmount) && $displayOpt && $invoicing) {
301 $qf->assign('displayOpt', $displayOpt);
302 $qf->assign('taxTerm', $taxTerm);
303 $qf->assign('invoicing', $invoicing);
304 }
305 $priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName] + $taxAmount, $count, $max_value));
306
307 $extra = array();
308 if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) {
309 foreach($fieldOptions as &$fieldOption) {
310 if ($fieldOption['name'] == 'other_amount') {
311 $fieldOption['label'] = $fieldOption['label'] . ' ' . $currencySymbol;
312 }
313 }
314 $qf->assign('priceset', $elementName);
315 $extra = array('onclick' => 'useAmountOther();');
316 }
317
318 if (!empty($qf->_membershipBlock) && !empty($qf->_quickConfig) && $field->name == 'other_amount' && empty($qf->_contributionAmount)) {
319 $useRequired = 0;
320 }
321 elseif (!empty($fieldOptions[$optionKey]['label'])) { //check for label.
322 $label = $fieldOptions[$optionKey]['label'];
323 }
324
325 $element = &$qf->add('text', $elementName, $label,
326 array_merge($extra,
327 array('price' => json_encode(array($optionKey, $priceVal)),
328 'size' => '4',
329 )
330 ),
331 $useRequired && $field->is_required
332 );
333 if ($is_pay_later) {
334 $qf->add( 'text', 'txt-'.$elementName, $label, array( 'size' => '4'));
335 }
336
337 // CRM-6902
338 if (in_array($optionKey, $feezeOptions)) {
339 $element->freeze();
340 }
341
342 //CRM-10117
343 if (!empty($qf->_quickConfig)) {
344 $message = ts('Please enter a valid amount.');
345 $type = 'money';
346 }
347 else {
348 $message = ts('%1 must be an integer (whole number).', array(1 => $label));
349 $type = 'positiveInteger';
350 }
351 // integers will have numeric rule applied to them.
352 $qf->addRule($elementName, $message, $type);
353 break;
354
355 case 'Radio':
356 $choice = array();
357
358 if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) {
359 $qf->assign('contriPriceset', $elementName);
360 }
361
362 foreach ($customOption as $opId => $opt) {
363 $taxAmount = CRM_Utils_Array::value('tax_amount', $opt);
364 if ($field->is_display_amounts) {
365 $opt['label'] = !empty($opt['label']) ? $opt['label'] : '';
366 if (isset($taxAmount) && $invoicing) {
367 if ($displayOpt == 'Do_not_show') {
368 $opt['label'] = '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span> <span class="crm-price-amount-label">' . $opt['label'] . '</span>';
369 }
370 else if ($displayOpt == 'Inclusive') {
371 $opt['label'] = '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span> <span class="crm-price-amount-label">' . $opt['label'] . '</span>';
372 $opt['label'] .= '<span class="crm-price-amount-label"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>';
373 }
374 else {
375 $opt['label'] = '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span> <span class="crm-price-amount-label">' . $opt['label'] . '</span>';
376 $opt['label'] .= '<span class="crm-price-amount-label"> + '. CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>';
377 }
378 }
379 else {
380 $opt['label'] = '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span> <span class="crm-price-amount-label">' . $opt['label'] . '</span>';
381 }
382 }
383 $count = CRM_Utils_Array::value('count', $opt, '');
384 $max_value = CRM_Utils_Array::value('max_value', $opt, '');
385 $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
386 $extra = array('price' => json_encode(array($elementName, $priceVal)),
387 'data-amount' => $opt[$valueFieldName],
388 'data-currency' => $currencyName,
389 );
390 if (!empty($qf->_quickConfig) && $field->name == 'contribution_amount') {
391 $extra += array('onclick' => 'clearAmountOther();');
392 }
393 elseif (!empty($qf->_quickConfig) && $field->name == 'membership_amount') {
394 $extra += array(
395 'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});",
396 'membership-type' => $opt['membership_type_id'],
397 );
398 $qf->assign('membershipFieldID',$field->id);
399 }
400
401 $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra);
402
403 if ($is_pay_later) {
404 $qf->add( 'text', 'txt-'.$elementName, $label, array( 'size' => '4'));
405 }
406
407 // CRM-6902
408 if (in_array($opId, $feezeOptions)) {
409 $choice[$opId]->freeze();
410 }
411 }
412 if (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') {
413 $choice[] = $qf->createElement('radio', NULL, '', ts('No thank you'), '-1',
414 array(
415 'onclick' => 'clearAmountOther();',
416 )
417 );
418 }
419
420 if (!$field->is_required) {
421 // add "none" option
422 if (!empty($otherAmount['is_allow_other_amount']) && $field->name == 'contribution_amount') {
423 $none = ts('Other Amount');
424 }
425 elseif (!empty($qf->_membershipBlock) && empty($qf->_membershipBlock['is_required']) && $field->name == 'membership_amount') {
426 $none = ts('No thank you');
427 }
428 else {
429 $none = ts('- none -');
430 }
431
432 $choice[] = $qf->createElement('radio', NULL, '', $none, '0',
433 array('price' => json_encode(array($elementName, '0')))
434 );
435 }
436
437 $element = &$qf->addGroup($choice, $elementName, $label);
438
439 // make contribution field required for quick config when membership block is enabled
440 if (($field->name == 'membership_amount' || $field->name == 'contribution_amount')
441 && !empty($qf->_membershipBlock) && !$field->is_required) {
442 $useRequired = $field->is_required = TRUE;
443 }
444
445 if ($useRequired && $field->is_required) {
446 $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required');
447 }
448 break;
449
450 case 'Select':
451 $selectOption = $allowedOptions = $priceVal = array();
452
453 foreach ($customOption as $opt) {
454 $taxAmount = CRM_Utils_Array::value('tax_amount', $opt);
455 $count = CRM_Utils_Array::value('count', $opt, '');
456 $max_value = CRM_Utils_Array::value('max_value', $opt, '');
457
458 if ($field->is_display_amounts) {
459 $opt['label'] .= '&nbsp;-&nbsp;';
460 if (isset($taxAmount) && $invoicing) {
461 $opt['label'] .= self::getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm);
462 }
463 else {
464 $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
465 }
466 }
467 $selectOption[$opt['id']] = $opt['label'];
468 $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
469
470 if (!in_array($opt['id'], $feezeOptions)) {
471 $allowedOptions[] = $opt['id'];
472 }
473 if ($is_pay_later) {
474 $qf->add( 'text', 'txt-'.$elementName, $label, array( 'size' => '4'));
475 }
476 }
477 $element = &$qf->add('select', $elementName, $label,
478 array(
479 '' => ts('- select -')) + $selectOption,
480 $useRequired && $field->is_required,
481 array('price' => json_encode($priceVal))
482 );
483
484 // CRM-6902
485 $button = substr($qf->controller->getButtonName(), -4);
486 if (!empty($feezeOptions) && $button != 'skip') {
487 $qf->addRule($elementName, ts('Sorry, this option is currently sold out.'), 'regex', "/" . implode('|', $allowedOptions) . "/");
488 }
489 break;
490
491 case 'CheckBox':
492
493 $check = array();
494 foreach ($customOption as $opId => $opt) {
495 $taxAmount = CRM_Utils_Array::value('tax_amount', $opt);
496 $count = CRM_Utils_Array::value('count', $opt, '');
497 $max_value = CRM_Utils_Array::value('max_value', $opt, '');
498
499 if ($field->is_display_amounts) {
500 $opt['label'] .= '&nbsp;-&nbsp;';
501 if (isset($taxAmount) && $invoicing) {
502 $opt['label'] .= self::getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm);
503 }
504 else {
505 $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
506 }
507 }
508 $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
509 $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'],
510 array('price' => json_encode(array($opt['id'], $priceVal)),
511 'data-amount' => $opt[$valueFieldName],
512 'data-currency' => $currencyName,
513 )
514 );
515 if ($is_pay_later) {
516 $txtcheck[$opId] =& $qf->createElement( 'text', $opId, $opt['label'], array( 'size' => '4' ) );
517 $qf->addGroup($txtcheck, 'txt-'.$elementName, $label);
518 }
519 // CRM-6902
520 if (in_array($opId, $feezeOptions)) {
521 $check[$opId]->freeze();
522 }
523 }
524 $element = &$qf->addGroup($check, $elementName, $label);
525 if ($useRequired && $field->is_required) {
526 $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required');
527 }
528 break;
529 }
530 if (isset($qf->_online) && $qf->_online) {
531 $element->freeze();
532 }
533 }
534
535 /**
536 * Retrieve a list of options for the specified field
537 *
538 * @param int $fieldId price field ID
539 * @param bool $inactiveNeeded include inactive options
540 * @param bool $reset ignore stored values\
541 *
542 * @return array array of options
543 */
544 public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE) {
545 static $options = array();
546
547 if ($reset || empty($options[$fieldId])) {
548 $values = array();
549 CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $values, 'weight', !$inactiveNeeded);
550 $options[$fieldId] = $values;
551 $taxRates = CRM_Core_PseudoConstant::getTaxRates();
552
553 // ToDo - Code for Hook Invoke
554
555 foreach ($options[$fieldId] as $priceFieldId => $priceFieldValues) {
556 if (array_key_exists($priceFieldValues['financial_type_id'], $taxRates)) {
557 $options[$fieldId][$priceFieldId]['tax_rate'] = $taxRates[$priceFieldValues['financial_type_id']];
558 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceFieldValues['amount'], $options[$fieldId][$priceFieldId]['tax_rate']);
559 $options[$fieldId][$priceFieldId]['tax_amount'] = round($taxAmount['tax_amount'],2);
560 }
561 }
562 }
563
564 return $options[$fieldId];
565 }
566
567 /**
568 * @param $optionLabel
569 * @param $fid
570 *
571 * @return mixed
572 */
573 public static function getOptionId($optionLabel, $fid) {
574 if (!$optionLabel || !$fid) {
575 return;
576 }
577
578 $optionGroupName = "civicrm_price_field.amount.{$fid}";
579
580 $query = "
581 SELECT
582 option_value.id as id
583 FROM
584 civicrm_option_value option_value,
585 civicrm_option_group option_group
586 WHERE
587 option_group.name = %1
588 AND option_group.id = option_value.option_group_id
589 AND option_value.label = %2";
590
591 $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($optionGroupName, 'String'), 2 => array($optionLabel, 'String')));
592
593 while ($dao->fetch()) {
594 return $dao->id;
595 }
596 }
597
598 /**
599 * Delete the price set field.
600 *
601 * @param int $id Field Id
602 *
603 * @return boolean
604 *
605 * @access public
606 * @static
607 *
608 */
609 public static function deleteField($id) {
610 $field = new CRM_Price_DAO_PriceField();
611 $field->id = $id;
612
613 if ($field->find(TRUE)) {
614 // delete the options for this field
615 CRM_Price_BAO_PriceFieldValue::deleteValues($id);
616
617 // reorder the weight before delete
618 $fieldValues = array('price_set_id' => $field->price_set_id);
619
620 CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceField', $field->id, $fieldValues);
621
622 // now delete the field
623 return $field->delete();
624 }
625
626 return NULL;
627 }
628
629 /**
630 * @return array
631 */
632 static function &htmlTypes() {
633 static $htmlTypes = NULL;
634 if (!$htmlTypes) {
635 $htmlTypes = array(
636 'Text' => ts('Text / Numeric Quantity'),
637 'Select' => ts('Select'),
638 'Radio' => ts('Radio'),
639 'CheckBox' => ts('CheckBox'),
640 );
641 }
642 return $htmlTypes;
643 }
644
645 /**
646 * Validate the priceset
647 *
648 * @param int $priceSetId , array $fields
649 *
650 * retrun the error string
651 *
652 * @param $fields
653 * @param $error
654 * @param bool $allowNoneSelection
655 *
656 * @access public
657 * @static
658 */
659
660 public static function priceSetValidation($priceSetId, $fields, &$error, $allowNoneSelection = FALSE) {
661 // check for at least one positive
662 // amount price field should be selected.
663 $priceField = new CRM_Price_DAO_PriceField();
664 $priceField->price_set_id = $priceSetId;
665 $priceField->find();
666
667 $priceFields = array();
668
669 if ($allowNoneSelection) {
670 $noneSelectedPriceFields = array();
671 }
672
673 while ($priceField->fetch()) {
674 $key = "price_{$priceField->id}";
675
676 if ($allowNoneSelection) {
677 if (array_key_exists($key, $fields)) {
678 if ($fields[$key] == 0 && !$priceField->is_required) {
679 $noneSelectedPriceFields[] = $priceField->id;
680 }
681 }
682 }
683
684 if (!empty($fields[$key])) {
685 $priceFields[$priceField->id] = $fields[$key];
686 }
687 }
688
689 if (!empty($priceFields)) {
690 // we should has to have positive amount.
691 $sql = "
692 SELECT id, html_type
693 FROM civicrm_price_field
694 WHERE id IN (" . implode(',', array_keys($priceFields)) . ')';
695 $fieldDAO = CRM_Core_DAO::executeQuery($sql);
696 $htmlTypes = array();
697 while ($fieldDAO->fetch()) {
698 $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type;
699 }
700
701 $selectedAmounts = array();
702
703 foreach ($htmlTypes as $fieldId => $type) {
704 $options = array();
705 CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $options);
706
707 if (empty($options)) {
708 continue;
709 }
710
711 if ($type == 'Text') {
712 foreach ($options as $opId => $option) {
713 $selectedAmounts[$opId] = $priceFields[$fieldId] * $option['amount'];
714 break;
715 }
716 }
717 elseif (is_array($fields["price_{$fieldId}"])) {
718 foreach (array_keys($fields["price_{$fieldId}"]) as $opId) {
719 $selectedAmounts[$opId] = $options[$opId]['amount'];
720 }
721 }
722 elseif (in_array($fields["price_{$fieldId}"], array_keys($options))) {
723 $selectedAmounts[$fields["price_{$fieldId}"]] = $options[$fields["price_{$fieldId}"]]['amount'];
724 }
725 }
726
727 list($componentName) = explode(':', $fields['_qf_default']);
728 // now we have all selected amount in hand.
729 $totalAmount = array_sum($selectedAmounts);
730 if ($totalAmount < 0) {
731 $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', array(1 => $componentName));
732 }
733 }
734 else {
735 if ($allowNoneSelection) {
736 if (empty($noneSelectedPriceFields)) {
737 $error['_qf_default'] = ts('Please select at least one option from price set.');
738 }
739 } else {
740 $error['_qf_default'] = ts('Please select at least one option from price set.');
741 }
742 }
743 }
744
745 /**
746 * Generate the label for price fields based on tax display setting option on CiviContribute Component Settings page.
747 *
748 * @param array $opt
749 * @param string $valueFieldName amount
750 * @param string $displayOpt tax display setting option
751 *
752 * @return string $label tax label for custom field
753 *
754 * @access public
755 * @static
756 *
757 */
758 public static function getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm) {
759 if ($displayOpt == 'Do_not_show') {
760 $label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']);
761 }
762 else if ($displayOpt == 'Inclusive') {
763 $label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']);
764 $label .= '<span class="crm-price-amount-label"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>';
765 }
766 else {
767 $label = CRM_Utils_Money::format($opt[$valueFieldName]);
768 $label .= '<span class="crm-price-amount-label"> + '. CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>';
769 }
770
771 return $label;
772 }
773 }
774