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 | */ | |
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) { | |
84 | $transaction = new CRM_Core_Transaction(); | |
85 | ||
86 | $priceField = self::add($params); | |
87 | ||
88 | if (is_a($priceField, 'CRM_Core_Error')) { | |
89 | $transaction->rollback(); | |
90 | return $priceField; | |
91 | } | |
92 | ||
93 | $options = $optionsIds = array(); | |
94 | $maxIndex = CRM_Price_Form_Field::NUM_OPTION; | |
95 | ||
96 | if ($priceField->html_type == 'Text') { | |
97 | $maxIndex = 1; | |
98 | ||
9da8dc8c | 99 | $fieldValue = new CRM_Price_DAO_PriceFieldValue(); |
6a488035 TO |
100 | $fieldValue->price_field_id = $priceField->id; |
101 | ||
102 | // update previous field values( if any ) | |
103 | if ($fieldValue->find(TRUE)) { | |
104 | $optionsIds['id'] = $fieldValue->id; | |
105 | } | |
106 | } | |
107 | $defaultArray = array(); | |
108 | if ($params['html_type'] == 'CheckBox' && isset($params['default_checkbox_option'])) { | |
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 | ||
147 | if (CRM_Utils_Array::value( $index, CRM_Utils_Array::value('option_financial_type_id', $params))) { | |
148 | $options['financial_type_id'] = $params['option_financial_type_id'][$index]; | |
a7488080 | 149 | } elseif (!empty($params['financial_type_id'])) { |
6a488035 TO |
150 | $options['financial_type_id'] = $params['financial_type_id']; |
151 | } | |
152 | ||
153 | if ($opIds = CRM_Utils_Array::value('option_id', $params)) { | |
154 | if ($opId = CRM_Utils_Array::value($index, $opIds)) { | |
155 | $optionsIds['id'] = $opId; | |
156 | } else { | |
157 | $optionsIds['id'] = NULL; | |
158 | } | |
159 | } | |
9da8dc8c | 160 | CRM_Price_BAO_PriceFieldValue::create($options, $optionsIds); |
6a488035 TO |
161 | } |
162 | } | |
163 | ||
164 | $transaction->commit(); | |
165 | return $priceField; | |
166 | } | |
167 | ||
168 | /** | |
169 | * Takes a bunch of params that are needed to match certain criteria and | |
170 | * retrieves the relevant objects. Typically the valid params are only | |
171 | * contact_id. We'll tweak this function to be more full featured over a period | |
172 | * of time. This is the inverse function of create. It also stores all the retrieved | |
173 | * values in the default array | |
174 | * | |
175 | * @param array $params (reference ) an assoc array of name/value pairs | |
176 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
177 | * | |
9da8dc8c | 178 | * @return object CRM_Price_DAO_PriceField object |
6a488035 TO |
179 | * @access public |
180 | * @static | |
181 | */ | |
182 | static function retrieve(&$params, &$defaults) { | |
9da8dc8c | 183 | return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceField', $params, $defaults); |
6a488035 TO |
184 | } |
185 | ||
186 | /** | |
187 | * update the is_active flag in the db | |
188 | * | |
189 | * @param int $id Id of the database record | |
190 | * @param boolean $is_active Value we want to set the is_active field | |
191 | * | |
192 | * @return Object DAO object on sucess, null otherwise | |
193 | * | |
194 | * @access public | |
195 | * @static | |
196 | */ | |
197 | static function setIsActive($id, $is_active) { | |
9da8dc8c | 198 | return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceField', $id, 'is_active', $is_active); |
6a488035 TO |
199 | } |
200 | ||
201 | /** | |
202 | * Get the field title. | |
203 | * | |
204 | * @param int $id id of field. | |
205 | * | |
206 | * @return string name | |
207 | * | |
208 | * @access public | |
209 | * @static | |
210 | * | |
211 | */ | |
212 | public static function getTitle($id) { | |
9da8dc8c | 213 | return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $id, 'label'); |
6a488035 TO |
214 | } |
215 | ||
216 | /** | |
217 | * This function for building custom fields | |
218 | * | |
77b97be7 EM |
219 | * @param object $qf form object (reference) |
220 | * @param string $elementName name of the custom field | |
221 | * @param $fieldId | |
6a488035 | 222 | * @param boolean $inactiveNeeded |
77b97be7 EM |
223 | * @param boolean $useRequired true if required else false |
224 | * @param string $label label for custom field | |
225 | * | |
226 | * @param null $fieldOptions | |
227 | * @param array $feezeOptions | |
6a488035 | 228 | * |
77b97be7 EM |
229 | * @return null |
230 | * @internal param bool $search true if used for search else false | |
6a488035 TO |
231 | * @access public |
232 | * @static | |
233 | */ | |
234 | public static function addQuickFormElement(&$qf, | |
235 | $elementName, | |
236 | $fieldId, | |
237 | $inactiveNeeded, | |
535a8e1e CW |
238 | $useRequired = TRUE, |
239 | $label = NULL, | |
6a488035 TO |
240 | $fieldOptions = NULL, |
241 | $feezeOptions = array() | |
242 | ) { | |
243 | ||
9da8dc8c | 244 | $field = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
245 | $field->id = $fieldId; |
246 | if (!$field->find(TRUE)) { | |
247 | /* FIXME: failure! */ | |
248 | return NULL; | |
249 | } | |
366fe2a3 | 250 | |
6a488035 TO |
251 | $is_pay_later = 0; |
252 | if (isset($qf->_mode) && empty($qf->_mode)) { | |
253 | $is_pay_later = 1; | |
254 | } | |
255 | elseif (isset($qf->_values)) { | |
256 | $is_pay_later = CRM_Utils_Array::value( 'is_pay_later', $qf->_values); | |
257 | } | |
258 | ||
259 | $otherAmount = $qf->get('values'); | |
260 | $config = CRM_Core_Config::singleton(); | |
261 | $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency',$config->defaultCurrency,'symbol','name'); | |
262 | $qf->assign('currencySymbol', $currencySymbol); | |
263 | // get currency name for price field and option attributes | |
264 | $currencyName = $config->defaultCurrency; | |
265 | ||
266 | if (!isset($label)) { | |
15831617 | 267 | $label = (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') ? ts('Additional Contribution') : $field->label; |
6a488035 TO |
268 | } |
269 | ||
270 | if ($field->name == 'contribution_amount') { | |
271 | $qf->_contributionAmount = 1; | |
272 | } | |
273 | ||
274 | if (isset($qf->_online) && $qf->_online) { | |
275 | $useRequired = FALSE; | |
276 | } | |
277 | ||
278 | $customOption = $fieldOptions; | |
279 | if (!is_array($customOption)) { | |
9da8dc8c | 280 | $customOption = CRM_Price_BAO_PriceField::getOptions($field->id, $inactiveNeeded); |
6a488035 TO |
281 | } |
282 | ||
283 | //use value field. | |
284 | $valueFieldName = 'amount'; | |
285 | $seperator = '|'; | |
286 | switch ($field->html_type) { | |
287 | case 'Text': | |
288 | $optionKey = key($customOption); | |
535a8e1e | 289 | $count = CRM_Utils_Array::value('count', $customOption[$optionKey], ''); |
6a488035 | 290 | $max_value = CRM_Utils_Array::value('max_value', $customOption[$optionKey], ''); |
535a8e1e | 291 | $priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName], $count, $max_value)); |
6a488035 TO |
292 | |
293 | $extra = array(); | |
15831617 | 294 | if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) { |
6a488035 TO |
295 | foreach($fieldOptions as &$fieldOption) { |
296 | if ($fieldOption['name'] == 'other_amount') { | |
297 | $fieldOption['label'] = $fieldOption['label'] . ' ' . $currencySymbol; | |
298 | } | |
299 | } | |
300 | $qf->assign('priceset', $elementName); | |
301 | $extra = array('onclick' => 'useAmountOther();'); | |
6a488035 TO |
302 | } |
303 | ||
15831617 | 304 | if (!empty($qf->_membershipBlock) && !empty($qf->_quickConfig) && $field->name == 'other_amount' && empty($qf->_contributionAmount)) { |
6a488035 | 305 | $useRequired = 0; |
366fe2a3 | 306 | } |
a7488080 | 307 | elseif (!empty($fieldOptions[$optionKey]['label'])) { //check for label. |
6a488035 TO |
308 | $label = $fieldOptions[$optionKey]['label']; |
309 | } | |
310 | ||
311 | $element = &$qf->add('text', $elementName, $label, | |
312 | array_merge($extra, | |
313 | array('price' => json_encode(array($optionKey, $priceVal)), | |
314 | 'size' => '4', | |
315 | ) | |
316 | ), | |
317 | $useRequired && $field->is_required | |
318 | ); | |
319 | if ($is_pay_later) { | |
320 | $qf->add( 'text', 'txt-'.$elementName, $label, array( 'size' => '4')); | |
321 | } | |
322 | ||
323 | // CRM-6902 | |
324 | if (in_array($optionKey, $feezeOptions)) { | |
325 | $element->freeze(); | |
326 | } | |
327 | ||
328 | //CRM-10117 | |
15831617 | 329 | if (!empty($qf->_quickConfig)) { |
6a488035 TO |
330 | $message = ts('Please enter a valid amount.'); |
331 | $type = 'money'; | |
366fe2a3 | 332 | } |
6a488035 TO |
333 | else { |
334 | $message = ts('%1 must be an integer (whole number).', array(1 => $label)); | |
335 | $type = 'positiveInteger'; | |
336 | } | |
337 | // integers will have numeric rule applied to them. | |
338 | $qf->addRule($elementName, $message, $type); | |
339 | break; | |
340 | ||
341 | case 'Radio': | |
342 | $choice = array(); | |
343 | ||
15831617 | 344 | if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) { |
6a488035 TO |
345 | $qf->assign('contriPriceset', $elementName); |
346 | } | |
347 | ||
348 | foreach ($customOption as $opId => $opt) { | |
349 | if ($field->is_display_amounts) { | |
0d8afee2 | 350 | $opt['label'] = !empty($opt['label']) ? $opt['label'] : ''; |
6a488035 TO |
351 | $opt['label'] = '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span> <span class="crm-price-amount-label">' . $opt['label'] . '</span>'; |
352 | } | |
535a8e1e | 353 | $count = CRM_Utils_Array::value('count', $opt, ''); |
6a488035 | 354 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
535a8e1e | 355 | $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); |
6a488035 TO |
356 | $extra = array('price' => json_encode(array($elementName, $priceVal)), |
357 | 'data-amount' => $opt[$valueFieldName], | |
358 | 'data-currency' => $currencyName, | |
359 | ); | |
15831617 | 360 | if (!empty($qf->_quickConfig) && $field->name == 'contribution_amount') { |
6a488035 | 361 | $extra += array('onclick' => 'clearAmountOther();'); |
366fe2a3 | 362 | } |
15831617 | 363 | elseif (!empty($qf->_quickConfig) && $field->name == 'membership_amount') { |
6a488035 TO |
364 | $extra += array( |
365 | 'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});", | |
366 | 'membership-type' => $opt['membership_type_id'], | |
366fe2a3 | 367 | ); |
6a488035 TO |
368 | $qf->assign('membershipFieldID',$field->id); |
369 | } | |
366fe2a3 | 370 | |
6a488035 TO |
371 | $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra); |
372 | ||
373 | if ($is_pay_later) { | |
374 | $qf->add( 'text', 'txt-'.$elementName, $label, array( 'size' => '4')); | |
375 | } | |
376 | ||
377 | // CRM-6902 | |
378 | if (in_array($opId, $feezeOptions)) { | |
379 | $choice[$opId]->freeze(); | |
380 | } | |
381 | } | |
15831617 | 382 | if (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') { |
6a488035 | 383 | $choice[] = $qf->createElement('radio', NULL, '', ts('No thank you'), '-1', |
abcf506a PN |
384 | array( |
385 | 'onclick' => 'clearAmountOther();', | |
386 | ) | |
6a488035 TO |
387 | ); |
388 | } | |
389 | ||
390 | if (!$field->is_required) { | |
391 | // add "none" option | |
a7488080 | 392 | if (!empty($otherAmount['is_allow_other_amount']) && $field->name == 'contribution_amount') { |
6a488035 | 393 | $none = ts('Other Amount'); |
366fe2a3 | 394 | } |
8cc574cf | 395 | elseif (!empty($qf->_membershipBlock) && empty($qf->_membershipBlock['is_required']) && $field->name == 'membership_amount') { |
6a488035 | 396 | $none = ts('No thank you'); |
366fe2a3 | 397 | } |
6a488035 TO |
398 | else { |
399 | $none = ts('- none -'); | |
400 | } | |
401 | ||
402 | $choice[] = $qf->createElement('radio', NULL, '', $none, '0', | |
403 | array('price' => json_encode(array($elementName, '0'))) | |
404 | ); | |
405 | } | |
406 | ||
407 | $element = &$qf->addGroup($choice, $elementName, $label); | |
408 | ||
409 | // make contribution field required for quick config when membership block is enabled | |
abcf506a | 410 | if (($field->name == 'membership_amount' || $field->name == 'contribution_amount') |
15831617 | 411 | && !empty($qf->_membershipBlock) && !$field->is_required) { |
6a488035 TO |
412 | $useRequired = $field->is_required = TRUE; |
413 | } | |
414 | ||
415 | if ($useRequired && $field->is_required) { | |
416 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
417 | } | |
418 | break; | |
419 | ||
420 | case 'Select': | |
421 | $selectOption = $allowedOptions = $priceVal = array(); | |
422 | ||
423 | foreach ($customOption as $opt) { | |
424 | $count = CRM_Utils_Array::value('count', $opt, ''); | |
425 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); | |
426 | $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); | |
427 | ||
428 | if ($field->is_display_amounts) { | |
429 | $opt['label'] .= ' - '; | |
430 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); | |
431 | } | |
432 | $selectOption[$opt['id']] = $opt['label']; | |
433 | ||
434 | if (!in_array($opt['id'], $feezeOptions)) { | |
435 | $allowedOptions[] = $opt['id']; | |
436 | } | |
437 | if ($is_pay_later) { | |
438 | $qf->add( 'text', 'txt-'.$elementName, $label, array( 'size' => '4')); | |
439 | } | |
440 | } | |
441 | $element = &$qf->add('select', $elementName, $label, | |
442 | array( | |
443 | '' => ts('- select -')) + $selectOption, | |
444 | $useRequired && $field->is_required, | |
445 | array('price' => json_encode($priceVal)) | |
446 | ); | |
447 | ||
448 | // CRM-6902 | |
449 | $button = substr($qf->controller->getButtonName(), -4); | |
450 | if (!empty($feezeOptions) && $button != 'skip') { | |
451 | $qf->addRule($elementName, ts('Sorry, this option is currently sold out.'), 'regex', "/" . implode('|', $allowedOptions) . "/"); | |
452 | } | |
453 | break; | |
454 | ||
455 | case 'CheckBox': | |
456 | ||
457 | $check = array(); | |
458 | foreach ($customOption as $opId => $opt) { | |
535a8e1e | 459 | $count = CRM_Utils_Array::value('count', $opt, ''); |
6a488035 | 460 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
535a8e1e | 461 | $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); |
6a488035 TO |
462 | |
463 | if ($field->is_display_amounts) { | |
464 | $opt['label'] .= ' - '; | |
465 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); | |
466 | } | |
467 | $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'], | |
468 | array('price' => json_encode(array($opt['id'], $priceVal)), | |
469 | 'data-amount' => $opt[$valueFieldName], | |
470 | 'data-currency' => $currencyName, | |
471 | ) | |
472 | ); | |
473 | if ($is_pay_later) { | |
474 | $txtcheck[$opId] =& $qf->createElement( 'text', $opId, $opt['label'], array( 'size' => '4' ) ); | |
475 | $qf->addGroup($txtcheck, 'txt-'.$elementName, $label); | |
476 | } | |
477 | // CRM-6902 | |
478 | if (in_array($opId, $feezeOptions)) { | |
479 | $check[$opId]->freeze(); | |
480 | } | |
481 | } | |
482 | $element = &$qf->addGroup($check, $elementName, $label); | |
483 | if ($useRequired && $field->is_required) { | |
484 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
485 | } | |
486 | break; | |
487 | } | |
488 | if (isset($qf->_online) && $qf->_online) { | |
489 | $element->freeze(); | |
490 | } | |
491 | } | |
492 | ||
493 | /** | |
494 | * Retrieve a list of options for the specified field | |
495 | * | |
496 | * @param int $fieldId price field ID | |
497 | * @param bool $inactiveNeeded include inactive options | |
498 | * @param bool $reset ignore stored values\ | |
499 | * | |
500 | * @return array array of options | |
501 | */ | |
502 | public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE) { | |
503 | static $options = array(); | |
504 | ||
505 | if ($reset || empty($options[$fieldId])) { | |
506 | $values = array(); | |
9da8dc8c | 507 | CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $values, 'weight', !$inactiveNeeded); |
6a488035 TO |
508 | $options[$fieldId] = $values; |
509 | } | |
510 | ||
511 | return $options[$fieldId]; | |
512 | } | |
513 | ||
514 | public static function getOptionId($optionLabel, $fid) { | |
515 | if (!$optionLabel || !$fid) { | |
516 | return; | |
517 | } | |
518 | ||
519 | $optionGroupName = "civicrm_price_field.amount.{$fid}"; | |
520 | ||
521 | $query = " | |
522 | SELECT | |
523 | option_value.id as id | |
524 | FROM | |
525 | civicrm_option_value option_value, | |
526 | civicrm_option_group option_group | |
527 | WHERE | |
535a8e1e CW |
528 | option_group.name = %1 |
529 | AND option_group.id = option_value.option_group_id | |
6a488035 TO |
530 | AND option_value.label = %2"; |
531 | ||
532 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($optionGroupName, 'String'), 2 => array($optionLabel, 'String'))); | |
533 | ||
534 | while ($dao->fetch()) { | |
535 | return $dao->id; | |
536 | } | |
537 | } | |
538 | ||
539 | /** | |
540 | * Delete the price set field. | |
541 | * | |
542 | * @param int $id Field Id | |
543 | * | |
544 | * @return boolean | |
545 | * | |
546 | * @access public | |
547 | * @static | |
548 | * | |
549 | */ | |
550 | public static function deleteField($id) { | |
9da8dc8c | 551 | $field = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
552 | $field->id = $id; |
553 | ||
554 | if ($field->find(TRUE)) { | |
555 | // delete the options for this field | |
9da8dc8c | 556 | CRM_Price_BAO_PriceFieldValue::deleteValues($id); |
6a488035 TO |
557 | |
558 | // reorder the weight before delete | |
559 | $fieldValues = array('price_set_id' => $field->price_set_id); | |
560 | ||
9da8dc8c | 561 | CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceField', $field->id, $fieldValues); |
6a488035 TO |
562 | |
563 | // now delete the field | |
564 | return $field->delete(); | |
565 | } | |
566 | ||
567 | return NULL; | |
568 | } | |
569 | ||
570 | static function &htmlTypes() { | |
571 | static $htmlTypes = NULL; | |
572 | if (!$htmlTypes) { | |
573 | $htmlTypes = array( | |
574 | 'Text' => ts('Text / Numeric Quantity'), | |
575 | 'Select' => ts('Select'), | |
576 | 'Radio' => ts('Radio'), | |
577 | 'CheckBox' => ts('CheckBox'), | |
578 | ); | |
579 | } | |
580 | return $htmlTypes; | |
581 | } | |
582 | ||
583 | /** | |
584 | * Validate the priceset | |
585 | * | |
2a6da8d7 | 586 | * @param int $priceSetId , array $fields |
6a488035 TO |
587 | * |
588 | * retrun the error string | |
589 | * | |
2a6da8d7 EM |
590 | * @param $fields |
591 | * @param $error | |
592 | * @param bool $allowNoneSelection | |
593 | * | |
6a488035 TO |
594 | * @access public |
595 | * @static | |
6a488035 TO |
596 | */ |
597 | ||
598 | public static function priceSetValidation($priceSetId, $fields, &$error, $allowNoneSelection = FALSE) { | |
599 | // check for at least one positive | |
600 | // amount price field should be selected. | |
9da8dc8c | 601 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
602 | $priceField->price_set_id = $priceSetId; |
603 | $priceField->find(); | |
604 | ||
605 | $priceFields = array(); | |
606 | ||
607 | if ($allowNoneSelection) { | |
608 | $noneSelectedPriceFields = array(); | |
609 | } | |
610 | ||
611 | while ($priceField->fetch()) { | |
612 | $key = "price_{$priceField->id}"; | |
613 | ||
614 | if ($allowNoneSelection) { | |
615 | if (array_key_exists($key, $fields)) { | |
616 | if ($fields[$key] == 0 && !$priceField->is_required) { | |
617 | $noneSelectedPriceFields[] = $priceField->id; | |
618 | } | |
619 | } | |
620 | } | |
621 | ||
a7488080 | 622 | if (!empty($fields[$key])) { |
6a488035 TO |
623 | $priceFields[$priceField->id] = $fields[$key]; |
624 | } | |
625 | } | |
626 | ||
627 | if (!empty($priceFields)) { | |
628 | // we should has to have positive amount. | |
629 | $sql = " | |
630 | SELECT id, html_type | |
631 | FROM civicrm_price_field | |
632 | WHERE id IN (" . implode(',', array_keys($priceFields)) . ')'; | |
633 | $fieldDAO = CRM_Core_DAO::executeQuery($sql); | |
634 | $htmlTypes = array(); | |
635 | while ($fieldDAO->fetch()) { | |
636 | $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type; | |
637 | } | |
638 | ||
639 | $selectedAmounts = array(); | |
640 | ||
641 | foreach ($htmlTypes as $fieldId => $type) { | |
642 | $options = array(); | |
9da8dc8c | 643 | CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $options); |
6a488035 TO |
644 | |
645 | if (empty($options)) { | |
646 | continue; | |
647 | } | |
648 | ||
649 | if ($type == 'Text') { | |
650 | foreach ($options as $opId => $option) { | |
651 | $selectedAmounts[$opId] = $priceFields[$fieldId] * $option['amount']; | |
652 | break; | |
653 | } | |
654 | } | |
655 | elseif (is_array($fields["price_{$fieldId}"])) { | |
656 | foreach (array_keys($fields["price_{$fieldId}"]) as $opId) { | |
657 | $selectedAmounts[$opId] = $options[$opId]['amount']; | |
658 | } | |
659 | } | |
660 | elseif (in_array($fields["price_{$fieldId}"], array_keys($options))) { | |
661 | $selectedAmounts[$fields["price_{$fieldId}"]] = $options[$fields["price_{$fieldId}"]]['amount']; | |
662 | } | |
663 | } | |
664 | ||
665 | list($componentName) = explode(':', $fields['_qf_default']); | |
666 | // now we have all selected amount in hand. | |
667 | $totalAmount = array_sum($selectedAmounts); | |
668 | if ($totalAmount < 0) { | |
669 | $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', array(1 => $componentName)); | |
670 | } | |
671 | } | |
672 | else { | |
673 | if ($allowNoneSelection) { | |
674 | if (empty($noneSelectedPriceFields)) { | |
675 | $error['_qf_default'] = ts('Please select at least one option from price set.'); | |
676 | } | |
677 | } else { | |
678 | $error['_qf_default'] = ts('Please select at least one option from price set.'); | |
679 | } | |
680 | } | |
681 | } | |
682 | } | |
683 |