Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
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 | 245 | * @return null |
6a488035 | 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(); | |
353ffa53 | 275 | $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency, 'symbol', 'name'); |
6a488035 TO |
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 = '|'; | |
aaffa79f | 300 | $invoiceSettings = Civi::settings()->get('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 | } |
353ffa53 | 315 | $priceVal = implode($seperator, array( |
c5c263ca AH |
316 | $customOption[$optionKey][$valueFieldName] + $taxAmount, |
317 | $count, | |
318 | $max_value, | |
319 | )); | |
6a488035 TO |
320 | |
321 | $extra = array(); | |
15831617 | 322 | if (!empty($qf->_membershipBlock) && !empty($qf->_quickConfig) && $field->name == 'other_amount' && empty($qf->_contributionAmount)) { |
6a488035 | 323 | $useRequired = 0; |
366fe2a3 | 324 | } |
d5cc0fc2 | 325 | elseif (!empty($fieldOptions[$optionKey]['label'])) { |
326 | //check for label. | |
6a488035 | 327 | $label = $fieldOptions[$optionKey]['label']; |
0d7d6391 | 328 | if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount) && strtolower($fieldOptions[$optionKey]['name']) == 'other_amount') { |
2882b6a4 | 329 | $label .= ' ' . $currencySymbol; |
0d7d6391 | 330 | $qf->assign('priceset', $elementName); |
331 | $extra = array('onclick' => 'useAmountOther();'); | |
332 | } | |
6a488035 TO |
333 | } |
334 | ||
335 | $element = &$qf->add('text', $elementName, $label, | |
353ffa53 TO |
336 | array_merge($extra, |
337 | array( | |
338 | 'price' => json_encode(array($optionKey, $priceVal)), | |
339 | 'size' => '4', | |
340 | ) | |
341 | ), | |
342 | $useRequired && $field->is_required | |
6a488035 TO |
343 | ); |
344 | if ($is_pay_later) { | |
353ffa53 | 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)) { |
0dc0b759 | 350 | self::freezeIfEnabled($element, $fieldOptions[$optionKey]); |
79b152ac | 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'] . ' - ' : ''; |
5bfef8fa SL |
379 | $pre_help = isset($opt['help_pre']) ? |
380 | '<span class="crm-price-amount-help-pre">' . $opt['help_pre'] . '</span>: ' : ''; | |
381 | $post_help = isset($opt['help_post']) ? | |
382 | ': <span class="crm-price-amount-help-post">' . $opt['help_post'] . '</span>' : ''; | |
3bd20e3f | 383 | if (isset($taxAmount) && $invoicing) { |
dc428161 | 384 | if ($displayOpt == 'Do_not_show') { |
5bfef8fa | 385 | $opt['label'] = $pre_help . '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span>' . $post_help; |
dc428161 | 386 | } |
4c9b6178 | 387 | elseif ($displayOpt == 'Inclusive') { |
5bfef8fa SL |
388 | $opt['label'] = $pre_help . '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span>'; |
389 | $opt['label'] .= '<span class="crm-price-amount-tax"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>' . $post_help; | |
dc428161 | 390 | } |
391 | else { | |
5bfef8fa SL |
392 | $opt['label'] = $pre_help . '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span>'; |
393 | $opt['label'] .= '<span class="crm-price-amount-tax"> + ' . CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>' . $post_help; | |
dc428161 | 394 | } |
395 | } | |
396 | else { | |
5bfef8fa | 397 | $opt['label'] = $pre_help . '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span>' . $post_help; |
dc428161 | 398 | } |
6a488035 | 399 | } |
535a8e1e | 400 | $count = CRM_Utils_Array::value('count', $opt, ''); |
6a488035 | 401 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
dc428161 | 402 | $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value)); |
6ea503d4 | 403 | $extra = array( |
353ffa53 TO |
404 | 'price' => json_encode(array($elementName, $priceVal)), |
405 | 'data-amount' => $opt[$valueFieldName], | |
406 | 'data-currency' => $currencyName, | |
c23c87f6 | 407 | 'data-price-field-values' => json_encode($customOption), |
6a488035 | 408 | ); |
15831617 | 409 | if (!empty($qf->_quickConfig) && $field->name == 'contribution_amount') { |
6a488035 | 410 | $extra += array('onclick' => 'clearAmountOther();'); |
366fe2a3 | 411 | } |
15831617 | 412 | elseif (!empty($qf->_quickConfig) && $field->name == 'membership_amount') { |
6a488035 TO |
413 | $extra += array( |
414 | 'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});", | |
415 | 'membership-type' => $opt['membership_type_id'], | |
366fe2a3 | 416 | ); |
353ffa53 | 417 | $qf->assign('membershipFieldID', $field->id); |
6a488035 | 418 | } |
366fe2a3 | 419 | |
6a488035 | 420 | $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra); |
6a488035 | 421 | if ($is_pay_later) { |
353ffa53 | 422 | $qf->add('text', 'txt-' . $elementName, $label, array('size' => '4')); |
6a488035 TO |
423 | } |
424 | ||
d2108f71 | 425 | // CRM-6902 - Add "max" option for a price set field |
1537c309 | 426 | if (in_array($opId, $freezeOptions)) { |
e4c6a396 | 427 | self::freezeIfEnabled($choice[$opId], $customOption[$opId]); |
d2108f71 | 428 | // CRM-14696 - Improve display for sold out price set options |
be6dcd4e | 429 | $choice[$opId]->setText('<span class="sold-out-option">' . $choice[$opId]->getText() . ' (' . ts('Sold out') . ')</span>'); |
6a488035 TO |
430 | } |
431 | } | |
15831617 | 432 | if (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') { |
6a488035 | 433 | $choice[] = $qf->createElement('radio', NULL, '', ts('No thank you'), '-1', |
abcf506a | 434 | array( |
2975161b | 435 | 'price' => json_encode(array($elementName, '0|0')), |
436 | 'data-currency' => $currencyName, | |
abcf506a PN |
437 | 'onclick' => 'clearAmountOther();', |
438 | ) | |
6a488035 TO |
439 | ); |
440 | } | |
441 | ||
442 | if (!$field->is_required) { | |
443 | // add "none" option | |
a7488080 | 444 | if (!empty($otherAmount['is_allow_other_amount']) && $field->name == 'contribution_amount') { |
6a488035 | 445 | $none = ts('Other Amount'); |
366fe2a3 | 446 | } |
8cc574cf | 447 | elseif (!empty($qf->_membershipBlock) && empty($qf->_membershipBlock['is_required']) && $field->name == 'membership_amount') { |
6a488035 | 448 | $none = ts('No thank you'); |
366fe2a3 | 449 | } |
6a488035 TO |
450 | else { |
451 | $none = ts('- none -'); | |
452 | } | |
453 | ||
454 | $choice[] = $qf->createElement('radio', NULL, '', $none, '0', | |
455 | array('price' => json_encode(array($elementName, '0'))) | |
456 | ); | |
457 | } | |
458 | ||
459 | $element = &$qf->addGroup($choice, $elementName, $label); | |
460 | ||
461 | // make contribution field required for quick config when membership block is enabled | |
abcf506a | 462 | if (($field->name == 'membership_amount' || $field->name == 'contribution_amount') |
353ffa53 TO |
463 | && !empty($qf->_membershipBlock) && !$field->is_required |
464 | ) { | |
6a488035 TO |
465 | $useRequired = $field->is_required = TRUE; |
466 | } | |
467 | ||
468 | if ($useRequired && $field->is_required) { | |
469 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
470 | } | |
471 | break; | |
472 | ||
473 | case 'Select': | |
474 | $selectOption = $allowedOptions = $priceVal = array(); | |
475 | ||
476 | foreach ($customOption as $opt) { | |
9d1c9154 | 477 | $taxAmount = CRM_Utils_Array::value('tax_amount', $opt); |
6a488035 TO |
478 | $count = CRM_Utils_Array::value('count', $opt, ''); |
479 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); | |
6a488035 TO |
480 | |
481 | if ($field->is_display_amounts) { | |
482 | $opt['label'] .= ' - '; | |
353ffa53 | 483 | if (isset($taxAmount) && $invoicing) { |
e2d2aeab | 484 | $opt['label'] = $opt['label'] . self::getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm); |
dc428161 | 485 | } |
486 | else { | |
e2d2aeab | 487 | $opt['label'] = $opt['label'] . CRM_Utils_Money::format($opt[$valueFieldName]); |
dc428161 | 488 | } |
6a488035 | 489 | } |
79b152ac | 490 | |
dc428161 | 491 | $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value)); |
79b152ac | 492 | |
1537c309 | 493 | if (!in_array($opt['id'], $freezeOptions)) { |
6a488035 TO |
494 | $allowedOptions[] = $opt['id']; |
495 | } | |
d2108f71 | 496 | // CRM-14696 - Improve display for sold out price set options |
76991db6 | 497 | else { |
a14eff52 | 498 | $opt['id'] = 'crm_disabled_opt-' . $opt['id']; |
be6dcd4e | 499 | $opt['label'] = $opt['label'] . ' (' . ts('Sold out') . ')'; |
d2108f71 | 500 | } |
79b152ac | 501 | |
d2108f71 J |
502 | $selectOption[$opt['id']] = $opt['label']; |
503 | ||
6a488035 | 504 | if ($is_pay_later) { |
353ffa53 | 505 | $qf->add('text', 'txt-' . $elementName, $label, array('size' => '4')); |
6a488035 TO |
506 | } |
507 | } | |
79b152ac | 508 | |
6a488035 TO |
509 | $element = &$qf->add('select', $elementName, $label, |
510 | array( | |
d5cc0fc2 | 511 | '' => ts('- select -'), |
353ffa53 TO |
512 | ) + $selectOption, |
513 | $useRequired && $field->is_required, | |
c23c87f6 | 514 | array('price' => json_encode($priceVal), 'class' => 'crm-select2', 'data-price-field-values' => json_encode($customOption)) |
353ffa53 | 515 | ); |
6a488035 | 516 | |
d2108f71 | 517 | // CRM-6902 - Add "max" option for a price set field |
6a488035 | 518 | $button = substr($qf->controller->getButtonName(), -4); |
1537c309 | 519 | if (!empty($freezeOptions) && $button != 'skip') { |
6a488035 TO |
520 | $qf->addRule($elementName, ts('Sorry, this option is currently sold out.'), 'regex', "/" . implode('|', $allowedOptions) . "/"); |
521 | } | |
522 | break; | |
523 | ||
524 | case 'CheckBox': | |
525 | ||
526 | $check = array(); | |
527 | foreach ($customOption as $opId => $opt) { | |
9d1c9154 | 528 | $taxAmount = CRM_Utils_Array::value('tax_amount', $opt); |
535a8e1e | 529 | $count = CRM_Utils_Array::value('count', $opt, ''); |
6a488035 | 530 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
6a488035 TO |
531 | |
532 | if ($field->is_display_amounts) { | |
5bfef8fa SL |
533 | $pre_help = isset($opt['help_pre']) ? |
534 | '<span class="crm-price-amount-help-pre">' . $opt['help_pre'] . '</span>: ' : ''; | |
535 | $post_help = isset($opt['help_post']) ? | |
536 | ': <span class="crm-price-amount-help-post">' . $opt['help_post'] . '</span>' : ''; | |
e2d2aeab | 537 | $opt['label'] = '<span class="crm-price-amount-label">' . $opt['label'] . '</span> - '; |
3bd20e3f | 538 | if (isset($taxAmount) && $invoicing) { |
5bfef8fa | 539 | $opt['label'] .= $pre_help . self::getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm) . $post_help; |
dc428161 | 540 | } |
541 | else { | |
5bfef8fa | 542 | $opt['label'] .= $pre_help . CRM_Utils_Money::format($opt[$valueFieldName]) . $post_help; |
dc428161 | 543 | } |
6a488035 | 544 | } |
dc428161 | 545 | $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value)); |
6a488035 | 546 | $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'], |
6ea503d4 | 547 | array( |
353ffa53 TO |
548 | 'price' => json_encode(array($opt['id'], $priceVal)), |
549 | 'data-amount' => $opt[$valueFieldName], | |
550 | 'data-currency' => $currencyName, | |
6a488035 TO |
551 | ) |
552 | ); | |
553 | if ($is_pay_later) { | |
481a74f4 | 554 | $txtcheck[$opId] =& $qf->createElement('text', $opId, $opt['label'], array('size' => '4')); |
92fcb95f | 555 | $qf->addGroup($txtcheck, 'txt-' . $elementName, $label); |
6a488035 | 556 | } |
d2108f71 | 557 | // CRM-6902 - Add "max" option for a price set field |
1537c309 | 558 | if (in_array($opId, $freezeOptions)) { |
e4c6a396 | 559 | self::freezeIfEnabled($check[$opId], $customOption[$opId]); |
d2108f71 | 560 | // CRM-14696 - Improve display for sold out price set options |
be6dcd4e | 561 | $check[$opId]->setText('<span class="sold-out-option">' . $check[$opId]->getText() . ' (' . ts('Sold out') . ')</span>'); |
6a488035 TO |
562 | } |
563 | } | |
564 | $element = &$qf->addGroup($check, $elementName, $label); | |
565 | if ($useRequired && $field->is_required) { | |
566 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
567 | } | |
568 | break; | |
569 | } | |
570 | if (isset($qf->_online) && $qf->_online) { | |
571 | $element->freeze(); | |
572 | } | |
573 | } | |
574 | ||
575 | /** | |
fe482240 | 576 | * Retrieve a list of options for the specified field. |
6a488035 | 577 | * |
414c1420 TO |
578 | * @param int $fieldId |
579 | * Price field ID. | |
580 | * @param bool $inactiveNeeded | |
581 | * Include inactive options. | |
582 | * @param bool $reset | |
ba6920b3 | 583 | * Discard stored values. |
6a488035 | 584 | * |
a6c01b45 CW |
585 | * @return array |
586 | * array of options | |
6a488035 TO |
587 | */ |
588 | public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE) { | |
589 | static $options = array(); | |
ba6920b3 | 590 | if ($reset) { |
591 | $options = array(); | |
057e7134 EM |
592 | // This would happen if the function was only called to clear the cache. |
593 | if (empty($fieldId)) { | |
594 | return array(); | |
595 | } | |
ba6920b3 | 596 | } |
6a488035 | 597 | |
ba6920b3 | 598 | if (empty($options[$fieldId])) { |
6a488035 | 599 | $values = array(); |
9da8dc8c | 600 | CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $values, 'weight', !$inactiveNeeded); |
6a488035 | 601 | $options[$fieldId] = $values; |
dc428161 | 602 | $taxRates = CRM_Core_PseudoConstant::getTaxRates(); |
603 | ||
604 | // ToDo - Code for Hook Invoke | |
605 | ||
606 | foreach ($options[$fieldId] as $priceFieldId => $priceFieldValues) { | |
17ab42dd | 607 | if (isset($priceFieldValues['financial_type_id']) && array_key_exists($priceFieldValues['financial_type_id'], $taxRates)) { |
dc428161 | 608 | $options[$fieldId][$priceFieldId]['tax_rate'] = $taxRates[$priceFieldValues['financial_type_id']]; |
9d1c9154 | 609 | $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceFieldValues['amount'], $options[$fieldId][$priceFieldId]['tax_rate']); |
ac9def5a | 610 | $options[$fieldId][$priceFieldId]['tax_amount'] = $taxAmount['tax_amount']; |
dc428161 | 611 | } |
612 | } | |
6a488035 TO |
613 | } |
614 | ||
615 | return $options[$fieldId]; | |
616 | } | |
617 | ||
ffd93213 EM |
618 | /** |
619 | * @param $optionLabel | |
100fef9d | 620 | * @param int $fid |
ffd93213 EM |
621 | * |
622 | * @return mixed | |
623 | */ | |
6a488035 TO |
624 | public static function getOptionId($optionLabel, $fid) { |
625 | if (!$optionLabel || !$fid) { | |
626 | return; | |
627 | } | |
628 | ||
629 | $optionGroupName = "civicrm_price_field.amount.{$fid}"; | |
630 | ||
631 | $query = " | |
632 | SELECT | |
633 | option_value.id as id | |
634 | FROM | |
635 | civicrm_option_value option_value, | |
636 | civicrm_option_group option_group | |
637 | WHERE | |
535a8e1e CW |
638 | option_group.name = %1 |
639 | AND option_group.id = option_value.option_group_id | |
6a488035 TO |
640 | AND option_value.label = %2"; |
641 | ||
353ffa53 | 642 | $dao = CRM_Core_DAO::executeQuery($query, array( |
c5c263ca AH |
643 | 1 => array($optionGroupName, 'String'), |
644 | 2 => array($optionLabel, 'String'), | |
645 | )); | |
6a488035 TO |
646 | |
647 | while ($dao->fetch()) { | |
648 | return $dao->id; | |
649 | } | |
650 | } | |
651 | ||
652 | /** | |
653 | * Delete the price set field. | |
654 | * | |
414c1420 TO |
655 | * @param int $id |
656 | * Field Id. | |
6a488035 | 657 | * |
d5cc0fc2 | 658 | * @return bool |
6a488035 | 659 | * |
6a488035 TO |
660 | */ |
661 | public static function deleteField($id) { | |
9da8dc8c | 662 | $field = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
663 | $field->id = $id; |
664 | ||
665 | if ($field->find(TRUE)) { | |
666 | // delete the options for this field | |
9da8dc8c | 667 | CRM_Price_BAO_PriceFieldValue::deleteValues($id); |
6a488035 TO |
668 | |
669 | // reorder the weight before delete | |
670 | $fieldValues = array('price_set_id' => $field->price_set_id); | |
671 | ||
9da8dc8c | 672 | CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceField', $field->id, $fieldValues); |
6a488035 TO |
673 | |
674 | // now delete the field | |
675 | return $field->delete(); | |
676 | } | |
677 | ||
678 | return NULL; | |
679 | } | |
680 | ||
ffd93213 EM |
681 | /** |
682 | * @return array | |
683 | */ | |
00be9182 | 684 | public static function &htmlTypes() { |
6a488035 TO |
685 | static $htmlTypes = NULL; |
686 | if (!$htmlTypes) { | |
687 | $htmlTypes = array( | |
688 | 'Text' => ts('Text / Numeric Quantity'), | |
689 | 'Select' => ts('Select'), | |
690 | 'Radio' => ts('Radio'), | |
691 | 'CheckBox' => ts('CheckBox'), | |
692 | ); | |
693 | } | |
694 | return $htmlTypes; | |
695 | } | |
696 | ||
697 | /** | |
fe482240 | 698 | * Validate the priceset. |
6a488035 | 699 | * |
414c1420 TO |
700 | * @param int $priceSetId |
701 | * , array $fields. | |
6a488035 TO |
702 | * |
703 | * retrun the error string | |
704 | * | |
2a6da8d7 EM |
705 | * @param $fields |
706 | * @param $error | |
707 | * @param bool $allowNoneSelection | |
708 | * | |
6a488035 | 709 | */ |
6a488035 TO |
710 | public static function priceSetValidation($priceSetId, $fields, &$error, $allowNoneSelection = FALSE) { |
711 | // check for at least one positive | |
712 | // amount price field should be selected. | |
9da8dc8c | 713 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
714 | $priceField->price_set_id = $priceSetId; |
715 | $priceField->find(); | |
716 | ||
717 | $priceFields = array(); | |
718 | ||
719 | if ($allowNoneSelection) { | |
720 | $noneSelectedPriceFields = array(); | |
721 | } | |
722 | ||
723 | while ($priceField->fetch()) { | |
724 | $key = "price_{$priceField->id}"; | |
725 | ||
726 | if ($allowNoneSelection) { | |
727 | if (array_key_exists($key, $fields)) { | |
728 | if ($fields[$key] == 0 && !$priceField->is_required) { | |
729 | $noneSelectedPriceFields[] = $priceField->id; | |
730 | } | |
731 | } | |
732 | } | |
733 | ||
a7488080 | 734 | if (!empty($fields[$key])) { |
6a488035 TO |
735 | $priceFields[$priceField->id] = $fields[$key]; |
736 | } | |
737 | } | |
738 | ||
739 | if (!empty($priceFields)) { | |
740 | // we should has to have positive amount. | |
741 | $sql = " | |
742 | SELECT id, html_type | |
743 | FROM civicrm_price_field | |
744 | WHERE id IN (" . implode(',', array_keys($priceFields)) . ')'; | |
745 | $fieldDAO = CRM_Core_DAO::executeQuery($sql); | |
746 | $htmlTypes = array(); | |
747 | while ($fieldDAO->fetch()) { | |
748 | $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type; | |
749 | } | |
750 | ||
751 | $selectedAmounts = array(); | |
752 | ||
753 | foreach ($htmlTypes as $fieldId => $type) { | |
754 | $options = array(); | |
9da8dc8c | 755 | CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $options); |
6a488035 TO |
756 | |
757 | if (empty($options)) { | |
758 | continue; | |
759 | } | |
760 | ||
761 | if ($type == 'Text') { | |
762 | foreach ($options as $opId => $option) { | |
763 | $selectedAmounts[$opId] = $priceFields[$fieldId] * $option['amount']; | |
764 | break; | |
765 | } | |
766 | } | |
767 | elseif (is_array($fields["price_{$fieldId}"])) { | |
768 | foreach (array_keys($fields["price_{$fieldId}"]) as $opId) { | |
769 | $selectedAmounts[$opId] = $options[$opId]['amount']; | |
770 | } | |
771 | } | |
772 | elseif (in_array($fields["price_{$fieldId}"], array_keys($options))) { | |
773 | $selectedAmounts[$fields["price_{$fieldId}"]] = $options[$fields["price_{$fieldId}"]]['amount']; | |
774 | } | |
775 | } | |
776 | ||
777 | list($componentName) = explode(':', $fields['_qf_default']); | |
778 | // now we have all selected amount in hand. | |
779 | $totalAmount = array_sum($selectedAmounts); | |
780 | if ($totalAmount < 0) { | |
781 | $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', array(1 => $componentName)); | |
782 | } | |
783 | } | |
784 | else { | |
785 | if ($allowNoneSelection) { | |
786 | if (empty($noneSelectedPriceFields)) { | |
787 | $error['_qf_default'] = ts('Please select at least one option from price set.'); | |
788 | } | |
0db6c3e1 TO |
789 | } |
790 | else { | |
6a488035 TO |
791 | $error['_qf_default'] = ts('Please select at least one option from price set.'); |
792 | } | |
793 | } | |
794 | } | |
dc428161 | 795 | |
796 | /** | |
9d1c9154 | 797 | * Generate the label for price fields based on tax display setting option on CiviContribute Component Settings page. |
dc428161 | 798 | * |
9d1c9154 | 799 | * @param array $opt |
414c1420 TO |
800 | * @param string $valueFieldName |
801 | * Amount. | |
802 | * @param string $displayOpt | |
803 | * Tax display setting option. | |
dc428161 | 804 | * |
ad37ac8e | 805 | * @param string $taxTerm |
dc428161 | 806 | * |
ad37ac8e | 807 | * @return string |
808 | * Tax label for custom field. | |
dc428161 | 809 | */ |
5ebee375 | 810 | public static function getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm) { |
9d1c9154 | 811 | if ($displayOpt == 'Do_not_show') { |
812 | $label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']); | |
813 | } | |
4c9b6178 | 814 | elseif ($displayOpt == 'Inclusive') { |
9d1c9154 | 815 | $label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']); |
e2d2aeab | 816 | $label .= '<span class="crm-price-amount-tax"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>'; |
9d1c9154 | 817 | } |
818 | else { | |
819 | $label = CRM_Utils_Money::format($opt[$valueFieldName]); | |
86bfa4f6 | 820 | $label .= '<span class="crm-price-amount-tax"> + ' . CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>'; |
9d1c9154 | 821 | } |
dc428161 | 822 | |
9d1c9154 | 823 | return $label; |
dc428161 | 824 | } |
96025800 | 825 | |
6a488035 | 826 | } |