Merge pull request #12974 from mattwire/recurringentity_dateformat
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Fee.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2018
31 */
32
33 /**
34 * This class generates form components for Event Fees.
35 */
36 class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
37
38 /**
39 * Constants for number of options for data types of multiple option.
40 */
41 const NUM_OPTION = 11;
42
43 /**
44 * Constants for number of discounts for the event.
45 */
46 const NUM_DISCOUNT = 6;
47
48 /**
49 * Page action.
50 */
51 public $_action;
52
53 /**
54 * In Date.
55 */
56 private $_inDate;
57
58 /**
59 * Set variables up before form is built.
60 */
61 public function preProcess() {
62 parent::preProcess();
63 }
64
65 /**
66 * Set default values for the form.
67 *
68 * For edit/view mode the default values are retrieved from the database.
69 */
70 public function setDefaultValues() {
71 parent::setDefaultValues();
72
73 $eventId = $this->_id;
74 $params = array();
75 $defaults = array();
76 if (isset($eventId)) {
77 $params = array('id' => $eventId);
78 }
79
80 CRM_Event_BAO_Event::retrieve($params, $defaults);
81
82 if (isset($eventId)) {
83 $price_set_id = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventId, NULL, 1);
84
85 if ($price_set_id) {
86 $defaults['price_set_id'] = $price_set_id;
87 }
88 else {
89 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventId, NULL);
90 if ($priceSetId) {
91 if ($isQuick = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config')) {
92 $this->assign('isQuick', $isQuick);
93 $priceField = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $priceSetId, 'id', 'price_set_id');
94 $options = array();
95 $priceFieldOptions = CRM_Price_BAO_PriceFieldValue::getValues($priceField, $options, 'weight', TRUE);
96 $defaults['price_field_id'] = $priceField;
97 $countRow = 0;
98 foreach ($options as $optionId => $optionValue) {
99 $countRow++;
100 $defaults['value'][$countRow] = CRM_Utils_Money::format($optionValue['amount'], NULL, '%a');
101 $defaults['label'][$countRow] = $optionValue['label'];
102 $defaults['name'][$countRow] = $optionValue['name'];
103 $defaults['weight'][$countRow] = $optionValue['weight'];
104 $defaults['price_field_value'][$countRow] = $optionValue['id'];
105 if ($optionValue['is_default']) {
106 $defaults['default'] = $countRow;
107 }
108 }
109 }
110 }
111 }
112 }
113
114 //check if discounted
115 $discountedEvent = CRM_Core_BAO_Discount::getOptionGroup($this->_id, 'civicrm_event');
116 if (!empty($discountedEvent)) {
117 $defaults['is_discount'] = $i = 1;
118 $totalLables = $maxSize = $defaultDiscounts = array();
119 foreach ($discountedEvent as $optionGroupId) {
120 $defaults['discount_price_set'][] = $optionGroupId;
121 $defaults["discount_name[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $optionGroupId, 'title');
122
123 $defaults["discount_start_date[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
124 'start_date', 'price_set_id');
125 $defaults["discount_end_date[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
126 'end_date', 'price_set_id');
127 $defaultDiscounts[] = CRM_Price_BAO_PriceSet::getSetDetail($optionGroupId);
128 $i++;
129 }
130
131 //avoid moving up value of lable when some labels don't
132 //have a value ,fixed for CRM-3088
133 $rowCount = 1;
134 foreach ($defaultDiscounts as $val) {
135 $discountFields = current($val);
136 $discountFields = current($discountFields['fields']);
137
138 foreach ($discountFields['options'] as $discountFieldsval) {
139 $defaults['discounted_label'][$discountFieldsval['weight']] = $discountFieldsval['label'];
140 $defaults['discounted_value'][$discountFieldsval['weight']][$rowCount] = CRM_Utils_Money::format($discountFieldsval['amount'], NULL, '%a');
141 $defaults['discount_option_id'][$rowCount][$discountFieldsval['weight']] = $discountFieldsval['id'];
142 if (!empty($discountFieldsval['is_default'])) {
143 $defaults['discounted_default'] = $discountFieldsval['weight'];
144 }
145 }
146 $rowCount++;
147 }
148 //CRM-12970
149 ksort($defaults['discounted_value']);
150 ksort($defaults['discounted_label']);
151 $rowCount = 1;
152 foreach ($defaults['discounted_label'] as $key => $value) {
153 if ($key != $rowCount) {
154 $defaults['discounted_label'][$rowCount] = $value;
155 $defaults['discounted_value'][$rowCount] = $defaults['discounted_value'][$key];
156 unset($defaults['discounted_value'][$key]);
157 unset($defaults['discounted_label'][$key]);
158 foreach ($defaults['discount_option_id'] as &$optionIds) {
159 if (array_key_exists($key, $optionIds)) {
160 $optionIds[$rowCount] = $optionIds[$key];
161 unset($optionIds[$key]);
162 }
163 }
164 }
165 $rowCount++;
166 }
167
168 $this->set('discountSection', 1);
169 $this->buildQuickForm();
170 }
171 elseif (!empty($defaults['label'])) {
172 //if Regular Fees are present in DB and event fee page is in update mode
173 $defaults['discounted_label'] = $defaults['label'];
174 }
175 elseif (!empty($this->_submitValues['label'])) {
176 //if event is newly created, use submitted values for
177 //discount labels
178 if (is_array($this->_submitValues['label'])) {
179 $k = 1;
180 foreach ($this->_submitValues['label'] as $value) {
181 if ($value) {
182 $defaults['discounted_label'][$k] = $value;
183 $k++;
184 }
185 }
186 }
187 }
188 $defaults['id'] = $eventId;
189 if (!empty($totalLables)) {
190 $maxKey = count($totalLables) - 1;
191 if (isset($maxKey) && !empty($totalLables[$maxKey]['value'])) {
192 foreach ($totalLables[$maxKey]['value'] as $i => $v) {
193 if ($totalLables[$maxKey]['amount_id'][$i] == CRM_Utils_Array::value('default_discount_fee_id', $defaults)) {
194 $defaults['discounted_default'] = $i;
195 break;
196 }
197 }
198 }
199 }
200
201 if (!isset($defaults['discounted_default'])) {
202 $defaults['discounted_default'] = 1;
203 }
204
205 if (!isset($defaults['is_monetary'])) {
206 $defaults['is_monetary'] = 1;
207 }
208
209 if (!isset($defaults['fee_label'])) {
210 $defaults['fee_label'] = ts('Event Fee(s)');
211 }
212
213 if (!isset($defaults['pay_later_text']) ||
214 empty($defaults['pay_later_text'])
215 ) {
216 $defaults['pay_later_text'] = ts('I will send payment by check');
217 }
218
219 $this->_showHide = new CRM_Core_ShowHideBlocks();
220 if (!$defaults['is_monetary']) {
221 $this->_showHide->addHide('event-fees');
222 }
223
224 if (isset($defaults['price_set_id'])) {
225 $this->_showHide->addHide('map-field');
226 }
227 $this->_showHide->addToTemplate();
228 $this->assign('inDate', $this->_inDate);
229 if (!empty($defaults['payment_processor'])) {
230 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
231 $defaults['payment_processor']
232 ), '1');
233 }
234
235 return $defaults;
236 }
237
238 /**
239 * Build the form object.
240 */
241 public function buildQuickForm() {
242 $this->addYesNo('is_monetary',
243 ts('Paid Event'),
244 NULL,
245 NULL,
246 array('onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);")
247 );
248
249 //add currency element.
250 $this->addCurrency('currency', ts('Currency'), FALSE);
251
252 $paymentProcessor = CRM_Core_PseudoConstant::paymentProcessor();
253
254 $this->assign('paymentProcessor', $paymentProcessor);
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 if (!CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() ||
263 (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && CRM_Core_Permission::check('administer CiviCRM Financial Types'))) {
264 $this->addSelect('financial_type_id');
265 }
266 else {
267 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD);
268 $this->addSelect('financial_type_id', array('context' => 'search', 'options' => $financialTypes));
269 }
270 // add pay later options
271 $this->addElement('checkbox', 'is_pay_later', ts('Enable Pay Later option?'), NULL,
272 array('onclick' => "return showHideByValue('is_pay_later','','payLaterOptions','block','radio',false);")
273 );
274 $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'),
275 CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'),
276 FALSE
277 );
278 $this->add('wysiwyg', 'pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'));
279
280 $this->addElement('checkbox', 'is_billing_required', ts('Billing address required'));
281 $this->add('text', 'fee_label', ts('Fee Label'));
282
283 $price = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviEvent');
284 if (CRM_Utils_System::isNull($price)) {
285 $this->assign('price', FALSE);
286 }
287 else {
288 $this->assign('price', TRUE);
289 }
290 $this->add('select', 'price_set_id', ts('Price Set'),
291 array(
292 '' => ts('- none -'),
293 ) + $price,
294 NULL, array('onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);")
295 );
296 $default = array($this->createElement('radio', NULL, NULL, NULL, 0));
297 $this->add('hidden', 'price_field_id', '', array('id' => 'price_field_id'));
298 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
299 // label
300 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
301 $this->add('hidden', "price_field_value[$i]", '', array('id' => "price_field_value[$i]"));
302
303 // value
304 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
305 $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');
306
307 // default
308 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
309 }
310
311 $this->addGroup($default, 'default');
312
313 $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL,
314 array('onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);")
315 );
316 $discountSection = $this->get('discountSection');
317
318 $this->assign('discountSection', $discountSection);
319
320 // form fields of Discount sets
321 $defaultOption = array();
322 $_showHide = new CRM_Core_ShowHideBlocks('', '');
323
324 for ($i = 1; $i <= self::NUM_DISCOUNT; $i++) {
325 //the show hide blocks
326 $showBlocks = 'discount_' . $i;
327 if ($i > 2) {
328 $_showHide->addHide($showBlocks);
329 }
330 else {
331 $_showHide->addShow($showBlocks);
332 }
333
334 //Increment by 1 of start date of previous end date.
335 if (is_array($this->_submitValues) &&
336 !empty($this->_submitValues['discount_name'][$i]) &&
337 !empty($this->_submitValues['discount_name'][$i + 1]) &&
338 isset($this->_submitValues['discount_end_date']) &&
339 isset($this->_submitValues['discount_end_date'][$i]) &&
340 $i < self::NUM_DISCOUNT - 1
341 ) {
342 if (!empty($this->_submitValues['discount_end_date'][$i + 1])
343 && empty($this->_submitValues['discount_start_date'][$i + 1])
344 ) {
345 $this->_submitValues['discount_start_date'][$i + 1] = date('Y-m-d', strtotime("+1 days " . $this->_submitValues['discount_end_date'][$i]));
346 }
347 }
348 //Decrement by 1 of end date from next start date.
349 if ($i > 1 &&
350 is_array($this->_submitValues) &&
351 !empty($this->_submitValues['discount_name'][$i]) &&
352 !empty($this->_submitValues['discount_name'][$i - 1]) &&
353 isset($this->_submitValues['discount_start_date']) &&
354 isset($this->_submitValues['discount_start_date'][$i])
355 ) {
356 if (!empty($this->_submitValues['discount_start_date'][$i])
357 && empty($this->_submitValues['discount_end_date'][$i - 1])
358 ) {
359 list($this->_submitValues['discount_end_date'][$i - 1]) = date('Y-m-d', strtotime("-1 days " . $this->_submitValues['discount_start_date'][$i]));
360 }
361 }
362
363 $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
364 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title')
365 );
366 $this->add('hidden', "discount_price_set[$i]", '', array('id' => "discount_price_set[$i]"));
367 $this->add('datepicker', 'discount_start_date[' . $i . ']', ts('Discount Start Date'), [], FALSE, array('time' => FALSE));
368 $this->add('datepicker', 'discount_end_date[' . $i . ']', ts('Discount End Date'), [], FALSE, array('time' => FALSE));
369 }
370 $_showHide->addToTemplate();
371 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
372 array('class' => 'crm-form-submit cancel')
373 );
374 if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
375 $deferredFinancialType = CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType();
376 $this->assign('deferredFinancialType', array_keys($deferredFinancialType));
377 }
378 $this->buildAmountLabel();
379 parent::buildQuickForm();
380 }
381
382 /**
383 * Add local and global form rules.
384 */
385 public function addRules() {
386 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Fee', 'formRule'));
387 }
388
389 /**
390 * Global validation rules for the form.
391 *
392 * @param array $values
393 * Posted values of the form.
394 *
395 * @return array
396 * list of errors to be posted back to the form
397 */
398 public static function formRule($values) {
399 $errors = array();
400 if (!empty($values['is_discount'])) {
401 $occurDiscount = array_count_values($values['discount_name']);
402 $countemptyrows = 0;
403 $countemptyvalue = 0;
404 for ($i = 1; $i <= self::NUM_DISCOUNT; $i++) {
405 $start_date = $end_date = NULL;
406 if (!empty($values['discount_name'][$i])) {
407 if (!empty($values['discount_start_date'][$i])) {
408 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date::processDate($values['discount_start_date'][$i]) : 0;
409 }
410 if (!empty($values['discount_end_date'][$i])) {
411 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date::processDate($values['discount_end_date'][$i]) : 0;
412 }
413
414 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
415 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
416 }
417
418 if (!$start_date && !$end_date) {
419 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
420 }
421
422 if ($i > 1) {
423 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date::processDate($values['discount_end_date'][$i - 1]) : 0;
424 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
425 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
426 }
427 elseif (!$start_date && !$end_date_1) {
428 $j = $i - 1;
429 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
430 }
431 }
432
433 foreach ($occurDiscount as $key => $value) {
434 if ($value > 1 && $key <> '') {
435 if ($key == $values['discount_name'][$i]) {
436 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', array(1 => $key));
437 }
438 }
439 }
440
441 //validation for discount labels and values
442 for ($index = (self::NUM_OPTION); $index > 0; $index--) {
443 $label = TRUE;
444 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
445 $label = FALSE;
446 if (!$label) {
447 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
448 }
449 }
450 if (!empty($values['discounted_label'][$index])) {
451 $duplicateIndex = CRM_Utils_Array::key($values['discounted_label'][$index], $values['discounted_label']);
452
453 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
454 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
455 }
456 }
457 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
458 $countemptyrows++;
459 }
460 if (empty($values['discounted_value'][$index][$i])) {
461 $countemptyvalue++;
462 }
463 }
464 if (!empty($values['_qf_Fee_next']) && ($countemptyrows == 11 || $countemptyvalue == 11)) {
465 $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.');
466 }
467 }
468 }
469 }
470 if ($values['is_monetary']) {
471 //check if financial type is selected
472 if (!$values['financial_type_id']) {
473 $errors['financial_type_id'] = ts("Please select financial type.");
474 }
475
476 //check for the event fee label (mandatory)
477 if (!$values['fee_label']) {
478 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
479 }
480
481 if (empty($values['price_set_id'])) {
482 //check fee label and amount
483 $check = 0;
484 $optionKeys = array();
485 foreach ($values['label'] as $key => $val) {
486 if (trim($val) && trim($values['value'][$key])) {
487 $optionKeys[$key] = $key;
488 $check++;
489 }
490 }
491
492 $default = CRM_Utils_Array::value('default', $values);
493 if ($default && !in_array($default, $optionKeys)) {
494 $errors['default'] = ts('Please select an appropriate option as default.');
495 }
496
497 if (!$check) {
498 if (!$values['label'][1]) {
499 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
500 }
501 if (!$values['value'][1]) {
502 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
503 }
504 }
505 }
506 if (isset($values['is_pay_later'])) {
507 if (empty($values['pay_later_text'])) {
508 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
509 }
510 if (empty($values['pay_later_receipt'])) {
511 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
512 }
513 }
514 else {
515 if (empty($values['payment_processor'])) {
516 $errors['payment_processor'] = ts('You have indicated that this is a paid event, but no payment option has been selected. If this is not a paid event, please select the "No" option at the top of the page. If this is a paid event, please select at least one payment processor and/or enable the pay later option.');
517 }
518 }
519 }
520 return empty($errors) ? TRUE : $errors;
521 }
522
523 public function buildAmountLabel() {
524 $default = array();
525 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
526 // label
527 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
528 // value
529 for ($j = 1; $j <= self::NUM_DISCOUNT; $j++) {
530 $this->add('text', "discounted_value[$i][$j]", ts('Value'), array('size' => 10));
531 $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');
532 }
533
534 // default
535 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
536 }
537
538 $this->addGroup($default, 'discounted_default');
539 }
540
541 /**
542 * Process the form.
543 */
544 public function postProcess() {
545 $eventTitle = '';
546 $params = $this->exportValues();
547
548 $this->set('discountSection', 0);
549
550 if (!empty($_POST['_qf_Fee_submit'])) {
551 $this->buildAmountLabel();
552 $this->set('discountSection', 2);
553 return;
554 }
555
556 if (!empty($params['payment_processor'])) {
557 $params['payment_processor'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['payment_processor']));
558 }
559 else {
560 $params['payment_processor'] = 'null';
561 }
562
563 $params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, 0);
564 $params['is_billing_required'] = CRM_Utils_Array::value('is_billing_required', $params, 0);
565
566 if ($this->_id) {
567
568 // delete all the prior label values or discounts in the custom options table
569 // and delete a price set if one exists
570 //@todo note that this removes the reference from existing participants -
571 // even where there is not change - redress?
572 // note that a more tentative form of this is invoked by passing price_set_id as an array
573 // to event.create see CRM-14069
574 // @todo get all of this logic out of form layer (currently partially in BAO/api layer)
575 if (CRM_Price_BAO_PriceSet::removeFrom('civicrm_event', $this->_id)) {
576 CRM_Core_BAO_Discount::del($this->_id, 'civicrm_event');
577 }
578 }
579
580 if ($params['is_monetary']) {
581 if (!empty($params['price_set_id'])) {
582 //@todo this is now being done in the event BAO if passed price_set_id as an array
583 // per notes on that fn - looking at the api converting to an array
584 // so calling via the api may cause this to be done in the api
585 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_id, $params['price_set_id']);
586 if (!empty($params['price_field_id'])) {
587 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
588 CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetID, 0);
589 }
590 }
591 else {
592 // if there are label / values, create custom options for them
593 $labels = CRM_Utils_Array::value('label', $params);
594 $values = CRM_Utils_Array::value('value', $params);
595 $default = CRM_Utils_Array::value('default', $params);
596 $options = array();
597 if (!CRM_Utils_System::isNull($labels) && !CRM_Utils_System::isNull($values)) {
598 for ($i = 1; $i < self::NUM_OPTION; $i++) {
599 if (!empty($labels[$i]) && !CRM_Utils_System::isNull($values[$i])) {
600 $options[] = array(
601 '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 (empty($params['price_set_id'])) {
612 if (empty($params['price_field_id'])) {
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_PriceSet', $eventTitle, 'id', 'name')) {
616 $setParams['name'] = $eventTitle;
617 }
618 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $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['financial_type_id'] = $params['financial_type_id'];
627 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent');
628 $priceSet = CRM_Price_BAO_PriceSet::create($setParams);
629
630 $fieldParams['name'] = strtolower(CRM_Utils_String::munge($params['fee_label'], '_', 245));
631 $fieldParams['price_set_id'] = $priceSet->id;
632 }
633 else {
634 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
635 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
636 CRM_Price_BAO_PriceFieldValue::setIsActive($fieldValueID, '0');
637 unset($params['price_field_value'][$arrayID]);
638 }
639 }
640 $fieldParams['id'] = CRM_Utils_Array::value('price_field_id', $params);
641 $fieldParams['option_id'] = $params['price_field_value'];
642
643 $priceSet = new CRM_Price_BAO_PriceSet();
644 $priceSet->id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array::value('price_field_id', $params), 'price_set_id');
645
646 if ($this->_defaultValues['financial_type_id'] != $params['financial_type_id']) {
647 CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceSet', $priceSet->id, 'financial_type_id', $params['financial_type_id']);
648 }
649 }
650 $fieldParams['label'] = $params['fee_label'];
651 $fieldParams['html_type'] = 'Radio';
652 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_id, $priceSet->id);
653 $fieldParams['option_label'] = $params['label'];
654 $fieldParams['option_amount'] = $params['value'];
655 $fieldParams['financial_type_id'] = $params['financial_type_id'];
656 foreach ($options as $value) {
657 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
658 }
659 $fieldParams['default_option'] = $params['default'];
660 $priceField = CRM_Price_BAO_PriceField::create($fieldParams);
661 }
662 }
663 }
664
665 $discountPriceSets = !empty($this->_defaultValues['discount_price_set']) ? $this->_defaultValues['discount_price_set'] : array();
666 $discountFieldIDs = !empty($this->_defaultValues['discount_option_id']) ? $this->_defaultValues['discount_option_id'] : array();
667 if (CRM_Utils_Array::value('is_discount', $params) == 1) {
668 // if there are discounted set of label / values,
669 // create custom options for them
670 $labels = CRM_Utils_Array::value('discounted_label', $params);
671 $values = CRM_Utils_Array::value('discounted_value', $params);
672 $default = CRM_Utils_Array::value('discounted_default', $params);
673
674 if (!CRM_Utils_System::isNull($labels) && !CRM_Utils_System::isNull($values)) {
675 for ($j = 1; $j <= self::NUM_DISCOUNT; $j++) {
676 $discountOptions = array();
677 for ($i = 1; $i < self::NUM_OPTION; $i++) {
678 if (!empty($labels[$i]) &&
679 !CRM_Utils_System::isNull(CRM_Utils_Array::value($j, $values[$i]))
680 ) {
681 $discountOptions[] = array(
682 'label' => trim($labels[$i]),
683 'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i][$j])),
684 'weight' => $i,
685 'is_active' => 1,
686 'is_default' => $default == $i,
687 );
688 }
689 }
690
691 if (!empty($discountOptions)) {
692 $fieldParams = array();
693 $params['default_discount_fee_id'] = NULL;
694 $keyCheck = $j - 1;
695 $setParams = array();
696 if (empty($discountPriceSets[$keyCheck])) {
697 if (!$eventTitle) {
698 $eventTitle = strtolower(CRM_Utils_String::munge($this->_defaultValues['title'], '_', 200));
699 }
700 $setParams['title'] = $params['discount_name'][$j];
701 if (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) {
702 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j];
703 }
704 elseif (!CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id, 'id', 'name')) {
705 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id;
706 }
707 else {
708 $timeSec = explode('.', microtime(TRUE));
709 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
710 }
711 $setParams['is_quick_config'] = 1;
712 $setParams['financial_type_id'] = $params['financial_type_id'];
713 $setParams['extends'] = CRM_Core_Component::getComponentID('CiviEvent');
714 $priceSet = CRM_Price_BAO_PriceSet::create($setParams);
715 $priceSetID = $priceSet->id;
716 }
717 else {
718 $priceSetID = $discountPriceSets[$j - 1];
719 $setParams = array(
720 'title' => $params['discount_name'][$j],
721 'id' => $priceSetID,
722 );
723 if ($this->_defaultValues['financial_type_id'] != $params['financial_type_id']) {
724 $setParams['financial_type_id'] = $params['financial_type_id'];
725 }
726 CRM_Price_BAO_PriceSet::create($setParams);
727 unset($discountPriceSets[$j - 1]);
728 $fieldParams['id'] = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $priceSetID, 'id', 'price_set_id');
729 }
730
731 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
732 $fieldParams['is_required'] = 1;
733 $fieldParams['price_set_id'] = $priceSetID;
734 $fieldParams['html_type'] = 'Radio';
735 $fieldParams['financial_type_id'] = $params['financial_type_id'];
736 foreach ($discountOptions as $value) {
737 $fieldParams['option_label'][$value['weight']] = $value['label'];
738 $fieldParams['option_amount'][$value['weight']] = $value['value'];
739 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
740 if (!empty($value['is_default'])) {
741 $fieldParams['default_option'] = $value['weight'];
742 }
743 if (!empty($discountFieldIDs[$j]) && !empty($discountFieldIDs[$j][$value['weight']])) {
744 $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']];
745 unset($discountFieldIDs[$j][$value['weight']]);
746 }
747 }
748 //create discount priceset
749 $priceField = CRM_Price_BAO_PriceField::create($fieldParams);
750 if (!empty($discountFieldIDs[$j])) {
751 foreach ($discountFieldIDs[$j] as $fID) {
752 CRM_Price_BAO_PriceFieldValue::setIsActive($fID, '0');
753 }
754 }
755
756 $discountParams = array(
757 'entity_table' => 'civicrm_event',
758 'entity_id' => $this->_id,
759 'price_set_id' => $priceSetID,
760 'start_date' => $params['discount_start_date'][$j],
761 'end_date' => $params['discount_end_date'][$j],
762 );
763 CRM_Core_BAO_Discount::add($discountParams);
764 }
765 }
766 }
767 }
768 if (!empty($discountPriceSets)) {
769 foreach ($discountPriceSets as $setId) {
770 CRM_Price_BAO_PriceSet::setIsQuickConfig($setId, 0);
771 }
772 }
773 }
774 }
775 else {
776 if (!empty($params['price_field_id'])) {
777 $priceSetID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
778 CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetID, 0);
779 }
780 $params['financial_type_id'] = '';
781 $params['is_pay_later'] = 0;
782 $params['is_billing_required'] = 0;
783 }
784
785 //update 'is_billing_required'
786 if (empty($params['is_pay_later'])) {
787 $params['is_billing_required'] = FALSE;
788 }
789
790 //update events table
791 $params['id'] = $this->_id;
792 // skip update of financial type in price set
793 $params['skipFinancialType'] = TRUE;
794 CRM_Event_BAO_Event::add($params);
795
796 // Update tab "disabled" css class
797 $this->ajaxResponse['tabValid'] = !empty($params['is_monetary']);
798 parent::endPostProcess();
799 }
800
801 /**
802 * Return a descriptive name for the page, used in wizard header
803 *
804 * @return string
805 */
806 public function getTitle() {
807 return ts('Event Fees');
808 }
809
810 }