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