3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * This class generates form components for Event Fees.
20 class CRM_Event_Form_ManageEvent_Fee
extends CRM_Event_Form_ManageEvent
{
23 * Constants for number of options for data types of multiple option.
25 const NUM_OPTION
= 11;
28 * Constants for number of discounts for the event.
30 const NUM_DISCOUNT
= 6;
45 * Set variables up before form is built.
47 public function preProcess() {
49 $this->setSelectedChild('fee');
53 * Set default values for the form.
55 * For edit/view mode the default values are retrieved from the database.
57 public function setDefaultValues() {
58 parent
::setDefaultValues();
60 $eventId = $this->_id
;
63 if (isset($eventId)) {
64 $params = ['id' => $eventId];
67 CRM_Event_BAO_Event
::retrieve($params, $defaults);
69 if (isset($eventId)) {
70 $price_set_id = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL, 1);
73 $defaults['price_set_id'] = $price_set_id;
76 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL);
78 if ($isQuick = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config')) {
79 $this->assign('isQuick', $isQuick);
80 $priceField = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $priceSetId, 'id', 'price_set_id');
82 $priceFieldOptions = CRM_Price_BAO_PriceFieldValue
::getValues($priceField, $options, 'weight', TRUE);
83 $defaults['price_field_id'] = $priceField;
85 foreach ($options as $optionId => $optionValue) {
87 $defaults['value'][$countRow] = CRM_Utils_Money
::format($optionValue['amount'], NULL, '%a');
88 $defaults['label'][$countRow] = $optionValue['label'];
89 $defaults['name'][$countRow] = $optionValue['name'];
90 $defaults['weight'][$countRow] = $optionValue['weight'];
91 $defaults['price_field_value'][$countRow] = $optionValue['id'];
92 if ($optionValue['is_default']) {
93 $defaults['default'] = $countRow;
101 //check if discounted
102 $discountedEvent = CRM_Core_BAO_Discount
::getOptionGroup($this->_id
, 'civicrm_event');
103 if (!empty($discountedEvent)) {
104 $defaults['is_discount'] = $i = 1;
105 $totalLables = $maxSize = $defaultDiscounts = [];
106 foreach ($discountedEvent as $optionGroupId) {
107 $defaults['discount_price_set'][] = $optionGroupId;
108 $defaults["discount_name[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $optionGroupId, 'title');
110 $defaults["discount_start_date[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
111 'start_date', 'price_set_id');
112 $defaults["discount_end_date[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
113 'end_date', 'price_set_id');
114 $defaultDiscounts[] = CRM_Price_BAO_PriceSet
::getSetDetail($optionGroupId);
118 //avoid moving up value of lable when some labels don't
119 //have a value ,fixed for CRM-3088
121 foreach ($defaultDiscounts as $val) {
122 $discountFields = current($val);
123 $discountFields = current($discountFields['fields']);
125 foreach ($discountFields['options'] as $discountFieldsval) {
126 $defaults['discounted_label'][$discountFieldsval['weight']] = $discountFieldsval['label'];
127 $defaults['discounted_value'][$discountFieldsval['weight']][$rowCount] = CRM_Utils_Money
::format($discountFieldsval['amount'], NULL, '%a');
128 $defaults['discount_option_id'][$rowCount][$discountFieldsval['weight']] = $discountFieldsval['id'];
129 if (!empty($discountFieldsval['is_default'])) {
130 $defaults['discounted_default'] = $discountFieldsval['weight'];
136 ksort($defaults['discounted_value']);
137 ksort($defaults['discounted_label']);
139 foreach ($defaults['discounted_label'] as $key => $value) {
140 if ($key != $rowCount) {
141 $defaults['discounted_label'][$rowCount] = $value;
142 $defaults['discounted_value'][$rowCount] = $defaults['discounted_value'][$key];
143 unset($defaults['discounted_value'][$key]);
144 unset($defaults['discounted_label'][$key]);
145 foreach ($defaults['discount_option_id'] as &$optionIds) {
146 if (array_key_exists($key, $optionIds)) {
147 $optionIds[$rowCount] = $optionIds[$key];
148 unset($optionIds[$key]);
155 $this->set('discountSection', 1);
156 $this->buildQuickForm();
158 elseif (!empty($defaults['label'])) {
159 //if Regular Fees are present in DB and event fee page is in update mode
160 $defaults['discounted_label'] = $defaults['label'];
162 elseif (!empty($this->_submitValues
['label'])) {
163 //if event is newly created, use submitted values for
165 if (is_array($this->_submitValues
['label'])) {
167 foreach ($this->_submitValues
['label'] as $value) {
169 $defaults['discounted_label'][$k] = $value;
175 $defaults['id'] = $eventId;
176 if (!empty($totalLables)) {
177 $maxKey = count($totalLables) - 1;
178 if (isset($maxKey) && !empty($totalLables[$maxKey]['value'])) {
179 foreach ($totalLables[$maxKey]['value'] as $i => $v) {
180 if ($totalLables[$maxKey]['amount_id'][$i] == CRM_Utils_Array
::value('default_discount_fee_id', $defaults)) {
181 $defaults['discounted_default'] = $i;
188 if (!isset($defaults['discounted_default'])) {
189 $defaults['discounted_default'] = 1;
192 if (!isset($defaults['is_monetary'])) {
193 $defaults['is_monetary'] = 1;
196 if (!isset($defaults['fee_label'])) {
197 $defaults['fee_label'] = ts('Event Fee(s)');
200 if (!isset($defaults['pay_later_text']) ||
201 empty($defaults['pay_later_text'])
203 $defaults['pay_later_text'] = ts('I will send payment by check');
206 $this->_showHide
= new CRM_Core_ShowHideBlocks();
207 if (!$defaults['is_monetary']) {
208 $this->_showHide
->addHide('event-fees');
211 if (isset($defaults['price_set_id'])) {
212 $this->_showHide
->addHide('map-field');
214 $this->_showHide
->addToTemplate();
215 $this->assign('inDate', $this->_inDate
);
216 if (!empty($defaults['payment_processor'])) {
217 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
218 $defaults['payment_processor']
226 * Build the form object.
228 public function buildQuickForm() {
229 $this->addYesNo('is_monetary',
233 ['onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);"]
236 //add currency element.
237 $this->addCurrency('currency', ts('Currency'), FALSE);
239 $paymentProcessor = CRM_Core_PseudoConstant
::paymentProcessor();
241 $this->assign('paymentProcessor', $paymentProcessor);
242 $this->addCheckBox('payment_processor', ts('Payment Processor'),
243 array_flip($paymentProcessor),
244 NULL, NULL, NULL, NULL,
245 [' ', ' ', ' ', '<br/>']
249 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() ||
250 (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() && CRM_Core_Permission
::check('administer CiviCRM Financial Types'))) {
251 $this->addSelect('financial_type_id');
254 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::ADD
);
255 $this->addSelect('financial_type_id', ['context' => 'search', 'options' => $financialTypes]);
257 // add pay later options
258 $this->addElement('checkbox', 'is_pay_later', ts('Enable Pay Later option?'), NULL,
259 ['onclick' => "return showHideByValue('is_pay_later','','payLaterOptions','block','radio',false);"]
261 $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'),
262 CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'),
265 $this->add('wysiwyg', 'pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'));
267 $this->addElement('checkbox', 'is_billing_required', ts('Billing address required'));
268 $this->add('text', 'fee_label', ts('Fee Label'));
270 $price = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviEvent');
271 if (CRM_Utils_System
::isNull($price)) {
272 $this->assign('price', FALSE);
275 $this->assign('price', TRUE);
277 $this->addField('price_set_id', [
278 'entity' => 'PriceSet',
280 'onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);",
283 $default = [$this->createElement('radio', NULL, NULL, NULL, 0)];
284 $this->add('hidden', 'price_field_id', '', ['id' => 'price_field_id']);
285 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
287 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
288 $this->add('hidden', "price_field_value[$i]", '', ['id' => "price_field_value[$i]"]);
291 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
292 $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');
295 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
298 $this->addGroup($default, 'default');
300 $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL,
301 ['onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);"]
303 $discountSection = $this->get('discountSection');
305 $this->assign('discountSection', $discountSection);
307 // form fields of Discount sets
309 $_showHide = new CRM_Core_ShowHideBlocks('', '');
311 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
312 //the show hide blocks
313 $showBlocks = 'discount_' . $i;
315 $_showHide->addHide($showBlocks);
318 $_showHide->addShow($showBlocks);
321 //Increment by 1 of start date of previous end date.
322 if (is_array($this->_submitValues
) &&
323 !empty($this->_submitValues
['discount_name'][$i]) &&
324 !empty($this->_submitValues
['discount_name'][$i +
1]) &&
325 isset($this->_submitValues
['discount_end_date']) &&
326 isset($this->_submitValues
['discount_end_date'][$i]) &&
327 $i < self
::NUM_DISCOUNT
- 1
329 if (!empty($this->_submitValues
['discount_end_date'][$i +
1])
330 && empty($this->_submitValues
['discount_start_date'][$i +
1])
332 $this->_submitValues
['discount_start_date'][$i +
1] = date('Y-m-d', strtotime("+1 days " . $this->_submitValues
['discount_end_date'][$i]));
335 //Decrement by 1 of end date from next start date.
337 is_array($this->_submitValues
) &&
338 !empty($this->_submitValues
['discount_name'][$i]) &&
339 !empty($this->_submitValues
['discount_name'][$i - 1]) &&
340 isset($this->_submitValues
['discount_start_date']) &&
341 isset($this->_submitValues
['discount_start_date'][$i])
343 if (!empty($this->_submitValues
['discount_start_date'][$i])
344 && empty($this->_submitValues
['discount_end_date'][$i - 1])
346 list($this->_submitValues
['discount_end_date'][$i - 1]) = date('Y-m-d', strtotime("-1 days " . $this->_submitValues
['discount_start_date'][$i]));
350 $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
351 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'title')
353 $this->add('hidden', "discount_price_set[$i]", '', ['id' => "discount_price_set[$i]"]);
354 $this->add('datepicker', 'discount_start_date[' . $i . ']', ts('Discount Start Date'), [], FALSE, ['time' => FALSE]);
355 $this->add('datepicker', 'discount_end_date[' . $i . ']', ts('Discount End Date'), [], FALSE, ['time' => FALSE]);
357 $_showHide->addToTemplate();
358 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
359 ['class' => 'crm-form-submit cancel']
361 if (Civi
::settings()->get('deferred_revenue_enabled')) {
362 $deferredFinancialType = CRM_Financial_BAO_FinancialAccount
::getDeferredFinancialType();
363 $this->assign('deferredFinancialType', array_keys($deferredFinancialType));
365 $this->buildAmountLabel();
366 parent
::buildQuickForm();
370 * Add local and global form rules.
372 public function addRules() {
373 $this->addFormRule(['CRM_Event_Form_ManageEvent_Fee', 'formRule']);
377 * Global validation rules for the form.
379 * @param array $values
380 * Posted values of the form.
383 * list of errors to be posted back to the form
385 public static function formRule($values) {
387 if (!empty($values['is_discount'])) {
388 $occurDiscount = array_count_values($values['discount_name']);
390 $countemptyvalue = 0;
391 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
392 $start_date = $end_date = NULL;
393 if (!empty($values['discount_name'][$i])) {
394 if (!empty($values['discount_start_date'][$i])) {
395 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_start_date'][$i]) : 0;
397 if (!empty($values['discount_end_date'][$i])) {
398 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i]) : 0;
401 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
402 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
405 if (!$start_date && !$end_date) {
406 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
410 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i - 1]) : 0;
411 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
412 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
414 elseif (!$start_date && !$end_date_1) {
416 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
420 foreach ($occurDiscount as $key => $value) {
421 if ($value > 1 && $key <> '') {
422 if ($key == $values['discount_name'][$i]) {
423 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', [1 => $key]);
428 //validation for discount labels and values
429 for ($index = (self
::NUM_OPTION
); $index > 0; $index--) {
431 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
434 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
437 if (!empty($values['discounted_label'][$index])) {
438 $duplicateIndex = CRM_Utils_Array
::key($values['discounted_label'][$index], $values['discounted_label']);
440 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
441 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
444 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
447 if (empty($values['discounted_value'][$index][$i])) {
451 if (!empty($values['_qf_Fee_next']) && ($countemptyrows == 11 ||
$countemptyvalue == 11)) {
452 $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.');
457 if ($values['is_monetary']) {
458 //check if financial type is selected
459 if (!$values['financial_type_id']) {
460 $errors['financial_type_id'] = ts("Please select financial type.");
463 //check for the event fee label (mandatory)
464 if (!$values['fee_label']) {
465 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
468 if (empty($values['price_set_id'])) {
469 //check fee label and amount
472 foreach ($values['label'] as $key => $val) {
473 if (trim($val) && trim($values['value'][$key])) {
474 $optionKeys[$key] = $key;
479 $default = CRM_Utils_Array
::value('default', $values);
480 if ($default && !in_array($default, $optionKeys)) {
481 $errors['default'] = ts('Please select an appropriate option as default.');
485 if (!$values['label'][1]) {
486 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
488 if (!$values['value'][1]) {
489 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
493 if (isset($values['is_pay_later'])) {
494 if (empty($values['pay_later_text'])) {
495 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
497 if (empty($values['pay_later_receipt'])) {
498 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
502 if (empty($values['payment_processor'])) {
503 $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.');
507 return empty($errors) ?
TRUE : $errors;
510 public function buildAmountLabel() {
512 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
514 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
516 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
517 $this->add('text', "discounted_value[$i][$j]", ts('Value'), ['size' => 10]);
518 $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');
522 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
525 $this->addGroup($default, 'discounted_default');
531 public function postProcess() {
533 $params = $this->cleanMoneyFields($this->exportValues());
535 $this->set('discountSection', 0);
537 if (!empty($_POST['_qf_Fee_submit'])) {
538 $this->buildAmountLabel();
539 $this->set('discountSection', 2);
543 if (!empty($params['payment_processor'])) {
544 $params['payment_processor'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($params['payment_processor']));
547 $params['payment_processor'] = 'null';
550 $params['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, 0);
551 $params['is_billing_required'] = CRM_Utils_Array
::value('is_billing_required', $params, 0);
555 // delete all the prior label values or discounts in the custom options table
556 // and delete a price set if one exists
557 //@todo note that this removes the reference from existing participants -
558 // even where there is not change - redress?
559 // note that a more tentative form of this is invoked by passing price_set_id as an array
560 // to event.create see CRM-14069
561 // @todo get all of this logic out of form layer (currently partially in BAO/api layer)
562 if (CRM_Price_BAO_PriceSet
::removeFrom('civicrm_event', $this->_id
)) {
563 CRM_Core_BAO_Discount
::del($this->_id
, 'civicrm_event');
567 if ($params['is_monetary']) {
568 if (!empty($params['price_set_id'])) {
569 //@todo this is now being done in the event BAO if passed price_set_id as an array
570 // per notes on that fn - looking at the api converting to an array
571 // so calling via the api may cause this to be done in the api
572 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $params['price_set_id']);
573 if (!empty($params['price_field_id'])) {
574 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
575 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID, 0);
579 // if there are label / values, create custom options for them
580 $labels = CRM_Utils_Array
::value('label', $params);
581 $values = CRM_Utils_Array
::value('value', $params);
582 $default = CRM_Utils_Array
::value('default', $params);
584 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
585 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
586 if (!empty($labels[$i]) && !CRM_Utils_System
::isNull($values[$i])) {
588 'label' => trim($labels[$i]),
589 'value' => $values[$i],
592 'is_default' => $default == $i,
596 if (!empty($options)) {
597 $params['default_fee_id'] = NULL;
598 if (empty($params['price_set_id'])) {
599 if (empty($params['price_field_id'])) {
600 $setParams['title'] = $eventTitle = ($this->_isTemplate
) ?
$this->_defaultValues
['template_title'] : $this->_defaultValues
['title'];
601 $eventTitle = strtolower(CRM_Utils_String
::munge($eventTitle, '_', 245));
602 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle, 'id', 'name')) {
603 $setParams['name'] = $eventTitle;
605 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $this->_id
, 'id', 'name')) {
606 $setParams['name'] = $eventTitle . '_' . $this->_id
;
609 $timeSec = explode('.', microtime(TRUE));
610 $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
612 $setParams['is_quick_config'] = 1;
613 $setParams['financial_type_id'] = $params['financial_type_id'];
614 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
615 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
617 $fieldParams['name'] = strtolower(CRM_Utils_String
::munge($params['fee_label'], '_', 245));
618 $fieldParams['price_set_id'] = $priceSet->id
;
621 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
622 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
623 CRM_Price_BAO_PriceFieldValue
::setIsActive($fieldValueID, '0');
624 unset($params['price_field_value'][$arrayID]);
627 $fieldParams['id'] = CRM_Utils_Array
::value('price_field_id', $params);
628 $fieldParams['option_id'] = $params['price_field_value'];
630 $priceSet = new CRM_Price_BAO_PriceSet();
631 $priceSet->id
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $params), 'price_set_id');
633 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
634 CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceSet', $priceSet->id
, 'financial_type_id', $params['financial_type_id']);
637 $fieldParams['label'] = $params['fee_label'];
638 $fieldParams['html_type'] = 'Radio';
639 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $priceSet->id
);
640 $fieldParams['option_label'] = $params['label'];
641 $fieldParams['option_amount'] = $params['value'];
642 $fieldParams['financial_type_id'] = $params['financial_type_id'];
643 foreach ($options as $value) {
644 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
646 $fieldParams['default_option'] = $params['default'];
647 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
652 $discountPriceSets = !empty($this->_defaultValues
['discount_price_set']) ?
$this->_defaultValues
['discount_price_set'] : [];
653 $discountFieldIDs = !empty($this->_defaultValues
['discount_option_id']) ?
$this->_defaultValues
['discount_option_id'] : [];
654 if (CRM_Utils_Array
::value('is_discount', $params) == 1) {
655 // if there are discounted set of label / values,
656 // create custom options for them
657 $labels = CRM_Utils_Array
::value('discounted_label', $params);
658 $values = CRM_Utils_Array
::value('discounted_value', $params);
659 $default = CRM_Utils_Array
::value('discounted_default', $params);
661 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
662 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
663 $discountOptions = [];
664 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
665 if (!empty($labels[$i]) &&
666 !CRM_Utils_System
::isNull(CRM_Utils_Array
::value($j, $values[$i]))
668 $discountOptions[] = [
669 'label' => trim($labels[$i]),
670 'value' => $values[$i][$j],
673 'is_default' => $default == $i,
678 if (!empty($discountOptions)) {
680 $params['default_discount_fee_id'] = NULL;
683 if (empty($discountPriceSets[$keyCheck])) {
685 $eventTitle = strtolower(CRM_Utils_String
::munge($this->_defaultValues
['title'], '_', 200));
687 $setParams['title'] = $params['discount_name'][$j];
688 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) {
689 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j];
691 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
, 'id', 'name')) {
692 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
;
695 $timeSec = explode('.', microtime(TRUE));
696 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
698 $setParams['is_quick_config'] = 1;
699 $setParams['financial_type_id'] = $params['financial_type_id'];
700 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
701 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
702 $priceSetID = $priceSet->id
;
705 $priceSetID = $discountPriceSets[$j - 1];
707 'title' => $params['discount_name'][$j],
710 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
711 $setParams['financial_type_id'] = $params['financial_type_id'];
713 CRM_Price_BAO_PriceSet
::create($setParams);
714 unset($discountPriceSets[$j - 1]);
715 $fieldParams['id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceField', $priceSetID, 'id', 'price_set_id');
718 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
719 $fieldParams['is_required'] = 1;
720 $fieldParams['price_set_id'] = $priceSetID;
721 $fieldParams['html_type'] = 'Radio';
722 $fieldParams['financial_type_id'] = $params['financial_type_id'];
723 foreach ($discountOptions as $value) {
724 $fieldParams['option_label'][$value['weight']] = $value['label'];
725 $fieldParams['option_amount'][$value['weight']] = $value['value'];
726 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
727 if (!empty($value['is_default'])) {
728 $fieldParams['default_option'] = $value['weight'];
730 if (!empty($discountFieldIDs[$j]) && !empty($discountFieldIDs[$j][$value['weight']])) {
731 $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']];
732 unset($discountFieldIDs[$j][$value['weight']]);
735 //create discount priceset
736 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
737 if (!empty($discountFieldIDs[$j])) {
738 foreach ($discountFieldIDs[$j] as $fID) {
739 CRM_Price_BAO_PriceFieldValue
::setIsActive($fID, '0');
744 'entity_table' => 'civicrm_event',
745 'entity_id' => $this->_id
,
746 'price_set_id' => $priceSetID,
747 'start_date' => $params['discount_start_date'][$j],
748 'end_date' => $params['discount_end_date'][$j],
750 CRM_Core_BAO_Discount
::add($discountParams);
755 if (!empty($discountPriceSets)) {
756 foreach ($discountPriceSets as $setId) {
757 CRM_Price_BAO_PriceSet
::setIsQuickConfig($setId, 0);
763 if (!empty($params['price_field_id'])) {
764 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
765 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID, 0);
767 $params['financial_type_id'] = '';
768 $params['is_pay_later'] = 0;
769 $params['is_billing_required'] = 0;
772 //update 'is_billing_required'
773 if (empty($params['is_pay_later'])) {
774 $params['is_billing_required'] = FALSE;
777 //update events table
778 $params['id'] = $this->_id
;
779 // skip update of financial type in price set
780 $params['skipFinancialType'] = TRUE;
781 CRM_Event_BAO_Event
::add($params);
783 // Update tab "disabled" css class
784 $this->ajaxResponse
['tabValid'] = !empty($params['is_monetary']);
785 parent
::endPostProcess();
789 * Return a descriptive name for the page, used in wizard header
793 public function getTitle() {
794 return ts('Event Fees');
798 * Clean money fields in submitted params to remove formatting.
800 * @param array $params
804 protected function cleanMoneyFields($params) {
805 foreach ($params['value'] as $index => $value) {
806 if (CRM_Utils_System
::isNull($value)) {
807 unset($params['value'][$index]);
810 $params['value'][$index] = CRM_Utils_Rule
::cleanMoney(trim($value));
813 foreach ($params['discounted_value'] as $index => $discountedValueSet) {
814 foreach ($discountedValueSet as $innerIndex => $value) {
815 if (CRM_Utils_System
::isNull($value)) {
816 unset($params['discounted_value'][$index][$innerIndex]);
819 $params['discounted_value'][$index][$innerIndex] = CRM_Utils_Rule
::cleanMoney(trim($value));
822 if (empty($params['discounted_value'][$index])) {
823 unset($params['discounted_value'][$index]);