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