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