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;
59 * Set variables up before form is built.
61 public function preProcess() {
63 $this->assign('selectedChild', 'fee');
67 * Set default values for the form.
69 * For edit/view mode the default values are retrieved from the database.
71 public function setDefaultValues() {
72 parent
::setDefaultValues();
74 $eventId = $this->_id
;
77 if (isset($eventId)) {
78 $params = array('id' => $eventId);
81 CRM_Event_BAO_Event
::retrieve($params, $defaults);
83 if (isset($eventId)) {
84 $price_set_id = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL, 1);
87 $defaults['price_set_id'] = $price_set_id;
90 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL);
92 if ($isQuick = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config')) {
93 $this->assign('isQuick', $isQuick);
94 $priceField = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $priceSetId, 'id', 'price_set_id');
96 $priceFieldOptions = CRM_Price_BAO_PriceFieldValue
::getValues($priceField, $options, 'weight', TRUE);
97 $defaults['price_field_id'] = $priceField;
99 foreach ($options as $optionId => $optionValue) {
101 $defaults['value'][$countRow] = CRM_Utils_Money
::format($optionValue['amount'], NULL, '%a');
102 $defaults['label'][$countRow] = $optionValue['label'];
103 $defaults['name'][$countRow] = $optionValue['name'];
104 $defaults['weight'][$countRow] = $optionValue['weight'];
105 $defaults['price_field_value'][$countRow] = $optionValue['id'];
106 if ($optionValue['is_default']) {
107 $defaults['default'] = $countRow;
115 //check if discounted
116 $discountedEvent = CRM_Core_BAO_Discount
::getOptionGroup($this->_id
, 'civicrm_event');
117 if (!empty($discountedEvent)) {
118 $defaults['is_discount'] = $i = 1;
119 $totalLables = $maxSize = $defaultDiscounts = array();
120 foreach ($discountedEvent as $optionGroupId) {
121 $defaults['discount_price_set'][] = $optionGroupId;
122 $defaults["discount_name[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $optionGroupId, 'title');
124 $defaults["discount_start_date[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
125 'start_date', 'price_set_id');
126 $defaults["discount_end_date[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
127 'end_date', 'price_set_id');
128 $defaultDiscounts[] = CRM_Price_BAO_PriceSet
::getSetDetail($optionGroupId);
132 //avoid moving up value of lable when some labels don't
133 //have a value ,fixed for CRM-3088
135 foreach ($defaultDiscounts as $val) {
136 $discountFields = current($val);
137 $discountFields = current($discountFields['fields']);
139 foreach ($discountFields['options'] as $discountFieldsval) {
140 $defaults['discounted_label'][$discountFieldsval['weight']] = $discountFieldsval['label'];
141 $defaults['discounted_value'][$discountFieldsval['weight']][$rowCount] = CRM_Utils_Money
::format($discountFieldsval['amount'], NULL, '%a');
142 $defaults['discount_option_id'][$rowCount][$discountFieldsval['weight']] = $discountFieldsval['id'];
143 if (!empty($discountFieldsval['is_default'])) {
144 $defaults['discounted_default'] = $discountFieldsval['weight'];
150 ksort($defaults['discounted_value']);
151 ksort($defaults['discounted_label']);
153 foreach ($defaults['discounted_label'] as $key => $value) {
154 if ($key != $rowCount) {
155 $defaults['discounted_label'][$rowCount] = $value;
156 $defaults['discounted_value'][$rowCount] = $defaults['discounted_value'][$key];
157 unset($defaults['discounted_value'][$key]);
158 unset($defaults['discounted_label'][$key]);
159 foreach ($defaults['discount_option_id'] as &$optionIds) {
160 if (array_key_exists($key, $optionIds)) {
161 $optionIds[$rowCount] = $optionIds[$key];
162 unset($optionIds[$key]);
169 $this->set('discountSection', 1);
170 $this->buildQuickForm();
172 elseif (!empty($defaults['label'])) {
173 //if Regular Fees are present in DB and event fee page is in update mode
174 $defaults['discounted_label'] = $defaults['label'];
176 elseif (!empty($this->_submitValues
['label'])) {
177 //if event is newly created, use submitted values for
179 if (is_array($this->_submitValues
['label'])) {
181 foreach ($this->_submitValues
['label'] as $value) {
183 $defaults['discounted_label'][$k] = $value;
189 $defaults['id'] = $eventId;
190 if (!empty($totalLables)) {
191 $maxKey = count($totalLables) - 1;
192 if (isset($maxKey) && !empty($totalLables[$maxKey]['value'])) {
193 foreach ($totalLables[$maxKey]['value'] as $i => $v) {
194 if ($totalLables[$maxKey]['amount_id'][$i] == CRM_Utils_Array
::value('default_discount_fee_id', $defaults)) {
195 $defaults['discounted_default'] = $i;
202 if (!isset($defaults['discounted_default'])) {
203 $defaults['discounted_default'] = 1;
206 if (!isset($defaults['is_monetary'])) {
207 $defaults['is_monetary'] = 1;
210 if (!isset($defaults['fee_label'])) {
211 $defaults['fee_label'] = ts('Event Fee(s)');
214 if (!isset($defaults['pay_later_text']) ||
215 empty($defaults['pay_later_text'])
217 $defaults['pay_later_text'] = ts('I will send payment by check');
220 $this->_showHide
= new CRM_Core_ShowHideBlocks();
221 if (!$defaults['is_monetary']) {
222 $this->_showHide
->addHide('event-fees');
225 if (isset($defaults['price_set_id'])) {
226 $this->_showHide
->addHide('map-field');
228 $this->_showHide
->addToTemplate();
229 $this->assign('inDate', $this->_inDate
);
230 if (!empty($defaults['payment_processor'])) {
231 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
232 $defaults['payment_processor']
240 * Build the form object.
242 public function buildQuickForm() {
243 $this->addYesNo('is_monetary',
247 array('onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);")
250 //add currency element.
251 $this->addCurrency('currency', ts('Currency'), FALSE);
253 $paymentProcessor = CRM_Core_PseudoConstant
::paymentProcessor();
255 $this->assign('paymentProcessor', $paymentProcessor);
256 $this->addCheckBox('payment_processor', ts('Payment Processor'),
257 array_flip($paymentProcessor),
258 NULL, NULL, NULL, NULL,
259 array(' ', ' ', ' ', '<br/>')
263 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() ||
264 (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() && CRM_Core_Permission
::check('administer CiviCRM Financial Types'))) {
265 $this->addSelect('financial_type_id');
268 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::ADD
);
269 $this->addSelect('financial_type_id', array('context' => 'search', 'options' => $financialTypes));
271 // add pay later options
272 $this->addElement('checkbox', 'is_pay_later', ts('Enable Pay Later option?'), NULL,
273 array('onclick' => "return showHideByValue('is_pay_later','','payLaterOptions','block','radio',false);")
275 $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'),
276 CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'),
279 $this->add('wysiwyg', 'pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'));
281 $this->addElement('checkbox', 'is_billing_required', ts('Billing address required'));
282 $this->add('text', 'fee_label', ts('Fee Label'));
284 $price = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviEvent');
285 if (CRM_Utils_System
::isNull($price)) {
286 $this->assign('price', FALSE);
289 $this->assign('price', TRUE);
291 $this->add('select', 'price_set_id', ts('Price Set'),
293 '' => ts('- none -'),
295 NULL, array('onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);")
297 $default = array($this->createElement('radio', NULL, NULL, NULL, 0));
298 $this->add('hidden', 'price_field_id', '', array('id' => 'price_field_id'));
299 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
301 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
302 $this->add('hidden', "price_field_value[$i]", '', array('id' => "price_field_value[$i]"));
305 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
306 $this->addRule("value[$i]", ts('Please enter a valid money value for this field (e.g. %1).', array(1 => CRM_Utils_Money
::format('99.99', ' '))), 'money');
309 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
312 $this->addGroup($default, 'default');
314 $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL,
315 array('onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);")
317 $discountSection = $this->get('discountSection');
319 $this->assign('discountSection', $discountSection);
321 // form fields of Discount sets
322 $defaultOption = array();
323 $_showHide = new CRM_Core_ShowHideBlocks('', '');
325 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
326 //the show hide blocks
327 $showBlocks = 'discount_' . $i;
329 $_showHide->addHide($showBlocks);
332 $_showHide->addShow($showBlocks);
335 //Increment by 1 of start date of previous end date.
336 if (is_array($this->_submitValues
) &&
337 !empty($this->_submitValues
['discount_name'][$i]) &&
338 !empty($this->_submitValues
['discount_name'][$i +
1]) &&
339 isset($this->_submitValues
['discount_end_date']) &&
340 isset($this->_submitValues
['discount_end_date'][$i]) &&
341 $i < self
::NUM_DISCOUNT
- 1
343 if (!empty($this->_submitValues
['discount_end_date'][$i +
1])
344 && empty($this->_submitValues
['discount_start_date'][$i +
1])
346 $this->_submitValues
['discount_start_date'][$i +
1] = date('Y-m-d', strtotime("+1 days " . $this->_submitValues
['discount_end_date'][$i]));
349 //Decrement by 1 of end date from next start date.
351 is_array($this->_submitValues
) &&
352 !empty($this->_submitValues
['discount_name'][$i]) &&
353 !empty($this->_submitValues
['discount_name'][$i - 1]) &&
354 isset($this->_submitValues
['discount_start_date']) &&
355 isset($this->_submitValues
['discount_start_date'][$i])
357 if (!empty($this->_submitValues
['discount_start_date'][$i])
358 && empty($this->_submitValues
['discount_end_date'][$i - 1])
360 list($this->_submitValues
['discount_end_date'][$i - 1]) = date('Y-m-d', strtotime("-1 days " . $this->_submitValues
['discount_start_date'][$i]));
364 $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
365 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'title')
367 $this->add('hidden', "discount_price_set[$i]", '', array('id' => "discount_price_set[$i]"));
368 $this->add('datepicker', 'discount_start_date[' . $i . ']', ts('Discount Start Date'), [], FALSE, array('time' => FALSE));
369 $this->add('datepicker', 'discount_end_date[' . $i . ']', ts('Discount End Date'), [], FALSE, array('time' => FALSE));
371 $_showHide->addToTemplate();
372 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
373 array('class' => 'crm-form-submit cancel')
375 if (CRM_Contribute_BAO_Contribution
::checkContributeSettings('deferred_revenue_enabled')) {
376 $deferredFinancialType = CRM_Financial_BAO_FinancialAccount
::getDeferredFinancialType();
377 $this->assign('deferredFinancialType', array_keys($deferredFinancialType));
379 $this->buildAmountLabel();
380 parent
::buildQuickForm();
384 * Add local and global form rules.
386 public function addRules() {
387 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Fee', 'formRule'));
391 * Global validation rules for the form.
393 * @param array $values
394 * Posted values of the form.
397 * list of errors to be posted back to the form
399 public static function formRule($values) {
401 if (!empty($values['is_discount'])) {
402 $occurDiscount = array_count_values($values['discount_name']);
404 $countemptyvalue = 0;
405 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
406 $start_date = $end_date = NULL;
407 if (!empty($values['discount_name'][$i])) {
408 if (!empty($values['discount_start_date'][$i])) {
409 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_start_date'][$i]) : 0;
411 if (!empty($values['discount_end_date'][$i])) {
412 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i]) : 0;
415 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
416 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
419 if (!$start_date && !$end_date) {
420 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
424 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i - 1]) : 0;
425 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
426 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
428 elseif (!$start_date && !$end_date_1) {
430 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
434 foreach ($occurDiscount as $key => $value) {
435 if ($value > 1 && $key <> '') {
436 if ($key == $values['discount_name'][$i]) {
437 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', array(1 => $key));
442 //validation for discount labels and values
443 for ($index = (self
::NUM_OPTION
); $index > 0; $index--) {
445 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
448 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
451 if (!empty($values['discounted_label'][$index])) {
452 $duplicateIndex = CRM_Utils_Array
::key($values['discounted_label'][$index], $values['discounted_label']);
454 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
455 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
458 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
461 if (empty($values['discounted_value'][$index][$i])) {
465 if (!empty($values['_qf_Fee_next']) && ($countemptyrows == 11 ||
$countemptyvalue == 11)) {
466 $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.');
471 if ($values['is_monetary']) {
472 //check if financial type is selected
473 if (!$values['financial_type_id']) {
474 $errors['financial_type_id'] = ts("Please select financial type.");
477 //check for the event fee label (mandatory)
478 if (!$values['fee_label']) {
479 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
482 if (empty($values['price_set_id'])) {
483 //check fee label and amount
485 $optionKeys = array();
486 foreach ($values['label'] as $key => $val) {
487 if (trim($val) && trim($values['value'][$key])) {
488 $optionKeys[$key] = $key;
493 $default = CRM_Utils_Array
::value('default', $values);
494 if ($default && !in_array($default, $optionKeys)) {
495 $errors['default'] = ts('Please select an appropriate option as default.');
499 if (!$values['label'][1]) {
500 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
502 if (!$values['value'][1]) {
503 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
507 if (isset($values['is_pay_later'])) {
508 if (empty($values['pay_later_text'])) {
509 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
511 if (empty($values['pay_later_receipt'])) {
512 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
516 if (empty($values['payment_processor'])) {
517 $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.');
521 return empty($errors) ?
TRUE : $errors;
524 public function buildAmountLabel() {
526 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
528 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
530 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
531 $this->add('text', "discounted_value[$i][$j]", ts('Value'), array('size' => 10));
532 $this->addRule("discounted_value[$i][$j]", ts('Please enter a valid money value for this field (e.g. %1).', array(1 => CRM_Utils_Money
::format('99.99', ' '))), 'money');
536 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
539 $this->addGroup($default, 'discounted_default');
545 public function postProcess() {
547 $params = $this->cleanMoneyFields($this->exportValues());
549 $this->set('discountSection', 0);
551 if (!empty($_POST['_qf_Fee_submit'])) {
552 $this->buildAmountLabel();
553 $this->set('discountSection', 2);
557 if (!empty($params['payment_processor'])) {
558 $params['payment_processor'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($params['payment_processor']));
561 $params['payment_processor'] = 'null';
564 $params['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, 0);
565 $params['is_billing_required'] = CRM_Utils_Array
::value('is_billing_required', $params, 0);
569 // delete all the prior label values or discounts in the custom options table
570 // and delete a price set if one exists
571 //@todo note that this removes the reference from existing participants -
572 // even where there is not change - redress?
573 // note that a more tentative form of this is invoked by passing price_set_id as an array
574 // to event.create see CRM-14069
575 // @todo get all of this logic out of form layer (currently partially in BAO/api layer)
576 if (CRM_Price_BAO_PriceSet
::removeFrom('civicrm_event', $this->_id
)) {
577 CRM_Core_BAO_Discount
::del($this->_id
, 'civicrm_event');
581 if ($params['is_monetary']) {
582 if (!empty($params['price_set_id'])) {
583 //@todo this is now being done in the event BAO if passed price_set_id as an array
584 // per notes on that fn - looking at the api converting to an array
585 // so calling via the api may cause this to be done in the api
586 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $params['price_set_id']);
587 if (!empty($params['price_field_id'])) {
588 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
589 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID, 0);
593 // if there are label / values, create custom options for them
594 $labels = CRM_Utils_Array
::value('label', $params);
595 $values = CRM_Utils_Array
::value('value', $params);
596 $default = CRM_Utils_Array
::value('default', $params);
598 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
599 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
600 if (!empty($labels[$i]) && !CRM_Utils_System
::isNull($values[$i])) {
602 'label' => trim($labels[$i]),
603 'value' => $values[$i],
606 'is_default' => $default == $i,
610 if (!empty($options)) {
611 $params['default_fee_id'] = NULL;
612 if (empty($params['price_set_id'])) {
613 if (empty($params['price_field_id'])) {
614 $setParams['title'] = $eventTitle = ($this->_isTemplate
) ?
$this->_defaultValues
['template_title'] : $this->_defaultValues
['title'];
615 $eventTitle = strtolower(CRM_Utils_String
::munge($eventTitle, '_', 245));
616 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle, 'id', 'name')) {
617 $setParams['name'] = $eventTitle;
619 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $this->_id
, 'id', 'name')) {
620 $setParams['name'] = $eventTitle . '_' . $this->_id
;
623 $timeSec = explode('.', microtime(TRUE));
624 $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
626 $setParams['is_quick_config'] = 1;
627 $setParams['financial_type_id'] = $params['financial_type_id'];
628 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
629 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
631 $fieldParams['name'] = strtolower(CRM_Utils_String
::munge($params['fee_label'], '_', 245));
632 $fieldParams['price_set_id'] = $priceSet->id
;
635 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
636 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
637 CRM_Price_BAO_PriceFieldValue
::setIsActive($fieldValueID, '0');
638 unset($params['price_field_value'][$arrayID]);
641 $fieldParams['id'] = CRM_Utils_Array
::value('price_field_id', $params);
642 $fieldParams['option_id'] = $params['price_field_value'];
644 $priceSet = new CRM_Price_BAO_PriceSet();
645 $priceSet->id
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $params), 'price_set_id');
647 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
648 CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceSet', $priceSet->id
, 'financial_type_id', $params['financial_type_id']);
651 $fieldParams['label'] = $params['fee_label'];
652 $fieldParams['html_type'] = 'Radio';
653 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $priceSet->id
);
654 $fieldParams['option_label'] = $params['label'];
655 $fieldParams['option_amount'] = $params['value'];
656 $fieldParams['financial_type_id'] = $params['financial_type_id'];
657 foreach ($options as $value) {
658 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
660 $fieldParams['default_option'] = $params['default'];
661 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
666 $discountPriceSets = !empty($this->_defaultValues
['discount_price_set']) ?
$this->_defaultValues
['discount_price_set'] : array();
667 $discountFieldIDs = !empty($this->_defaultValues
['discount_option_id']) ?
$this->_defaultValues
['discount_option_id'] : array();
668 if (CRM_Utils_Array
::value('is_discount', $params) == 1) {
669 // if there are discounted set of label / values,
670 // create custom options for them
671 $labels = CRM_Utils_Array
::value('discounted_label', $params);
672 $values = CRM_Utils_Array
::value('discounted_value', $params);
673 $default = CRM_Utils_Array
::value('discounted_default', $params);
675 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
676 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
677 $discountOptions = array();
678 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
679 if (!empty($labels[$i]) &&
680 !CRM_Utils_System
::isNull(CRM_Utils_Array
::value($j, $values[$i]))
682 $discountOptions[] = array(
683 'label' => trim($labels[$i]),
684 'value' => $values[$i][$j],
687 'is_default' => $default == $i,
692 if (!empty($discountOptions)) {
693 $fieldParams = array();
694 $params['default_discount_fee_id'] = NULL;
696 $setParams = array();
697 if (empty($discountPriceSets[$keyCheck])) {
699 $eventTitle = strtolower(CRM_Utils_String
::munge($this->_defaultValues
['title'], '_', 200));
701 $setParams['title'] = $params['discount_name'][$j];
702 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) {
703 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j];
705 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
, 'id', 'name')) {
706 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
;
709 $timeSec = explode('.', microtime(TRUE));
710 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
712 $setParams['is_quick_config'] = 1;
713 $setParams['financial_type_id'] = $params['financial_type_id'];
714 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
715 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
716 $priceSetID = $priceSet->id
;
719 $priceSetID = $discountPriceSets[$j - 1];
721 'title' => $params['discount_name'][$j],
724 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
725 $setParams['financial_type_id'] = $params['financial_type_id'];
727 CRM_Price_BAO_PriceSet
::create($setParams);
728 unset($discountPriceSets[$j - 1]);
729 $fieldParams['id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceField', $priceSetID, 'id', 'price_set_id');
732 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
733 $fieldParams['is_required'] = 1;
734 $fieldParams['price_set_id'] = $priceSetID;
735 $fieldParams['html_type'] = 'Radio';
736 $fieldParams['financial_type_id'] = $params['financial_type_id'];
737 foreach ($discountOptions as $value) {
738 $fieldParams['option_label'][$value['weight']] = $value['label'];
739 $fieldParams['option_amount'][$value['weight']] = $value['value'];
740 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
741 if (!empty($value['is_default'])) {
742 $fieldParams['default_option'] = $value['weight'];
744 if (!empty($discountFieldIDs[$j]) && !empty($discountFieldIDs[$j][$value['weight']])) {
745 $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']];
746 unset($discountFieldIDs[$j][$value['weight']]);
749 //create discount priceset
750 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
751 if (!empty($discountFieldIDs[$j])) {
752 foreach ($discountFieldIDs[$j] as $fID) {
753 CRM_Price_BAO_PriceFieldValue
::setIsActive($fID, '0');
757 $discountParams = array(
758 'entity_table' => 'civicrm_event',
759 'entity_id' => $this->_id
,
760 'price_set_id' => $priceSetID,
761 'start_date' => $params['discount_start_date'][$j],
762 'end_date' => $params['discount_end_date'][$j],
764 CRM_Core_BAO_Discount
::add($discountParams);
769 if (!empty($discountPriceSets)) {
770 foreach ($discountPriceSets as $setId) {
771 CRM_Price_BAO_PriceSet
::setIsQuickConfig($setId, 0);
777 if (!empty($params['price_field_id'])) {
778 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
779 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID, 0);
781 $params['financial_type_id'] = '';
782 $params['is_pay_later'] = 0;
783 $params['is_billing_required'] = 0;
786 //update 'is_billing_required'
787 if (empty($params['is_pay_later'])) {
788 $params['is_billing_required'] = FALSE;
791 //update events table
792 $params['id'] = $this->_id
;
793 // skip update of financial type in price set
794 $params['skipFinancialType'] = TRUE;
795 CRM_Event_BAO_Event
::add($params);
797 // Update tab "disabled" css class
798 $this->ajaxResponse
['tabValid'] = !empty($params['is_monetary']);
799 parent
::endPostProcess();
803 * Return a descriptive name for the page, used in wizard header
807 public function getTitle() {
808 return ts('Event Fees');
812 * Clean money fields in submitted params to remove formatting.
814 * @param array $params
818 protected function cleanMoneyFields($params) {
819 foreach ($params['value'] as $index => $value) {
820 if (CRM_Utils_System
::isNull($value)) {
821 unset($params['value'][$index]);
824 $params['value'][$index] = CRM_Utils_Rule
::cleanMoney(trim($value));
827 foreach ($params['discounted_value'] as $index => $discountedValueSet) {
828 foreach ($discountedValueSet as $innerIndex => $value) {
829 if (CRM_Utils_System
::isNull($value)) {
830 unset($params['discounted_value'][$index][$innerIndex]);
833 $params['discounted_value'][$index][$innerIndex] = CRM_Utils_Rule
::cleanMoney(trim($value));
836 if (empty($params['discounted_value'][$index])) {
837 unset($params['discounted_value'][$index]);