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