3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for Event Fees
40 class CRM_Event_Form_ManageEvent_Fee
extends CRM_Event_Form_ManageEvent
{
43 * Constants for number of options for data types of multiple option.
45 CONST NUM_OPTION
= 11;
48 * Constants for number of discounts for the event.
50 CONST NUM_DISCOUNT
= 6;
63 * Function to set variables up before form is built
68 function preProcess() {
73 * This function sets the default values for the form. For edit/view mode
74 * the default values are retrieved from the database
80 function setDefaultValues() {
81 $parentDefaults = parent
::setDefaultValues();
83 $eventId = $this->_id
;
86 if (isset($eventId)) {
87 $params = array('id' => $eventId);
90 CRM_Event_BAO_Event
::retrieve($params, $defaults);
92 if (isset($eventId)) {
93 $price_set_id = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL, 1);
96 $defaults['price_set_id'] = $price_set_id;
99 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL);
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');
105 $priceFieldOptions = CRM_Price_BAO_PriceFieldValue
::getValues($priceField, $options, 'weight', true);
106 $defaults['price_field_id'] = $priceField;
108 foreach ($options as $optionId => $optionValue) {
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;
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');
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'
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'
139 $defaultDiscounts[] = CRM_Price_BAO_PriceSet
::getSetDetail($optionGroupId);
143 //avoid moving up value of lable when some labels don't
144 //have a value ,fixed for CRM-3088
146 foreach ($defaultDiscounts as $val) {
147 $discountFields = current($val);
148 $discountFields = current($discountFields['fields']);
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'];
162 ksort($defaults['discounted_value']);
163 ksort($defaults['discounted_label']);
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]);
181 $this->set('discountSection', 1);
182 $this->buildQuickForm();
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'];
188 elseif (!empty($this->_submitValues
['label'])) {
189 //if event is newly created, use submitted values for
191 if (is_array($this->_submitValues
['label'])) {
193 foreach ($this->_submitValues
['label'] as $value) {
195 $defaults['discounted_label'][$k] = $value;
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;
214 if (!isset($defaults['discounted_default'])) {
215 $defaults['discounted_default'] = 1;
218 if (!isset($defaults['is_monetary'])) {
219 $defaults['is_monetary'] = 1;
222 if (!isset($defaults['fee_label'])) {
223 $defaults['fee_label'] = ts('Event Fee(s)');
226 if (!isset($defaults['pay_later_text']) ||
227 empty($defaults['pay_later_text'])
229 $defaults['pay_later_text'] = ts('I will send payment by check');
232 $this->_showHide
= new CRM_Core_ShowHideBlocks();
233 if (!$defaults['is_monetary']) {
234 $this->_showHide
->addHide('event-fees');
237 if (isset($defaults['price_set_id'])) {
238 $this->_showHide
->addHide('map-field');
240 $this->_showHide
->addToTemplate();
241 $this->assign('inDate', $this->_inDate
);
243 if (!empty($defaults['payment_processor'])) {
244 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
245 $defaults['payment_processor']
252 * Function to build the form
257 public function buildQuickForm() {
259 $this->addYesNo('is_monetary',
263 array('onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);")
266 //add currency element.
267 $this->addCurrency('currency', ts('Currency'), FALSE);
269 $paymentProcessor = CRM_Core_PseudoConstant
::paymentProcessor();
271 $this->assign('paymentProcessor', $paymentProcessor);
273 $this->addCheckBox('payment_processor', ts('Payment Processor'),
274 array_flip($paymentProcessor),
275 NULL, NULL, NULL, NULL,
276 array(' ', ' ', ' ', '<br/>')
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);")
285 $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'),
286 CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'),
289 $this->addWysiwyg('pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'));
291 $this->addElement('checkbox', 'is_billing_required', ts('Billing address required'));
292 $this->add('text', 'fee_label', ts('Fee Label'));
294 $price = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviEvent');
295 if (CRM_Utils_System
::isNull($price)) {
296 $this->assign('price', FALSE);
299 $this->assign('price', TRUE);
301 $this->add('select', 'price_set_id', ts('Price Set'),
303 '' => ts('- none -')) +
$price,
304 NULL, array('onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);")
306 $default = array($this->createElement('radio', NULL, NULL, NULL, 0));
307 $this->add('hidden', 'price_field_id', '', array('id' => 'price_field_id'));
308 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
310 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
311 $this->add('hidden', "price_field_value[$i]", '', array('id' => "price_field_value[$i]"));
314 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
315 $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');
318 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
321 $this->addGroup($default, 'default');
323 $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL,
324 array('onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);")
326 $discountSection = $this->get('discountSection');
328 $this->assign('discountSection', $discountSection);
330 // form fields of Discount sets
331 $defaultOption = array();
332 $_showHide = new CRM_Core_ShowHideBlocks('', '');
334 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
335 //the show hide blocks
336 $showBlocks = 'discount_' . $i;
338 $_showHide->addHide($showBlocks);
341 $_showHide->addShow($showBlocks);
344 //Increment by 1 of start date of previous end date.
345 if (is_array($this->_submitValues
) &&
346 !empty($this->_submitValues
['discount_name'][$i]) &&
347 !empty($this->_submitValues
['discount_name'][$i +
1]) &&
348 isset($this->_submitValues
['discount_end_date']) &&
349 isset($this->_submitValues
['discount_end_date'][$i]) &&
350 $i < self
::NUM_DISCOUNT
- 1
352 $end_date = CRM_Utils_Date
::processDate($this->_submitValues
['discount_end_date'][$i]);
353 if (!empty($this->_submitValues
['discount_end_date'][$i +
1])
354 && empty($this->_submitValues
['discount_start_date'][$i +
1])
356 list($this->_submitValues
['discount_start_date'][$i +
1]) = CRM_Utils_Date
::setDateDefaults(date('Y-m-d', strtotime("+1 days $end_date")));
359 //Decrement by 1 of end date from next start date.
361 is_array($this->_submitValues
) &&
362 !empty($this->_submitValues
['discount_name'][$i]) &&
363 !empty($this->_submitValues
['discount_name'][$i - 1]) &&
364 isset($this->_submitValues
['discount_start_date']) &&
365 isset($this->_submitValues
['discount_start_date'][$i])
367 $start_date = CRM_Utils_Date
::processDate($this->_submitValues
['discount_start_date'][$i]);
368 if (!empty($this->_submitValues
['discount_start_date'][$i])
369 && empty($this->_submitValues
['discount_end_date'][$i - 1])
371 list($this->_submitValues
['discount_end_date'][$i - 1]) = CRM_Utils_Date
::setDateDefaults(date('Y-m-d', strtotime("-1 days $start_date")));
376 $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
377 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'title')
380 $this->add('hidden', "discount_price_set[$i]", '', array('id' => "discount_price_set[$i]"));
382 //discount start date
383 $this->addDate('discount_start_date[' . $i . ']', ts('Discount Start Date'), FALSE, array('formatType' => 'activityDate'));
386 $this->addDate('discount_end_date[' . $i . ']', ts('Discount End Date'), FALSE, array('formatType' => 'activityDate'));
388 $_showHide->addToTemplate();
389 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
390 array('class' => 'crm-form-submit cancel')
393 $this->buildAmountLabel();
394 parent
::buildQuickForm();
398 * Add local and global form rules
404 function addRules() {
405 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Fee', 'formRule'));
409 * global validation rules for the form
411 * @param array $values posted values of the form
413 * @return array list of errors to be posted back to the form
417 static function formRule($values) {
419 if (!empty($values['is_discount'])) {
420 $occurDiscount = array_count_values($values['discount_name']);
422 $countemptyvalue = 0;
423 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
424 $start_date = $end_date = NULL;
425 if (!empty($values['discount_name'][$i])) {
426 if (!empty($values['discount_start_date'][$i])) {
427 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_start_date'][$i]) : 0;
429 if (!empty($values['discount_end_date'][$i])) {
430 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i]) : 0;
433 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
434 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
437 if (!$start_date && !$end_date) {
438 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
442 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i - 1]) : 0;
443 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
444 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
446 elseif (!$start_date && !$end_date_1) {
448 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
452 foreach ($occurDiscount as $key => $value) if ($value > 1 && $key <> '') {
453 if ($key == $values['discount_name'][$i]) {
454 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', array(1 => $key));
458 //validation for discount labels and values
459 for ($index = (self
::NUM_OPTION
); $index > 0; $index--) {
461 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
464 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
467 if (!empty($values['discounted_label'][$index])) {
468 $duplicateIndex = CRM_Utils_Array
::key($values['discounted_label'][$index], $values['discounted_label']);
470 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
471 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
474 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
477 if (empty($values['discounted_value'][$index][$i])) {
481 if (!empty($values['_qf_Fee_next']) && ($countemptyrows == 11 ||
$countemptyvalue == 11)) {
482 $errors["discounted_label[1]"] = $errors["discounted_value[1][$i]"] = ts('At least one fee should be entered for your Discount Set. If you do not see the table to enter discount fees, click the "Add Discount Set to Fee Table" button.');
487 if ($values['is_monetary']) {
488 //check if financial type is selected
489 if (!$values['financial_type_id']) {
490 $errors['financial_type_id'] = ts("Please select financial type.");
493 //check for the event fee label (mandatory)
494 if (!$values['fee_label']) {
495 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
498 if (empty($values['price_set_id'])) {
499 //check fee label and amount
501 $optionKeys = array();
502 foreach ($values['label'] as $key => $val) {
503 if (trim($val) && trim($values['value'][$key])) {
504 $optionKeys[$key] = $key;
509 $default = CRM_Utils_Array
::value('default', $values);
510 if ($default && !in_array($default, $optionKeys)) {
511 $errors['default'] = ts('Please select an appropriate option as default.');
515 if (!$values['label'][1]) {
516 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
518 if (!$values['value'][1]) {
519 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
523 if (isset($values['is_pay_later'])) {
524 if (empty($values['pay_later_text'])) {
525 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
527 if (empty($values['pay_later_receipt'])) {
528 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
532 return empty($errors) ?
TRUE : $errors;
535 public function buildAmountLabel() {
537 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
539 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
541 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
542 $this->add('text', "discounted_value[$i][$j]", ts('Value'), array('size' => 10));
543 $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');
547 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
550 $this->addGroup($default, 'discounted_default');
559 public function postProcess() {
561 $params = $this->exportValues();
563 $this->set('discountSection', 0);
565 if (!empty($_POST['_qf_Fee_submit'])) {
566 $this->buildAmountLabel();
567 $this->set('discountSection', 2);
571 if (array_key_exists('payment_processor', $params) &&
572 !CRM_Utils_System
::isNull($params['payment_processor'])
574 $params['payment_processor'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($params['payment_processor']));
577 $params['payment_processor'] = 'null';
580 $params['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, 0);
581 $params['is_billing_required'] = CRM_Utils_Array
::value('is_billing_required', $params, 0);
585 // delete all the prior label values or discounts in the custom options table
586 // and delete a price set if one exists
587 //@todo note that this removes the reference from existing participants -
588 // even where there is not change - redress?
589 // note that a more tentative form of this is invoked by passing price_set_id as an array
590 // to event.create see CRM-14069
591 // @todo get all of this logic out of form layer (currently partially in BAO/api layer)
592 if (CRM_Price_BAO_PriceSet
::removeFrom('civicrm_event', $this->_id
)) {
593 CRM_Core_BAO_Discount
::del($this->_id
,'civicrm_event');
597 if ($params['is_monetary']) {
598 if (!empty($params['price_set_id'])) {
599 //@todo this is now being done in the event BAO if passed price_set_id as an array
600 // per notes on that fn - looking at the api converting to an array
601 // so calling via the api may cause this to be done in the api
602 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $params['price_set_id']);
603 if (!empty($params['price_field_id'])) {
604 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
605 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID,0);
609 // if there are label / values, create custom options for them
610 $labels = CRM_Utils_Array
::value('label', $params);
611 $values = CRM_Utils_Array
::value('value', $params);
612 $default = CRM_Utils_Array
::value('default', $params);
614 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
615 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
616 if (!empty($labels[$i]) && !CRM_Utils_System
::isNull($values[$i])) {
617 $options[] = array('label' => trim($labels[$i]),
618 'value' => CRM_Utils_Rule
::cleanMoney(trim($values[$i])),
621 'is_default' => $default == $i
625 if (!empty($options)) {
626 $params['default_fee_id'] = NULL;
627 if (empty($params['price_set_id'])) {
628 if (empty($params['price_field_id'])) {
629 $setParams['title'] = $eventTitle = ($this->_isTemplate
) ?
$this->_defaultValues
['template_title'] : $this->_defaultValues
['title'];
630 $eventTitle = strtolower(CRM_Utils_String
::munge($eventTitle, '_', 245));
631 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle, 'id', 'name')) {
632 $setParams['name'] = $eventTitle;
634 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $this->_id
, 'id', 'name')) {
635 $setParams['name'] = $eventTitle . '_' . $this->_id
;
638 $timeSec = explode('.', microtime(true));
639 $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
641 $setParams['is_quick_config'] = 1;
642 $setParams['financial_type_id'] = $params['financial_type_id'];
643 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
644 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
646 $fieldParams['name'] = strtolower(CRM_Utils_String
::munge($params['fee_label'], '_', 245));
647 $fieldParams['price_set_id'] = $priceSet->id
;
650 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
651 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
652 CRM_Price_BAO_PriceFieldValue
::setIsActive($fieldValueID, '0');
653 unset($params['price_field_value'][$arrayID]);
656 $fieldParams['id'] = CRM_Utils_Array
::value('price_field_id', $params);
657 $fieldParams['option_id'] = $params['price_field_value'];
659 $priceSet = new CRM_Price_BAO_PriceSet();
660 $priceSet->id
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $params), 'price_set_id');
662 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
663 CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceSet', $priceSet->id
, 'financial_type_id', $params['financial_type_id']);
666 $fieldParams['label'] = $params['fee_label'];
667 $fieldParams['html_type'] = 'Radio';
668 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $priceSet->id
);
669 $fieldParams['option_label'] = $params['label'];
670 $fieldParams['option_amount'] = $params['value'];
671 $fieldParams['financial_type_id'] = $params['financial_type_id'];
672 foreach ($options as $value) $fieldParams['option_weight'][$value['weight']] = $value['weight'];
673 $fieldParams['default_option'] = $params['default'];
674 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
679 $discountPriceSets = !empty($this->_defaultValues
['discount_price_set']) ?
$this->_defaultValues
['discount_price_set']: array();
680 $discountFieldIDs = !empty($this->_defaultValues
['discount_option_id']) ?
$this->_defaultValues
['discount_option_id']: array();
681 if (CRM_Utils_Array
::value('is_discount', $params) == 1) {
682 // if there are discounted set of label / values,
683 // create custom options for them
684 $labels = CRM_Utils_Array
::value('discounted_label', $params);
685 $values = CRM_Utils_Array
::value('discounted_value', $params);
686 $default = CRM_Utils_Array
::value('discounted_default', $params);
688 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
689 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
690 $discountOptions = array();
691 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
692 if (!empty($labels[$i]) &&
693 !CRM_Utils_System
::isNull(CRM_Utils_Array
::value($j, $values[$i]))
695 $discountOptions[] = array(
696 'label' => trim($labels[$i]),
697 'value' => CRM_Utils_Rule
::cleanMoney(trim($values[$i][$j])),
700 'is_default' => $default == $i
705 if (!empty($discountOptions)) {
706 $fieldParams = array();
707 $params['default_discount_fee_id'] = NULL;
709 $setParams = array();
710 if (empty($discountPriceSets[$keyCheck])) {
712 $eventTitle = strtolower(CRM_Utils_String
::munge($this->_defaultValues
['title'], '_', 200));
714 $setParams['title'] = $params['discount_name'][$j];
715 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) {
716 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j];
718 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
, 'id', 'name')) {
719 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
;
722 $timeSec = explode('.', microtime(true));
723 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
725 $setParams['is_quick_config'] = 1;
726 $setParams['financial_type_id'] = $params['financial_type_id'];
727 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
728 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
729 $priceSetID = $priceSet->id
;
732 $priceSetID = $discountPriceSets[$j-1];
734 'title' => $params['discount_name'][$j],
737 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
738 $setParams['financial_type_id'] = $params['financial_type_id'];
740 CRM_Price_BAO_PriceSet
::create($setParams);
741 unset($discountPriceSets[$j-1]);
742 $fieldParams['id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceField', $priceSetID, 'id', 'price_set_id');
745 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
746 $fieldParams['is_required'] = 1;
747 $fieldParams['price_set_id'] = $priceSetID;
748 $fieldParams['html_type'] = 'Radio';
749 $fieldParams['financial_type_id'] = $params['financial_type_id'];
750 foreach ($discountOptions as $value) {
751 $fieldParams['option_label'][$value['weight']] = $value['label'];
752 $fieldParams['option_amount'][$value['weight']] = $value['value'];
753 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
754 if (!empty($value['is_default'])) {
755 $fieldParams['default_option'] = $value['weight'];
757 if (!empty($discountFieldIDs[$j]) && !empty($discountFieldIDs[$j][$value['weight']])) {
758 $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']];
759 unset($discountFieldIDs[$j][$value['weight']]);
762 //create discount priceset
763 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
764 if (!empty($discountFieldIDs[$j])) {
765 foreach($discountFieldIDs[$j] as $fID){
766 CRM_Price_BAO_PriceFieldValue
::setIsActive($fID, '0');
770 $discountParams = array(
771 'entity_table' => 'civicrm_event',
772 'entity_id' => $this->_id
,
773 'price_set_id' => $priceSetID,
774 'start_date' => CRM_Utils_Date
::processDate($params['discount_start_date'][$j]),
775 'end_date' => CRM_Utils_Date
::processDate($params['discount_end_date'][$j]),
777 CRM_Core_BAO_Discount
::add($discountParams);
782 if (!empty($discountPriceSets)) {
783 foreach ($discountPriceSets as $setId) {
784 CRM_Price_BAO_PriceSet
::setIsQuickConfig($setId, 0);
790 if (!empty($params['price_field_id'])) {
791 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
792 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID,0);
794 $params['financial_type_id'] = '';
795 $params['is_pay_later'] = 0;
796 $params['is_billing_required'] = 0;
799 //update 'is_billing_required'
800 if (empty($params['is_pay_later'])) {
801 $params['is_billing_required'] = False;
804 //update events table
805 $params['id'] = $this->_id
;
806 // skip update of financial type in price set
807 $params['skipFinancialType'] = TRUE;
808 CRM_Event_BAO_Event
::add($params);
810 // Update tab "disabled" css class
811 $this->ajaxResponse
['tabValid'] = !empty($params['is_monetary']);
812 parent
::endPostProcess();
816 * Return a descriptive name for the page, used in wizard header
821 public function getTitle() {
822 return ts('Event Fees');