3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2019
34 * This class generates form components for Event Fees.
36 class CRM_Event_Form_ManageEvent_Fee
extends CRM_Event_Form_ManageEvent
{
39 * Constants for number of options for data types of multiple option.
41 const NUM_OPTION
= 11;
44 * Constants for number of discounts for the event.
46 const NUM_DISCOUNT
= 6;
61 * Set variables up before form is built.
63 public function preProcess() {
65 $this->assign('selectedChild', 'fee');
69 * Set default values for the form.
71 * For edit/view mode the default values are retrieved from the database.
73 public function setDefaultValues() {
74 parent
::setDefaultValues();
76 $eventId = $this->_id
;
79 if (isset($eventId)) {
80 $params = ['id' => $eventId];
83 CRM_Event_BAO_Event
::retrieve($params, $defaults);
85 if (isset($eventId)) {
86 $price_set_id = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL, 1);
89 $defaults['price_set_id'] = $price_set_id;
92 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL);
94 if ($isQuick = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config')) {
95 $this->assign('isQuick', $isQuick);
96 $priceField = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $priceSetId, 'id', 'price_set_id');
98 $priceFieldOptions = CRM_Price_BAO_PriceFieldValue
::getValues($priceField, $options, 'weight', TRUE);
99 $defaults['price_field_id'] = $priceField;
101 foreach ($options as $optionId => $optionValue) {
103 $defaults['value'][$countRow] = CRM_Utils_Money
::format($optionValue['amount'], NULL, '%a');
104 $defaults['label'][$countRow] = $optionValue['label'];
105 $defaults['name'][$countRow] = $optionValue['name'];
106 $defaults['weight'][$countRow] = $optionValue['weight'];
107 $defaults['price_field_value'][$countRow] = $optionValue['id'];
108 if ($optionValue['is_default']) {
109 $defaults['default'] = $countRow;
117 //check if discounted
118 $discountedEvent = CRM_Core_BAO_Discount
::getOptionGroup($this->_id
, 'civicrm_event');
119 if (!empty($discountedEvent)) {
120 $defaults['is_discount'] = $i = 1;
121 $totalLables = $maxSize = $defaultDiscounts = [];
122 foreach ($discountedEvent as $optionGroupId) {
123 $defaults['discount_price_set'][] = $optionGroupId;
124 $defaults["discount_name[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $optionGroupId, 'title');
126 $defaults["discount_start_date[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
127 'start_date', 'price_set_id');
128 $defaults["discount_end_date[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
129 'end_date', 'price_set_id');
130 $defaultDiscounts[] = CRM_Price_BAO_PriceSet
::getSetDetail($optionGroupId);
134 //avoid moving up value of lable when some labels don't
135 //have a value ,fixed for CRM-3088
137 foreach ($defaultDiscounts as $val) {
138 $discountFields = current($val);
139 $discountFields = current($discountFields['fields']);
141 foreach ($discountFields['options'] as $discountFieldsval) {
142 $defaults['discounted_label'][$discountFieldsval['weight']] = $discountFieldsval['label'];
143 $defaults['discounted_value'][$discountFieldsval['weight']][$rowCount] = CRM_Utils_Money
::format($discountFieldsval['amount'], NULL, '%a');
144 $defaults['discount_option_id'][$rowCount][$discountFieldsval['weight']] = $discountFieldsval['id'];
145 if (!empty($discountFieldsval['is_default'])) {
146 $defaults['discounted_default'] = $discountFieldsval['weight'];
152 ksort($defaults['discounted_value']);
153 ksort($defaults['discounted_label']);
155 foreach ($defaults['discounted_label'] as $key => $value) {
156 if ($key != $rowCount) {
157 $defaults['discounted_label'][$rowCount] = $value;
158 $defaults['discounted_value'][$rowCount] = $defaults['discounted_value'][$key];
159 unset($defaults['discounted_value'][$key]);
160 unset($defaults['discounted_label'][$key]);
161 foreach ($defaults['discount_option_id'] as &$optionIds) {
162 if (array_key_exists($key, $optionIds)) {
163 $optionIds[$rowCount] = $optionIds[$key];
164 unset($optionIds[$key]);
171 $this->set('discountSection', 1);
172 $this->buildQuickForm();
174 elseif (!empty($defaults['label'])) {
175 //if Regular Fees are present in DB and event fee page is in update mode
176 $defaults['discounted_label'] = $defaults['label'];
178 elseif (!empty($this->_submitValues
['label'])) {
179 //if event is newly created, use submitted values for
181 if (is_array($this->_submitValues
['label'])) {
183 foreach ($this->_submitValues
['label'] as $value) {
185 $defaults['discounted_label'][$k] = $value;
191 $defaults['id'] = $eventId;
192 if (!empty($totalLables)) {
193 $maxKey = count($totalLables) - 1;
194 if (isset($maxKey) && !empty($totalLables[$maxKey]['value'])) {
195 foreach ($totalLables[$maxKey]['value'] as $i => $v) {
196 if ($totalLables[$maxKey]['amount_id'][$i] == CRM_Utils_Array
::value('default_discount_fee_id', $defaults)) {
197 $defaults['discounted_default'] = $i;
204 if (!isset($defaults['discounted_default'])) {
205 $defaults['discounted_default'] = 1;
208 if (!isset($defaults['is_monetary'])) {
209 $defaults['is_monetary'] = 1;
212 if (!isset($defaults['fee_label'])) {
213 $defaults['fee_label'] = ts('Event Fee(s)');
216 if (!isset($defaults['pay_later_text']) ||
217 empty($defaults['pay_later_text'])
219 $defaults['pay_later_text'] = ts('I will send payment by check');
222 $this->_showHide
= new CRM_Core_ShowHideBlocks();
223 if (!$defaults['is_monetary']) {
224 $this->_showHide
->addHide('event-fees');
227 if (isset($defaults['price_set_id'])) {
228 $this->_showHide
->addHide('map-field');
230 $this->_showHide
->addToTemplate();
231 $this->assign('inDate', $this->_inDate
);
232 if (!empty($defaults['payment_processor'])) {
233 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
234 $defaults['payment_processor']
242 * Build the form object.
244 public function buildQuickForm() {
245 $this->addYesNo('is_monetary',
249 ['onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);"]
252 //add currency element.
253 $this->addCurrency('currency', ts('Currency'), FALSE);
255 $paymentProcessor = CRM_Core_PseudoConstant
::paymentProcessor();
257 $this->assign('paymentProcessor', $paymentProcessor);
258 $this->addCheckBox('payment_processor', ts('Payment Processor'),
259 array_flip($paymentProcessor),
260 NULL, NULL, NULL, NULL,
261 [' ', ' ', ' ', '<br/>']
265 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() ||
266 (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() && CRM_Core_Permission
::check('administer CiviCRM Financial Types'))) {
267 $this->addSelect('financial_type_id');
270 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::ADD
);
271 $this->addSelect('financial_type_id', ['context' => 'search', 'options' => $financialTypes]);
273 // add pay later options
274 $this->addElement('checkbox', 'is_pay_later', ts('Enable Pay Later option?'), NULL,
275 ['onclick' => "return showHideByValue('is_pay_later','','payLaterOptions','block','radio',false);"]
277 $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'),
278 CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'),
281 $this->add('wysiwyg', 'pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'));
283 $this->addElement('checkbox', 'is_billing_required', ts('Billing address required'));
284 $this->add('text', 'fee_label', ts('Fee Label'));
286 $price = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviEvent');
287 if (CRM_Utils_System
::isNull($price)) {
288 $this->assign('price', FALSE);
291 $this->assign('price', TRUE);
293 $this->add('select', 'price_set_id', ts('Price Set'),
295 '' => ts('- none -'),
297 NULL, ['onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);"]
299 $default = [$this->createElement('radio', NULL, NULL, NULL, 0)];
300 $this->add('hidden', 'price_field_id', '', ['id' => 'price_field_id']);
301 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
303 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
304 $this->add('hidden', "price_field_value[$i]", '', ['id' => "price_field_value[$i]"]);
307 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
308 $this->addRule("value[$i]", ts('Please enter a valid money value for this field (e.g. %1).', [1 => CRM_Utils_Money
::format('99.99', ' ')]), 'money');
311 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
314 $this->addGroup($default, 'default');
316 $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL,
317 ['onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);"]
319 $discountSection = $this->get('discountSection');
321 $this->assign('discountSection', $discountSection);
323 // form fields of Discount sets
325 $_showHide = new CRM_Core_ShowHideBlocks('', '');
327 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
328 //the show hide blocks
329 $showBlocks = 'discount_' . $i;
331 $_showHide->addHide($showBlocks);
334 $_showHide->addShow($showBlocks);
337 //Increment by 1 of start date of previous end date.
338 if (is_array($this->_submitValues
) &&
339 !empty($this->_submitValues
['discount_name'][$i]) &&
340 !empty($this->_submitValues
['discount_name'][$i +
1]) &&
341 isset($this->_submitValues
['discount_end_date']) &&
342 isset($this->_submitValues
['discount_end_date'][$i]) &&
343 $i < self
::NUM_DISCOUNT
- 1
345 if (!empty($this->_submitValues
['discount_end_date'][$i +
1])
346 && empty($this->_submitValues
['discount_start_date'][$i +
1])
348 $this->_submitValues
['discount_start_date'][$i +
1] = date('Y-m-d', strtotime("+1 days " . $this->_submitValues
['discount_end_date'][$i]));
351 //Decrement by 1 of end date from next start date.
353 is_array($this->_submitValues
) &&
354 !empty($this->_submitValues
['discount_name'][$i]) &&
355 !empty($this->_submitValues
['discount_name'][$i - 1]) &&
356 isset($this->_submitValues
['discount_start_date']) &&
357 isset($this->_submitValues
['discount_start_date'][$i])
359 if (!empty($this->_submitValues
['discount_start_date'][$i])
360 && empty($this->_submitValues
['discount_end_date'][$i - 1])
362 list($this->_submitValues
['discount_end_date'][$i - 1]) = date('Y-m-d', strtotime("-1 days " . $this->_submitValues
['discount_start_date'][$i]));
366 $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
367 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'title')
369 $this->add('hidden', "discount_price_set[$i]", '', ['id' => "discount_price_set[$i]"]);
370 $this->add('datepicker', 'discount_start_date[' . $i . ']', ts('Discount Start Date'), [], FALSE, ['time' => FALSE]);
371 $this->add('datepicker', 'discount_end_date[' . $i . ']', ts('Discount End Date'), [], FALSE, ['time' => FALSE]);
373 $_showHide->addToTemplate();
374 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
375 ['class' => 'crm-form-submit cancel']
377 if (Civi
::settings()->get('deferred_revenue_enabled')) {
378 $deferredFinancialType = CRM_Financial_BAO_FinancialAccount
::getDeferredFinancialType();
379 $this->assign('deferredFinancialType', array_keys($deferredFinancialType));
381 $this->buildAmountLabel();
382 parent
::buildQuickForm();
386 * Add local and global form rules.
388 public function addRules() {
389 $this->addFormRule(['CRM_Event_Form_ManageEvent_Fee', 'formRule']);
393 * Global validation rules for the form.
395 * @param array $values
396 * Posted values of the form.
399 * list of errors to be posted back to the form
401 public static function formRule($values) {
403 if (!empty($values['is_discount'])) {
404 $occurDiscount = array_count_values($values['discount_name']);
406 $countemptyvalue = 0;
407 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
408 $start_date = $end_date = NULL;
409 if (!empty($values['discount_name'][$i])) {
410 if (!empty($values['discount_start_date'][$i])) {
411 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_start_date'][$i]) : 0;
413 if (!empty($values['discount_end_date'][$i])) {
414 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i]) : 0;
417 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
418 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
421 if (!$start_date && !$end_date) {
422 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
426 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i - 1]) : 0;
427 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
428 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
430 elseif (!$start_date && !$end_date_1) {
432 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
436 foreach ($occurDiscount as $key => $value) {
437 if ($value > 1 && $key <> '') {
438 if ($key == $values['discount_name'][$i]) {
439 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', [1 => $key]);
444 //validation for discount labels and values
445 for ($index = (self
::NUM_OPTION
); $index > 0; $index--) {
447 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
450 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
453 if (!empty($values['discounted_label'][$index])) {
454 $duplicateIndex = CRM_Utils_Array
::key($values['discounted_label'][$index], $values['discounted_label']);
456 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
457 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
460 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
463 if (empty($values['discounted_value'][$index][$i])) {
467 if (!empty($values['_qf_Fee_next']) && ($countemptyrows == 11 ||
$countemptyvalue == 11)) {
468 $errors["discounted_label[1]"] = $errors["discounted_value[1][$i]"] = ts('At least one fee should be entered for your Discount Set. If you do not see the table to enter discount fees, click the "Add Discount Set to Fee Table" button.');
473 if ($values['is_monetary']) {
474 //check if financial type is selected
475 if (!$values['financial_type_id']) {
476 $errors['financial_type_id'] = ts("Please select financial type.");
479 //check for the event fee label (mandatory)
480 if (!$values['fee_label']) {
481 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
484 if (empty($values['price_set_id'])) {
485 //check fee label and amount
488 foreach ($values['label'] as $key => $val) {
489 if (trim($val) && trim($values['value'][$key])) {
490 $optionKeys[$key] = $key;
495 $default = CRM_Utils_Array
::value('default', $values);
496 if ($default && !in_array($default, $optionKeys)) {
497 $errors['default'] = ts('Please select an appropriate option as default.');
501 if (!$values['label'][1]) {
502 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
504 if (!$values['value'][1]) {
505 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
509 if (isset($values['is_pay_later'])) {
510 if (empty($values['pay_later_text'])) {
511 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
513 if (empty($values['pay_later_receipt'])) {
514 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
518 if (empty($values['payment_processor'])) {
519 $errors['payment_processor'] = ts('You have indicated that this is a paid event, but no payment option has been selected. If this is not a paid event, please select the "No" option at the top of the page. If this is a paid event, please select at least one payment processor and/or enable the pay later option.');
523 return empty($errors) ?
TRUE : $errors;
526 public function buildAmountLabel() {
528 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
530 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
532 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
533 $this->add('text', "discounted_value[$i][$j]", ts('Value'), ['size' => 10]);
534 $this->addRule("discounted_value[$i][$j]", ts('Please enter a valid money value for this field (e.g. %1).', [1 => CRM_Utils_Money
::format('99.99', ' ')]), 'money');
538 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
541 $this->addGroup($default, 'discounted_default');
547 public function postProcess() {
549 $params = $this->cleanMoneyFields($this->exportValues());
551 $this->set('discountSection', 0);
553 if (!empty($_POST['_qf_Fee_submit'])) {
554 $this->buildAmountLabel();
555 $this->set('discountSection', 2);
559 if (!empty($params['payment_processor'])) {
560 $params['payment_processor'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($params['payment_processor']));
563 $params['payment_processor'] = 'null';
566 $params['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, 0);
567 $params['is_billing_required'] = CRM_Utils_Array
::value('is_billing_required', $params, 0);
571 // delete all the prior label values or discounts in the custom options table
572 // and delete a price set if one exists
573 //@todo note that this removes the reference from existing participants -
574 // even where there is not change - redress?
575 // note that a more tentative form of this is invoked by passing price_set_id as an array
576 // to event.create see CRM-14069
577 // @todo get all of this logic out of form layer (currently partially in BAO/api layer)
578 if (CRM_Price_BAO_PriceSet
::removeFrom('civicrm_event', $this->_id
)) {
579 CRM_Core_BAO_Discount
::del($this->_id
, 'civicrm_event');
583 if ($params['is_monetary']) {
584 if (!empty($params['price_set_id'])) {
585 //@todo this is now being done in the event BAO if passed price_set_id as an array
586 // per notes on that fn - looking at the api converting to an array
587 // so calling via the api may cause this to be done in the api
588 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $params['price_set_id']);
589 if (!empty($params['price_field_id'])) {
590 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
591 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID, 0);
595 // if there are label / values, create custom options for them
596 $labels = CRM_Utils_Array
::value('label', $params);
597 $values = CRM_Utils_Array
::value('value', $params);
598 $default = CRM_Utils_Array
::value('default', $params);
600 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
601 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
602 if (!empty($labels[$i]) && !CRM_Utils_System
::isNull($values[$i])) {
604 'label' => trim($labels[$i]),
605 'value' => $values[$i],
608 'is_default' => $default == $i,
612 if (!empty($options)) {
613 $params['default_fee_id'] = NULL;
614 if (empty($params['price_set_id'])) {
615 if (empty($params['price_field_id'])) {
616 $setParams['title'] = $eventTitle = ($this->_isTemplate
) ?
$this->_defaultValues
['template_title'] : $this->_defaultValues
['title'];
617 $eventTitle = strtolower(CRM_Utils_String
::munge($eventTitle, '_', 245));
618 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle, 'id', 'name')) {
619 $setParams['name'] = $eventTitle;
621 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $this->_id
, 'id', 'name')) {
622 $setParams['name'] = $eventTitle . '_' . $this->_id
;
625 $timeSec = explode('.', microtime(TRUE));
626 $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
628 $setParams['is_quick_config'] = 1;
629 $setParams['financial_type_id'] = $params['financial_type_id'];
630 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
631 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
633 $fieldParams['name'] = strtolower(CRM_Utils_String
::munge($params['fee_label'], '_', 245));
634 $fieldParams['price_set_id'] = $priceSet->id
;
637 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
638 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
639 CRM_Price_BAO_PriceFieldValue
::setIsActive($fieldValueID, '0');
640 unset($params['price_field_value'][$arrayID]);
643 $fieldParams['id'] = CRM_Utils_Array
::value('price_field_id', $params);
644 $fieldParams['option_id'] = $params['price_field_value'];
646 $priceSet = new CRM_Price_BAO_PriceSet();
647 $priceSet->id
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $params), 'price_set_id');
649 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
650 CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceSet', $priceSet->id
, 'financial_type_id', $params['financial_type_id']);
653 $fieldParams['label'] = $params['fee_label'];
654 $fieldParams['html_type'] = 'Radio';
655 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $priceSet->id
);
656 $fieldParams['option_label'] = $params['label'];
657 $fieldParams['option_amount'] = $params['value'];
658 $fieldParams['financial_type_id'] = $params['financial_type_id'];
659 foreach ($options as $value) {
660 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
662 $fieldParams['default_option'] = $params['default'];
663 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
668 $discountPriceSets = !empty($this->_defaultValues
['discount_price_set']) ?
$this->_defaultValues
['discount_price_set'] : [];
669 $discountFieldIDs = !empty($this->_defaultValues
['discount_option_id']) ?
$this->_defaultValues
['discount_option_id'] : [];
670 if (CRM_Utils_Array
::value('is_discount', $params) == 1) {
671 // if there are discounted set of label / values,
672 // create custom options for them
673 $labels = CRM_Utils_Array
::value('discounted_label', $params);
674 $values = CRM_Utils_Array
::value('discounted_value', $params);
675 $default = CRM_Utils_Array
::value('discounted_default', $params);
677 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
678 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
679 $discountOptions = [];
680 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
681 if (!empty($labels[$i]) &&
682 !CRM_Utils_System
::isNull(CRM_Utils_Array
::value($j, $values[$i]))
684 $discountOptions[] = [
685 'label' => trim($labels[$i]),
686 'value' => $values[$i][$j],
689 'is_default' => $default == $i,
694 if (!empty($discountOptions)) {
696 $params['default_discount_fee_id'] = NULL;
699 if (empty($discountPriceSets[$keyCheck])) {
701 $eventTitle = strtolower(CRM_Utils_String
::munge($this->_defaultValues
['title'], '_', 200));
703 $setParams['title'] = $params['discount_name'][$j];
704 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) {
705 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j];
707 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
, 'id', 'name')) {
708 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
;
711 $timeSec = explode('.', microtime(TRUE));
712 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
714 $setParams['is_quick_config'] = 1;
715 $setParams['financial_type_id'] = $params['financial_type_id'];
716 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
717 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
718 $priceSetID = $priceSet->id
;
721 $priceSetID = $discountPriceSets[$j - 1];
723 'title' => $params['discount_name'][$j],
726 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
727 $setParams['financial_type_id'] = $params['financial_type_id'];
729 CRM_Price_BAO_PriceSet
::create($setParams);
730 unset($discountPriceSets[$j - 1]);
731 $fieldParams['id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceField', $priceSetID, 'id', 'price_set_id');
734 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
735 $fieldParams['is_required'] = 1;
736 $fieldParams['price_set_id'] = $priceSetID;
737 $fieldParams['html_type'] = 'Radio';
738 $fieldParams['financial_type_id'] = $params['financial_type_id'];
739 foreach ($discountOptions as $value) {
740 $fieldParams['option_label'][$value['weight']] = $value['label'];
741 $fieldParams['option_amount'][$value['weight']] = $value['value'];
742 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
743 if (!empty($value['is_default'])) {
744 $fieldParams['default_option'] = $value['weight'];
746 if (!empty($discountFieldIDs[$j]) && !empty($discountFieldIDs[$j][$value['weight']])) {
747 $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']];
748 unset($discountFieldIDs[$j][$value['weight']]);
751 //create discount priceset
752 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
753 if (!empty($discountFieldIDs[$j])) {
754 foreach ($discountFieldIDs[$j] as $fID) {
755 CRM_Price_BAO_PriceFieldValue
::setIsActive($fID, '0');
760 'entity_table' => 'civicrm_event',
761 'entity_id' => $this->_id
,
762 'price_set_id' => $priceSetID,
763 'start_date' => $params['discount_start_date'][$j],
764 'end_date' => $params['discount_end_date'][$j],
766 CRM_Core_BAO_Discount
::add($discountParams);
771 if (!empty($discountPriceSets)) {
772 foreach ($discountPriceSets as $setId) {
773 CRM_Price_BAO_PriceSet
::setIsQuickConfig($setId, 0);
779 if (!empty($params['price_field_id'])) {
780 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
781 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID, 0);
783 $params['financial_type_id'] = '';
784 $params['is_pay_later'] = 0;
785 $params['is_billing_required'] = 0;
788 //update 'is_billing_required'
789 if (empty($params['is_pay_later'])) {
790 $params['is_billing_required'] = FALSE;
793 //update events table
794 $params['id'] = $this->_id
;
795 // skip update of financial type in price set
796 $params['skipFinancialType'] = TRUE;
797 CRM_Event_BAO_Event
::add($params);
799 // Update tab "disabled" css class
800 $this->ajaxResponse
['tabValid'] = !empty($params['is_monetary']);
801 parent
::endPostProcess();
805 * Return a descriptive name for the page, used in wizard header
809 public function getTitle() {
810 return ts('Event Fees');
814 * Clean money fields in submitted params to remove formatting.
816 * @param array $params
820 protected function cleanMoneyFields($params) {
821 foreach ($params['value'] as $index => $value) {
822 if (CRM_Utils_System
::isNull($value)) {
823 unset($params['value'][$index]);
826 $params['value'][$index] = CRM_Utils_Rule
::cleanMoney(trim($value));
829 foreach ($params['discounted_value'] as $index => $discountedValueSet) {
830 foreach ($discountedValueSet as $innerIndex => $value) {
831 if (CRM_Utils_System
::isNull($value)) {
832 unset($params['discounted_value'][$index][$innerIndex]);
835 $params['discounted_value'][$index][$innerIndex] = CRM_Utils_Rule
::cleanMoney(trim($value));
838 if (empty($params['discounted_value'][$index])) {
839 unset($params['discounted_value'][$index]);