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