3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This class generates form components for Event Fees
40 class CRM_Event_Form_ManageEvent_Fee
extends CRM_Event_Form_ManageEvent
{
43 * Constants for number of options for data types of multiple option.
45 CONST NUM_OPTION
= 11;
48 * Constants for number of discounts for the event.
50 CONST NUM_DISCOUNT
= 6;
63 * Function to set variables up before form is built
68 function preProcess() {
73 * This function sets the default values for the form. For edit/view mode
74 * the default values are retrieved from the database
80 function setDefaultValues() {
81 $parentDefaults = parent
::setDefaultValues();
83 $eventId = $this->_id
;
86 if (isset($eventId)) {
87 $params = array('id' => $eventId);
90 CRM_Event_BAO_Event
::retrieve($params, $defaults);
92 if (isset($eventId)) {
93 $price_set_id = CRM_Price_BAO_Set
::getFor('civicrm_event', $eventId, NULL, 1);
96 $defaults['price_set_id'] = $price_set_id;
99 $priceSetId = CRM_Price_BAO_Set
::getFor('civicrm_event', $eventId, NULL);
101 if ($isQuick = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_Set', $priceSetId, 'is_quick_config')) {
102 $this->assign('isQuick', $isQuick);
103 $priceField = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_Field', $priceSetId, 'id', 'price_set_id');
105 $priceFieldOptions = CRM_Price_BAO_FieldValue
::getValues($priceField, $options, 'weight', true);
106 $defaults['price_field_id'] = $priceField;
108 foreach ($options as $optionId => $optionValue) {
110 $defaults['value'][$countRow] = $optionValue['amount'];
111 $defaults['label'][$countRow] = $optionValue['label'];
112 $defaults['name'][$countRow] = $optionValue['name'];
113 $defaults['weight'][$countRow] = $optionValue['weight'];
114 $defaults['price_field_value'][$countRow] = $optionValue['id'];
115 if ($optionValue['is_default']) {
116 $defaults['default'] = $countRow;
124 //check if discounted
125 $discountedEvent = CRM_Core_BAO_Discount
::getOptionGroup($this->_id
, 'civicrm_event');
126 if (!empty($discountedEvent)) {
127 $defaults['is_discount'] = $i = 1;
128 $totalLables = $maxSize = $defaultDiscounts = array();
129 foreach ($discountedEvent as $optionGroupId) {
130 $defaults['discount_price_set'][] = $optionGroupId;
131 $name = $defaults["discount_name[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_Set', $optionGroupId, 'title');
133 list($defaults["discount_start_date[$i]"]) = CRM_Utils_Date
::setDateDefaults(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
134 'start_date', 'price_set_id'
136 list($defaults["discount_end_date[$i]"]) = CRM_Utils_Date
::setDateDefaults(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
137 'end_date', 'price_set_id'
139 $defaultDiscounts[] = CRM_Price_BAO_Set
::getSetDetail($optionGroupId);
143 //avoid moving up value of lable when some labels don't
144 //have a value ,fixed for CRM-3088
146 foreach ($defaultDiscounts as $val) {
147 $discountFields = current($val);
148 $discountFields = current($discountFields['fields']);
150 foreach ($discountFields['options'] as $discountFieldsval) {
151 $defaults['discounted_label'][$discountFieldsval['weight']] = $discountFieldsval['label'];
152 $defaults['discounted_value'][$discountFieldsval['weight']][$rowCount] = $discountFieldsval['amount'];
153 $defaults['discount_option_id'][$rowCount][]= $discountFieldsval['id'];
154 if (CRM_Utils_Array
::value('is_default', $discountFieldsval)) {
155 $defaults['discounted_default'] = $discountFieldsval['weight'];
161 $this->set('discountSection', 1);
162 $this->buildQuickForm();
164 elseif (!empty($defaults['label'])) {
165 //if Regular Fees are present in DB and event fee page is in update mode
166 $defaults['discounted_label'] = $defaults['label'];
168 elseif (CRM_Utils_Array
::value('label', $this->_submitValues
)) {
169 //if event is newly created, use submitted values for
171 if (is_array($this->_submitValues
['label'])) {
173 foreach ($this->_submitValues
['label'] as $value) {
175 $defaults['discounted_label'][$k] = $value;
181 $defaults['id'] = $eventId;
182 if (!empty($totalLables)) {
183 $maxKey = count($totalLables) - 1;
184 if (isset($maxKey) &&
185 CRM_Utils_Array
::value('value', $totalLables[$maxKey])
187 foreach ($totalLables[$maxKey]['value'] as $i => $v) {
188 if ($totalLables[$maxKey]['amount_id'][$i] == CRM_Utils_Array
::value('default_discount_fee_id', $defaults)) {
189 $defaults['discounted_default'] = $i;
196 if (!isset($defaults['discounted_default'])) {
197 $defaults['discounted_default'] = 1;
200 if (!isset($defaults['is_monetary'])) {
201 $defaults['is_monetary'] = 1;
204 if (!isset($defaults['fee_label'])) {
205 $defaults['fee_label'] = ts('Event Fee(s)');
208 if (!isset($defaults['pay_later_text']) ||
209 empty($defaults['pay_later_text'])
211 $defaults['pay_later_text'] = ts('I will send payment by check');
214 $this->_showHide
= new CRM_Core_ShowHideBlocks();
215 if (!$defaults['is_monetary']) {
216 $this->_showHide
->addHide('event-fees');
219 if (isset($defaults['price_set_id'])) {
220 $this->_showHide
->addHide('map-field');
222 $this->_showHide
->addToTemplate();
223 $this->assign('inDate', $this->_inDate
);
225 if (CRM_Utils_Array
::value('payment_processor', $defaults)) {
226 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
227 $defaults['payment_processor']
234 * Function to build the form
239 public function buildQuickForm() {
241 $this->addYesNo('is_monetary',
245 array('onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);")
248 //add currency element.
249 $this->addCurrency('currency', ts('Currency'), FALSE);
251 $paymentProcessor = CRM_Core_PseudoConstant
::paymentProcessor();
253 $this->assign('paymentProcessor', $paymentProcessor);
255 $this->addCheckBox('payment_processor', ts('Payment Processor'),
256 array_flip($paymentProcessor),
257 NULL, NULL, NULL, NULL,
258 array(' ', ' ', ' ', '<br/>')
262 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
263 $this->add('select', 'financial_type_id', ts('Financial Type'),
264 array('' => ts('- select -')) +
$financialType
266 // add pay later options
267 $this->addElement('checkbox', 'is_pay_later', ts('Enable Pay Later option?'), NULL,
268 array('onclick' => "return showHideByValue('is_pay_later','','payLaterOptions','block','radio',false);")
270 $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'),
271 CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'),
274 $this->addWysiwyg('pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'));
276 $this->add('text', 'fee_label', ts('Fee Label'));
278 $price = CRM_Price_BAO_Set
::getAssoc(FALSE, 'CiviEvent');
279 if (CRM_Utils_System
::isNull($price)) {
280 $this->assign('price', FALSE);
283 $this->assign('price', TRUE);
285 $this->add('select', 'price_set_id', ts('Price Set'),
287 '' => ts('- none -')) +
$price,
288 NULL, array('onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);")
291 $this->add('hidden', 'price_field_id', '', array('id' => 'price_field_id'));
292 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
294 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
295 $this->add('hidden', "price_field_value[$i]", '', array('id' => "price_field_value[$i]"));
298 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
299 $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');
302 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
305 $this->addGroup($default, 'default');
307 $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL,
308 array('onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);")
310 $discountSection = $this->get('discountSection');
312 $this->assign('discountSection', $discountSection);
314 // form fields of Discount sets
315 $defaultOption = array();
316 $_showHide = new CRM_Core_ShowHideBlocks('', '');
318 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
319 //the show hide blocks
320 $showBlocks = 'discount_' . $i;
322 $_showHide->addHide($showBlocks);
325 $_showHide->addShow($showBlocks);
328 //Increment by 1 of start date of previous end date.
329 if (is_array($this->_submitValues
) &&
330 !empty($this->_submitValues
['discount_name'][$i]) &&
331 !empty($this->_submitValues
['discount_name'][$i +
1]) &&
332 isset($this->_submitValues
['discount_end_date']) &&
333 isset($this->_submitValues
['discount_end_date'][$i]) &&
334 $i < self
::NUM_DISCOUNT
- 1
336 $end_date = CRM_Utils_Date
::processDate($this->_submitValues
['discount_end_date'][$i]);
337 if (!empty($this->_submitValues
['discount_end_date'][$i +
1])
338 && empty($this->_submitValues
['discount_start_date'][$i +
1])
340 list($this->_submitValues
['discount_start_date'][$i +
1]) = CRM_Utils_Date
::setDateDefaults(date('Y-m-d', strtotime("+1 days $end_date")));
343 //Decrement by 1 of end date from next start date.
345 is_array($this->_submitValues
) &&
346 !empty($this->_submitValues
['discount_name'][$i]) &&
347 !empty($this->_submitValues
['discount_name'][$i - 1]) &&
348 isset($this->_submitValues
['discount_start_date']) &&
349 isset($this->_submitValues
['discount_start_date'][$i])
351 $start_date = CRM_Utils_Date
::processDate($this->_submitValues
['discount_start_date'][$i]);
352 if (!empty($this->_submitValues
['discount_start_date'][$i])
353 && empty($this->_submitValues
['discount_end_date'][$i - 1])
355 list($this->_submitValues
['discount_end_date'][$i - 1]) = CRM_Utils_Date
::setDateDefaults(date('Y-m-d', strtotime("-1 days $start_date")));
360 $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
361 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_Set', 'title')
364 $this->add('hidden', "discount_price_set[$i]", '', array('id' => "discount_price_set[$i]"));
365 // add a rule to ensure that discount name is not more than 24 characters to prevent overflow
366 // in option group name, CRM-7915
367 // 24 characters will make the option group name less than 64 characters
368 $this->addRule("discount_name[$i]",
369 ts('Discount Name cannot be more than 24 characters'),
374 //discount start date
375 $this->addDate('discount_start_date[' . $i . ']', ts('Discount Start Date'), FALSE, array('formatType' => 'activityDate'));
378 $this->addDate('discount_end_date[' . $i . ']', ts('Discount End Date'), FALSE, array('formatType' => 'activityDate'));
380 $_showHide->addToTemplate();
381 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
382 array('class' => 'form-submit')
385 $this->buildAmountLabel();
386 parent
::buildQuickForm();
390 * Add local and global form rules
396 function addRules() {
397 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Fee', 'formRule'));
401 * global validation rules for the form
403 * @param array $values posted values of the form
405 * @return array list of errors to be posted back to the form
409 static function formRule($values) {
411 if (CRM_Utils_Array
::value('is_discount', $values)) {
412 $occurDiscount = array_count_values($values['discount_name']);
414 $countemptyvalue = 0;
415 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
416 $start_date = $end_date = NULL;
417 if (CRM_Utils_Array
::value($i, $values['discount_name'])) {
418 if (CRM_Utils_Array
::value($i, $values['discount_start_date'])) {
419 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_start_date'][$i]) : 0;
421 if (CRM_Utils_Array
::value($i, $values['discount_end_date'])) {
422 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i]) : 0;
425 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
426 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
429 if (!$start_date && !$end_date) {
430 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
434 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i - 1]) : 0;
435 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
436 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
438 elseif (!$start_date && !$end_date_1) {
440 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
444 foreach ($occurDiscount as $key => $value) if ($value > 1 && $key <> '') {
445 if ($key == $values['discount_name'][$i]) {
446 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', array(1 => $key));
450 //validation for discount labels and values
451 for ($index = (self
::NUM_OPTION
); $index > 0; $index--) {
453 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
456 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
459 if (!empty($values['discounted_label'][$index])) {
460 $duplicateIndex = CRM_Utils_Array
::key($values['discounted_label'][$index], $values['discounted_label']);
462 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
463 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
466 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
469 if (empty($values['discounted_value'][$index][$i])) {
473 if (CRM_Utils_Array
::value('_qf_Fee_next', $values) && ($countemptyrows == 11 ||
$countemptyvalue == 11)) {
474 $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.');
479 if ($values['is_monetary']) {
480 //check if financial type is selected
481 if (!$values['financial_type_id']) {
482 $errors['financial_type_id'] = ts("Please select financial type.");
485 //check for the event fee label (mandatory)
486 if (!$values['fee_label']) {
487 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
490 if (!CRM_Utils_Array
::value('price_set_id', $values)) {
491 //check fee label and amount
493 $optionKeys = array();
494 foreach ($values['label'] as $key => $val) {
495 if (trim($val) && trim($values['value'][$key])) {
496 $optionKeys[$key] = $key;
501 $default = CRM_Utils_Array
::value('default', $values);
502 if ($default && !in_array($default, $optionKeys)) {
503 $errors['default'] = ts('Please select an appropriate option as default.');
507 if (!$values['label'][1]) {
508 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
510 if (!$values['value'][1]) {
511 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
515 if (isset($values['is_pay_later'])) {
516 if (empty($values['pay_later_text'])) {
517 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
519 if (empty($values['pay_later_receipt'])) {
520 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
524 return empty($errors) ?
TRUE : $errors;
527 public function buildAmountLabel() {
529 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
531 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
533 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
534 $this->add('text', "discounted_value[$i][$j]", ts('Value'), array('size' => 10));
535 $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');
539 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
542 $this->addGroup($default, 'discounted_default');
551 public function postProcess() {
554 $params = $this->exportValues();
556 $this->set('discountSection', 0);
558 if (CRM_Utils_Array
::value('_qf_Fee_submit', $_POST)) {
559 $this->buildAmountLabel();
560 $this->set('discountSection', 2);
564 if (array_key_exists('payment_processor', $params) &&
565 !CRM_Utils_System
::isNull($params['payment_processor'])
567 $params['payment_processor'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($params['payment_processor']));
570 $params['payment_processor'] = 'null';
573 $params['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, 0);
577 // delete all the prior label values or discounts in the custom options table
578 // and delete a price set if one exists
579 if (CRM_Price_BAO_Set
::removeFrom('civicrm_event', $this->_id
)) {
580 CRM_Core_BAO_Discount
::del($this->_id
,'civicrm_event');
584 if ($params['is_monetary']) {
585 if (CRM_Utils_Array
::value('price_set_id', $params)) {
586 CRM_Price_BAO_Set
::addTo('civicrm_event', $this->_id
, $params['price_set_id']);
587 if (CRM_Utils_Array
::value('price_field_id', $params)) {
588 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_Field', $params['price_field_id'], 'price_set_id');
589 CRM_Price_BAO_Set
::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])) {
601 $options[] = array('label' => trim($labels[$i]),
602 'value' => CRM_Utils_Rule
::cleanMoney(trim($values[$i])),
605 'is_default' => $default == $i
609 if (!empty($options)) {
610 $params['default_fee_id'] = NULL;
611 if (!CRM_Utils_Array
::value('price_set_id', $params)) {
612 if (!CRM_Utils_Array
::value('price_field_id', $params)) {
613 $setParams['title'] = $eventTitle = ($this->_isTemplate
) ?
$this->_defaultValues
['template_title'] : $this->_defaultValues
['title'];
614 $eventTitle = strtolower(CRM_Utils_String
::munge($eventTitle, '_', 245));
615 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_Set', $eventTitle, 'id', 'name')) {
616 $setParams['name'] = $eventTitle;
618 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_Set', $eventTitle . '_' . $this->_id
, 'id', 'name')) {
619 $setParams['name'] = $eventTitle . '_' . $this->_id
;
622 $timeSec = explode('.', microtime(true));
623 $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
625 $setParams['is_quick_config'] = 1;
626 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
627 $priceSet = CRM_Price_BAO_Set
::create($setParams);
629 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
630 $fieldParams['price_set_id'] = $priceSet->id
;
633 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
634 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
635 CRM_Price_BAO_FieldValue
::setIsActive($fieldValueID, '0');
636 unset($params['price_field_value'][$arrayID]);
639 $fieldParams['id'] = CRM_Utils_Array
::value('price_field_id', $params);
640 $fieldParams['option_id'] = $params['price_field_value'];
641 $priceSet = new CRM_Price_BAO_Set();
642 $priceSet->id
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_Field', CRM_Utils_Array
::value('price_field_id', $params), 'price_set_id');
644 $fieldParams['html_type'] = 'Radio';
645 CRM_Price_BAO_Set
::addTo('civicrm_event', $this->_id
, $priceSet->id
);
646 $fieldParams['option_label'] = $params['label'];
647 $fieldParams['option_amount'] = $params['value'];
648 $fieldParams['financial_type_id'] = $params['financial_type_id'];
649 foreach ($options as $value) $fieldParams['option_weight'][$value['weight']] = $value['weight'];
650 $fieldParams['default_option'] = $params['default'];
651 $priceField = CRM_Price_BAO_Field
::create($fieldParams);
656 $discountPriceSets = CRM_Utils_Array
::value('discount_price_set', $this->_defaultValues
) ?
$this->_defaultValues
['discount_price_set']: array();
657 $discountFieldIDs = CRM_Utils_Array
::value('discount_option_id', $this->_defaultValues
) ?
$this->_defaultValues
['discount_option_id']: array();
658 if (CRM_Utils_Array
::value('is_discount', $params) == 1) {
659 // if there are discounted set of label / values,
660 // create custom options for them
661 $labels = CRM_Utils_Array
::value('discounted_label', $params);
662 $values = CRM_Utils_Array
::value('discounted_value', $params);
663 $default = CRM_Utils_Array
::value('discounted_default', $params);
665 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
666 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
667 $discountOptions = array();
668 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
669 if (!empty($labels[$i]) &&
670 !empty($values[$i][$j])
672 $discountOptions[] = array('label' => trim($labels[$i]),
673 'value' => CRM_Utils_Rule
::cleanMoney(trim($values[$i][$j])),
676 'is_default' => $default == $i
681 if (!empty($discountOptions)) {
682 $fieldParams = array();
683 $params['default_discount_fee_id'] = NULL;
685 if (!CRM_Utils_Array
::value($keyCheck, $discountPriceSets)) {
687 $eventTitle = strtolower(CRM_Utils_String
::munge($this->_defaultValues
['title'], '_', 200));
689 $setParams['title'] = $params['discount_name'][$j];
690 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_Set', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) {
691 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j];
693 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_Set', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
, 'id', 'name')) {
694 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
;
697 $timeSec = explode('.', microtime(true));
698 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
700 $setParams['is_quick_config'] = 1;
701 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
702 $priceSet = CRM_Price_BAO_Set
::create($setParams);
703 $priceSetID = $priceSet->id
;
705 $priceSetID = $discountPriceSets[$j-1];
706 unset($discountPriceSets[$j-1]);
707 $fieldParams['id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_Field', $priceSetID, 'id', 'price_set_id');
710 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
711 $fieldParams['is_required'] = 1;
712 $fieldParams['price_set_id'] = $priceSetID;
713 $fieldParams['html_type'] = 'Radio';
714 $fieldParams['financial_type_id'] = $params['financial_type_id'];
715 foreach ($discountOptions as $value) {
716 $fieldParams['option_label'][$value['weight']] = $value['label'];
717 $fieldParams['option_amount'][$value['weight']] = $value['value'];
718 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
719 if (CRM_Utils_Array
::value('is_default', $value)) {
720 $fieldParams['default_option'] = $value['weight'];
722 if (CRM_Utils_Array
::value($j, $discountFieldIDs) && CRM_Utils_Array
::value($value['weight']-1, $discountFieldIDs[$j])) {
723 $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']-1];
724 unset($discountFieldIDs[$j][$value['weight']-1]);
727 //create discount priceset
728 $priceField = CRM_Price_BAO_Field
::create($fieldParams);
729 if (!empty($discountFieldIDs)) {
730 foreach($discountFieldIDs as $fID){
731 CRM_Price_BAO_FieldValue
::setIsActive($fID, '0');
735 $discountParams = array(
736 'entity_table' => 'civicrm_event',
737 'entity_id' => $this->_id
,
738 'price_set_id' => $priceSetID,
739 'start_date' => CRM_Utils_Date
::processDate($params['discount_start_date'][$j]),
740 'end_date' => CRM_Utils_Date
::processDate($params['discount_end_date'][$j]),
742 CRM_Core_BAO_Discount
::add($discountParams);
747 if (!empty($discountPriceSets)) {
748 foreach ($discountPriceSets as $setId) {
749 CRM_Price_BAO_Set
::setIsQuickConfig($setId, 0);
755 if (CRM_Utils_Array
::value('price_field_id', $params)) {
756 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_Field', $params['price_field_id'], 'price_set_id');
757 CRM_Price_BAO_Set
::setIsQuickConfig($priceSetID,0);
759 $params['financial_type_id'] = '';
760 $params['is_pay_later'] = 0;
763 //update events table
764 $params['id'] = $this->_id
;
765 CRM_Event_BAO_Event
::add($params);
767 parent
::endPostProcess();
771 * Return a descriptive name for the page, used in wizard header
776 public function getTitle() {
777 return ts('Event Fees');