Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
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 | /** | |
fe482240 | 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 | * | |
414c1420 TO |
51 | * @param array $params |
52 | * (reference) an assoc array of name/value pairs. | |
6a488035 | 53 | * |
16b10e64 | 54 | * @return CRM_Price_BAO_PriceField |
6a488035 | 55 | */ |
00be9182 | 56 | public static function add(&$params) { |
9da8dc8c | 57 | $priceFieldBAO = new CRM_Price_BAO_PriceField(); |
6a488035 TO |
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 | /** | |
fe482240 | 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 | * | |
414c1420 TO |
74 | * @param array $params |
75 | * (reference) an assoc array of name/value pairs. | |
6a488035 | 76 | * |
16b10e64 | 77 | * @return CRM_Price_DAO_PriceField |
6a488035 | 78 | */ |
00be9182 | 79 | public static function create(&$params) { |
22e263ad | 80 | if (empty($params['id']) && empty($params['name'])) { |
b10b974d | 81 | $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242)); |
291ebece | 82 | } |
6a488035 TO |
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 | ||
68a15a85 | 92 | if (!empty($params['id']) && empty($priceField->html_type)) { |
93 | $priceField->html_type = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['id'], 'html_type'); | |
94 | } | |
e0c1764e | 95 | $optionsIds = array(); |
6a488035 | 96 | $maxIndex = CRM_Price_Form_Field::NUM_OPTION; |
e1defd06 | 97 | if ($priceField->html_type == 'Text') { |
6a488035 | 98 | $maxIndex = 1; |
1019b2fe SL |
99 | $fieldOptions = civicrm_api3('price_field_value', 'get', array( |
100 | 'price_field_id' => $priceField->id, | |
101 | 'sequential' => 1, | |
102 | )); | |
103 | foreach ($fieldOptions['values'] as $option) { | |
104 | $optionsIds['id'] = $option['id']; | |
e1defd06 SL |
105 | // CRM-19741 If we are dealing with price fields that are Text only set the field value label to match |
106 | if (!empty($params['id']) && $priceField->label != $option['label']) { | |
107 | $fieldValue = new CRM_Price_DAO_PriceFieldValue(); | |
108 | $fieldValue->label = $priceField->label; | |
109 | $fieldValue->id = $option['id']; | |
110 | $fieldValue->save(); | |
111 | } | |
68a15a85 | 112 | } |
6a488035 TO |
113 | } |
114 | $defaultArray = array(); | |
a4537ddd EM |
115 | //html type would be empty in update scenario not sure what would happen ... |
116 | if (!empty($params['html_type']) && $params['html_type'] == 'CheckBox' && isset($params['default_checkbox_option'])) { | |
6a488035 TO |
117 | $tempArray = array_keys($params['default_checkbox_option']); |
118 | foreach ($tempArray as $v) { | |
119 | if ($params['option_amount'][$v]) { | |
120 | $defaultArray[$v] = 1; | |
121 | } | |
122 | } | |
123 | } | |
124 | else { | |
a7488080 | 125 | if (!empty($params['default_option'])) { |
6a488035 TO |
126 | $defaultArray[$params['default_option']] = 1; |
127 | } | |
128 | } | |
129 | ||
130 | for ($index = 1; $index <= $maxIndex; $index++) { | |
131 | if (array_key_exists('option_amount', $params) && | |
132 | array_key_exists($index, $params['option_amount']) && | |
8cc574cf | 133 | (CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_label', $params)) || !empty($params['is_quick_config'])) && |
6a488035 TO |
134 | !CRM_Utils_System::isNull($params['option_amount'][$index]) |
135 | ) { | |
136 | $options = array( | |
137 | 'price_field_id' => $priceField->id, | |
138 | 'label' => trim($params['option_label'][$index]), | |
139 | 'name' => CRM_Utils_String::munge($params['option_label'][$index], '_', 64), | |
140 | 'amount' => CRM_Utils_Rule::cleanMoney(trim($params['option_amount'][$index])), | |
141 | 'count' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_count', $params), NULL), | |
142 | 'max_value' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_max_value', $params), NULL), | |
143 | 'description' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_description', $params), NULL), | |
144 | 'membership_type_id' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('membership_type_id', $params), NULL), | |
145 | 'weight' => $params['option_weight'][$index], | |
146 | 'is_active' => 1, | |
147 | 'is_default' => CRM_Utils_Array::value($params['option_weight'][$index], $defaultArray) ? $defaultArray[$params['option_weight'][$index]] : 0, | |
148 | 'membership_num_terms' => NULL, | |
05465712 | 149 | 'non_deductible_amount' => CRM_Utils_Array::value('non_deductible_amount', $params), |
2db35bf6 | 150 | 'visibility_id' => $params['option_visibility_id'][$index], |
6a488035 TO |
151 | ); |
152 | ||
153 | if ($options['membership_type_id']) { | |
154 | $options['membership_num_terms'] = CRM_Utils_Array::value($index, CRM_Utils_Array::value('membership_num_terms', $params), 1); | |
fbd40a12 | 155 | $options['is_default'] = CRM_Utils_Array::value($params['membership_type_id'][$index], $defaultArray) ? $defaultArray[$params['membership_type_id'][$index]] : 0; |
6a488035 TO |
156 | } |
157 | ||
481a74f4 | 158 | if (CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_financial_type_id', $params))) { |
353ffa53 | 159 | $options['financial_type_id'] = $params['option_financial_type_id'][$index]; |
0db6c3e1 TO |
160 | } |
161 | elseif (!empty($params['financial_type_id'])) { | |
6a488035 TO |
162 | $options['financial_type_id'] = $params['financial_type_id']; |
163 | } | |
6a488035 TO |
164 | if ($opIds = CRM_Utils_Array::value('option_id', $params)) { |
165 | if ($opId = CRM_Utils_Array::value($index, $opIds)) { | |
166 | $optionsIds['id'] = $opId; | |
0db6c3e1 TO |
167 | } |
168 | else { | |
6a488035 TO |
169 | $optionsIds['id'] = NULL; |
170 | } | |
171 | } | |
1019b2fe SL |
172 | try { |
173 | CRM_Price_BAO_PriceFieldValue::create($options, $optionsIds); | |
174 | } | |
175 | catch (Exception $e) { | |
176 | $transaction->rollback(); | |
177 | throw new CRM_Core_Exception($e->getMessage()); | |
178 | } | |
179 | } | |
026a0d69 | 180 | elseif (!empty($optionsIds) && !empty($optionsIds['id'])) { |
e1defd06 SL |
181 | $optionsLoad = civicrm_api3('price_field_value', 'get', array('id' => $optionsIds['id'])); |
182 | $options = $optionsLoad['values'][$optionsIds['id']]; | |
1019b2fe SL |
183 | $options['is_active'] = CRM_Utils_Array::value('is_active', $params, 1); |
184 | try { | |
e1defd06 | 185 | CRM_Price_BAO_PriceFieldValue::create($options, $optionsIds); |
1019b2fe SL |
186 | } |
187 | catch (Exception $e) { | |
188 | $transaction->rollback(); | |
189 | throw new CRM_Core_Exception($e->getMessage()); | |
190 | } | |
6a488035 TO |
191 | } |
192 | } | |
193 | ||
194 | $transaction->commit(); | |
195 | return $priceField; | |
196 | } | |
197 | ||
198 | /** | |
fe482240 | 199 | * Fetch object based on array of properties. |
6a488035 | 200 | * |
414c1420 TO |
201 | * @param array $params |
202 | * (reference ) an assoc array of name/value pairs. | |
203 | * @param array $defaults | |
204 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 205 | * |
16b10e64 | 206 | * @return CRM_Price_DAO_PriceField |
6a488035 | 207 | */ |
00be9182 | 208 | public static function retrieve(&$params, &$defaults) { |
9da8dc8c | 209 | return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceField', $params, $defaults); |
6a488035 TO |
210 | } |
211 | ||
212 | /** | |
fe482240 | 213 | * Update the is_active flag in the db. |
6a488035 | 214 | * |
414c1420 TO |
215 | * @param int $id |
216 | * Id of the database record. | |
217 | * @param bool $is_active | |
218 | * Value we want to set the is_active field. | |
6a488035 | 219 | * |
a6c01b45 | 220 | * @return Object |
2e2605fe | 221 | * DAO object on success, null otherwise. |
6a488035 | 222 | */ |
00be9182 | 223 | public static function setIsActive($id, $is_active) { |
9da8dc8c | 224 | return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceField', $id, 'is_active', $is_active); |
6a488035 TO |
225 | } |
226 | ||
2e2605fe EM |
227 | /** |
228 | * Freeze form if the event is full. | |
229 | * | |
230 | * @param $element | |
231 | * @param $fieldOptions | |
232 | * | |
233 | * @return null | |
234 | */ | |
00be9182 | 235 | public static function freezeIfEnabled(&$element, $fieldOptions) { |
79b152ac | 236 | if (!empty($fieldOptions['is_full'])) { |
237 | $element->freeze(); | |
238 | } | |
d5cc0fc2 | 239 | return NULL; |
79b152ac | 240 | } |
241 | ||
6a488035 TO |
242 | /** |
243 | * Get the field title. | |
244 | * | |
414c1420 TO |
245 | * @param int $id |
246 | * Id of field. | |
6a488035 | 247 | * |
a6c01b45 CW |
248 | * @return string |
249 | * name | |
6a488035 | 250 | * |
6a488035 TO |
251 | */ |
252 | public static function getTitle($id) { | |
9da8dc8c | 253 | return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $id, 'label'); |
6a488035 TO |
254 | } |
255 | ||
256 | /** | |
fe482240 | 257 | * This function for building custom fields. |
6a488035 | 258 | * |
414c1420 TO |
259 | * @param CRM_Core_Form $qf |
260 | * Form object (reference). | |
261 | * @param string $elementName | |
262 | * Name of the custom field. | |
100fef9d | 263 | * @param int $fieldId |
414c1420 TO |
264 | * @param bool $inactiveNeeded |
265 | * @param bool $useRequired | |
266 | * True if required else false. | |
267 | * @param string $label | |
268 | * Label for custom field. | |
77b97be7 EM |
269 | * |
270 | * @param null $fieldOptions | |
1537c309 | 271 | * @param array $freezeOptions |
6a488035 | 272 | * |
77b97be7 | 273 | * @return null |
6a488035 | 274 | */ |
9277d9e4 TO |
275 | public static function addQuickFormElement( |
276 | &$qf, | |
6a488035 TO |
277 | $elementName, |
278 | $fieldId, | |
279 | $inactiveNeeded, | |
535a8e1e CW |
280 | $useRequired = TRUE, |
281 | $label = NULL, | |
6a488035 | 282 | $fieldOptions = NULL, |
1537c309 | 283 | $freezeOptions = array() |
6a488035 TO |
284 | ) { |
285 | ||
9da8dc8c | 286 | $field = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
287 | $field->id = $fieldId; |
288 | if (!$field->find(TRUE)) { | |
289 | /* FIXME: failure! */ | |
290 | return NULL; | |
291 | } | |
366fe2a3 | 292 | |
6a488035 TO |
293 | $is_pay_later = 0; |
294 | if (isset($qf->_mode) && empty($qf->_mode)) { | |
295 | $is_pay_later = 1; | |
296 | } | |
297 | elseif (isset($qf->_values)) { | |
481a74f4 | 298 | $is_pay_later = CRM_Utils_Array::value('is_pay_later', $qf->_values); |
6a488035 TO |
299 | } |
300 | ||
301 | $otherAmount = $qf->get('values'); | |
302 | $config = CRM_Core_Config::singleton(); | |
353ffa53 | 303 | $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency, 'symbol', 'name'); |
6a488035 TO |
304 | $qf->assign('currencySymbol', $currencySymbol); |
305 | // get currency name for price field and option attributes | |
306 | $currencyName = $config->defaultCurrency; | |
307 | ||
308 | if (!isset($label)) { | |
15831617 | 309 | $label = (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') ? ts('Additional Contribution') : $field->label; |
6a488035 TO |
310 | } |
311 | ||
312 | if ($field->name == 'contribution_amount') { | |
313 | $qf->_contributionAmount = 1; | |
314 | } | |
315 | ||
316 | if (isset($qf->_online) && $qf->_online) { | |
317 | $useRequired = FALSE; | |
318 | } | |
319 | ||
320 | $customOption = $fieldOptions; | |
321 | if (!is_array($customOption)) { | |
9da8dc8c | 322 | $customOption = CRM_Price_BAO_PriceField::getOptions($field->id, $inactiveNeeded); |
6a488035 TO |
323 | } |
324 | ||
325 | //use value field. | |
326 | $valueFieldName = 'amount'; | |
327 | $seperator = '|'; | |
aaffa79f | 328 | $invoiceSettings = Civi::settings()->get('contribution_invoice_settings'); |
5ebee375 | 329 | $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings); |
330 | $displayOpt = CRM_Utils_Array::value('tax_display_settings', $invoiceSettings); | |
fff0f449 | 331 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); |
6a488035 TO |
332 | switch ($field->html_type) { |
333 | case 'Text': | |
334 | $optionKey = key($customOption); | |
535a8e1e | 335 | $count = CRM_Utils_Array::value('count', $customOption[$optionKey], ''); |
6a488035 | 336 | $max_value = CRM_Utils_Array::value('max_value', $customOption[$optionKey], ''); |
9d1c9154 | 337 | $taxAmount = CRM_Utils_Array::value('tax_amount', $customOption[$optionKey]); |
3bd20e3f | 338 | if (isset($taxAmount) && $displayOpt && $invoicing) { |
dc428161 | 339 | $qf->assign('displayOpt', $displayOpt); |
5ebee375 | 340 | $qf->assign('taxTerm', $taxTerm); |
fff0f449 | 341 | $qf->assign('invoicing', $invoicing); |
dc428161 | 342 | } |
353ffa53 | 343 | $priceVal = implode($seperator, array( |
c5c263ca AH |
344 | $customOption[$optionKey][$valueFieldName] + $taxAmount, |
345 | $count, | |
346 | $max_value, | |
347 | )); | |
6a488035 TO |
348 | |
349 | $extra = array(); | |
15831617 | 350 | if (!empty($qf->_membershipBlock) && !empty($qf->_quickConfig) && $field->name == 'other_amount' && empty($qf->_contributionAmount)) { |
6a488035 | 351 | $useRequired = 0; |
366fe2a3 | 352 | } |
d5cc0fc2 | 353 | elseif (!empty($fieldOptions[$optionKey]['label'])) { |
354 | //check for label. | |
6a488035 | 355 | $label = $fieldOptions[$optionKey]['label']; |
0d7d6391 | 356 | if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount) && strtolower($fieldOptions[$optionKey]['name']) == 'other_amount') { |
2882b6a4 | 357 | $label .= ' ' . $currencySymbol; |
0d7d6391 | 358 | $qf->assign('priceset', $elementName); |
359 | $extra = array('onclick' => 'useAmountOther();'); | |
360 | } | |
6a488035 TO |
361 | } |
362 | ||
363 | $element = &$qf->add('text', $elementName, $label, | |
353ffa53 TO |
364 | array_merge($extra, |
365 | array( | |
366 | 'price' => json_encode(array($optionKey, $priceVal)), | |
367 | 'size' => '4', | |
368 | ) | |
369 | ), | |
370 | $useRequired && $field->is_required | |
6a488035 TO |
371 | ); |
372 | if ($is_pay_later) { | |
353ffa53 | 373 | $qf->add('text', 'txt-' . $elementName, $label, array('size' => '4')); |
6a488035 TO |
374 | } |
375 | ||
d2108f71 | 376 | // CRM-6902 - Add "max" option for a price set field |
1537c309 | 377 | if (in_array($optionKey, $freezeOptions)) { |
0dc0b759 | 378 | self::freezeIfEnabled($element, $fieldOptions[$optionKey]); |
79b152ac | 379 | // CRM-14696 - Improve display for sold out price set options |
380 | $element->setLabel($label . ' <span class="sold-out-option">' . ts('Sold out') . '</span>'); | |
6a488035 TO |
381 | } |
382 | ||
383 | //CRM-10117 | |
55c0b49a RK |
384 | if (!empty($qf->_quickConfig)) { |
385 | $message = ts('Please enter a valid amount.'); | |
386 | $type = 'money'; | |
387 | } | |
388 | else { | |
389 | $message = ts('%1 must be a number (with or without decimal point).', array(1 => $label)); | |
390 | $type = 'numeric'; | |
391 | } | |
6a488035 TO |
392 | // integers will have numeric rule applied to them. |
393 | $qf->addRule($elementName, $message, $type); | |
394 | break; | |
395 | ||
396 | case 'Radio': | |
397 | $choice = array(); | |
398 | ||
15831617 | 399 | if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) { |
6a488035 TO |
400 | $qf->assign('contriPriceset', $elementName); |
401 | } | |
402 | ||
403 | foreach ($customOption as $opId => $opt) { | |
9d1c9154 | 404 | $taxAmount = CRM_Utils_Array::value('tax_amount', $opt); |
6a488035 | 405 | if ($field->is_display_amounts) { |
b2a106bd | 406 | $opt['label'] = !empty($opt['label']) ? $opt['label'] . '<span class="crm-price-amount-label-separator"> - </span>' : ''; |
08b8b2d9 | 407 | $preHelpText = $postHelpText = ''; |
408 | if (isset($opt['help_pre'])) { | |
409 | $preHelpText = '<span class="crm-price-amount-help-pre description">' . $opt['help_pre'] . '</span>: '; | |
410 | } | |
411 | if (isset($opt['help_post'])) { | |
412 | $postHelpText = ': <span class="crm-price-amount-help-post description">' . $opt['help_post'] . '</span>'; | |
413 | } | |
3bd20e3f | 414 | if (isset($taxAmount) && $invoicing) { |
dc428161 | 415 | if ($displayOpt == 'Do_not_show') { |
08b8b2d9 | 416 | $opt['label'] = '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span>'; |
dc428161 | 417 | } |
4c9b6178 | 418 | elseif ($displayOpt == 'Inclusive') { |
08b8b2d9 | 419 | $opt['label'] = '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span>'; |
420 | $opt['label'] .= '<span class="crm-price-amount-tax"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>'; | |
dc428161 | 421 | } |
422 | else { | |
08b8b2d9 | 423 | $opt['label'] = '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span>'; |
424 | $opt['label'] .= '<span class="crm-price-amount-tax"> + ' . CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>'; | |
dc428161 | 425 | } |
426 | } | |
427 | else { | |
08b8b2d9 | 428 | $opt['label'] = '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span>'; |
dc428161 | 429 | } |
08b8b2d9 | 430 | $opt['label'] = $preHelpText . $opt['label'] . $postHelpText; |
6a488035 | 431 | } |
535a8e1e | 432 | $count = CRM_Utils_Array::value('count', $opt, ''); |
6a488035 | 433 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
dc428161 | 434 | $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value)); |
2db35bf6 AF |
435 | if (isset($opt['visibility_id'])) { |
436 | $visibility_id = $opt['visibility_id']; | |
437 | } | |
438 | else { | |
439 | $visibility_id = 1; | |
440 | } | |
6ea503d4 | 441 | $extra = array( |
353ffa53 TO |
442 | 'price' => json_encode(array($elementName, $priceVal)), |
443 | 'data-amount' => $opt[$valueFieldName], | |
444 | 'data-currency' => $currencyName, | |
c23c87f6 | 445 | 'data-price-field-values' => json_encode($customOption), |
2db35bf6 | 446 | 'visibility' => $visibility_id, |
6a488035 | 447 | ); |
15831617 | 448 | if (!empty($qf->_quickConfig) && $field->name == 'contribution_amount') { |
6a488035 | 449 | $extra += array('onclick' => 'clearAmountOther();'); |
366fe2a3 | 450 | } |
15831617 | 451 | elseif (!empty($qf->_quickConfig) && $field->name == 'membership_amount') { |
6a488035 TO |
452 | $extra += array( |
453 | 'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});", | |
454 | 'membership-type' => $opt['membership_type_id'], | |
366fe2a3 | 455 | ); |
353ffa53 | 456 | $qf->assign('membershipFieldID', $field->id); |
6a488035 | 457 | } |
366fe2a3 | 458 | |
6a488035 | 459 | $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra); |
6a488035 | 460 | if ($is_pay_later) { |
353ffa53 | 461 | $qf->add('text', 'txt-' . $elementName, $label, array('size' => '4')); |
6a488035 TO |
462 | } |
463 | ||
d2108f71 | 464 | // CRM-6902 - Add "max" option for a price set field |
1537c309 | 465 | if (in_array($opId, $freezeOptions)) { |
e4c6a396 | 466 | self::freezeIfEnabled($choice[$opId], $customOption[$opId]); |
d2108f71 | 467 | // CRM-14696 - Improve display for sold out price set options |
be6dcd4e | 468 | $choice[$opId]->setText('<span class="sold-out-option">' . $choice[$opId]->getText() . ' (' . ts('Sold out') . ')</span>'); |
6a488035 TO |
469 | } |
470 | } | |
15831617 | 471 | if (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') { |
6a488035 | 472 | $choice[] = $qf->createElement('radio', NULL, '', ts('No thank you'), '-1', |
abcf506a | 473 | array( |
2975161b | 474 | 'price' => json_encode(array($elementName, '0|0')), |
475 | 'data-currency' => $currencyName, | |
abcf506a PN |
476 | 'onclick' => 'clearAmountOther();', |
477 | ) | |
6a488035 TO |
478 | ); |
479 | } | |
480 | ||
481 | if (!$field->is_required) { | |
482 | // add "none" option | |
a7488080 | 483 | if (!empty($otherAmount['is_allow_other_amount']) && $field->name == 'contribution_amount') { |
6a488035 | 484 | $none = ts('Other Amount'); |
366fe2a3 | 485 | } |
8cc574cf | 486 | elseif (!empty($qf->_membershipBlock) && empty($qf->_membershipBlock['is_required']) && $field->name == 'membership_amount') { |
6a488035 | 487 | $none = ts('No thank you'); |
366fe2a3 | 488 | } |
6a488035 TO |
489 | else { |
490 | $none = ts('- none -'); | |
491 | } | |
492 | ||
493 | $choice[] = $qf->createElement('radio', NULL, '', $none, '0', | |
494 | array('price' => json_encode(array($elementName, '0'))) | |
495 | ); | |
496 | } | |
497 | ||
498 | $element = &$qf->addGroup($choice, $elementName, $label); | |
499 | ||
500 | // make contribution field required for quick config when membership block is enabled | |
abcf506a | 501 | if (($field->name == 'membership_amount' || $field->name == 'contribution_amount') |
353ffa53 TO |
502 | && !empty($qf->_membershipBlock) && !$field->is_required |
503 | ) { | |
6a488035 TO |
504 | $useRequired = $field->is_required = TRUE; |
505 | } | |
506 | ||
507 | if ($useRequired && $field->is_required) { | |
508 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
509 | } | |
510 | break; | |
511 | ||
512 | case 'Select': | |
513 | $selectOption = $allowedOptions = $priceVal = array(); | |
514 | ||
515 | foreach ($customOption as $opt) { | |
9d1c9154 | 516 | $taxAmount = CRM_Utils_Array::value('tax_amount', $opt); |
6a488035 TO |
517 | $count = CRM_Utils_Array::value('count', $opt, ''); |
518 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); | |
6a488035 TO |
519 | |
520 | if ($field->is_display_amounts) { | |
521 | $opt['label'] .= ' - '; | |
353ffa53 | 522 | if (isset($taxAmount) && $invoicing) { |
e2d2aeab | 523 | $opt['label'] = $opt['label'] . self::getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm); |
dc428161 | 524 | } |
525 | else { | |
e2d2aeab | 526 | $opt['label'] = $opt['label'] . CRM_Utils_Money::format($opt[$valueFieldName]); |
dc428161 | 527 | } |
6a488035 | 528 | } |
79b152ac | 529 | |
dc428161 | 530 | $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value)); |
79b152ac | 531 | |
1537c309 | 532 | if (!in_array($opt['id'], $freezeOptions)) { |
6a488035 TO |
533 | $allowedOptions[] = $opt['id']; |
534 | } | |
d2108f71 | 535 | // CRM-14696 - Improve display for sold out price set options |
76991db6 | 536 | else { |
a14eff52 | 537 | $opt['id'] = 'crm_disabled_opt-' . $opt['id']; |
be6dcd4e | 538 | $opt['label'] = $opt['label'] . ' (' . ts('Sold out') . ')'; |
d2108f71 | 539 | } |
79b152ac | 540 | |
d2108f71 J |
541 | $selectOption[$opt['id']] = $opt['label']; |
542 | ||
6a488035 | 543 | if ($is_pay_later) { |
353ffa53 | 544 | $qf->add('text', 'txt-' . $elementName, $label, array('size' => '4')); |
6a488035 TO |
545 | } |
546 | } | |
2db35bf6 AF |
547 | if (isset($opt['visibility_id'])) { |
548 | $visibility_id = $opt['visibility_id']; | |
549 | } | |
550 | else { | |
551 | $visibility_id = 1; | |
552 | } | |
6a488035 TO |
553 | $element = &$qf->add('select', $elementName, $label, |
554 | array( | |
d5cc0fc2 | 555 | '' => ts('- select -'), |
353ffa53 TO |
556 | ) + $selectOption, |
557 | $useRequired && $field->is_required, | |
c23c87f6 | 558 | array('price' => json_encode($priceVal), 'class' => 'crm-select2', 'data-price-field-values' => json_encode($customOption)) |
353ffa53 | 559 | ); |
6a488035 | 560 | |
d2108f71 | 561 | // CRM-6902 - Add "max" option for a price set field |
6a488035 | 562 | $button = substr($qf->controller->getButtonName(), -4); |
1537c309 | 563 | if (!empty($freezeOptions) && $button != 'skip') { |
6a488035 TO |
564 | $qf->addRule($elementName, ts('Sorry, this option is currently sold out.'), 'regex', "/" . implode('|', $allowedOptions) . "/"); |
565 | } | |
566 | break; | |
567 | ||
568 | case 'CheckBox': | |
569 | ||
570 | $check = array(); | |
571 | foreach ($customOption as $opId => $opt) { | |
9d1c9154 | 572 | $taxAmount = CRM_Utils_Array::value('tax_amount', $opt); |
535a8e1e | 573 | $count = CRM_Utils_Array::value('count', $opt, ''); |
6a488035 | 574 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
6a488035 TO |
575 | |
576 | if ($field->is_display_amounts) { | |
08b8b2d9 | 577 | $preHelpText = $postHelpText = ''; |
578 | if (isset($opt['help_pre'])) { | |
579 | $preHelpText = '<span class="crm-price-amount-help-pre description">' . $opt['help_pre'] . '</span>: '; | |
580 | } | |
581 | if (isset($opt['help_post'])) { | |
582 | $postHelpText = ': <span class="crm-price-amount-help-post description">' . $opt['help_post'] . '</span>'; | |
583 | } | |
e2d2aeab | 584 | $opt['label'] = '<span class="crm-price-amount-label">' . $opt['label'] . '</span> - '; |
3bd20e3f | 585 | if (isset($taxAmount) && $invoicing) { |
08b8b2d9 | 586 | $opt['label'] .= self::getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm); |
dc428161 | 587 | } |
588 | else { | |
08b8b2d9 | 589 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); |
dc428161 | 590 | } |
08b8b2d9 | 591 | $opt['label'] = $preHelpText . $opt['label'] . $postHelpText; |
6a488035 | 592 | } |
dc428161 | 593 | $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value)); |
6a488035 | 594 | $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'], |
6ea503d4 | 595 | array( |
353ffa53 TO |
596 | 'price' => json_encode(array($opt['id'], $priceVal)), |
597 | 'data-amount' => $opt[$valueFieldName], | |
598 | 'data-currency' => $currencyName, | |
6a488035 TO |
599 | ) |
600 | ); | |
601 | if ($is_pay_later) { | |
481a74f4 | 602 | $txtcheck[$opId] =& $qf->createElement('text', $opId, $opt['label'], array('size' => '4')); |
92fcb95f | 603 | $qf->addGroup($txtcheck, 'txt-' . $elementName, $label); |
6a488035 | 604 | } |
d2108f71 | 605 | // CRM-6902 - Add "max" option for a price set field |
1537c309 | 606 | if (in_array($opId, $freezeOptions)) { |
e4c6a396 | 607 | self::freezeIfEnabled($check[$opId], $customOption[$opId]); |
d2108f71 | 608 | // CRM-14696 - Improve display for sold out price set options |
be6dcd4e | 609 | $check[$opId]->setText('<span class="sold-out-option">' . $check[$opId]->getText() . ' (' . ts('Sold out') . ')</span>'); |
6a488035 TO |
610 | } |
611 | } | |
612 | $element = &$qf->addGroup($check, $elementName, $label); | |
613 | if ($useRequired && $field->is_required) { | |
614 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
615 | } | |
616 | break; | |
617 | } | |
618 | if (isset($qf->_online) && $qf->_online) { | |
619 | $element->freeze(); | |
620 | } | |
621 | } | |
622 | ||
623 | /** | |
fe482240 | 624 | * Retrieve a list of options for the specified field. |
6a488035 | 625 | * |
414c1420 TO |
626 | * @param int $fieldId |
627 | * Price field ID. | |
628 | * @param bool $inactiveNeeded | |
629 | * Include inactive options. | |
630 | * @param bool $reset | |
ba6920b3 | 631 | * Discard stored values. |
265807d1 PN |
632 | * @param bool $isDefaultContributionPriceSet |
633 | * Discard tax amount calculation for price set = default_contribution_amount. | |
6a488035 | 634 | * |
a6c01b45 CW |
635 | * @return array |
636 | * array of options | |
6a488035 | 637 | */ |
265807d1 | 638 | public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE, $isDefaultContributionPriceSet = FALSE) { |
6a488035 | 639 | static $options = array(); |
ba6920b3 | 640 | if ($reset) { |
641 | $options = array(); | |
057e7134 EM |
642 | // This would happen if the function was only called to clear the cache. |
643 | if (empty($fieldId)) { | |
644 | return array(); | |
645 | } | |
ba6920b3 | 646 | } |
6a488035 | 647 | |
ba6920b3 | 648 | if (empty($options[$fieldId])) { |
6a488035 | 649 | $values = array(); |
9da8dc8c | 650 | CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $values, 'weight', !$inactiveNeeded); |
6a488035 | 651 | $options[$fieldId] = $values; |
dc428161 | 652 | $taxRates = CRM_Core_PseudoConstant::getTaxRates(); |
653 | ||
654 | // ToDo - Code for Hook Invoke | |
655 | ||
656 | foreach ($options[$fieldId] as $priceFieldId => $priceFieldValues) { | |
265807d1 | 657 | if (isset($priceFieldValues['financial_type_id']) && array_key_exists($priceFieldValues['financial_type_id'], $taxRates) && !$isDefaultContributionPriceSet) { |
dc428161 | 658 | $options[$fieldId][$priceFieldId]['tax_rate'] = $taxRates[$priceFieldValues['financial_type_id']]; |
9d1c9154 | 659 | $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceFieldValues['amount'], $options[$fieldId][$priceFieldId]['tax_rate']); |
ac9def5a | 660 | $options[$fieldId][$priceFieldId]['tax_amount'] = $taxAmount['tax_amount']; |
dc428161 | 661 | } |
662 | } | |
6a488035 TO |
663 | } |
664 | ||
665 | return $options[$fieldId]; | |
666 | } | |
667 | ||
ffd93213 EM |
668 | /** |
669 | * @param $optionLabel | |
100fef9d | 670 | * @param int $fid |
ffd93213 EM |
671 | * |
672 | * @return mixed | |
673 | */ | |
6a488035 TO |
674 | public static function getOptionId($optionLabel, $fid) { |
675 | if (!$optionLabel || !$fid) { | |
676 | return; | |
677 | } | |
678 | ||
679 | $optionGroupName = "civicrm_price_field.amount.{$fid}"; | |
680 | ||
681 | $query = " | |
682 | SELECT | |
683 | option_value.id as id | |
684 | FROM | |
685 | civicrm_option_value option_value, | |
686 | civicrm_option_group option_group | |
687 | WHERE | |
535a8e1e CW |
688 | option_group.name = %1 |
689 | AND option_group.id = option_value.option_group_id | |
6a488035 TO |
690 | AND option_value.label = %2"; |
691 | ||
353ffa53 | 692 | $dao = CRM_Core_DAO::executeQuery($query, array( |
c5c263ca AH |
693 | 1 => array($optionGroupName, 'String'), |
694 | 2 => array($optionLabel, 'String'), | |
695 | )); | |
6a488035 TO |
696 | |
697 | while ($dao->fetch()) { | |
698 | return $dao->id; | |
699 | } | |
700 | } | |
701 | ||
702 | /** | |
703 | * Delete the price set field. | |
704 | * | |
414c1420 TO |
705 | * @param int $id |
706 | * Field Id. | |
6a488035 | 707 | * |
d5cc0fc2 | 708 | * @return bool |
6a488035 | 709 | * |
6a488035 TO |
710 | */ |
711 | public static function deleteField($id) { | |
9da8dc8c | 712 | $field = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
713 | $field->id = $id; |
714 | ||
715 | if ($field->find(TRUE)) { | |
716 | // delete the options for this field | |
9da8dc8c | 717 | CRM_Price_BAO_PriceFieldValue::deleteValues($id); |
6a488035 TO |
718 | |
719 | // reorder the weight before delete | |
720 | $fieldValues = array('price_set_id' => $field->price_set_id); | |
721 | ||
9da8dc8c | 722 | CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceField', $field->id, $fieldValues); |
6a488035 TO |
723 | |
724 | // now delete the field | |
725 | return $field->delete(); | |
726 | } | |
727 | ||
728 | return NULL; | |
729 | } | |
730 | ||
ffd93213 EM |
731 | /** |
732 | * @return array | |
733 | */ | |
00be9182 | 734 | public static function &htmlTypes() { |
6a488035 TO |
735 | static $htmlTypes = NULL; |
736 | if (!$htmlTypes) { | |
737 | $htmlTypes = array( | |
738 | 'Text' => ts('Text / Numeric Quantity'), | |
739 | 'Select' => ts('Select'), | |
740 | 'Radio' => ts('Radio'), | |
741 | 'CheckBox' => ts('CheckBox'), | |
742 | ); | |
743 | } | |
744 | return $htmlTypes; | |
745 | } | |
746 | ||
747 | /** | |
fe482240 | 748 | * Validate the priceset. |
6a488035 | 749 | * |
414c1420 TO |
750 | * @param int $priceSetId |
751 | * , array $fields. | |
6a488035 TO |
752 | * |
753 | * retrun the error string | |
754 | * | |
2a6da8d7 EM |
755 | * @param $fields |
756 | * @param $error | |
757 | * @param bool $allowNoneSelection | |
758 | * | |
6a488035 | 759 | */ |
6a488035 TO |
760 | public static function priceSetValidation($priceSetId, $fields, &$error, $allowNoneSelection = FALSE) { |
761 | // check for at least one positive | |
762 | // amount price field should be selected. | |
9da8dc8c | 763 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
764 | $priceField->price_set_id = $priceSetId; |
765 | $priceField->find(); | |
766 | ||
767 | $priceFields = array(); | |
768 | ||
769 | if ($allowNoneSelection) { | |
770 | $noneSelectedPriceFields = array(); | |
771 | } | |
772 | ||
773 | while ($priceField->fetch()) { | |
774 | $key = "price_{$priceField->id}"; | |
775 | ||
776 | if ($allowNoneSelection) { | |
777 | if (array_key_exists($key, $fields)) { | |
778 | if ($fields[$key] == 0 && !$priceField->is_required) { | |
779 | $noneSelectedPriceFields[] = $priceField->id; | |
780 | } | |
781 | } | |
782 | } | |
783 | ||
a7488080 | 784 | if (!empty($fields[$key])) { |
6a488035 TO |
785 | $priceFields[$priceField->id] = $fields[$key]; |
786 | } | |
787 | } | |
788 | ||
789 | if (!empty($priceFields)) { | |
790 | // we should has to have positive amount. | |
791 | $sql = " | |
792 | SELECT id, html_type | |
793 | FROM civicrm_price_field | |
794 | WHERE id IN (" . implode(',', array_keys($priceFields)) . ')'; | |
795 | $fieldDAO = CRM_Core_DAO::executeQuery($sql); | |
796 | $htmlTypes = array(); | |
797 | while ($fieldDAO->fetch()) { | |
798 | $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type; | |
799 | } | |
800 | ||
801 | $selectedAmounts = array(); | |
802 | ||
803 | foreach ($htmlTypes as $fieldId => $type) { | |
804 | $options = array(); | |
9da8dc8c | 805 | CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $options); |
6a488035 TO |
806 | |
807 | if (empty($options)) { | |
808 | continue; | |
809 | } | |
810 | ||
811 | if ($type == 'Text') { | |
812 | foreach ($options as $opId => $option) { | |
813 | $selectedAmounts[$opId] = $priceFields[$fieldId] * $option['amount']; | |
814 | break; | |
815 | } | |
816 | } | |
817 | elseif (is_array($fields["price_{$fieldId}"])) { | |
818 | foreach (array_keys($fields["price_{$fieldId}"]) as $opId) { | |
819 | $selectedAmounts[$opId] = $options[$opId]['amount']; | |
820 | } | |
821 | } | |
822 | elseif (in_array($fields["price_{$fieldId}"], array_keys($options))) { | |
823 | $selectedAmounts[$fields["price_{$fieldId}"]] = $options[$fields["price_{$fieldId}"]]['amount']; | |
824 | } | |
825 | } | |
826 | ||
827 | list($componentName) = explode(':', $fields['_qf_default']); | |
828 | // now we have all selected amount in hand. | |
829 | $totalAmount = array_sum($selectedAmounts); | |
830 | if ($totalAmount < 0) { | |
831 | $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', array(1 => $componentName)); | |
832 | } | |
833 | } | |
834 | else { | |
835 | if ($allowNoneSelection) { | |
836 | if (empty($noneSelectedPriceFields)) { | |
837 | $error['_qf_default'] = ts('Please select at least one option from price set.'); | |
838 | } | |
0db6c3e1 TO |
839 | } |
840 | else { | |
6a488035 TO |
841 | $error['_qf_default'] = ts('Please select at least one option from price set.'); |
842 | } | |
843 | } | |
844 | } | |
dc428161 | 845 | |
846 | /** | |
9d1c9154 | 847 | * Generate the label for price fields based on tax display setting option on CiviContribute Component Settings page. |
dc428161 | 848 | * |
9d1c9154 | 849 | * @param array $opt |
414c1420 TO |
850 | * @param string $valueFieldName |
851 | * Amount. | |
852 | * @param string $displayOpt | |
853 | * Tax display setting option. | |
dc428161 | 854 | * |
ad37ac8e | 855 | * @param string $taxTerm |
dc428161 | 856 | * |
ad37ac8e | 857 | * @return string |
858 | * Tax label for custom field. | |
dc428161 | 859 | */ |
5ebee375 | 860 | public static function getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm) { |
9d1c9154 | 861 | if ($displayOpt == 'Do_not_show') { |
862 | $label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']); | |
863 | } | |
4c9b6178 | 864 | elseif ($displayOpt == 'Inclusive') { |
9d1c9154 | 865 | $label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']); |
e2d2aeab | 866 | $label .= '<span class="crm-price-amount-tax"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>'; |
9d1c9154 | 867 | } |
868 | else { | |
869 | $label = CRM_Utils_Money::format($opt[$valueFieldName]); | |
86bfa4f6 | 870 | $label .= '<span class="crm-price-amount-tax"> + ' . CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>'; |
9d1c9154 | 871 | } |
dc428161 | 872 | |
9d1c9154 | 873 | return $label; |
dc428161 | 874 | } |
96025800 | 875 | |
6a488035 | 876 | } |