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