Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Business objects for managing price fields. | |
38 | * | |
39 | */ | |
9da8dc8c | 40 | class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { |
6a488035 TO |
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 | * | |
77b97be7 EM |
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 | |
6a488035 | 54 | * |
9da8dc8c | 55 | * @return object CRM_Price_BAO_PriceField object |
6a488035 TO |
56 | * @access public |
57 | * @static | |
58 | */ | |
e0c1764e | 59 | static function add(&$params) { |
9da8dc8c | 60 | $priceFieldBAO = new CRM_Price_BAO_PriceField(); |
6a488035 TO |
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 | * | |
9da8dc8c | 79 | * @return object CRM_Price_DAO_PriceField object |
6a488035 TO |
80 | * @access public |
81 | * @static | |
82 | */ | |
83 | static function create(&$params) { | |
291ebece | 84 | if(empty($params['id']) && empty($params['name'])) { |
b10b974d | 85 | $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242)); |
291ebece | 86 | } |
6a488035 TO |
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 | ||
e0c1764e | 96 | $optionsIds = array(); |
6a488035 TO |
97 | $maxIndex = CRM_Price_Form_Field::NUM_OPTION; |
98 | ||
99 | if ($priceField->html_type == 'Text') { | |
100 | $maxIndex = 1; | |
101 | ||
9da8dc8c | 102 | $fieldValue = new CRM_Price_DAO_PriceFieldValue(); |
6a488035 TO |
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(); | |
a4537ddd EM |
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'])) { | |
6a488035 TO |
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 { | |
a7488080 | 121 | if (!empty($params['default_option'])) { |
6a488035 TO |
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']) && | |
8cc574cf | 129 | (CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_label', $params)) || !empty($params['is_quick_config'])) && |
6a488035 TO |
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]; | |
a7488080 | 153 | } elseif (!empty($params['financial_type_id'])) { |
6a488035 TO |
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 | } | |
9da8dc8c | 164 | CRM_Price_BAO_PriceFieldValue::create($options, $optionsIds); |
6a488035 TO |
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 | * | |
9da8dc8c | 182 | * @return object CRM_Price_DAO_PriceField object |
6a488035 TO |
183 | * @access public |
184 | * @static | |
185 | */ | |
186 | static function retrieve(&$params, &$defaults) { | |
9da8dc8c | 187 | return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceField', $params, $defaults); |
6a488035 TO |
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) { | |
9da8dc8c | 202 | return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceField', $id, 'is_active', $is_active); |
6a488035 TO |
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) { | |
9da8dc8c | 217 | return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $id, 'label'); |
6a488035 TO |
218 | } |
219 | ||
220 | /** | |
221 | * This function for building custom fields | |
222 | * | |
f9a5ad43 | 223 | * @param CRM_Core_Form $qf form object (reference) |
77b97be7 EM |
224 | * @param string $elementName name of the custom field |
225 | * @param $fieldId | |
6a488035 | 226 | * @param boolean $inactiveNeeded |
77b97be7 EM |
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 | |
6a488035 | 232 | * |
77b97be7 EM |
233 | * @return null |
234 | * @internal param bool $search true if used for search else false | |
6a488035 TO |
235 | * @access public |
236 | * @static | |
237 | */ | |
238 | public static function addQuickFormElement(&$qf, | |
239 | $elementName, | |
240 | $fieldId, | |
241 | $inactiveNeeded, | |
535a8e1e CW |
242 | $useRequired = TRUE, |
243 | $label = NULL, | |
6a488035 TO |
244 | $fieldOptions = NULL, |
245 | $feezeOptions = array() | |
246 | ) { | |
247 | ||
9da8dc8c | 248 | $field = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
249 | $field->id = $fieldId; |
250 | if (!$field->find(TRUE)) { | |
251 | /* FIXME: failure! */ | |
252 | return NULL; | |
253 | } | |
366fe2a3 | 254 | |
6a488035 TO |
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)) { | |
15831617 | 271 | $label = (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') ? ts('Additional Contribution') : $field->label; |
6a488035 TO |
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)) { | |
9da8dc8c | 284 | $customOption = CRM_Price_BAO_PriceField::getOptions($field->id, $inactiveNeeded); |
6a488035 TO |
285 | } |
286 | ||
287 | //use value field. | |
288 | $valueFieldName = 'amount'; | |
289 | $seperator = '|'; | |
e0f7075f | 290 | $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); |
5ebee375 | 291 | $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings); |
292 | $displayOpt = CRM_Utils_Array::value('tax_display_settings', $invoiceSettings); | |
fff0f449 | 293 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); |
6a488035 TO |
294 | switch ($field->html_type) { |
295 | case 'Text': | |
296 | $optionKey = key($customOption); | |
535a8e1e | 297 | $count = CRM_Utils_Array::value('count', $customOption[$optionKey], ''); |
6a488035 | 298 | $max_value = CRM_Utils_Array::value('max_value', $customOption[$optionKey], ''); |
9d1c9154 | 299 | $taxAmount = CRM_Utils_Array::value('tax_amount', $customOption[$optionKey]); |
3bd20e3f | 300 | if (isset($taxAmount) && $displayOpt && $invoicing) { |
dc428161 | 301 | $qf->assign('displayOpt', $displayOpt); |
5ebee375 | 302 | $qf->assign('taxTerm', $taxTerm); |
fff0f449 | 303 | $qf->assign('invoicing', $invoicing); |
dc428161 | 304 | } |
305 | $priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName] + $taxAmount, $count, $max_value)); | |
6a488035 TO |
306 | |
307 | $extra = array(); | |
15831617 | 308 | if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) { |
6a488035 TO |
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();'); | |
6a488035 TO |
316 | } |
317 | ||
15831617 | 318 | if (!empty($qf->_membershipBlock) && !empty($qf->_quickConfig) && $field->name == 'other_amount' && empty($qf->_contributionAmount)) { |
6a488035 | 319 | $useRequired = 0; |
366fe2a3 | 320 | } |
a7488080 | 321 | elseif (!empty($fieldOptions[$optionKey]['label'])) { //check for label. |
6a488035 TO |
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 | |
15831617 | 343 | if (!empty($qf->_quickConfig)) { |
6a488035 TO |
344 | $message = ts('Please enter a valid amount.'); |
345 | $type = 'money'; | |
366fe2a3 | 346 | } |
6a488035 TO |
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 | ||
15831617 | 358 | if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) { |
6a488035 TO |
359 | $qf->assign('contriPriceset', $elementName); |
360 | } | |
361 | ||
362 | foreach ($customOption as $opId => $opt) { | |
9d1c9154 | 363 | $taxAmount = CRM_Utils_Array::value('tax_amount', $opt); |
6a488035 | 364 | if ($field->is_display_amounts) { |
0d8afee2 | 365 | $opt['label'] = !empty($opt['label']) ? $opt['label'] : ''; |
3bd20e3f | 366 | if (isset($taxAmount) && $invoicing) { |
dc428161 | 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>'; | |
a13f764f | 372 | $opt['label'] .= '<span class="crm-price-amount-label"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>'; |
dc428161 | 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>'; | |
a13f764f | 376 | $opt['label'] .= '<span class="crm-price-amount-label"> + '. CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>'; |
dc428161 | 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 | } | |
6a488035 | 382 | } |
535a8e1e | 383 | $count = CRM_Utils_Array::value('count', $opt, ''); |
6a488035 | 384 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
dc428161 | 385 | $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value)); |
6a488035 TO |
386 | $extra = array('price' => json_encode(array($elementName, $priceVal)), |
387 | 'data-amount' => $opt[$valueFieldName], | |
388 | 'data-currency' => $currencyName, | |
389 | ); | |
15831617 | 390 | if (!empty($qf->_quickConfig) && $field->name == 'contribution_amount') { |
6a488035 | 391 | $extra += array('onclick' => 'clearAmountOther();'); |
366fe2a3 | 392 | } |
15831617 | 393 | elseif (!empty($qf->_quickConfig) && $field->name == 'membership_amount') { |
6a488035 TO |
394 | $extra += array( |
395 | 'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});", | |
396 | 'membership-type' => $opt['membership_type_id'], | |
366fe2a3 | 397 | ); |
6a488035 TO |
398 | $qf->assign('membershipFieldID',$field->id); |
399 | } | |
366fe2a3 | 400 | |
6a488035 TO |
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 | } | |
15831617 | 412 | if (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') { |
6a488035 | 413 | $choice[] = $qf->createElement('radio', NULL, '', ts('No thank you'), '-1', |
abcf506a PN |
414 | array( |
415 | 'onclick' => 'clearAmountOther();', | |
416 | ) | |
6a488035 TO |
417 | ); |
418 | } | |
419 | ||
420 | if (!$field->is_required) { | |
421 | // add "none" option | |
a7488080 | 422 | if (!empty($otherAmount['is_allow_other_amount']) && $field->name == 'contribution_amount') { |
6a488035 | 423 | $none = ts('Other Amount'); |
366fe2a3 | 424 | } |
8cc574cf | 425 | elseif (!empty($qf->_membershipBlock) && empty($qf->_membershipBlock['is_required']) && $field->name == 'membership_amount') { |
6a488035 | 426 | $none = ts('No thank you'); |
366fe2a3 | 427 | } |
6a488035 TO |
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 | |
abcf506a | 440 | if (($field->name == 'membership_amount' || $field->name == 'contribution_amount') |
15831617 | 441 | && !empty($qf->_membershipBlock) && !$field->is_required) { |
6a488035 TO |
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) { | |
9d1c9154 | 454 | $taxAmount = CRM_Utils_Array::value('tax_amount', $opt); |
6a488035 TO |
455 | $count = CRM_Utils_Array::value('count', $opt, ''); |
456 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); | |
6a488035 TO |
457 | |
458 | if ($field->is_display_amounts) { | |
459 | $opt['label'] .= ' - '; | |
3bd20e3f | 460 | if (isset($taxAmount) && $invoicing) { |
5ebee375 | 461 | $opt['label'] .= self::getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm); |
dc428161 | 462 | } |
463 | else { | |
464 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); | |
465 | } | |
6a488035 TO |
466 | } |
467 | $selectOption[$opt['id']] = $opt['label']; | |
dc428161 | 468 | $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value)); |
6a488035 TO |
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) { | |
9d1c9154 | 495 | $taxAmount = CRM_Utils_Array::value('tax_amount', $opt); |
535a8e1e | 496 | $count = CRM_Utils_Array::value('count', $opt, ''); |
6a488035 | 497 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
6a488035 TO |
498 | |
499 | if ($field->is_display_amounts) { | |
500 | $opt['label'] .= ' - '; | |
3bd20e3f | 501 | if (isset($taxAmount) && $invoicing) { |
5ebee375 | 502 | $opt['label'] .= self::getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm); |
dc428161 | 503 | } |
504 | else { | |
505 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); | |
506 | } | |
6a488035 | 507 | } |
dc428161 | 508 | $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value)); |
6a488035 TO |
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(); | |
9da8dc8c | 549 | CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $values, 'weight', !$inactiveNeeded); |
6a488035 | 550 | $options[$fieldId] = $values; |
dc428161 | 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']]; | |
9d1c9154 | 558 | $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceFieldValues['amount'], $options[$fieldId][$priceFieldId]['tax_rate']); |
dc428161 | 559 | $options[$fieldId][$priceFieldId]['tax_amount'] = round($taxAmount['tax_amount'],2); |
560 | } | |
561 | } | |
6a488035 TO |
562 | } |
563 | ||
564 | return $options[$fieldId]; | |
565 | } | |
566 | ||
ffd93213 EM |
567 | /** |
568 | * @param $optionLabel | |
569 | * @param $fid | |
570 | * | |
571 | * @return mixed | |
572 | */ | |
6a488035 TO |
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 | |
535a8e1e CW |
587 | option_group.name = %1 |
588 | AND option_group.id = option_value.option_group_id | |
6a488035 TO |
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) { | |
9da8dc8c | 610 | $field = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
611 | $field->id = $id; |
612 | ||
613 | if ($field->find(TRUE)) { | |
614 | // delete the options for this field | |
9da8dc8c | 615 | CRM_Price_BAO_PriceFieldValue::deleteValues($id); |
6a488035 TO |
616 | |
617 | // reorder the weight before delete | |
618 | $fieldValues = array('price_set_id' => $field->price_set_id); | |
619 | ||
9da8dc8c | 620 | CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceField', $field->id, $fieldValues); |
6a488035 TO |
621 | |
622 | // now delete the field | |
623 | return $field->delete(); | |
624 | } | |
625 | ||
626 | return NULL; | |
627 | } | |
628 | ||
ffd93213 EM |
629 | /** |
630 | * @return array | |
631 | */ | |
6a488035 TO |
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 | * | |
2a6da8d7 | 648 | * @param int $priceSetId , array $fields |
6a488035 TO |
649 | * |
650 | * retrun the error string | |
651 | * | |
2a6da8d7 EM |
652 | * @param $fields |
653 | * @param $error | |
654 | * @param bool $allowNoneSelection | |
655 | * | |
6a488035 TO |
656 | * @access public |
657 | * @static | |
6a488035 TO |
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. | |
9da8dc8c | 663 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
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 | ||
a7488080 | 684 | if (!empty($fields[$key])) { |
6a488035 TO |
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(); | |
9da8dc8c | 705 | CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $options); |
6a488035 TO |
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 | } | |
dc428161 | 744 | |
745 | /** | |
9d1c9154 | 746 | * Generate the label for price fields based on tax display setting option on CiviContribute Component Settings page. |
dc428161 | 747 | * |
9d1c9154 | 748 | * @param array $opt |
749 | * @param string $valueFieldName amount | |
750 | * @param string $displayOpt tax display setting option | |
dc428161 | 751 | * |
9d1c9154 | 752 | * @return string $label tax label for custom field |
dc428161 | 753 | * |
754 | * @access public | |
755 | * @static | |
756 | * | |
757 | */ | |
5ebee375 | 758 | public static function getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm) { |
9d1c9154 | 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']); | |
a13f764f | 764 | $label .= '<span class="crm-price-amount-label"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>'; |
9d1c9154 | 765 | } |
766 | else { | |
767 | $label = CRM_Utils_Money::format($opt[$valueFieldName]); | |
a13f764f | 768 | $label .= '<span class="crm-price-amount-label"> + '. CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>'; |
9d1c9154 | 769 | } |
dc428161 | 770 | |
9d1c9154 | 771 | return $label; |
dc428161 | 772 | } |
6a488035 TO |
773 | } |
774 |