js & css fix for CRM-12859
[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 );
03e04002 260
261 // financial type
6a488035
TO
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 );
d11a05d6 274 $this->addWysiwyg('pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'));
6a488035
TO
275
276 $this->add('text', 'fee_label', ts('Fee Label'));
277
278 $price = CRM_Price_BAO_Set::getAssoc(FALSE, 'CiviEvent');
279 if (CRM_Utils_System::isNull($price)) {
280 $this->assign('price', FALSE);
281 }
282 else {
283 $this->assign('price', TRUE);
284 }
285 $this->add('select', 'price_set_id', ts('Price Set'),
286 array(
287 '' => ts('- none -')) + $price,
288 NULL, array('onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);")
289 );
290 $default = array();
291 $this->add('hidden', 'price_field_id', '', array('id' => 'price_field_id'));
292 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
293 // label
294 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
295 $this->add('hidden', "price_field_value[$i]", '', array('id' => "price_field_value[$i]"));
296
297 // value
298 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
299 $this->addRule("value[$i]", ts('Please enter a valid money value for this field (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
300
301 // default
302 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
303 }
304
305 $this->addGroup($default, 'default');
306
307 $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL,
308 array('onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);")
309 );
310 $discountSection = $this->get('discountSection');
311
312 $this->assign('discountSection', $discountSection);
313
314 // form fields of Discount sets
315 $defaultOption = array();
316 $_showHide = new CRM_Core_ShowHideBlocks('', '');
317
318 for ($i = 1; $i <= self::NUM_DISCOUNT; $i++) {
319 //the show hide blocks
320 $showBlocks = 'discount_' . $i;
321 if ($i > 2) {
322 $_showHide->addHide($showBlocks);
323 }
324 else {
325 $_showHide->addShow($showBlocks);
326 }
327
328 //Increment by 1 of start date of previous end date.
329 if (is_array($this->_submitValues) &&
330 !empty($this->_submitValues['discount_name'][$i]) &&
331 !empty($this->_submitValues['discount_name'][$i + 1]) &&
332 isset($this->_submitValues['discount_end_date']) &&
333 isset($this->_submitValues['discount_end_date'][$i]) &&
334 $i < self::NUM_DISCOUNT - 1
335 ) {
336 $end_date = CRM_Utils_Date::processDate($this->_submitValues['discount_end_date'][$i]);
337 if (!empty($this->_submitValues['discount_end_date'][$i + 1])
338 && empty($this->_submitValues['discount_start_date'][$i + 1])
339 ) {
340 list($this->_submitValues['discount_start_date'][$i + 1]) = CRM_Utils_Date::setDateDefaults(date('Y-m-d', strtotime("+1 days $end_date")));
341 }
342 }
343 //Decrement by 1 of end date from next start date.
344 if ($i > 1 &&
345 is_array($this->_submitValues) &&
346 !empty($this->_submitValues['discount_name'][$i]) &&
347 !empty($this->_submitValues['discount_name'][$i - 1]) &&
348 isset($this->_submitValues['discount_start_date']) &&
349 isset($this->_submitValues['discount_start_date'][$i])
350 ) {
351 $start_date = CRM_Utils_Date::processDate($this->_submitValues['discount_start_date'][$i]);
352 if (!empty($this->_submitValues['discount_start_date'][$i])
353 && empty($this->_submitValues['discount_end_date'][$i - 1])
354 ) {
355 list($this->_submitValues['discount_end_date'][$i - 1]) = CRM_Utils_Date::setDateDefaults(date('Y-m-d', strtotime("-1 days $start_date")));
356 }
357 }
358
359 //discount name
360 $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
361 CRM_Core_DAO::getAttribute('CRM_Price_DAO_Set', 'title')
362 );
363
364 $this->add('hidden', "discount_price_set[$i]", '', array('id' => "discount_price_set[$i]"));
365 // add a rule to ensure that discount name is not more than 24 characters to prevent overflow
366 // in option group name, CRM-7915
367 // 24 characters will make the option group name less than 64 characters
368 $this->addRule("discount_name[$i]",
369 ts('Discount Name cannot be more than 24 characters'),
370 'maxlength',
371 24
372 );
373
374 //discount start date
375 $this->addDate('discount_start_date[' . $i . ']', ts('Discount Start Date'), FALSE, array('formatType' => 'activityDate'));
376
377 //discount end date
378 $this->addDate('discount_end_date[' . $i . ']', ts('Discount End Date'), FALSE, array('formatType' => 'activityDate'));
379 }
380 $_showHide->addToTemplate();
381 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
382 array('class' => 'form-submit')
383 );
384
385 $this->buildAmountLabel();
386 parent::buildQuickForm();
387 }
388
389 /**
390 * Add local and global form rules
391 *
392 * @access protected
393 *
394 * @return void
395 */
396 function addRules() {
397 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Fee', 'formRule'));
398 }
399
400 /**
401 * global validation rules for the form
402 *
403 * @param array $values posted values of the form
404 *
405 * @return array list of errors to be posted back to the form
406 * @static
407 * @access public
408 */
409 static function formRule($values) {
410 $errors = array();
411 if (CRM_Utils_Array::value('is_discount', $values)) {
412 $occurDiscount = array_count_values($values['discount_name']);
413 $countemptyrows = 0;
414 $countemptyvalue = 0;
415 for ($i = 1; $i <= self::NUM_DISCOUNT; $i++) {
416 $start_date = $end_date = NULL;
417 if (CRM_Utils_Array::value($i, $values['discount_name'])) {
418 if (CRM_Utils_Array::value($i, $values['discount_start_date'])) {
419 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date::processDate($values['discount_start_date'][$i]) : 0;
420 }
421 if (CRM_Utils_Array::value($i, $values['discount_end_date'])) {
422 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date::processDate($values['discount_end_date'][$i]) : 0;
423 }
424
425 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
426 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
427 }
428
429 if (!$start_date && !$end_date) {
430 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
431 }
432
433 if ($i > 1) {
434 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date::processDate($values['discount_end_date'][$i - 1]) : 0;
435 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
436 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
437 }
438 elseif (!$start_date && !$end_date_1) {
439 $j = $i - 1;
440 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
441 }
442 }
443
444 foreach ($occurDiscount as $key => $value) if ($value > 1 && $key <> '') {
445 if ($key == $values['discount_name'][$i]) {
446 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', array(1 => $key));
447 }
448 }
449
450 //validation for discount labels and values
451 for ($index = (self::NUM_OPTION); $index > 0; $index--) {
452 $label = TRUE;
453 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
454 $label = FALSE;
455 if (!$label) {
456 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
457 }
458 }
459 if (!empty($values['discounted_label'][$index])) {
460 $duplicateIndex = CRM_Utils_Array::key($values['discounted_label'][$index], $values['discounted_label']);
461
462 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
463 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
464 }
465 }
466 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
467 $countemptyrows++;
468 }
469 if (empty($values['discounted_value'][$index][$i])) {
470 $countemptyvalue++;
471 }
472 }
473 if (CRM_Utils_Array::value('_qf_Fee_next', $values) && ($countemptyrows == 11 || $countemptyvalue == 11)) {
474 $errors["discounted_label[1]"] = $errors["discounted_value[1][$i]"] = ts('At least one fee should be entered for your Discount Set. If you do not see the table to enter discount fees, click the "Add Discount Set to Fee Table" button.');
475 }
476 }
477 }
478 }
03e04002 479 if ($values['is_monetary']) {
6a488035
TO
480 //check if financial type is selected
481 if (!$values['financial_type_id']) {
482 $errors['financial_type_id'] = ts("Please select financial type.");
483 }
484
485 //check for the event fee label (mandatory)
486 if (!$values['fee_label']) {
487 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
488 }
489
490 if (!CRM_Utils_Array::value('price_set_id', $values)) {
491 //check fee label and amount
492 $check = 0;
493 $optionKeys = array();
494 foreach ($values['label'] as $key => $val) {
495 if (trim($val) && trim($values['value'][$key])) {
496 $optionKeys[$key] = $key;
497 $check++;
498 }
499 }
500
501 $default = CRM_Utils_Array::value('default', $values);
502 if ($default && !in_array($default, $optionKeys)) {
503 $errors['default'] = ts('Please select an appropriate option as default.');
504 }
505
506 if (!$check) {
507 if (!$values['label'][1]) {
508 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
509 }
510 if (!$values['value'][1]) {
511 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
512 }
513 }
514 }
515 if (isset($values['is_pay_later'])) {
516 if (empty($values['pay_later_text'])) {
517 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
518 }
519 if (empty($values['pay_later_receipt'])) {
520 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
521 }
522 }
523 }
524 return empty($errors) ? TRUE : $errors;
525 }
526
527 public function buildAmountLabel() {
528 $default = array();
529 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
530 // label
531 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
532 // value
533 for ($j = 1; $j <= self::NUM_DISCOUNT; $j++) {
534 $this->add('text', "discounted_value[$i][$j]", ts('Value'), array('size' => 10));
535 $this->addRule("discounted_value[$i][$j]", ts('Please enter a valid money value for this field (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
536 }
537
538 // default
539 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
540 }
541
542 $this->addGroup($default, 'discounted_default');
543 }
544
545 /**
546 * Process the form
547 *
548 * @return void
549 * @access public
550 */
551 public function postProcess() {
552 $params = array();
553 $eventTitle = '';
554 $params = $this->exportValues();
555
556 $this->set('discountSection', 0);
557
558 if (CRM_Utils_Array::value('_qf_Fee_submit', $_POST)) {
559 $this->buildAmountLabel();
560 $this->set('discountSection', 2);
561 return;
562 }
563
564 if (array_key_exists('payment_processor', $params) &&
565 !CRM_Utils_System::isNull($params['payment_processor'])
566 ) {
567 $params['payment_processor'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['payment_processor']));
568 }
569 else {
570 $params['payment_processor'] = 'null';
571 }
572
573 $params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, 0);
574
575 if ($this->_id) {
576
577 // delete all the prior label values or discounts in the custom options table
578 // and delete a price set if one exists
579 if (CRM_Price_BAO_Set::removeFrom('civicrm_event', $this->_id)) {
580 CRM_Core_BAO_Discount::del($this->_id,'civicrm_event');
581 }
582 }
583
584 if ($params['is_monetary']) {
585 if (CRM_Utils_Array::value('price_set_id', $params)) {
586 CRM_Price_BAO_Set::addTo('civicrm_event', $this->_id, $params['price_set_id']);
587 if (CRM_Utils_Array::value('price_field_id', $params)) {
588 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $params['price_field_id'], 'price_set_id');
589 CRM_Price_BAO_Set::setIsQuickConfig($priceSetID,0);
590 }
591 }
592 else {
593 // if there are label / values, create custom options for them
594 $labels = CRM_Utils_Array::value('label', $params);
595 $values = CRM_Utils_Array::value('value', $params);
596 $default = CRM_Utils_Array::value('default', $params);
597 $options = array();
598 if (!CRM_Utils_System::isNull($labels) && !CRM_Utils_System::isNull($values)) {
599 for ($i = 1; $i < self::NUM_OPTION; $i++) {
600 if (!empty($labels[$i]) && !CRM_Utils_System::isNull($values[$i])) {
601 $options[] = array('label' => trim($labels[$i]),
602 'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i])),
603 'weight' => $i,
604 'is_active' => 1,
605 'is_default' => $default == $i
606 );
607 }
608 }
609 if (!empty($options)) {
610 $params['default_fee_id'] = NULL;
611 if (!CRM_Utils_Array::value('price_set_id', $params)) {
612 if (!CRM_Utils_Array::value('price_field_id', $params)) {
613 $setParams['title'] = $eventTitle = ($this->_isTemplate) ? $this->_defaultValues['template_title'] : $this->_defaultValues['title'];
614 $eventTitle = strtolower(CRM_Utils_String::munge($eventTitle, '_', 245));
615 if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Set', $eventTitle, 'id', 'name')) {
616 $setParams['name'] = $eventTitle;
617 }
618 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Set', $eventTitle . '_' . $this->_id, 'id', 'name')) {
619 $setParams['name'] = $eventTitle . '_' . $this->_id;
620 }
621 else {
622 $timeSec = explode('.', microtime(true));
623 $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
624 }
625 $setParams['is_quick_config'] = 1;
626 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent');
627 $priceSet = CRM_Price_BAO_Set::create($setParams);
628
629 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
630 $fieldParams['price_set_id'] = $priceSet->id;
631 }
632 else {
633 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
634 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
635 CRM_Price_BAO_FieldValue::setIsActive($fieldValueID, '0');
636 unset($params['price_field_value'][$arrayID]);
637 }
638 }
639 $fieldParams['id'] = CRM_Utils_Array::value('price_field_id', $params);
640 $fieldParams['option_id'] = $params['price_field_value'];
641 $priceSet = new CRM_Price_BAO_Set();
642 $priceSet->id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', CRM_Utils_Array::value('price_field_id', $params), 'price_set_id');
643 }
644 $fieldParams['html_type'] = 'Radio';
645 CRM_Price_BAO_Set::addTo('civicrm_event', $this->_id, $priceSet->id);
646 $fieldParams['option_label'] = $params['label'];
647 $fieldParams['option_amount'] = $params['value'];
648 $fieldParams['financial_type_id'] = $params['financial_type_id'];
649 foreach ($options as $value) $fieldParams['option_weight'][$value['weight']] = $value['weight'];
650 $fieldParams['default_option'] = $params['default'];
651 $priceField = CRM_Price_BAO_Field::create($fieldParams);
652 }
653 }
654 }
655
656 $discountPriceSets = CRM_Utils_Array::value('discount_price_set', $this->_defaultValues) ? $this->_defaultValues['discount_price_set']: array();
657 $discountFieldIDs = CRM_Utils_Array::value('discount_option_id', $this->_defaultValues) ? $this->_defaultValues['discount_option_id']: array();
658 if (CRM_Utils_Array::value('is_discount', $params) == 1) {
659 // if there are discounted set of label / values,
660 // create custom options for them
661 $labels = CRM_Utils_Array::value('discounted_label', $params);
662 $values = CRM_Utils_Array::value('discounted_value', $params);
663 $default = CRM_Utils_Array::value('discounted_default', $params);
664
665 if (!CRM_Utils_System::isNull($labels) && !CRM_Utils_System::isNull($values)) {
666 for ($j = 1; $j <= self::NUM_DISCOUNT; $j++) {
667 $discountOptions = array();
668 for ($i = 1; $i < self::NUM_OPTION; $i++) {
669 if (!empty($labels[$i]) &&
670 !empty($values[$i][$j])
671 ) {
672 $discountOptions[] = array('label' => trim($labels[$i]),
673 'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i][$j])),
674 'weight' => $i,
675 'is_active' => 1,
676 'is_default' => $default == $i
677 );
678 }
679 }
680
681 if (!empty($discountOptions)) {
682 $fieldParams = array();
683 $params['default_discount_fee_id'] = NULL;
684 $keyCheck = $j-1;
685 if (!CRM_Utils_Array::value($keyCheck, $discountPriceSets)) {
686 if (!$eventTitle) {
687 $eventTitle = strtolower(CRM_Utils_String::munge($this->_defaultValues['title'], '_', 200));
688 }
689 $setParams['title'] = $params['discount_name'][$j];
690 if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Set', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) {
691 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j];
692 }
693 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Set', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id, 'id', 'name')) {
694 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id;
695 }
696 else {
697 $timeSec = explode('.', microtime(true));
698 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
699 }
700 $setParams['is_quick_config'] = 1;
701 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent');
702 $priceSet = CRM_Price_BAO_Set::create($setParams);
703 $priceSetID = $priceSet->id;
704 } else {
705 $priceSetID = $discountPriceSets[$j-1];
706 unset($discountPriceSets[$j-1]);
707 $fieldParams['id'] = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Field', $priceSetID, 'id', 'price_set_id');
708 }
709
710 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
711 $fieldParams['is_required'] = 1;
712 $fieldParams['price_set_id'] = $priceSetID;
713 $fieldParams['html_type'] = 'Radio';
714 $fieldParams['financial_type_id'] = $params['financial_type_id'];
715 foreach ($discountOptions as $value) {
716 $fieldParams['option_label'][$value['weight']] = $value['label'];
717 $fieldParams['option_amount'][$value['weight']] = $value['value'];
718 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
719 if (CRM_Utils_Array::value('is_default', $value)) {
720 $fieldParams['default_option'] = $value['weight'];
721 }
722 if (CRM_Utils_Array::value($j, $discountFieldIDs) && CRM_Utils_Array::value($value['weight']-1, $discountFieldIDs[$j])) {
723 $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']-1];
724 unset($discountFieldIDs[$j][$value['weight']-1]);
725 }
726 }
727 //create discount priceset
728 $priceField = CRM_Price_BAO_Field::create($fieldParams);
729 if (!empty($discountFieldIDs)) {
730 foreach($discountFieldIDs as $fID){
731 CRM_Price_BAO_FieldValue::setIsActive($fID, '0');
732 }
733 }
734
735 $discountParams = array(
736 'entity_table' => 'civicrm_event',
737 'entity_id' => $this->_id,
738 'price_set_id' => $priceSetID,
739 'start_date' => CRM_Utils_Date::processDate($params['discount_start_date'][$j]),
740 'end_date' => CRM_Utils_Date::processDate($params['discount_end_date'][$j]),
741 );
742 CRM_Core_BAO_Discount::add($discountParams);
743 }
744 }
745 }
746 }
747 if (!empty($discountPriceSets)) {
748 foreach ($discountPriceSets as $setId) {
749 CRM_Price_BAO_Set::setIsQuickConfig($setId, 0);
750 }
751 }
752 }
a06f8589
PJ
753 }
754 else {
6a488035
TO
755 if (CRM_Utils_Array::value('price_field_id', $params)) {
756 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $params['price_field_id'], 'price_set_id');
757 CRM_Price_BAO_Set::setIsQuickConfig($priceSetID,0);
758 }
759 $params['financial_type_id'] = '';
a06f8589 760 $params['is_pay_later'] = 0;
6a488035
TO
761 }
762
763 //update events table
764 $params['id'] = $this->_id;
765 CRM_Event_BAO_Event::add($params);
766
767 parent::endPostProcess();
768 }
769
770 /**
771 * Return a descriptive name for the page, used in wizard header
772 *
773 * @return string
774 * @access public
775 */
776 public function getTitle() {
777 return ts('Event Fees');
778 }
779}
780