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 | 180 | * |
2b37475d | 181 | * @return Object DAO object on sucess, null otherwise |
6a488035 | 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; | |
0db6c3e1 TO |
285 | } |
286 | elseif (!empty($fieldOptions[$optionKey]['label'])) { //check for label. | |
e418776c | 287 | $label = $fieldOptions[$optionKey]['label']; |
6a488035 TO |
288 | } |
289 | ||
290 | if ($field->is_display_amounts) { | |
291 | $label .= ' - '; | |
292 | $label .= CRM_Utils_Money::format(CRM_Utils_Array::value($valueFieldName, $customOption[$optionKey])); | |
293 | } | |
294 | ||
295 | $element = &$qf->add('text', $elementName, $label, | |
296 | array_merge($extra, | |
e418776c TO |
297 | array( |
298 | 'price' => json_encode(array($optionKey, $priceVal)), | |
6a488035 TO |
299 | 'size' => '4', |
300 | ) | |
301 | ), | |
302 | $useRequired && $field->is_required | |
303 | ); | |
304 | ||
305 | // CRM-6902 | |
c490a46a | 306 | if (in_array($optionKey, $freezeOptions)) { |
6a488035 TO |
307 | $element->freeze(); |
308 | } | |
309 | ||
310 | //CRM-10117 | |
311 | if (property_exists($qf, '_quickConfig') && $qf->_quickConfig) { | |
312 | $message = ts("Please enter a valid amount."); | |
313 | $type = "money"; | |
0db6c3e1 TO |
314 | } |
315 | else { | |
6a488035 TO |
316 | $message = ts('%1 must be an integer (whole number).', array(1 => $label)); |
317 | $type = "positiveInteger"; | |
318 | } | |
319 | // integers will have numeric rule applied to them. | |
320 | $qf->addRule($elementName, $message, $type); | |
321 | break; | |
322 | ||
323 | case 'Radio': | |
324 | $choice = array(); | |
325 | ||
e418776c | 326 | if (property_exists($qf, '_quickConfig') && $qf->_quickConfig && property_exists($qf, '_contributionAmount') && $qf->_contributionAmount) { |
6a488035 TO |
327 | $qf->assign('contriPriceset', $elementName); |
328 | } | |
329 | ||
330 | foreach ($customOption as $opId => $opt) { | |
331 | if ($field->is_display_amounts) { | |
0d8afee2 | 332 | $opt['label'] = !empty($opt['label']) ? $opt['label'] . ' - ' : ''; |
6a488035 TO |
333 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); |
334 | } | |
335 | $count = CRM_Utils_Array::value('count', $opt, ''); | |
336 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); | |
337 | $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); | |
e418776c TO |
338 | $extra = array( |
339 | 'price' => json_encode(array($elementName, $priceVal)), | |
6a488035 TO |
340 | 'data-amount' => $opt[$valueFieldName], |
341 | 'data-currency' => $currencyName, | |
342 | ); | |
343 | if (property_exists($qf, '_quickConfig') && $qf->_quickConfig && $field->name == 'contribution_amount') { | |
344 | $extra += array('onclick' => 'clearAmountOther();'); | |
0db6c3e1 TO |
345 | } |
346 | elseif (property_exists($qf, '_quickConfig') && $qf->_quickConfig && $field->name == 'membership_amount') { | |
e418776c TO |
347 | $extra += array( |
348 | 'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});", | |
6a488035 TO |
349 | 'membership-type' => $opt['membership_type_id'], |
350 | ); | |
e418776c | 351 | $qf->assign('membershipFieldID', $field->id); |
6a488035 TO |
352 | } |
353 | $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra); | |
354 | ||
355 | // CRM-6902 | |
c490a46a | 356 | if (in_array($opId, $freezeOptions)) { |
6a488035 TO |
357 | $choice[$opId]->freeze(); |
358 | } | |
359 | } | |
360 | ||
8cc574cf | 361 | if (property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock['is_separate_payment']) && $field->name == 'contribution_amount') { |
6a488035 TO |
362 | $choice[] = $qf->createElement('radio', NULL, '', 'No thank you', '-1', |
363 | array( | |
364 | 'onclick' => 'clearAmountOther();', | |
365 | ) | |
366 | ); | |
367 | } | |
368 | ||
369 | if (!$field->is_required) { | |
370 | // add "none" option | |
a7488080 | 371 | if (!empty($otherAmount['is_allow_other_amount']) && $field->name == 'contribution_amount') { |
6a488035 | 372 | $none = ts('Other Amount'); |
0db6c3e1 TO |
373 | } |
374 | elseif (property_exists($qf, '_membershipBlock') && empty($qf->_membershipBlock['is_required']) && $field->name == 'membership_amount') { | |
6a488035 | 375 | $none = ts('No thank you'); |
0db6c3e1 TO |
376 | } |
377 | else { | |
6a488035 TO |
378 | $none = ts('-none-'); |
379 | } | |
380 | ||
381 | $choice[] = $qf->createElement('radio', NULL, '', $none, '0', | |
382 | array('price' => json_encode(array($elementName, "0"))) | |
383 | ); | |
384 | } | |
385 | ||
386 | $element = &$qf->addGroup($choice, $elementName, $label); | |
387 | ||
388 | // make contribution field required for quick config when membership block is enabled | |
389 | if (($field->name == 'contribution_amount' || $field->name == 'membership_amount') && property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock) && !$field->is_required) { | |
390 | $useRequired = $field->is_required = TRUE; | |
391 | } | |
392 | ||
393 | if ($useRequired && $field->is_required) { | |
394 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
395 | } | |
396 | break; | |
397 | ||
398 | case 'Select': | |
399 | $selectOption = $allowedOptions = $priceVal = array(); | |
400 | ||
401 | foreach ($customOption as $opt) { | |
402 | $count = CRM_Utils_Array::value('count', $opt, ''); | |
403 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); | |
404 | $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); | |
405 | ||
406 | if ($field->is_display_amounts) { | |
407 | $opt['label'] .= ' - '; | |
408 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); | |
409 | } | |
410 | $selectOption[$opt['id']] = $opt['label']; | |
411 | ||
c490a46a | 412 | if (!in_array($opt['id'], $freezeOptions)) { |
6a488035 TO |
413 | $allowedOptions[] = $opt['id']; |
414 | } | |
415 | } | |
416 | $element = &$qf->add('select', $elementName, $label, | |
417 | array( | |
418 | '' => ts('- select -')) + $selectOption, | |
419 | $useRequired && $field->is_required, | |
420 | array('price' => json_encode($priceVal)) | |
421 | ); | |
422 | ||
423 | // CRM-6902 | |
424 | $button = substr($qf->controller->getButtonName(), -4); | |
c490a46a | 425 | if (!empty($freezeOptions) && $button != 'skip') { |
6a488035 TO |
426 | $qf->addRule($elementName, ts('Sorry, this option is currently sold out.'), 'regex', "/" . implode('|', $allowedOptions) . "/"); |
427 | } | |
428 | break; | |
429 | ||
430 | case 'CheckBox': | |
431 | ||
432 | $check = array(); | |
433 | foreach ($customOption as $opId => $opt) { | |
434 | $count = CRM_Utils_Array::value('count', $opt, ''); | |
435 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); | |
436 | $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); | |
437 | ||
438 | if ($field->is_display_amounts) { | |
439 | $opt['label'] .= ' - '; | |
440 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); | |
441 | } | |
442 | $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'], | |
e418776c TO |
443 | array( |
444 | 'price' => json_encode(array($opt['id'], $priceVal)), | |
6a488035 TO |
445 | 'data-amount' => $opt[$valueFieldName], |
446 | 'data-currency' => $currencyName, | |
447 | ) | |
448 | ); | |
449 | ||
450 | // CRM-6902 | |
c490a46a | 451 | if (in_array($opId, $freezeOptions)) { |
6a488035 TO |
452 | $check[$opId]->freeze(); |
453 | } | |
454 | } | |
455 | $element = &$qf->addGroup($check, $elementName, $label); | |
456 | if ($useRequired && $field->is_required) { | |
457 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
458 | } | |
459 | break; | |
460 | } | |
461 | if (isset($qf->_online) && $qf->_online) { | |
462 | $element->freeze(); | |
463 | } | |
464 | } | |
465 | ||
466 | /** | |
467 | * Retrieve a list of options for the specified field | |
468 | * | |
c68f8bfa TO |
469 | * @param int $fieldId |
470 | * Price field ID. | |
471 | * @param bool $inactiveNeeded | |
472 | * Include inactive options. | |
473 | * @param bool $reset | |
474 | * Ignore stored values\. | |
6a488035 TO |
475 | * |
476 | * @return array array of options | |
477 | */ | |
478 | public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE) { | |
479 | static $options = array(); | |
480 | ||
481 | if ($reset || empty($options[$fieldId])) { | |
482 | $values = array(); | |
483 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::getValues($fieldId, $values, 'weight', !$inactiveNeeded); | |
484 | $options[$fieldId] = $values; | |
485 | } | |
486 | ||
487 | return $options[$fieldId]; | |
488 | } | |
489 | ||
624e56fa EM |
490 | /** |
491 | * @param $optionLabel | |
100fef9d | 492 | * @param int $fid |
624e56fa EM |
493 | * |
494 | * @return mixed | |
495 | */ | |
6a488035 TO |
496 | public static function getOptionId($optionLabel, $fid) { |
497 | if (!$optionLabel || !$fid) { | |
498 | return; | |
499 | } | |
500 | ||
501 | $optionGroupName = "civicrm_price_field.amount.{$fid}"; | |
502 | ||
503 | $query = " | |
504 | SELECT | |
505 | option_value.id as id | |
506 | FROM | |
507 | civicrm_option_value option_value, | |
508 | civicrm_option_group option_group | |
509 | WHERE | |
510 | option_group.name = %1 | |
511 | AND option_group.id = option_value.option_group_id | |
512 | AND option_value.label = %2"; | |
513 | ||
514 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($optionGroupName, 'String'), 2 => array($optionLabel, 'String'))); | |
515 | ||
516 | while ($dao->fetch()) { | |
517 | return $dao->id; | |
518 | } | |
519 | } | |
520 | ||
521 | /** | |
522 | * Delete the price set field. | |
523 | * | |
c68f8bfa TO |
524 | * @param int $id |
525 | * Field Id. | |
6a488035 | 526 | * |
2b37475d | 527 | * @return boolean |
6a488035 | 528 | * |
6a488035 TO |
529 | * @static |
530 | * | |
531 | */ | |
532 | public static function deleteField($id) { | |
533 | $field = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field(); | |
534 | $field->id = $id; | |
535 | ||
536 | if ($field->find(TRUE)) { | |
537 | // delete the options for this field | |
538 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::deleteValues($id); | |
539 | ||
540 | // reorder the weight before delete | |
541 | $fieldValues = array('price_set_id' => $field->price_set_id); | |
542 | ||
543 | CRM_Utils_Weight::delWeight('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $field->id, $fieldValues); | |
544 | ||
545 | // now delete the field | |
546 | return $field->delete(); | |
547 | } | |
548 | ||
549 | return NULL; | |
550 | } | |
551 | ||
624e56fa EM |
552 | /** |
553 | * @return array | |
554 | */ | |
00be9182 | 555 | public static function &htmlTypes() { |
6a488035 TO |
556 | static $htmlTypes = NULL; |
557 | if (!$htmlTypes) { | |
558 | $htmlTypes = array( | |
559 | 'Text' => ts('Text / Numeric Quantity'), | |
560 | 'Select' => ts('Select'), | |
561 | 'Radio' => ts('Radio'), | |
562 | 'CheckBox' => ts('CheckBox'), | |
563 | ); | |
564 | } | |
565 | return $htmlTypes; | |
566 | } | |
567 | ||
568 | /** | |
569 | * Validate the priceset | |
570 | * | |
c68f8bfa TO |
571 | * @param int $priceSetId |
572 | * , array $fields. | |
6a488035 TO |
573 | * |
574 | * retrun the error string | |
575 | * | |
2a6da8d7 EM |
576 | * @param $fields |
577 | * @param $error | |
578 | * | |
6a488035 | 579 | * @static |
6a488035 TO |
580 | */ |
581 | ||
582 | public static function priceSetValidation($priceSetId, $fields, &$error) { | |
583 | // check for at least one positive | |
584 | // amount price field should be selected. | |
585 | $priceField = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field(); | |
586 | $priceField->price_set_id = $priceSetId; | |
587 | $priceField->find(); | |
588 | ||
589 | $priceFields = array(); | |
590 | ||
591 | while ($priceField->fetch()) { | |
592 | $key = "price_{$priceField->id}"; | |
a7488080 | 593 | if (!empty($fields[$key])) { |
6a488035 TO |
594 | $priceFields[$priceField->id] = $fields[$key]; |
595 | } | |
596 | } | |
597 | ||
598 | if (!empty($priceFields)) { | |
599 | // we should has to have positive amount. | |
600 | $sql = " | |
601 | SELECT id, html_type | |
602 | FROM civicrm_price_field | |
603 | WHERE id IN (" . implode(',', array_keys($priceFields)) . ')'; | |
604 | $fieldDAO = CRM_Core_DAO::executeQuery($sql); | |
605 | $htmlTypes = array(); | |
606 | while ($fieldDAO->fetch()) { | |
607 | $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type; | |
608 | } | |
609 | ||
610 | $selectedAmounts = array(); | |
611 | ||
612 | foreach ($htmlTypes as $fieldId => $type) { | |
613 | $options = array(); | |
614 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::getValues($fieldId, $options); | |
615 | ||
616 | if (empty($options)) { | |
617 | ||
618 | continue; | |
619 | ||
620 | } | |
621 | ||
622 | if ($type == 'Text') { | |
623 | foreach ($options as $opId => $option) { | |
624 | $selectedAmounts[$opId] = $priceFields[$fieldId] * $option['amount']; | |
625 | break; | |
626 | } | |
627 | } | |
628 | elseif (is_array($fields["price_{$fieldId}"])) { | |
629 | foreach (array_keys($fields["price_{$fieldId}"]) as $opId) { | |
630 | $selectedAmounts[$opId] = $options[$opId]['amount']; | |
631 | } | |
632 | } | |
633 | elseif (in_array($fields["price_{$fieldId}"], array_keys($options))) { | |
634 | $selectedAmounts[$fields["price_{$fieldId}"]] = $options[$fields["price_{$fieldId}"]]['amount']; | |
635 | } | |
636 | } | |
637 | ||
638 | list($componentName) = explode(':', $fields['_qf_default']); | |
639 | // now we have all selected amount in hand. | |
640 | $totalAmount = array_sum($selectedAmounts); | |
641 | if ($totalAmount < 0) { | |
642 | $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', array(1 => $componentName)); | |
643 | } | |
644 | } | |
645 | else { | |
646 | $error['_qf_default'] = ts("Please select at least one option from price set."); | |
647 | } | |
648 | } | |
649 | } |