3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * Business objects for managing price fields.
40 class CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field
extends CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field
{
45 * Takes an associative array and creates a price field object.
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
51 * @param array $params
52 * (reference ) an assoc array of name/value pairs.
54 * @return CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field
56 public static function &add(&$params) {
57 $priceFieldBAO = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field();
59 $priceFieldBAO->copyValues($params);
61 if ($id = CRM_Utils_Array
::value('id', $params)) {
62 $priceFieldBAO->id
= $id;
65 $priceFieldBAO->save();
66 return $priceFieldBAO;
70 * Takes an associative array and creates a price field object.
72 * This function is invoked from within the web form layer and also from the api layer
74 * @param array $params
75 * (reference) an assoc array of name/value pairs.
77 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field
79 public static function create(&$params) {
81 $transaction = new CRM_Core_Transaction();
83 $priceField = self
::add($params);
85 if (is_a($priceField, 'CRM_Core_Error')) {
86 $transaction->rollback();
90 $options = $optionsIds = array();
91 $maxIndex = CRM_Price_Form_Field
::NUM_OPTION
;
93 if ($priceField->html_type
== 'Text') {
96 $fieldValue = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
97 $fieldValue->price_field_id
= $priceField->id
;
99 // update previous field values( if any )
100 if ($fieldValue->find(TRUE)) {
101 $optionsIds['id'] = $fieldValue->id
;
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;
114 if (!empty($params['default_option'])) {
115 $defaultArray[$params['default_option']] = 1;
119 for ($index = 1; $index <= $maxIndex; $index++
) {
121 if (array_key_exists('option_amount', $params) &&
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])
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],
137 'is_default' => CRM_Utils_Array
::value($params['option_weight'][$index], $defaultArray) ?
$defaultArray[$params['option_weight'][$index]] : 0,
140 if ($opIds = CRM_Utils_Array
::value('option_id', $params)) {
141 if ($opId = CRM_Utils_Array
::value($index, $opIds)) {
142 $optionsIds['id'] = $opId;
145 $optionsIds['id'] = NULL;
148 CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue
::create($options, $optionsIds);
152 $transaction->commit();
157 * Fetch object based on array of properties.
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.
164 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field
166 public static function retrieve(&$params, &$defaults) {
167 return CRM_Core_DAO
::commonRetrieve('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $params, $defaults);
171 * Update the is_active flag in the db.
174 * Id of the database record.
175 * @param bool $is_active
176 * Value we want to set the is_active field.
179 * DAO object on success, null otherwise
182 public static function setIsActive($id, $is_active) {
183 return CRM_Core_DAO
::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $id, 'is_active', $is_active);
187 * Get the field title.
196 public static function getTitle($id) {
197 return CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $id, 'label');
201 * This function for building custom fields.
203 * @param CRM_Core_Form $qf
204 * Form object (reference).
205 * @param string $elementName
206 * Name of the custom field.
207 * @param int $fieldId
208 * @param bool $inactiveNeeded
209 * @param bool $useRequired
210 * True if required else false.
211 * @param string $label
212 * Label for custom field.
213 * @param null $fieldOptions
214 * @param array $freezeOptions
218 public static function addQuickFormElement(
225 $fieldOptions = NULL,
226 $freezeOptions = array()
229 $field = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field();
230 $field->id
= $fieldId;
231 if (!$field->find(TRUE)) {
232 /* FIXME: failure! */
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
;
243 if (!isset($label)) {
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
;
247 if ($field->name
== 'contribution_amount') {
248 $qf->_contributionAmount
= 1;
251 if (isset($qf->_online
) && $qf->_online
) {
252 $useRequired = FALSE;
255 $customOption = $fieldOptions;
256 if (!is_array($customOption)) {
257 $customOption = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field
::getOptions($field->id
, $inactiveNeeded);
261 $valueFieldName = 'amount';
263 switch ($field->html_type
) {
265 $optionKey = key($customOption);
266 $count = CRM_Utils_Array
::value('count', $customOption[$optionKey], '');
267 $max_value = CRM_Utils_Array
::value('max_value', $customOption[$optionKey], '');
268 $priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName], $count, $max_value));
271 if (property_exists($qf, '_quickConfig') && $qf->_quickConfig
&& property_exists($qf, '_contributionAmount') && $qf->_contributionAmount
) {
272 $qf->assign('priceset', $elementName);
273 $extra = array('onclick' => 'useAmountOther();');
276 // if separate membership payment is used with quick config priceset then change the other amount label
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');
281 elseif (!empty($fieldOptions[$optionKey]['label'])) {
282 $label = $fieldOptions[$optionKey]['label'];
285 if ($field->is_display_amounts
) {
286 $label .= ' - ';
287 $label .= CRM_Utils_Money
::format(CRM_Utils_Array
::value($valueFieldName, $customOption[$optionKey]));
290 $element = &$qf->add('text', $elementName, $label,
293 'price' => json_encode(array($optionKey, $priceVal)),
297 $useRequired && $field->is_required
301 if (in_array($optionKey, $freezeOptions)) {
306 if (property_exists($qf, '_quickConfig') && $qf->_quickConfig
) {
307 $message = ts("Please enter a valid amount.");
311 $message = ts('%1 must be an integer (whole number).', array(1 => $label));
312 $type = "positiveInteger";
314 // integers will have numeric rule applied to them.
315 $qf->addRule($elementName, $message, $type);
321 if (property_exists($qf, '_quickConfig') && $qf->_quickConfig
&& property_exists($qf, '_contributionAmount') && $qf->_contributionAmount
) {
322 $qf->assign('contriPriceset', $elementName);
325 foreach ($customOption as $opId => $opt) {
326 if ($field->is_display_amounts
) {
327 $opt['label'] = !empty($opt['label']) ?
$opt['label'] . ' - ' : '';
328 $opt['label'] .= CRM_Utils_Money
::format($opt[$valueFieldName]);
330 $count = CRM_Utils_Array
::value('count', $opt, '');
331 $max_value = CRM_Utils_Array
::value('max_value', $opt, '');
332 $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value));
334 'price' => json_encode(array($elementName, $priceVal)),
335 'data-amount' => $opt[$valueFieldName],
336 'data-currency' => $currencyName,
338 if (property_exists($qf, '_quickConfig') && $qf->_quickConfig
&& $field->name
== 'contribution_amount') {
339 $extra +
= array('onclick' => 'clearAmountOther();');
341 elseif (property_exists($qf, '_quickConfig') && $qf->_quickConfig
&& $field->name
== 'membership_amount') {
343 'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});",
344 'membership-type' => $opt['membership_type_id'],
346 $qf->assign('membershipFieldID', $field->id
);
348 $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra);
351 if (in_array($opId, $freezeOptions)) {
352 $choice[$opId]->freeze();
356 if (property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock
['is_separate_payment']) && $field->name
== 'contribution_amount') {
357 $choice[] = $qf->createElement('radio', NULL, '', 'No thank you', '-1',
359 'onclick' => 'clearAmountOther();',
364 if (!$field->is_required
) {
366 if (!empty($otherAmount['is_allow_other_amount']) && $field->name
== 'contribution_amount') {
367 $none = ts('Other Amount');
369 elseif (property_exists($qf, '_membershipBlock') && empty($qf->_membershipBlock
['is_required']) && $field->name
== 'membership_amount') {
370 $none = ts('No thank you');
373 $none = ts('-none-');
376 $choice[] = $qf->createElement('radio', NULL, '', $none, '0',
377 array('price' => json_encode(array($elementName, "0")))
381 $element = &$qf->addGroup($choice, $elementName, $label);
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;
388 if ($useRequired && $field->is_required
) {
389 $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required');
394 $selectOption = $allowedOptions = $priceVal = array();
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));
401 if ($field->is_display_amounts
) {
402 $opt['label'] .= ' - ';
403 $opt['label'] .= CRM_Utils_Money
::format($opt[$valueFieldName]);
405 $selectOption[$opt['id']] = $opt['label'];
407 if (!in_array($opt['id'], $freezeOptions)) {
408 $allowedOptions[] = $opt['id'];
411 $element = &$qf->add('select', $elementName, $label,
412 array('' => ts('- select -')) +
$selectOption,
413 $useRequired && $field->is_required
,
414 array('price' => json_encode($priceVal))
418 $button = substr($qf->controller
->getButtonName(), -4);
419 if (!empty($freezeOptions) && $button != 'skip') {
420 $qf->addRule($elementName, ts('Sorry, this option is currently sold out.'), 'regex', "/" . implode('|', $allowedOptions) . "/");
427 foreach ($customOption as $opId => $opt) {
428 $count = CRM_Utils_Array
::value('count', $opt, '');
429 $max_value = CRM_Utils_Array
::value('max_value', $opt, '');
430 $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value));
432 if ($field->is_display_amounts
) {
433 $opt['label'] .= ' - ';
434 $opt['label'] .= CRM_Utils_Money
::format($opt[$valueFieldName]);
436 $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'],
438 'price' => json_encode(array($opt['id'], $priceVal)),
439 'data-amount' => $opt[$valueFieldName],
440 'data-currency' => $currencyName,
445 if (in_array($opId, $freezeOptions)) {
446 $check[$opId]->freeze();
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');
455 if (isset($qf->_online
) && $qf->_online
) {
461 * Retrieve a list of options for the specified field.
463 * @param int $fieldId
465 * @param bool $inactiveNeeded
466 * Include inactive options.
468 * Ignore stored values\.
473 public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE) {
474 static $options = array();
476 if ($reset ||
empty($options[$fieldId])) {
478 CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue
::getValues($fieldId, $values, 'weight', !$inactiveNeeded);
479 $options[$fieldId] = $values;
482 return $options[$fieldId];
486 * @param $optionLabel
491 public static function getOptionId($optionLabel, $fid) {
492 if (!$optionLabel ||
!$fid) {
496 $optionGroupName = "civicrm_price_field.amount.{$fid}";
500 option_value.id as id
502 civicrm_option_value option_value,
503 civicrm_option_group option_group
505 option_group.name = %1
506 AND option_group.id = option_value.option_group_id
507 AND option_value.label = %2";
509 $dao = CRM_Core_DAO
::executeQuery($query, array(
510 1 => array($optionGroupName, 'String'),
511 2 => array($optionLabel, 'String'),
514 while ($dao->fetch()) {
520 * Delete the price set field.
528 public static function deleteField($id) {
529 $field = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field();
532 if ($field->find(TRUE)) {
533 // delete the options for this field
534 CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue
::deleteValues($id);
536 // reorder the weight before delete
537 $fieldValues = array('price_set_id' => $field->price_set_id
);
539 CRM_Utils_Weight
::delWeight('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $field->id
, $fieldValues);
541 // now delete the field
542 return $field->delete();
551 public static function &htmlTypes() {
552 static $htmlTypes = NULL;
555 'Text' => ts('Text / Numeric Quantity'),
556 'Select' => ts('Select'),
557 'Radio' => ts('Radio'),
558 'CheckBox' => ts('CheckBox'),
565 * Validate the priceset.
567 * @param int $priceSetId
568 * @param array $fields
569 * @param array $error
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;
578 $priceFields = array();
580 while ($priceField->fetch()) {
581 $key = "price_{$priceField->id}";
582 if (!empty($fields[$key])) {
583 $priceFields[$priceField->id
] = $fields[$key];
587 if (!empty($priceFields)) {
588 // we should has to have positive amount.
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
;
599 $selectedAmounts = array();
601 foreach ($htmlTypes as $fieldId => $type) {
603 CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue
::getValues($fieldId, $options);
605 if (empty($options)) {
611 if ($type == 'Text') {
612 foreach ($options as $opId => $option) {
613 $selectedAmounts[$opId] = $priceFields[$fieldId] * $option['amount'];
617 elseif (is_array($fields["price_{$fieldId}"])) {
618 foreach (array_keys($fields["price_{$fieldId}"]) as $opId) {
619 $selectedAmounts[$opId] = $options[$opId]['amount'];
622 elseif (in_array($fields["price_{$fieldId}"], array_keys($options))) {
623 $selectedAmounts[$fields["price_{$fieldId}"]] = $options[$fields["price_{$fieldId}"]]['amount'];
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));
635 $error['_qf_default'] = ts("Please select at least one option from price set.");