Merge pull request #31 from eileenmcnaughton/CRM-12008
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Fee.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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)) {
93 $price_set_id = CRM_Price_BAO_Set::getFor('civicrm_event', $eventId, NULL, 1);
94
95 if ($price_set_id) {
96 $defaults['price_set_id'] = $price_set_id;
97 }
98 else {
99 $priceSetId = CRM_Price_BAO_Set::getFor('civicrm_event', $eventId, NULL);
100 if ($priceSetId) {
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');
104 $options = array();
105 $priceFieldOptions = CRM_Price_BAO_FieldValue::getValues($priceField, $options, 'weight', true);
106 $defaults['price_field_id'] = $priceField;
107 $countRow = 0;
108 foreach ($options as $optionId => $optionValue) {
109 $countRow++;
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;
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;
131 $name = $defaults["discount_name[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $optionGroupId, 'title');
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 ));
139 $defaultDiscounts[] = CRM_Price_BAO_Set::getSetDetail($optionGroupId);
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) {
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'];
156 }
157 }
158 $rowCount++;
159 }
160
161 $this->set('discountSection', 1);
162 $this->buildQuickForm();
163 }
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'];
167 }
168 elseif (CRM_Utils_Array::value('label', $this->_submitValues)) {
169 //if event is newly created, use submitted values for
170 //discount labels
171 if (is_array($this->_submitValues['label'])) {
172 $k = 1;
173 foreach ($this->_submitValues['label'] as $value) {
174 if ($value) {
175 $defaults['discounted_label'][$k] = $value;
176 $k++;
177 }
178 }
179 }
180 }
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])
186 ) {
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;
190 break;
191 }
192 }
193 }
194 }
195
196 if (!isset($defaults['discounted_default'])) {
197 $defaults['discounted_default'] = 1;
198 }
199
200 if (!isset($defaults['is_monetary'])) {
201 $defaults['is_monetary'] = 1;
202 }
203
204 if (!isset($defaults['fee_label'])) {
205 $defaults['fee_label'] = ts('Event Fee(s)');
206 }
207
208 if (!isset($defaults['pay_later_text']) ||
209 empty($defaults['pay_later_text'])
210 ) {
211 $defaults['pay_later_text'] = ts('I will send payment by check');
212 }
213
214 $this->_showHide = new CRM_Core_ShowHideBlocks();
215 if (!$defaults['is_monetary']) {
216 $this->_showHide->addHide('event-fees');
217 }
218
219 if (isset($defaults['price_set_id'])) {
220 $this->_showHide->addHide('map-field');
221 }
222 $this->_showHide->addToTemplate();
223 $this->assign('inDate', $this->_inDate);
224
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']
228 ), '1');
229 }
230 return $defaults;
231 }
232
233 /**
234 * Function to build the form
235 *
236 * @return None
237 * @access public
238 */
239 public function buildQuickForm() {
240
241 $this->addYesNo('is_monetary',
242 ts('Paid Event'),
243 NULL,
244 NULL,
245 array('onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);")
246 );
247
248 //add currency element.
249 $this->addCurrency('currency', ts('Currency'), FALSE);
250
251 $paymentProcessor = CRM_Core_PseudoConstant::paymentProcessor();
252
253 $this->assign('paymentProcessor', $paymentProcessor);
254
255 $this->addCheckBox('payment_processor', ts('Payment Processor'),
256 array_flip($paymentProcessor),
257 NULL, NULL, NULL, NULL,
258 array('&nbsp;&nbsp;', '&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>')
259 );
260
261 // financial type
262 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
263 $this->add('select', 'financial_type_id', ts('Financial Type'),
264 array('' => ts('- select -')) + $financialType
265 );
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);")
269 );
270 $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'),
271 CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'),
272 FALSE
273 );
274 $this->addElement('textarea', 'pay_later_receipt', ts('Pay Later Instructions'),
275 CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'),
276 FALSE
277 );
278
279 $this->add('text', 'fee_label', ts('Fee Label'));
280
281 $price = CRM_Price_BAO_Set::getAssoc(FALSE, 'CiviEvent');
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'),
364 CRM_Core_DAO::getAttribute('CRM_Price_DAO_Set', 'title')
365 );
366
367 $this->add('hidden', "discount_price_set[$i]", '', array('id' => "discount_price_set[$i]"));
368 // add a rule to ensure that discount name is not more than 24 characters to prevent overflow
369 // in option group name, CRM-7915
370 // 24 characters will make the option group name less than 64 characters
371 $this->addRule("discount_name[$i]",
372 ts('Discount Name cannot be more than 24 characters'),
373 'maxlength',
374 24
375 );
376
377 //discount start date
378 $this->addDate('discount_start_date[' . $i . ']', ts('Discount Start Date'), FALSE, array('formatType' => 'activityDate'));
379
380 //discount end date
381 $this->addDate('discount_end_date[' . $i . ']', ts('Discount End Date'), FALSE, array('formatType' => 'activityDate'));
382 }
383 $_showHide->addToTemplate();
384 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
385 array('class' => 'form-submit')
386 );
387
388 $this->buildAmountLabel();
389 parent::buildQuickForm();
390 }
391
392 /**
393 * Add local and global form rules
394 *
395 * @access protected
396 *
397 * @return void
398 */
399 function addRules() {
400 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Fee', 'formRule'));
401 }
402
403 /**
404 * global validation rules for the form
405 *
406 * @param array $values posted values of the form
407 *
408 * @return array list of errors to be posted back to the form
409 * @static
410 * @access public
411 */
412 static function formRule($values) {
413 $errors = array();
414 if (CRM_Utils_Array::value('is_discount', $values)) {
415 $occurDiscount = array_count_values($values['discount_name']);
416 $countemptyrows = 0;
417 $countemptyvalue = 0;
418 for ($i = 1; $i <= self::NUM_DISCOUNT; $i++) {
419 $start_date = $end_date = NULL;
420 if (CRM_Utils_Array::value($i, $values['discount_name'])) {
421 if (CRM_Utils_Array::value($i, $values['discount_start_date'])) {
422 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date::processDate($values['discount_start_date'][$i]) : 0;
423 }
424 if (CRM_Utils_Array::value($i, $values['discount_end_date'])) {
425 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date::processDate($values['discount_end_date'][$i]) : 0;
426 }
427
428 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
429 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
430 }
431
432 if (!$start_date && !$end_date) {
433 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
434 }
435
436 if ($i > 1) {
437 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date::processDate($values['discount_end_date'][$i - 1]) : 0;
438 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
439 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
440 }
441 elseif (!$start_date && !$end_date_1) {
442 $j = $i - 1;
443 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
444 }
445 }
446
447 foreach ($occurDiscount as $key => $value) if ($value > 1 && $key <> '') {
448 if ($key == $values['discount_name'][$i]) {
449 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', array(1 => $key));
450 }
451 }
452
453 //validation for discount labels and values
454 for ($index = (self::NUM_OPTION); $index > 0; $index--) {
455 $label = TRUE;
456 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
457 $label = FALSE;
458 if (!$label) {
459 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
460 }
461 }
462 if (!empty($values['discounted_label'][$index])) {
463 $duplicateIndex = CRM_Utils_Array::key($values['discounted_label'][$index], $values['discounted_label']);
464
465 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
466 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
467 }
468 }
469 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
470 $countemptyrows++;
471 }
472 if (empty($values['discounted_value'][$index][$i])) {
473 $countemptyvalue++;
474 }
475 }
476 if (CRM_Utils_Array::value('_qf_Fee_next', $values) && ($countemptyrows == 11 || $countemptyvalue == 11)) {
477 $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.');
478 }
479 }
480 }
481 }
482 if ($values['is_monetary']) {
483 //check if financial type is selected
484 if (!$values['financial_type_id']) {
485 $errors['financial_type_id'] = ts("Please select financial type.");
486 }
487
488 //check for the event fee label (mandatory)
489 if (!$values['fee_label']) {
490 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
491 }
492
493 if (!CRM_Utils_Array::value('price_set_id', $values)) {
494 //check fee label and amount
495 $check = 0;
496 $optionKeys = array();
497 foreach ($values['label'] as $key => $val) {
498 if (trim($val) && trim($values['value'][$key])) {
499 $optionKeys[$key] = $key;
500 $check++;
501 }
502 }
503
504 $default = CRM_Utils_Array::value('default', $values);
505 if ($default && !in_array($default, $optionKeys)) {
506 $errors['default'] = ts('Please select an appropriate option as default.');
507 }
508
509 if (!$check) {
510 if (!$values['label'][1]) {
511 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
512 }
513 if (!$values['value'][1]) {
514 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
515 }
516 }
517 }
518 if (isset($values['is_pay_later'])) {
519 if (empty($values['pay_later_text'])) {
520 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
521 }
522 if (empty($values['pay_later_receipt'])) {
523 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
524 }
525 }
526 }
527 return empty($errors) ? TRUE : $errors;
528 }
529
530 public function buildAmountLabel() {
531 $default = array();
532 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
533 // label
534 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
535 // value
536 for ($j = 1; $j <= self::NUM_DISCOUNT; $j++) {
537 $this->add('text', "discounted_value[$i][$j]", ts('Value'), array('size' => 10));
538 $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 }
540
541 // default
542 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
543 }
544
545 $this->addGroup($default, 'discounted_default');
546 }
547
548 /**
549 * Process the form
550 *
551 * @return void
552 * @access public
553 */
554 public function postProcess() {
555 $params = array();
556 $eventTitle = '';
557 $params = $this->exportValues();
558
559 $this->set('discountSection', 0);
560
561 if (CRM_Utils_Array::value('_qf_Fee_submit', $_POST)) {
562 $this->buildAmountLabel();
563 $this->set('discountSection', 2);
564 return;
565 }
566
567 if (array_key_exists('payment_processor', $params) &&
568 !CRM_Utils_System::isNull($params['payment_processor'])
569 ) {
570 $params['payment_processor'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['payment_processor']));
571 }
572 else {
573 $params['payment_processor'] = 'null';
574 }
575
576 $params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, 0);
577
578 if ($this->_id) {
579
580 // delete all the prior label values or discounts in the custom options table
581 // and delete a price set if one exists
582 if (CRM_Price_BAO_Set::removeFrom('civicrm_event', $this->_id)) {
583 CRM_Core_BAO_Discount::del($this->_id,'civicrm_event');
584 }
585 }
586
587 if ($params['is_monetary']) {
588 if (CRM_Utils_Array::value('price_set_id', $params)) {
589 CRM_Price_BAO_Set::addTo('civicrm_event', $this->_id, $params['price_set_id']);
590 if (CRM_Utils_Array::value('price_field_id', $params)) {
591 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $params['price_field_id'], 'price_set_id');
592 CRM_Price_BAO_Set::setIsQuickConfig($priceSetID,0);
593 }
594 }
595 else {
596 // if there are label / values, create custom options for them
597 $labels = CRM_Utils_Array::value('label', $params);
598 $values = CRM_Utils_Array::value('value', $params);
599 $default = CRM_Utils_Array::value('default', $params);
600 $options = array();
601 if (!CRM_Utils_System::isNull($labels) && !CRM_Utils_System::isNull($values)) {
602 for ($i = 1; $i < self::NUM_OPTION; $i++) {
603 if (!empty($labels[$i]) && !CRM_Utils_System::isNull($values[$i])) {
604 $options[] = array('label' => trim($labels[$i]),
605 'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i])),
606 'weight' => $i,
607 'is_active' => 1,
608 'is_default' => $default == $i
609 );
610 }
611 }
612 if (!empty($options)) {
613 $params['default_fee_id'] = NULL;
614 if (!CRM_Utils_Array::value('price_set_id', $params)) {
615 if (!CRM_Utils_Array::value('price_field_id', $params)) {
616 $setParams['title'] = $eventTitle = ($this->_isTemplate) ? $this->_defaultValues['template_title'] : $this->_defaultValues['title'];
617 $eventTitle = strtolower(CRM_Utils_String::munge($eventTitle, '_', 245));
618 if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Set', $eventTitle, 'id', 'name')) {
619 $setParams['name'] = $eventTitle;
620 }
621 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Set', $eventTitle . '_' . $this->_id, 'id', 'name')) {
622 $setParams['name'] = $eventTitle . '_' . $this->_id;
623 }
624 else {
625 $timeSec = explode('.', microtime(true));
626 $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
627 }
628 $setParams['is_quick_config'] = 1;
629 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent');
630 $priceSet = CRM_Price_BAO_Set::create($setParams);
631
632 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
633 $fieldParams['price_set_id'] = $priceSet->id;
634 }
635 else {
636 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
637 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
638 CRM_Price_BAO_FieldValue::setIsActive($fieldValueID, '0');
639 unset($params['price_field_value'][$arrayID]);
640 }
641 }
642 $fieldParams['id'] = CRM_Utils_Array::value('price_field_id', $params);
643 $fieldParams['option_id'] = $params['price_field_value'];
644 $priceSet = new CRM_Price_BAO_Set();
645 $priceSet->id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', CRM_Utils_Array::value('price_field_id', $params), 'price_set_id');
646 }
647 $fieldParams['html_type'] = 'Radio';
648 CRM_Price_BAO_Set::addTo('civicrm_event', $this->_id, $priceSet->id);
649 $fieldParams['option_label'] = $params['label'];
650 $fieldParams['option_amount'] = $params['value'];
651 $fieldParams['financial_type_id'] = $params['financial_type_id'];
652 foreach ($options as $value) $fieldParams['option_weight'][$value['weight']] = $value['weight'];
653 $fieldParams['default_option'] = $params['default'];
654 $priceField = CRM_Price_BAO_Field::create($fieldParams);
655 }
656 }
657 }
658
659 $discountPriceSets = CRM_Utils_Array::value('discount_price_set', $this->_defaultValues) ? $this->_defaultValues['discount_price_set']: array();
660 $discountFieldIDs = CRM_Utils_Array::value('discount_option_id', $this->_defaultValues) ? $this->_defaultValues['discount_option_id']: array();
661 if (CRM_Utils_Array::value('is_discount', $params) == 1) {
662 // if there are discounted set of label / values,
663 // create custom options for them
664 $labels = CRM_Utils_Array::value('discounted_label', $params);
665 $values = CRM_Utils_Array::value('discounted_value', $params);
666 $default = CRM_Utils_Array::value('discounted_default', $params);
667
668 if (!CRM_Utils_System::isNull($labels) && !CRM_Utils_System::isNull($values)) {
669 for ($j = 1; $j <= self::NUM_DISCOUNT; $j++) {
670 $discountOptions = array();
671 for ($i = 1; $i < self::NUM_OPTION; $i++) {
672 if (!empty($labels[$i]) &&
673 !empty($values[$i][$j])
674 ) {
675 $discountOptions[] = array('label' => trim($labels[$i]),
676 'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i][$j])),
677 'weight' => $i,
678 'is_active' => 1,
679 'is_default' => $default == $i
680 );
681 }
682 }
683
684 if (!empty($discountOptions)) {
685 $fieldParams = array();
686 $params['default_discount_fee_id'] = NULL;
687 $keyCheck = $j-1;
688 if (!CRM_Utils_Array::value($keyCheck, $discountPriceSets)) {
689 if (!$eventTitle) {
690 $eventTitle = strtolower(CRM_Utils_String::munge($this->_defaultValues['title'], '_', 200));
691 }
692 $setParams['title'] = $params['discount_name'][$j];
693 if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Set', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) {
694 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j];
695 }
696 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Set', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id, 'id', 'name')) {
697 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id;
698 }
699 else {
700 $timeSec = explode('.', microtime(true));
701 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
702 }
703 $setParams['is_quick_config'] = 1;
704 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent');
705 $priceSet = CRM_Price_BAO_Set::create($setParams);
706 $priceSetID = $priceSet->id;
707 } else {
708 $priceSetID = $discountPriceSets[$j-1];
709 unset($discountPriceSets[$j-1]);
710 $fieldParams['id'] = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Field', $priceSetID, 'id', 'price_set_id');
711 }
712
713 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
714 $fieldParams['is_required'] = 1;
715 $fieldParams['price_set_id'] = $priceSetID;
716 $fieldParams['html_type'] = 'Radio';
717 $fieldParams['financial_type_id'] = $params['financial_type_id'];
718 foreach ($discountOptions as $value) {
719 $fieldParams['option_label'][$value['weight']] = $value['label'];
720 $fieldParams['option_amount'][$value['weight']] = $value['value'];
721 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
722 if (CRM_Utils_Array::value('is_default', $value)) {
723 $fieldParams['default_option'] = $value['weight'];
724 }
725 if (CRM_Utils_Array::value($j, $discountFieldIDs) && CRM_Utils_Array::value($value['weight']-1, $discountFieldIDs[$j])) {
726 $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']-1];
727 unset($discountFieldIDs[$j][$value['weight']-1]);
728 }
729 }
730 //create discount priceset
731 $priceField = CRM_Price_BAO_Field::create($fieldParams);
732 if (!empty($discountFieldIDs)) {
733 foreach($discountFieldIDs as $fID){
734 CRM_Price_BAO_FieldValue::setIsActive($fID, '0');
735 }
736 }
737
738 $discountParams = array(
739 'entity_table' => 'civicrm_event',
740 'entity_id' => $this->_id,
741 'price_set_id' => $priceSetID,
742 'start_date' => CRM_Utils_Date::processDate($params['discount_start_date'][$j]),
743 'end_date' => CRM_Utils_Date::processDate($params['discount_end_date'][$j]),
744 );
745 CRM_Core_BAO_Discount::add($discountParams);
746 }
747 }
748 }
749 }
750 if (!empty($discountPriceSets)) {
751 foreach ($discountPriceSets as $setId) {
752 CRM_Price_BAO_Set::setIsQuickConfig($setId, 0);
753 }
754 }
755 }
756 } else {
757 if (CRM_Utils_Array::value('price_field_id', $params)) {
758 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $params['price_field_id'], 'price_set_id');
759 CRM_Price_BAO_Set::setIsQuickConfig($priceSetID,0);
760 }
761 $params['financial_type_id'] = '';
762 }
763
764 //update events table
765 $params['id'] = $this->_id;
766 CRM_Event_BAO_Event::add($params);
767
768 parent::endPostProcess();
769 }
770
771 /**
772 * Return a descriptive name for the page, used in wizard header
773 *
774 * @return string
775 * @access public
776 */
777 public function getTitle() {
778 return ts('Event Fees');
779 }
780}
781