3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * Set variables up before form is built
67 public function preProcess() {
72 * Set default values for the form. For edit/view mode
73 * the default values are retrieved from the database
78 public function setDefaultValues() {
79 $parentDefaults = parent
::setDefaultValues();
81 $eventId = $this->_id
;
84 if (isset($eventId)) {
85 $params = array('id' => $eventId);
88 CRM_Event_BAO_Event
::retrieve($params, $defaults);
90 if (isset($eventId)) {
91 $price_set_id = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL, 1);
94 $defaults['price_set_id'] = $price_set_id;
97 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL);
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');
103 $priceFieldOptions = CRM_Price_BAO_PriceFieldValue
::getValues($priceField, $options, 'weight', TRUE);
104 $defaults['price_field_id'] = $priceField;
106 foreach ($options as $optionId => $optionValue) {
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;
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');
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'
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'
137 $defaultDiscounts[] = CRM_Price_BAO_PriceSet
::getSetDetail($optionGroupId);
141 //avoid moving up value of lable when some labels don't
142 //have a value ,fixed for CRM-3088
144 foreach ($defaultDiscounts as $val) {
145 $discountFields = current($val);
146 $discountFields = current($discountFields['fields']);
148 foreach ($discountFields['options'] as $discountFieldsval) {
149 $defaults['discounted_label'][$discountFieldsval['weight']] = $discountFieldsval['label'];
150 $defaults['discounted_value'][$discountFieldsval['weight']][$rowCount] =
151 CRM_Utils_Money
::format($discountFieldsval['amount'], NULL, '%a');
152 $defaults['discount_option_id'][$rowCount][$discountFieldsval['weight']] = $discountFieldsval['id'];
153 if (!empty($discountFieldsval['is_default'])) {
154 $defaults['discounted_default'] = $discountFieldsval['weight'];
160 ksort($defaults['discounted_value']);
161 ksort($defaults['discounted_label']);
163 foreach ($defaults['discounted_label'] as $key => $value) {
164 if ($key != $rowCount) {
165 $defaults['discounted_label'][$rowCount] = $value;
166 $defaults['discounted_value'][$rowCount] = $defaults['discounted_value'][$key];
167 unset($defaults['discounted_value'][$key]);
168 unset($defaults['discounted_label'][$key]);
169 foreach ($defaults['discount_option_id'] as &$optionIds) {
170 if (array_key_exists($key, $optionIds)) {
171 $optionIds[$rowCount] = $optionIds[$key];
172 unset($optionIds[$key]);
179 $this->set('discountSection', 1);
180 $this->buildQuickForm();
182 elseif (!empty($defaults['label'])) {
183 //if Regular Fees are present in DB and event fee page is in update mode
184 $defaults['discounted_label'] = $defaults['label'];
186 elseif (!empty($this->_submitValues
['label'])) {
187 //if event is newly created, use submitted values for
189 if (is_array($this->_submitValues
['label'])) {
191 foreach ($this->_submitValues
['label'] as $value) {
193 $defaults['discounted_label'][$k] = $value;
199 $defaults['id'] = $eventId;
200 if (!empty($totalLables)) {
201 $maxKey = count($totalLables) - 1;
202 if (isset($maxKey) && !empty($totalLables[$maxKey]['value'])) {
203 foreach ($totalLables[$maxKey]['value'] as $i => $v) {
204 if ($totalLables[$maxKey]['amount_id'][$i] == CRM_Utils_Array
::value('default_discount_fee_id', $defaults)) {
205 $defaults['discounted_default'] = $i;
212 if (!isset($defaults['discounted_default'])) {
213 $defaults['discounted_default'] = 1;
216 if (!isset($defaults['is_monetary'])) {
217 $defaults['is_monetary'] = 1;
220 if (!isset($defaults['fee_label'])) {
221 $defaults['fee_label'] = ts('Event Fee(s)');
224 if (!isset($defaults['pay_later_text']) ||
225 empty($defaults['pay_later_text'])
227 $defaults['pay_later_text'] = ts('I will send payment by check');
230 $this->_showHide
= new CRM_Core_ShowHideBlocks();
231 if (!$defaults['is_monetary']) {
232 $this->_showHide
->addHide('event-fees');
235 if (isset($defaults['price_set_id'])) {
236 $this->_showHide
->addHide('map-field');
238 $this->_showHide
->addToTemplate();
239 $this->assign('inDate', $this->_inDate
);
241 if (!empty($defaults['payment_processor'])) {
242 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
243 $defaults['payment_processor']
250 * Build the form object
254 public function buildQuickForm() {
256 $this->addYesNo('is_monetary',
260 array('onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);")
263 //add currency element.
264 $this->addCurrency('currency', ts('Currency'), FALSE);
266 $paymentProcessor = CRM_Core_PseudoConstant
::paymentProcessor();
268 $this->assign('paymentProcessor', $paymentProcessor);
270 $this->addCheckBox('payment_processor', ts('Payment Processor'),
271 array_flip($paymentProcessor),
272 NULL, NULL, NULL, NULL,
273 array(' ', ' ', ' ', '<br/>')
277 $this->addSelect('financial_type_id');
278 // add pay later options
279 $this->addElement('checkbox', 'is_pay_later', ts('Enable Pay Later option?'), NULL,
280 array('onclick' => "return showHideByValue('is_pay_later','','payLaterOptions','block','radio',false);")
282 $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'),
283 CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'),
286 $this->addWysiwyg('pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'));
288 $this->addElement('checkbox', 'is_billing_required', ts('Billing address required'));
289 $this->add('text', 'fee_label', ts('Fee Label'));
291 $price = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviEvent');
292 if (CRM_Utils_System
::isNull($price)) {
293 $this->assign('price', FALSE);
296 $this->assign('price', TRUE);
298 $this->add('select', 'price_set_id', ts('Price Set'),
300 '' => ts('- none -')) +
$price,
301 NULL, array('onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);")
303 $default = array($this->createElement('radio', NULL, NULL, NULL, 0));
304 $this->add('hidden', 'price_field_id', '', array('id' => 'price_field_id'));
305 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
307 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
308 $this->add('hidden', "price_field_value[$i]", '', array('id' => "price_field_value[$i]"));
311 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
312 $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 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
318 $this->addGroup($default, 'default');
320 $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL,
321 array('onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);")
323 $discountSection = $this->get('discountSection');
325 $this->assign('discountSection', $discountSection);
327 // form fields of Discount sets
328 $defaultOption = array();
329 $_showHide = new CRM_Core_ShowHideBlocks('', '');
331 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
332 //the show hide blocks
333 $showBlocks = 'discount_' . $i;
335 $_showHide->addHide($showBlocks);
338 $_showHide->addShow($showBlocks);
341 //Increment by 1 of start date of previous end date.
342 if (is_array($this->_submitValues
) &&
343 !empty($this->_submitValues
['discount_name'][$i]) &&
344 !empty($this->_submitValues
['discount_name'][$i +
1]) &&
345 isset($this->_submitValues
['discount_end_date']) &&
346 isset($this->_submitValues
['discount_end_date'][$i]) &&
347 $i < self
::NUM_DISCOUNT
- 1
349 $end_date = CRM_Utils_Date
::processDate($this->_submitValues
['discount_end_date'][$i]);
350 if (!empty($this->_submitValues
['discount_end_date'][$i +
1])
351 && empty($this->_submitValues
['discount_start_date'][$i +
1])
353 list($this->_submitValues
['discount_start_date'][$i +
1]) = CRM_Utils_Date
::setDateDefaults(date('Y-m-d', strtotime("+1 days $end_date")));
356 //Decrement by 1 of end date from next start date.
358 is_array($this->_submitValues
) &&
359 !empty($this->_submitValues
['discount_name'][$i]) &&
360 !empty($this->_submitValues
['discount_name'][$i - 1]) &&
361 isset($this->_submitValues
['discount_start_date']) &&
362 isset($this->_submitValues
['discount_start_date'][$i])
364 $start_date = CRM_Utils_Date
::processDate($this->_submitValues
['discount_start_date'][$i]);
365 if (!empty($this->_submitValues
['discount_start_date'][$i])
366 && empty($this->_submitValues
['discount_end_date'][$i - 1])
368 list($this->_submitValues
['discount_end_date'][$i - 1]) = CRM_Utils_Date
::setDateDefaults(date('Y-m-d', strtotime("-1 days $start_date")));
373 $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
374 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'title')
377 $this->add('hidden', "discount_price_set[$i]", '', array('id' => "discount_price_set[$i]"));
379 //discount start date
380 $this->addDate('discount_start_date[' . $i . ']', ts('Discount Start Date'), FALSE, array('formatType' => 'activityDate'));
383 $this->addDate('discount_end_date[' . $i . ']', ts('Discount End Date'), FALSE, array('formatType' => 'activityDate'));
385 $_showHide->addToTemplate();
386 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
387 array('class' => 'crm-form-submit cancel')
390 $this->buildAmountLabel();
391 parent
::buildQuickForm();
395 * Add local and global form rules
400 public function addRules() {
401 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Fee', 'formRule'));
405 * Global validation rules for the form
407 * @param array $values
408 * Posted values of the form.
410 * @return array list of errors to be posted back to the form
413 public static function formRule($values) {
415 if (!empty($values['is_discount'])) {
416 $occurDiscount = array_count_values($values['discount_name']);
418 $countemptyvalue = 0;
419 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
420 $start_date = $end_date = NULL;
421 if (!empty($values['discount_name'][$i])) {
422 if (!empty($values['discount_start_date'][$i])) {
423 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_start_date'][$i]) : 0;
425 if (!empty($values['discount_end_date'][$i])) {
426 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i]) : 0;
429 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
430 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
433 if (!$start_date && !$end_date) {
434 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
438 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i - 1]) : 0;
439 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
440 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
442 elseif (!$start_date && !$end_date_1) {
444 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
448 foreach ($occurDiscount as $key => $value) { if ($value > 1 && $key <> '') {
449 if ($key == $values['discount_name'][$i]) {
450 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', array(1 => $key));
455 //validation for discount labels and values
456 for ($index = (self
::NUM_OPTION
); $index > 0; $index--) {
458 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
461 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
464 if (!empty($values['discounted_label'][$index])) {
465 $duplicateIndex = CRM_Utils_Array
::key($values['discounted_label'][$index], $values['discounted_label']);
467 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
468 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
471 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
474 if (empty($values['discounted_value'][$index][$i])) {
478 if (!empty($values['_qf_Fee_next']) && ($countemptyrows == 11 ||
$countemptyvalue == 11)) {
479 $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.');
484 if ($values['is_monetary']) {
485 //check if financial type is selected
486 if (!$values['financial_type_id']) {
487 $errors['financial_type_id'] = ts("Please select financial type.");
490 //check for the event fee label (mandatory)
491 if (!$values['fee_label']) {
492 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
495 if (empty($values['price_set_id'])) {
496 //check fee label and amount
498 $optionKeys = array();
499 foreach ($values['label'] as $key => $val) {
500 if (trim($val) && trim($values['value'][$key])) {
501 $optionKeys[$key] = $key;
506 $default = CRM_Utils_Array
::value('default', $values);
507 if ($default && !in_array($default, $optionKeys)) {
508 $errors['default'] = ts('Please select an appropriate option as default.');
512 if (!$values['label'][1]) {
513 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
515 if (!$values['value'][1]) {
516 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
520 if (isset($values['is_pay_later'])) {
521 if (empty($values['pay_later_text'])) {
522 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
524 if (empty($values['pay_later_receipt'])) {
525 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
529 return empty($errors) ?
TRUE : $errors;
532 public function buildAmountLabel() {
534 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
536 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
538 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
539 $this->add('text', "discounted_value[$i][$j]", ts('Value'), array('size' => 10));
540 $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');
544 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
547 $this->addGroup($default, 'discounted_default');
555 public function postProcess() {
557 $params = $this->exportValues();
559 $this->set('discountSection', 0);
561 if (!empty($_POST['_qf_Fee_submit'])) {
562 $this->buildAmountLabel();
563 $this->set('discountSection', 2);
567 if (array_key_exists('payment_processor', $params) &&
568 !CRM_Utils_System
::isNull($params['payment_processor'])
570 $params['payment_processor'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($params['payment_processor']));
573 $params['payment_processor'] = 'null';
576 $params['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, 0);
577 $params['is_billing_required'] = CRM_Utils_Array
::value('is_billing_required', $params, 0);
581 // delete all the prior label values or discounts in the custom options table
582 // and delete a price set if one exists
583 //@todo note that this removes the reference from existing participants -
584 // even where there is not change - redress?
585 // note that a more tentative form of this is invoked by passing price_set_id as an array
586 // to event.create see CRM-14069
587 // @todo get all of this logic out of form layer (currently partially in BAO/api layer)
588 if (CRM_Price_BAO_PriceSet
::removeFrom('civicrm_event', $this->_id
)) {
589 CRM_Core_BAO_Discount
::del($this->_id
, 'civicrm_event');
593 if ($params['is_monetary']) {
594 if (!empty($params['price_set_id'])) {
595 //@todo this is now being done in the event BAO if passed price_set_id as an array
596 // per notes on that fn - looking at the api converting to an array
597 // so calling via the api may cause this to be done in the api
598 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $params['price_set_id']);
599 if (!empty($params['price_field_id'])) {
600 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
601 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID, 0);
605 // if there are label / values, create custom options for them
606 $labels = CRM_Utils_Array
::value('label', $params);
607 $values = CRM_Utils_Array
::value('value', $params);
608 $default = CRM_Utils_Array
::value('default', $params);
610 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
611 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
612 if (!empty($labels[$i]) && !CRM_Utils_System
::isNull($values[$i])) {
614 'label' => trim($labels[$i]),
615 'value' => CRM_Utils_Rule
::cleanMoney(trim($values[$i])),
618 'is_default' => $default == $i
622 if (!empty($options)) {
623 $params['default_fee_id'] = NULL;
624 if (empty($params['price_set_id'])) {
625 if (empty($params['price_field_id'])) {
626 $setParams['title'] = $eventTitle = ($this->_isTemplate
) ?
$this->_defaultValues
['template_title'] : $this->_defaultValues
['title'];
627 $eventTitle = strtolower(CRM_Utils_String
::munge($eventTitle, '_', 245));
628 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle, 'id', 'name')) {
629 $setParams['name'] = $eventTitle;
631 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $this->_id
, 'id', 'name')) {
632 $setParams['name'] = $eventTitle . '_' . $this->_id
;
635 $timeSec = explode('.', microtime(TRUE));
636 $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
638 $setParams['is_quick_config'] = 1;
639 $setParams['financial_type_id'] = $params['financial_type_id'];
640 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
641 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
643 $fieldParams['name'] = strtolower(CRM_Utils_String
::munge($params['fee_label'], '_', 245));
644 $fieldParams['price_set_id'] = $priceSet->id
;
647 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
648 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
649 CRM_Price_BAO_PriceFieldValue
::setIsActive($fieldValueID, '0');
650 unset($params['price_field_value'][$arrayID]);
653 $fieldParams['id'] = CRM_Utils_Array
::value('price_field_id', $params);
654 $fieldParams['option_id'] = $params['price_field_value'];
656 $priceSet = new CRM_Price_BAO_PriceSet();
657 $priceSet->id
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $params), 'price_set_id');
659 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
660 CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceSet', $priceSet->id
, 'financial_type_id', $params['financial_type_id']);
663 $fieldParams['label'] = $params['fee_label'];
664 $fieldParams['html_type'] = 'Radio';
665 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $priceSet->id
);
666 $fieldParams['option_label'] = $params['label'];
667 $fieldParams['option_amount'] = $params['value'];
668 $fieldParams['financial_type_id'] = $params['financial_type_id'];
669 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);
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);
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]))
693 $discountOptions[] = array(
694 'label' => trim($labels[$i]),
695 'value' => CRM_Utils_Rule
::cleanMoney(trim($values[$i][$j])),
698 'is_default' => $default == $i
703 if (!empty($discountOptions)) {
704 $fieldParams = array();
705 $params['default_discount_fee_id'] = NULL;
707 $setParams = array();
708 if (empty($discountPriceSets[$keyCheck])) {
710 $eventTitle = strtolower(CRM_Utils_String
::munge($this->_defaultValues
['title'], '_', 200));
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];
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
;
720 $timeSec = explode('.', microtime(TRUE));
721 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
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
;
730 $priceSetID = $discountPriceSets[$j - 1];
732 'title' => $params['discount_name'][$j],
735 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
736 $setParams['financial_type_id'] = $params['financial_type_id'];
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');
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'];
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']]);
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');
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]),
775 CRM_Core_BAO_Discount
::add($discountParams);
780 if (!empty($discountPriceSets)) {
781 foreach ($discountPriceSets as $setId) {
782 CRM_Price_BAO_PriceSet
::setIsQuickConfig($setId, 0);
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);
792 $params['financial_type_id'] = '';
793 $params['is_pay_later'] = 0;
794 $params['is_billing_required'] = 0;
797 //update 'is_billing_required'
798 if (empty($params['is_pay_later'])) {
799 $params['is_billing_required'] = FALSE;
802 //update events table
803 $params['id'] = $this->_id
;
804 // skip update of financial type in price set
805 $params['skipFinancialType'] = TRUE;
806 CRM_Event_BAO_Event
::add($params);
808 // Update tab "disabled" css class
809 $this->ajaxResponse
['tabValid'] = !empty($params['is_monetary']);
810 parent
::endPostProcess();
814 * Return a descriptive name for the page, used in wizard header
818 public function getTitle() {
819 return ts('Event Fees');