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