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