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