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