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