CRM-13929 - Improve calendar clear button
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Fee.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for Event Fees
38 *
39 */
40class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
41
42 /**
43 * Constants for number of options for data types of multiple option.
44 */
45 CONST NUM_OPTION = 11;
46
47 /**
48 * Constants for number of discounts for the event.
49 */
50 CONST NUM_DISCOUNT = 6;
51
52 /**
53 * Page action
54 */
55 public $_action;
56
57 /**
58 * in Date
59 */
60 private $_inDate;
61
62 /**
63 * Function to set variables up before form is built
64 *
65 * @return void
66 * @access public
67 */
68 function preProcess() {
69 parent::preProcess();
70 }
71
72 /**
73 * This function sets the default values for the form. For edit/view mode
74 * the default values are retrieved from the database
75 *
76 * @access public
77 *
355ba699 78 * @return void
6a488035
TO
79 */
80 function setDefaultValues() {
81 $parentDefaults = parent::setDefaultValues();
82
83 $eventId = $this->_id;
84 $params = array();
85 $defaults = array();
86 if (isset($eventId)) {
87 $params = array('id' => $eventId);
88 }
89
90 CRM_Event_BAO_Event::retrieve($params, $defaults);
91
92 if (isset($eventId)) {
9da8dc8c 93 $price_set_id = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventId, NULL, 1);
6a488035
TO
94
95 if ($price_set_id) {
96 $defaults['price_set_id'] = $price_set_id;
97 }
98 else {
9da8dc8c 99 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventId, NULL);
6a488035 100 if ($priceSetId) {
9da8dc8c 101 if ($isQuick = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config')) {
6a488035 102 $this->assign('isQuick', $isQuick);
9da8dc8c 103 $priceField = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $priceSetId, 'id', 'price_set_id');
6a488035 104 $options = array();
9da8dc8c 105 $priceFieldOptions = CRM_Price_BAO_PriceFieldValue::getValues($priceField, $options, 'weight', true);
6a488035
TO
106 $defaults['price_field_id'] = $priceField;
107 $countRow = 0;
108 foreach ($options as $optionId => $optionValue) {
109 $countRow++;
6fe4c6f8 110 $defaults['value'][$countRow] = CRM_Utils_Money::format($optionValue['amount'], NULL, '%a');
6a488035
TO
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;
117 }
118 }
119 }
120 }
121 }
122 }
123
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;
9da8dc8c 131 $name = $defaults["discount_name[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $optionGroupId, 'title');
6a488035
TO
132
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'
135 ));
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'
138 ));
9da8dc8c 139 $defaultDiscounts[] = CRM_Price_BAO_PriceSet::getSetDetail($optionGroupId);
6a488035
TO
140 $i++;
141 }
142
143 //avoid moving up value of lable when some labels don't
144 //have a value ,fixed for CRM-3088
145 $rowCount = 1;
146 foreach ($defaultDiscounts as $val) {
147 $discountFields = current($val);
148 $discountFields = current($discountFields['fields']);
149
150 foreach ($discountFields['options'] as $discountFieldsval) {
96fb2c1c
PN
151 $defaults['discounted_label'][$discountFieldsval['weight']] = $discountFieldsval['label'];
152 $defaults['discounted_value'][$discountFieldsval['weight']][$rowCount] =
6fe4c6f8 153 CRM_Utils_Money::format($discountFieldsval['amount'], NULL, '%a');
96fb2c1c 154 $defaults['discount_option_id'][$rowCount][$discountFieldsval['weight']]= $discountFieldsval['id'];
a7488080 155 if (!empty($discountFieldsval['is_default'])) {
6a488035
TO
156 $defaults['discounted_default'] = $discountFieldsval['weight'];
157 }
158 }
159 $rowCount++;
160 }
5e4b4477
PN
161 //CRM-12970
162 ksort($defaults['discounted_value']);
93c3579d 163 ksort($defaults['discounted_label']);
5e4b4477
PN
164 $rowCount = 1;
165 foreach ($defaults['discounted_label'] as $key => $value) {
166 if ($key != $rowCount) {
167 $defaults['discounted_label'][$rowCount] = $value;
168 $defaults['discounted_value'][$rowCount] = $defaults['discounted_value'][$key];
169 unset($defaults['discounted_value'][$key]);
170 unset($defaults['discounted_label'][$key]);
171 foreach ($defaults['discount_option_id'] as &$optionIds) {
172 if (array_key_exists($key, $optionIds)) {
173 $optionIds[$rowCount] = $optionIds[$key];
174 unset($optionIds[$key]);
175 }
176 }
177 }
178 $rowCount++;
179 }
6a488035
TO
180
181 $this->set('discountSection', 1);
182 $this->buildQuickForm();
183 }
184 elseif (!empty($defaults['label'])) {
185 //if Regular Fees are present in DB and event fee page is in update mode
186 $defaults['discounted_label'] = $defaults['label'];
187 }
a7488080 188 elseif (!empty($this->_submitValues['label'])) {
6a488035
TO
189 //if event is newly created, use submitted values for
190 //discount labels
191 if (is_array($this->_submitValues['label'])) {
192 $k = 1;
193 foreach ($this->_submitValues['label'] as $value) {
194 if ($value) {
195 $defaults['discounted_label'][$k] = $value;
196 $k++;
197 }
198 }
199 }
200 }
201 $defaults['id'] = $eventId;
202 if (!empty($totalLables)) {
203 $maxKey = count($totalLables) - 1;
8cc574cf 204 if (isset($maxKey) && !empty($totalLables[$maxKey]['value'])) {
6a488035
TO
205 foreach ($totalLables[$maxKey]['value'] as $i => $v) {
206 if ($totalLables[$maxKey]['amount_id'][$i] == CRM_Utils_Array::value('default_discount_fee_id', $defaults)) {
207 $defaults['discounted_default'] = $i;
208 break;
209 }
210 }
211 }
212 }
213
214 if (!isset($defaults['discounted_default'])) {
215 $defaults['discounted_default'] = 1;
216 }
217
218 if (!isset($defaults['is_monetary'])) {
219 $defaults['is_monetary'] = 1;
220 }
221
222 if (!isset($defaults['fee_label'])) {
223 $defaults['fee_label'] = ts('Event Fee(s)');
224 }
225
226 if (!isset($defaults['pay_later_text']) ||
227 empty($defaults['pay_later_text'])
228 ) {
229 $defaults['pay_later_text'] = ts('I will send payment by check');
230 }
231
232 $this->_showHide = new CRM_Core_ShowHideBlocks();
233 if (!$defaults['is_monetary']) {
234 $this->_showHide->addHide('event-fees');
235 }
236
237 if (isset($defaults['price_set_id'])) {
238 $this->_showHide->addHide('map-field');
239 }
240 $this->_showHide->addToTemplate();
241 $this->assign('inDate', $this->_inDate);
242
a7488080 243 if (!empty($defaults['payment_processor'])) {
6a488035
TO
244 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
245 $defaults['payment_processor']
246 ), '1');
247 }
248 return $defaults;
249 }
250
251 /**
252 * Function to build the form
253 *
355ba699 254 * @return void
6a488035
TO
255 * @access public
256 */
257 public function buildQuickForm() {
258
259 $this->addYesNo('is_monetary',
260 ts('Paid Event'),
261 NULL,
262 NULL,
263 array('onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);")
264 );
265
266 //add currency element.
267 $this->addCurrency('currency', ts('Currency'), FALSE);
268
269 $paymentProcessor = CRM_Core_PseudoConstant::paymentProcessor();
270
271 $this->assign('paymentProcessor', $paymentProcessor);
272
273 $this->addCheckBox('payment_processor', ts('Payment Processor'),
274 array_flip($paymentProcessor),
275 NULL, NULL, NULL, NULL,
276 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>')
277 );
03e04002 278
279 // financial type
6a488035
TO
280 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
281 $this->add('select', 'financial_type_id', ts('Financial Type'),
282 array('' => ts('- select -')) + $financialType
283 );
284 // add pay later options
285 $this->addElement('checkbox', 'is_pay_later', ts('Enable Pay Later option?'), NULL,
286 array('onclick' => "return showHideByValue('is_pay_later','','payLaterOptions','block','radio',false);")
287 );
288 $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'),
289 CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'),
290 FALSE
291 );
d11a05d6 292 $this->addWysiwyg('pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'));
6a488035
TO
293
294 $this->add('text', 'fee_label', ts('Fee Label'));
295
9da8dc8c 296 $price = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviEvent');
6a488035
TO
297 if (CRM_Utils_System::isNull($price)) {
298 $this->assign('price', FALSE);
299 }
300 else {
301 $this->assign('price', TRUE);
302 }
303 $this->add('select', 'price_set_id', ts('Price Set'),
304 array(
305 '' => ts('- none -')) + $price,
306 NULL, array('onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);")
307 );
8a4f27dc 308 $default = array($this->createElement('radio', NULL, NULL, NULL, 0));
6a488035
TO
309 $this->add('hidden', 'price_field_id', '', array('id' => 'price_field_id'));
310 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
311 // label
312 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
313 $this->add('hidden', "price_field_value[$i]", '', array('id' => "price_field_value[$i]"));
314
315 // value
316 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
317 $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');
318
319 // default
320 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
321 }
322
323 $this->addGroup($default, 'default');
324
325 $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL,
326 array('onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);")
327 );
328 $discountSection = $this->get('discountSection');
329
330 $this->assign('discountSection', $discountSection);
331
332 // form fields of Discount sets
333 $defaultOption = array();
334 $_showHide = new CRM_Core_ShowHideBlocks('', '');
335
336 for ($i = 1; $i <= self::NUM_DISCOUNT; $i++) {
337 //the show hide blocks
338 $showBlocks = 'discount_' . $i;
339 if ($i > 2) {
340 $_showHide->addHide($showBlocks);
341 }
342 else {
343 $_showHide->addShow($showBlocks);
344 }
345
346 //Increment by 1 of start date of previous end date.
347 if (is_array($this->_submitValues) &&
348 !empty($this->_submitValues['discount_name'][$i]) &&
349 !empty($this->_submitValues['discount_name'][$i + 1]) &&
350 isset($this->_submitValues['discount_end_date']) &&
351 isset($this->_submitValues['discount_end_date'][$i]) &&
352 $i < self::NUM_DISCOUNT - 1
353 ) {
354 $end_date = CRM_Utils_Date::processDate($this->_submitValues['discount_end_date'][$i]);
355 if (!empty($this->_submitValues['discount_end_date'][$i + 1])
356 && empty($this->_submitValues['discount_start_date'][$i + 1])
357 ) {
358 list($this->_submitValues['discount_start_date'][$i + 1]) = CRM_Utils_Date::setDateDefaults(date('Y-m-d', strtotime("+1 days $end_date")));
359 }
360 }
361 //Decrement by 1 of end date from next start date.
362 if ($i > 1 &&
363 is_array($this->_submitValues) &&
364 !empty($this->_submitValues['discount_name'][$i]) &&
365 !empty($this->_submitValues['discount_name'][$i - 1]) &&
366 isset($this->_submitValues['discount_start_date']) &&
367 isset($this->_submitValues['discount_start_date'][$i])
368 ) {
369 $start_date = CRM_Utils_Date::processDate($this->_submitValues['discount_start_date'][$i]);
370 if (!empty($this->_submitValues['discount_start_date'][$i])
371 && empty($this->_submitValues['discount_end_date'][$i - 1])
372 ) {
373 list($this->_submitValues['discount_end_date'][$i - 1]) = CRM_Utils_Date::setDateDefaults(date('Y-m-d', strtotime("-1 days $start_date")));
374 }
375 }
376
377 //discount name
378 $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
9da8dc8c 379 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title')
6a488035
TO
380 );
381
382 $this->add('hidden', "discount_price_set[$i]", '', array('id' => "discount_price_set[$i]"));
6a488035
TO
383
384 //discount start date
385 $this->addDate('discount_start_date[' . $i . ']', ts('Discount Start Date'), FALSE, array('formatType' => 'activityDate'));
386
387 //discount end date
388 $this->addDate('discount_end_date[' . $i . ']', ts('Discount End Date'), FALSE, array('formatType' => 'activityDate'));
389 }
390 $_showHide->addToTemplate();
391 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
392 array('class' => 'form-submit')
393 );
394
395 $this->buildAmountLabel();
396 parent::buildQuickForm();
397 }
398
399 /**
400 * Add local and global form rules
401 *
402 * @access protected
403 *
404 * @return void
405 */
406 function addRules() {
407 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Fee', 'formRule'));
408 }
409
410 /**
411 * global validation rules for the form
412 *
413 * @param array $values posted values of the form
414 *
415 * @return array list of errors to be posted back to the form
416 * @static
417 * @access public
418 */
419 static function formRule($values) {
420 $errors = array();
a7488080 421 if (!empty($values['is_discount'])) {
6a488035
TO
422 $occurDiscount = array_count_values($values['discount_name']);
423 $countemptyrows = 0;
424 $countemptyvalue = 0;
425 for ($i = 1; $i <= self::NUM_DISCOUNT; $i++) {
426 $start_date = $end_date = NULL;
a7488080
CW
427 if (!empty($values['discount_name'][$i])) {
428 if (!empty($values['discount_start_date'][$i])) {
6a488035
TO
429 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date::processDate($values['discount_start_date'][$i]) : 0;
430 }
a7488080 431 if (!empty($values['discount_end_date'][$i])) {
6a488035
TO
432 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date::processDate($values['discount_end_date'][$i]) : 0;
433 }
434
435 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
436 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
437 }
438
439 if (!$start_date && !$end_date) {
440 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
441 }
442
443 if ($i > 1) {
444 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date::processDate($values['discount_end_date'][$i - 1]) : 0;
445 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
446 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
447 }
448 elseif (!$start_date && !$end_date_1) {
449 $j = $i - 1;
450 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
451 }
452 }
453
454 foreach ($occurDiscount as $key => $value) if ($value > 1 && $key <> '') {
455 if ($key == $values['discount_name'][$i]) {
456 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', array(1 => $key));
457 }
458 }
459
460 //validation for discount labels and values
461 for ($index = (self::NUM_OPTION); $index > 0; $index--) {
462 $label = TRUE;
463 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
464 $label = FALSE;
465 if (!$label) {
466 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
467 }
468 }
469 if (!empty($values['discounted_label'][$index])) {
470 $duplicateIndex = CRM_Utils_Array::key($values['discounted_label'][$index], $values['discounted_label']);
471
472 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
473 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
474 }
475 }
476 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
477 $countemptyrows++;
478 }
479 if (empty($values['discounted_value'][$index][$i])) {
480 $countemptyvalue++;
481 }
482 }
a7488080 483 if (!empty($values['_qf_Fee_next']) && ($countemptyrows == 11 || $countemptyvalue == 11)) {
6a488035
TO
484 $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.');
485 }
486 }
487 }
488 }
03e04002 489 if ($values['is_monetary']) {
6a488035
TO
490 //check if financial type is selected
491 if (!$values['financial_type_id']) {
492 $errors['financial_type_id'] = ts("Please select financial type.");
493 }
494
495 //check for the event fee label (mandatory)
496 if (!$values['fee_label']) {
497 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
498 }
499
a7488080 500 if (empty($values['price_set_id'])) {
6a488035
TO
501 //check fee label and amount
502 $check = 0;
503 $optionKeys = array();
504 foreach ($values['label'] as $key => $val) {
505 if (trim($val) && trim($values['value'][$key])) {
506 $optionKeys[$key] = $key;
507 $check++;
508 }
509 }
510
511 $default = CRM_Utils_Array::value('default', $values);
512 if ($default && !in_array($default, $optionKeys)) {
513 $errors['default'] = ts('Please select an appropriate option as default.');
514 }
515
516 if (!$check) {
517 if (!$values['label'][1]) {
518 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
519 }
520 if (!$values['value'][1]) {
521 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
522 }
523 }
524 }
525 if (isset($values['is_pay_later'])) {
526 if (empty($values['pay_later_text'])) {
527 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
528 }
529 if (empty($values['pay_later_receipt'])) {
530 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
531 }
532 }
533 }
534 return empty($errors) ? TRUE : $errors;
535 }
536
537 public function buildAmountLabel() {
538 $default = array();
539 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
540 // label
541 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
542 // value
543 for ($j = 1; $j <= self::NUM_DISCOUNT; $j++) {
544 $this->add('text', "discounted_value[$i][$j]", ts('Value'), array('size' => 10));
545 $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');
546 }
547
548 // default
549 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
550 }
551
552 $this->addGroup($default, 'discounted_default');
553 }
554
555 /**
556 * Process the form
557 *
558 * @return void
559 * @access public
560 */
561 public function postProcess() {
6a488035
TO
562 $eventTitle = '';
563 $params = $this->exportValues();
564
565 $this->set('discountSection', 0);
566
a7488080 567 if (!empty($_POST['_qf_Fee_submit'])) {
6a488035
TO
568 $this->buildAmountLabel();
569 $this->set('discountSection', 2);
570 return;
571 }
572
573 if (array_key_exists('payment_processor', $params) &&
574 !CRM_Utils_System::isNull($params['payment_processor'])
575 ) {
576 $params['payment_processor'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['payment_processor']));
577 }
578 else {
579 $params['payment_processor'] = 'null';
580 }
581
582 $params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, 0);
583
584 if ($this->_id) {
585
586 // delete all the prior label values or discounts in the custom options table
587 // and delete a price set if one exists
9da8dc8c 588 if (CRM_Price_BAO_PriceSet::removeFrom('civicrm_event', $this->_id)) {
6a488035
TO
589 CRM_Core_BAO_Discount::del($this->_id,'civicrm_event');
590 }
591 }
592
593 if ($params['is_monetary']) {
a7488080 594 if (!empty($params['price_set_id'])) {
9da8dc8c 595 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_id, $params['price_set_id']);
a7488080 596 if (!empty($params['price_field_id'])) {
9da8dc8c 597 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
598 CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetID,0);
6a488035
TO
599 }
600 }
601 else {
602 // if there are label / values, create custom options for them
603 $labels = CRM_Utils_Array::value('label', $params);
604 $values = CRM_Utils_Array::value('value', $params);
605 $default = CRM_Utils_Array::value('default', $params);
606 $options = array();
607 if (!CRM_Utils_System::isNull($labels) && !CRM_Utils_System::isNull($values)) {
608 for ($i = 1; $i < self::NUM_OPTION; $i++) {
609 if (!empty($labels[$i]) && !CRM_Utils_System::isNull($values[$i])) {
610 $options[] = array('label' => trim($labels[$i]),
611 'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i])),
612 'weight' => $i,
613 'is_active' => 1,
614 'is_default' => $default == $i
615 );
616 }
617 }
618 if (!empty($options)) {
619 $params['default_fee_id'] = NULL;
a7488080
CW
620 if (empty($params['price_set_id'])) {
621 if (empty($params['price_field_id'])) {
6a488035
TO
622 $setParams['title'] = $eventTitle = ($this->_isTemplate) ? $this->_defaultValues['template_title'] : $this->_defaultValues['title'];
623 $eventTitle = strtolower(CRM_Utils_String::munge($eventTitle, '_', 245));
9da8dc8c 624 if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle, 'id', 'name')) {
6a488035
TO
625 $setParams['name'] = $eventTitle;
626 }
9da8dc8c 627 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $this->_id, 'id', 'name')) {
6a488035
TO
628 $setParams['name'] = $eventTitle . '_' . $this->_id;
629 }
630 else {
631 $timeSec = explode('.', microtime(true));
632 $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
633 }
634 $setParams['is_quick_config'] = 1;
cc9b58f3 635 $setParams['financial_type_id'] = $params['financial_type_id'];
6a488035 636 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent');
9da8dc8c 637 $priceSet = CRM_Price_BAO_PriceSet::create($setParams);
6a488035 638
5778c444 639 $fieldParams['name'] = strtolower(CRM_Utils_String::munge($params['fee_label'], '_', 245));
6a488035
TO
640 $fieldParams['price_set_id'] = $priceSet->id;
641 }
642 else {
643 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
644 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
9da8dc8c 645 CRM_Price_BAO_PriceFieldValue::setIsActive($fieldValueID, '0');
6a488035
TO
646 unset($params['price_field_value'][$arrayID]);
647 }
648 }
649 $fieldParams['id'] = CRM_Utils_Array::value('price_field_id', $params);
650 $fieldParams['option_id'] = $params['price_field_value'];
cdeb4bdf 651
9da8dc8c 652 $priceSet = new CRM_Price_BAO_PriceSet();
653 $priceSet->id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array::value('price_field_id', $params), 'price_set_id');
cdeb4bdf 654
cc9b58f3 655 if ($this->_defaultValues['financial_type_id'] != $params['financial_type_id']) {
cdeb4bdf 656 CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceSet', $priceSet->id, 'financial_type_id', $params['financial_type_id']);
cc9b58f3 657 }
6a488035 658 }
5778c444 659 $fieldParams['label'] = $params['fee_label'];
6a488035 660 $fieldParams['html_type'] = 'Radio';
9da8dc8c 661 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_id, $priceSet->id);
6a488035
TO
662 $fieldParams['option_label'] = $params['label'];
663 $fieldParams['option_amount'] = $params['value'];
664 $fieldParams['financial_type_id'] = $params['financial_type_id'];
665 foreach ($options as $value) $fieldParams['option_weight'][$value['weight']] = $value['weight'];
666 $fieldParams['default_option'] = $params['default'];
9da8dc8c 667 $priceField = CRM_Price_BAO_PriceField::create($fieldParams);
6a488035
TO
668 }
669 }
670 }
671
0d8afee2
CW
672 $discountPriceSets = !empty($this->_defaultValues['discount_price_set']) ? $this->_defaultValues['discount_price_set']: array();
673 $discountFieldIDs = !empty($this->_defaultValues['discount_option_id']) ? $this->_defaultValues['discount_option_id']: array();
6a488035
TO
674 if (CRM_Utils_Array::value('is_discount', $params) == 1) {
675 // if there are discounted set of label / values,
676 // create custom options for them
677 $labels = CRM_Utils_Array::value('discounted_label', $params);
678 $values = CRM_Utils_Array::value('discounted_value', $params);
679 $default = CRM_Utils_Array::value('discounted_default', $params);
680
681 if (!CRM_Utils_System::isNull($labels) && !CRM_Utils_System::isNull($values)) {
682 for ($j = 1; $j <= self::NUM_DISCOUNT; $j++) {
683 $discountOptions = array();
684 for ($i = 1; $i < self::NUM_OPTION; $i++) {
685 if (!empty($labels[$i]) &&
d6399f06 686 !CRM_Utils_System::isNull(CRM_Utils_Array::value($j, $values[$i]))
687 ) {
688 $discountOptions[] = array(
689 'label' => trim($labels[$i]),
6a488035
TO
690 'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i][$j])),
691 'weight' => $i,
692 'is_active' => 1,
693 'is_default' => $default == $i
694 );
695 }
696 }
697
698 if (!empty($discountOptions)) {
699 $fieldParams = array();
700 $params['default_discount_fee_id'] = NULL;
701 $keyCheck = $j-1;
d8fe9408 702 $setParams = array();
a7488080 703 if (empty($discountPriceSets[$keyCheck])) {
6a488035
TO
704 if (!$eventTitle) {
705 $eventTitle = strtolower(CRM_Utils_String::munge($this->_defaultValues['title'], '_', 200));
706 }
707 $setParams['title'] = $params['discount_name'][$j];
9da8dc8c 708 if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) {
6a488035
TO
709 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j];
710 }
9da8dc8c 711 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id, 'id', 'name')) {
6a488035
TO
712 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id;
713 }
714 else {
715 $timeSec = explode('.', microtime(true));
716 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
717 }
718 $setParams['is_quick_config'] = 1;
cc9b58f3 719 $setParams['financial_type_id'] = $params['financial_type_id'];
6a488035 720 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent');
9da8dc8c 721 $priceSet = CRM_Price_BAO_PriceSet::create($setParams);
6a488035 722 $priceSetID = $priceSet->id;
d8fe9408
PN
723 }
724 else {
6a488035 725 $priceSetID = $discountPriceSets[$j-1];
d8fe9408
PN
726 $setParams = array (
727 'title' => $params['discount_name'][$j],
728 'id' => $priceSetID,
729 );
cc9b58f3 730 if ($this->_defaultValues['financial_type_id'] != $params['financial_type_id']) {
d8fe9408 731 $setParams['financial_type_id'] = $params['financial_type_id'];
cc9b58f3 732 }
d8fe9408 733 CRM_Price_BAO_PriceSet::create($setParams);
6a488035 734 unset($discountPriceSets[$j-1]);
9da8dc8c 735 $fieldParams['id'] = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $priceSetID, 'id', 'price_set_id');
6a488035
TO
736 }
737
738 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
739 $fieldParams['is_required'] = 1;
740 $fieldParams['price_set_id'] = $priceSetID;
741 $fieldParams['html_type'] = 'Radio';
742 $fieldParams['financial_type_id'] = $params['financial_type_id'];
743 foreach ($discountOptions as $value) {
744 $fieldParams['option_label'][$value['weight']] = $value['label'];
745 $fieldParams['option_amount'][$value['weight']] = $value['value'];
746 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
a7488080 747 if (!empty($value['is_default'])) {
6a488035
TO
748 $fieldParams['default_option'] = $value['weight'];
749 }
8cc574cf 750 if (!empty($discountFieldIDs[$j]) && !empty($discountFieldIDs[$j][$value['weight']])) {
d8fe9408
PN
751 $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']];
752 unset($discountFieldIDs[$j][$value['weight']]);
6a488035
TO
753 }
754 }
755 //create discount priceset
9da8dc8c 756 $priceField = CRM_Price_BAO_PriceField::create($fieldParams);
d8fe9408
PN
757 if (!empty($discountFieldIDs[$j])) {
758 foreach($discountFieldIDs[$j] as $fID){
9da8dc8c 759 CRM_Price_BAO_PriceFieldValue::setIsActive($fID, '0');
6a488035
TO
760 }
761 }
762
763 $discountParams = array(
764 'entity_table' => 'civicrm_event',
765 'entity_id' => $this->_id,
766 'price_set_id' => $priceSetID,
767 'start_date' => CRM_Utils_Date::processDate($params['discount_start_date'][$j]),
768 'end_date' => CRM_Utils_Date::processDate($params['discount_end_date'][$j]),
769 );
770 CRM_Core_BAO_Discount::add($discountParams);
771 }
772 }
773 }
774 }
775 if (!empty($discountPriceSets)) {
776 foreach ($discountPriceSets as $setId) {
9da8dc8c 777 CRM_Price_BAO_PriceSet::setIsQuickConfig($setId, 0);
6a488035
TO
778 }
779 }
780 }
a06f8589
PJ
781 }
782 else {
a7488080 783 if (!empty($params['price_field_id'])) {
9da8dc8c 784 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
785 CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetID,0);
6a488035
TO
786 }
787 $params['financial_type_id'] = '';
a06f8589 788 $params['is_pay_later'] = 0;
6a488035
TO
789 }
790
791 //update events table
792 $params['id'] = $this->_id;
cc9b58f3
PN
793 // skip update of financial type in price set
794 $params['skipFinancialType'] = TRUE;
795
6a488035
TO
796 CRM_Event_BAO_Event::add($params);
797
5d92a7e7
CW
798 // Update tab "disabled" css class
799 $this->ajaxResponse['tabValid'] = !empty($params['is_monetary']);
6a488035
TO
800 parent::endPostProcess();
801 }
802
803 /**
804 * Return a descriptive name for the page, used in wizard header
805 *
806 * @return string
807 * @access public
808 */
809 public function getTitle() {
810 return ts('Event Fees');
811 }
812}
813