3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2018
34 * This class generates form components for Event Fees.
36 class CRM_Event_Form_ManageEvent_Fee
extends CRM_Event_Form_ManageEvent
{
39 * Constants for number of options for data types of multiple option.
41 const NUM_OPTION
= 11;
44 * Constants for number of discounts for the event.
46 const NUM_DISCOUNT
= 6;
59 * Set variables up before form is built.
61 public function preProcess() {
66 * Set default values for the form.
68 * For edit/view mode the default values are retrieved from the database.
70 public function setDefaultValues() {
71 $parentDefaults = parent
::setDefaultValues();
73 $eventId = $this->_id
;
76 if (isset($eventId)) {
77 $params = array('id' => $eventId);
80 CRM_Event_BAO_Event
::retrieve($params, $defaults);
82 if (isset($eventId)) {
83 $price_set_id = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL, 1);
86 $defaults['price_set_id'] = $price_set_id;
89 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $eventId, NULL);
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');
95 $priceFieldOptions = CRM_Price_BAO_PriceFieldValue
::getValues($priceField, $options, 'weight', TRUE);
96 $defaults['price_field_id'] = $priceField;
98 foreach ($options as $optionId => $optionValue) {
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;
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 $name = $defaults["discount_name[$i]"] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $optionGroupId, 'title');
123 list($defaults["discount_start_date[$i]"]) = CRM_Utils_Date
::setDateDefaults(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
124 'start_date', 'price_set_id'
126 list($defaults["discount_end_date[$i]"]) = CRM_Utils_Date
::setDateDefaults(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
127 'end_date', 'price_set_id'
129 $defaultDiscounts[] = CRM_Price_BAO_PriceSet
::getSetDetail($optionGroupId);
133 //avoid moving up value of lable when some labels don't
134 //have a value ,fixed for CRM-3088
136 foreach ($defaultDiscounts as $val) {
137 $discountFields = current($val);
138 $discountFields = current($discountFields['fields']);
140 foreach ($discountFields['options'] as $discountFieldsval) {
141 $defaults['discounted_label'][$discountFieldsval['weight']] = $discountFieldsval['label'];
142 $defaults['discounted_value'][$discountFieldsval['weight']][$rowCount] = CRM_Utils_Money
::format($discountFieldsval['amount'], NULL, '%a');
143 $defaults['discount_option_id'][$rowCount][$discountFieldsval['weight']] = $discountFieldsval['id'];
144 if (!empty($discountFieldsval['is_default'])) {
145 $defaults['discounted_default'] = $discountFieldsval['weight'];
151 ksort($defaults['discounted_value']);
152 ksort($defaults['discounted_label']);
154 foreach ($defaults['discounted_label'] as $key => $value) {
155 if ($key != $rowCount) {
156 $defaults['discounted_label'][$rowCount] = $value;
157 $defaults['discounted_value'][$rowCount] = $defaults['discounted_value'][$key];
158 unset($defaults['discounted_value'][$key]);
159 unset($defaults['discounted_label'][$key]);
160 foreach ($defaults['discount_option_id'] as &$optionIds) {
161 if (array_key_exists($key, $optionIds)) {
162 $optionIds[$rowCount] = $optionIds[$key];
163 unset($optionIds[$key]);
170 $this->set('discountSection', 1);
171 $this->buildQuickForm();
173 elseif (!empty($defaults['label'])) {
174 //if Regular Fees are present in DB and event fee page is in update mode
175 $defaults['discounted_label'] = $defaults['label'];
177 elseif (!empty($this->_submitValues
['label'])) {
178 //if event is newly created, use submitted values for
180 if (is_array($this->_submitValues
['label'])) {
182 foreach ($this->_submitValues
['label'] as $value) {
184 $defaults['discounted_label'][$k] = $value;
190 $defaults['id'] = $eventId;
191 if (!empty($totalLables)) {
192 $maxKey = count($totalLables) - 1;
193 if (isset($maxKey) && !empty($totalLables[$maxKey]['value'])) {
194 foreach ($totalLables[$maxKey]['value'] as $i => $v) {
195 if ($totalLables[$maxKey]['amount_id'][$i] == CRM_Utils_Array
::value('default_discount_fee_id', $defaults)) {
196 $defaults['discounted_default'] = $i;
203 if (!isset($defaults['discounted_default'])) {
204 $defaults['discounted_default'] = 1;
207 if (!isset($defaults['is_monetary'])) {
208 $defaults['is_monetary'] = 1;
211 if (!isset($defaults['fee_label'])) {
212 $defaults['fee_label'] = ts('Event Fee(s)');
215 if (!isset($defaults['pay_later_text']) ||
216 empty($defaults['pay_later_text'])
218 $defaults['pay_later_text'] = ts('I will send payment by check');
221 $this->_showHide
= new CRM_Core_ShowHideBlocks();
222 if (!$defaults['is_monetary']) {
223 $this->_showHide
->addHide('event-fees');
226 if (isset($defaults['price_set_id'])) {
227 $this->_showHide
->addHide('map-field');
229 $this->_showHide
->addToTemplate();
230 $this->assign('inDate', $this->_inDate
);
231 if (!empty($defaults['payment_processor'])) {
232 $defaults['payment_processor'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
233 $defaults['payment_processor']
241 * Build the form object.
243 public function buildQuickForm() {
245 $this->addYesNo('is_monetary',
249 array('onclick' => "return showHideByValue('is_monetary','0','event-fees','block','radio',false);")
252 //add currency element.
253 $this->addCurrency('currency', ts('Currency'), FALSE);
255 $paymentProcessor = CRM_Core_PseudoConstant
::paymentProcessor();
257 $this->assign('paymentProcessor', $paymentProcessor);
258 $this->addCheckBox('payment_processor', ts('Payment Processor'),
259 array_flip($paymentProcessor),
260 NULL, NULL, NULL, NULL,
261 array(' ', ' ', ' ', '<br/>')
265 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() ||
266 (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() && CRM_Core_Permission
::check('administer CiviCRM Financial Types'))) {
267 $this->addSelect('financial_type_id');
270 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::ADD
);
271 $this->addSelect('financial_type_id', array('context' => 'search', 'options' => $financialTypes));
273 // add pay later options
274 $this->addElement('checkbox', 'is_pay_later', ts('Enable Pay Later option?'), NULL,
275 array('onclick' => "return showHideByValue('is_pay_later','','payLaterOptions','block','radio',false);")
277 $this->addElement('textarea', 'pay_later_text', ts('Pay Later Label'),
278 CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_text'),
281 $this->add('wysiwyg', 'pay_later_receipt', ts('Pay Later Instructions'), CRM_Core_DAO
::getAttribute('CRM_Event_DAO_Event', 'pay_later_receipt'));
283 $this->addElement('checkbox', 'is_billing_required', ts('Billing address required'));
284 $this->add('text', 'fee_label', ts('Fee Label'));
286 $price = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviEvent');
287 if (CRM_Utils_System
::isNull($price)) {
288 $this->assign('price', FALSE);
291 $this->assign('price', TRUE);
293 $this->add('select', 'price_set_id', ts('Price Set'),
295 '' => ts('- none -'),
297 NULL, array('onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);")
299 $default = array($this->createElement('radio', NULL, NULL, NULL, 0));
300 $this->add('hidden', 'price_field_id', '', array('id' => 'price_field_id'));
301 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
303 $this->add('text', "label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
304 $this->add('hidden', "price_field_value[$i]", '', array('id' => "price_field_value[$i]"));
307 $this->add('text', "value[$i]", ts('Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'));
308 $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');
311 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
314 $this->addGroup($default, 'default');
316 $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL,
317 array('onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);")
319 $discountSection = $this->get('discountSection');
321 $this->assign('discountSection', $discountSection);
323 // form fields of Discount sets
324 $defaultOption = array();
325 $_showHide = new CRM_Core_ShowHideBlocks('', '');
327 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
328 //the show hide blocks
329 $showBlocks = 'discount_' . $i;
331 $_showHide->addHide($showBlocks);
334 $_showHide->addShow($showBlocks);
337 //Increment by 1 of start date of previous end date.
338 if (is_array($this->_submitValues
) &&
339 !empty($this->_submitValues
['discount_name'][$i]) &&
340 !empty($this->_submitValues
['discount_name'][$i +
1]) &&
341 isset($this->_submitValues
['discount_end_date']) &&
342 isset($this->_submitValues
['discount_end_date'][$i]) &&
343 $i < self
::NUM_DISCOUNT
- 1
345 $end_date = CRM_Utils_Date
::processDate($this->_submitValues
['discount_end_date'][$i]);
346 if (!empty($this->_submitValues
['discount_end_date'][$i +
1])
347 && empty($this->_submitValues
['discount_start_date'][$i +
1])
349 list($this->_submitValues
['discount_start_date'][$i +
1]) = CRM_Utils_Date
::setDateDefaults(date('Y-m-d', strtotime("+1 days $end_date")));
352 //Decrement by 1 of end date from next start date.
354 is_array($this->_submitValues
) &&
355 !empty($this->_submitValues
['discount_name'][$i]) &&
356 !empty($this->_submitValues
['discount_name'][$i - 1]) &&
357 isset($this->_submitValues
['discount_start_date']) &&
358 isset($this->_submitValues
['discount_start_date'][$i])
360 $start_date = CRM_Utils_Date
::processDate($this->_submitValues
['discount_start_date'][$i]);
361 if (!empty($this->_submitValues
['discount_start_date'][$i])
362 && empty($this->_submitValues
['discount_end_date'][$i - 1])
364 list($this->_submitValues
['discount_end_date'][$i - 1]) = CRM_Utils_Date
::setDateDefaults(date('Y-m-d', strtotime("-1 days $start_date")));
369 $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
370 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'title')
373 $this->add('hidden', "discount_price_set[$i]", '', array('id' => "discount_price_set[$i]"));
375 //discount start date
376 $this->addDate('discount_start_date[' . $i . ']', ts('Discount Start Date'), FALSE, array('formatType' => 'activityDate'));
379 $this->addDate('discount_end_date[' . $i . ']', ts('Discount End Date'), FALSE, array('formatType' => 'activityDate'));
381 $_showHide->addToTemplate();
382 $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
383 array('class' => 'crm-form-submit cancel')
385 if (CRM_Contribute_BAO_Contribution
::checkContributeSettings('deferred_revenue_enabled')) {
386 $deferredFinancialType = CRM_Financial_BAO_FinancialAccount
::getDeferredFinancialType();
387 $this->assign('deferredFinancialType', array_keys($deferredFinancialType));
389 $this->buildAmountLabel();
390 parent
::buildQuickForm();
394 * Add local and global form rules.
396 public function addRules() {
397 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Fee', 'formRule'));
401 * Global validation rules for the form.
403 * @param array $values
404 * Posted values of the form.
407 * list of errors to be posted back to the form
409 public static function formRule($values) {
411 if (!empty($values['is_discount'])) {
412 $occurDiscount = array_count_values($values['discount_name']);
414 $countemptyvalue = 0;
415 for ($i = 1; $i <= self
::NUM_DISCOUNT
; $i++
) {
416 $start_date = $end_date = NULL;
417 if (!empty($values['discount_name'][$i])) {
418 if (!empty($values['discount_start_date'][$i])) {
419 $start_date = ($values['discount_start_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_start_date'][$i]) : 0;
421 if (!empty($values['discount_end_date'][$i])) {
422 $end_date = ($values['discount_end_date'][$i]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i]) : 0;
425 if ($start_date && $end_date && strcmp($end_date, $start_date) < 0) {
426 $errors["discount_end_date[$i]"] = ts('The discount end date cannot be prior to the start date.');
429 if (!$start_date && !$end_date) {
430 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$i]"] = ts('Please specify either start date or end date.');
434 $end_date_1 = ($values['discount_end_date'][$i - 1]) ? CRM_Utils_Date
::processDate($values['discount_end_date'][$i - 1]) : 0;
435 if ($start_date && $end_date_1 && strcmp($end_date_1, $start_date) >= 0) {
436 $errors["discount_start_date[$i]"] = ts('Select non-overlapping discount start date.');
438 elseif (!$start_date && !$end_date_1) {
440 $errors["discount_start_date[$i]"] = $errors["discount_end_date[$j]"] = ts('Select either of the dates.');
444 foreach ($occurDiscount as $key => $value) {
445 if ($value > 1 && $key <> '') {
446 if ($key == $values['discount_name'][$i]) {
447 $errors['discount_name[' . $i . ']'] = ts('%1 is already used for Discount Name.', array(1 => $key));
452 //validation for discount labels and values
453 for ($index = (self
::NUM_OPTION
); $index > 0; $index--) {
455 if (empty($values['discounted_label'][$index]) && !empty($values['discounted_value'][$index][$i])) {
458 $errors["discounted_label[{$index}]"] = ts('Label cannot be empty.');
461 if (!empty($values['discounted_label'][$index])) {
462 $duplicateIndex = CRM_Utils_Array
::key($values['discounted_label'][$index], $values['discounted_label']);
464 if ((!($duplicateIndex === FALSE)) && (!($duplicateIndex == $index))) {
465 $errors["discounted_label[{$index}]"] = ts('Duplicate label value');
468 if (empty($values['discounted_label'][$index]) && empty($values['discounted_value'][$index][$i])) {
471 if (empty($values['discounted_value'][$index][$i])) {
475 if (!empty($values['_qf_Fee_next']) && ($countemptyrows == 11 ||
$countemptyvalue == 11)) {
476 $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.');
481 if ($values['is_monetary']) {
482 //check if financial type is selected
483 if (!$values['financial_type_id']) {
484 $errors['financial_type_id'] = ts("Please select financial type.");
487 //check for the event fee label (mandatory)
488 if (!$values['fee_label']) {
489 $errors['fee_label'] = ts('Please enter the fee label for the paid event.');
492 if (empty($values['price_set_id'])) {
493 //check fee label and amount
495 $optionKeys = array();
496 foreach ($values['label'] as $key => $val) {
497 if (trim($val) && trim($values['value'][$key])) {
498 $optionKeys[$key] = $key;
503 $default = CRM_Utils_Array
::value('default', $values);
504 if ($default && !in_array($default, $optionKeys)) {
505 $errors['default'] = ts('Please select an appropriate option as default.');
509 if (!$values['label'][1]) {
510 $errors['label[1]'] = ts('Please enter a label for at least one fee level.');
512 if (!$values['value'][1]) {
513 $errors['value[1]'] = ts('Please enter an amount for at least one fee level.');
517 if (isset($values['is_pay_later'])) {
518 if (empty($values['pay_later_text'])) {
519 $errors['pay_later_text'] = ts('Please enter the Pay Later prompt to be displayed on the Registration form.');
521 if (empty($values['pay_later_receipt'])) {
522 $errors['pay_later_receipt'] = ts('Please enter the Pay Later instructions to be displayed to your users.');
526 return empty($errors) ?
TRUE : $errors;
529 public function buildAmountLabel() {
531 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
533 $this->add('text', "discounted_label[$i]", ts('Label'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'));
535 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
536 $this->add('text', "discounted_value[$i][$j]", ts('Value'), array('size' => 10));
537 $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');
541 $default[] = $this->createElement('radio', NULL, NULL, NULL, $i);
544 $this->addGroup($default, 'discounted_default');
550 public function postProcess() {
552 $params = $this->exportValues();
554 $this->set('discountSection', 0);
556 if (!empty($_POST['_qf_Fee_submit'])) {
557 $this->buildAmountLabel();
558 $this->set('discountSection', 2);
562 if (!empty($params['payment_processor'])) {
563 $params['payment_processor'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($params['payment_processor']));
566 $params['payment_processor'] = 'null';
569 $params['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, 0);
570 $params['is_billing_required'] = CRM_Utils_Array
::value('is_billing_required', $params, 0);
574 // delete all the prior label values or discounts in the custom options table
575 // and delete a price set if one exists
576 //@todo note that this removes the reference from existing participants -
577 // even where there is not change - redress?
578 // note that a more tentative form of this is invoked by passing price_set_id as an array
579 // to event.create see CRM-14069
580 // @todo get all of this logic out of form layer (currently partially in BAO/api layer)
581 if (CRM_Price_BAO_PriceSet
::removeFrom('civicrm_event', $this->_id
)) {
582 CRM_Core_BAO_Discount
::del($this->_id
, 'civicrm_event');
586 if ($params['is_monetary']) {
587 if (!empty($params['price_set_id'])) {
588 //@todo this is now being done in the event BAO if passed price_set_id as an array
589 // per notes on that fn - looking at the api converting to an array
590 // so calling via the api may cause this to be done in the api
591 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $params['price_set_id']);
592 if (!empty($params['price_field_id'])) {
593 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
594 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID, 0);
598 // if there are label / values, create custom options for them
599 $labels = CRM_Utils_Array
::value('label', $params);
600 $values = CRM_Utils_Array
::value('value', $params);
601 $default = CRM_Utils_Array
::value('default', $params);
603 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
604 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
605 if (!empty($labels[$i]) && !CRM_Utils_System
::isNull($values[$i])) {
607 'label' => trim($labels[$i]),
608 'value' => CRM_Utils_Rule
::cleanMoney(trim($values[$i])),
611 'is_default' => $default == $i,
615 if (!empty($options)) {
616 $params['default_fee_id'] = NULL;
617 if (empty($params['price_set_id'])) {
618 if (empty($params['price_field_id'])) {
619 $setParams['title'] = $eventTitle = ($this->_isTemplate
) ?
$this->_defaultValues
['template_title'] : $this->_defaultValues
['title'];
620 $eventTitle = strtolower(CRM_Utils_String
::munge($eventTitle, '_', 245));
621 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle, 'id', 'name')) {
622 $setParams['name'] = $eventTitle;
624 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $this->_id
, 'id', 'name')) {
625 $setParams['name'] = $eventTitle . '_' . $this->_id
;
628 $timeSec = explode('.', microtime(TRUE));
629 $setParams['name'] = $eventTitle . '_' . date('is', $timeSec[0]) . $timeSec[1];
631 $setParams['is_quick_config'] = 1;
632 $setParams['financial_type_id'] = $params['financial_type_id'];
633 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
634 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
636 $fieldParams['name'] = strtolower(CRM_Utils_String
::munge($params['fee_label'], '_', 245));
637 $fieldParams['price_set_id'] = $priceSet->id
;
640 foreach ($params['price_field_value'] as $arrayID => $fieldValueID) {
641 if (empty($params['label'][$arrayID]) && empty($params['value'][$arrayID]) && !empty($fieldValueID)) {
642 CRM_Price_BAO_PriceFieldValue
::setIsActive($fieldValueID, '0');
643 unset($params['price_field_value'][$arrayID]);
646 $fieldParams['id'] = CRM_Utils_Array
::value('price_field_id', $params);
647 $fieldParams['option_id'] = $params['price_field_value'];
649 $priceSet = new CRM_Price_BAO_PriceSet();
650 $priceSet->id
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $params), 'price_set_id');
652 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
653 CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceSet', $priceSet->id
, 'financial_type_id', $params['financial_type_id']);
656 $fieldParams['label'] = $params['fee_label'];
657 $fieldParams['html_type'] = 'Radio';
658 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_id
, $priceSet->id
);
659 $fieldParams['option_label'] = $params['label'];
660 $fieldParams['option_amount'] = $params['value'];
661 $fieldParams['financial_type_id'] = $params['financial_type_id'];
662 foreach ($options as $value) {
663 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
665 $fieldParams['default_option'] = $params['default'];
666 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
671 $discountPriceSets = !empty($this->_defaultValues
['discount_price_set']) ?
$this->_defaultValues
['discount_price_set'] : array();
672 $discountFieldIDs = !empty($this->_defaultValues
['discount_option_id']) ?
$this->_defaultValues
['discount_option_id'] : array();
673 if (CRM_Utils_Array
::value('is_discount', $params) == 1) {
674 // if there are discounted set of label / values,
675 // create custom options for them
676 $labels = CRM_Utils_Array
::value('discounted_label', $params);
677 $values = CRM_Utils_Array
::value('discounted_value', $params);
678 $default = CRM_Utils_Array
::value('discounted_default', $params);
680 if (!CRM_Utils_System
::isNull($labels) && !CRM_Utils_System
::isNull($values)) {
681 for ($j = 1; $j <= self
::NUM_DISCOUNT
; $j++
) {
682 $discountOptions = array();
683 for ($i = 1; $i < self
::NUM_OPTION
; $i++
) {
684 if (!empty($labels[$i]) &&
685 !CRM_Utils_System
::isNull(CRM_Utils_Array
::value($j, $values[$i]))
687 $discountOptions[] = array(
688 'label' => trim($labels[$i]),
689 'value' => CRM_Utils_Rule
::cleanMoney(trim($values[$i][$j])),
692 'is_default' => $default == $i,
697 if (!empty($discountOptions)) {
698 $fieldParams = array();
699 $params['default_discount_fee_id'] = NULL;
701 $setParams = array();
702 if (empty($discountPriceSets[$keyCheck])) {
704 $eventTitle = strtolower(CRM_Utils_String
::munge($this->_defaultValues
['title'], '_', 200));
706 $setParams['title'] = $params['discount_name'][$j];
707 if (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j], 'id', 'name')) {
708 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j];
710 elseif (!CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceSet', $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
, 'id', 'name')) {
711 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . $this->_id
;
714 $timeSec = explode('.', microtime(TRUE));
715 $setParams['name'] = $eventTitle . '_' . $params['discount_name'][$j] . '_' . date('is', $timeSec[0]) . $timeSec[1];
717 $setParams['is_quick_config'] = 1;
718 $setParams['financial_type_id'] = $params['financial_type_id'];
719 $setParams['extends'] = CRM_Core_Component
::getComponentID('CiviEvent');
720 $priceSet = CRM_Price_BAO_PriceSet
::create($setParams);
721 $priceSetID = $priceSet->id
;
724 $priceSetID = $discountPriceSets[$j - 1];
726 'title' => $params['discount_name'][$j],
729 if ($this->_defaultValues
['financial_type_id'] != $params['financial_type_id']) {
730 $setParams['financial_type_id'] = $params['financial_type_id'];
732 CRM_Price_BAO_PriceSet
::create($setParams);
733 unset($discountPriceSets[$j - 1]);
734 $fieldParams['id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceField', $priceSetID, 'id', 'price_set_id');
737 $fieldParams['name'] = $fieldParams['label'] = $params['fee_label'];
738 $fieldParams['is_required'] = 1;
739 $fieldParams['price_set_id'] = $priceSetID;
740 $fieldParams['html_type'] = 'Radio';
741 $fieldParams['financial_type_id'] = $params['financial_type_id'];
742 foreach ($discountOptions as $value) {
743 $fieldParams['option_label'][$value['weight']] = $value['label'];
744 $fieldParams['option_amount'][$value['weight']] = $value['value'];
745 $fieldParams['option_weight'][$value['weight']] = $value['weight'];
746 if (!empty($value['is_default'])) {
747 $fieldParams['default_option'] = $value['weight'];
749 if (!empty($discountFieldIDs[$j]) && !empty($discountFieldIDs[$j][$value['weight']])) {
750 $fieldParams['option_id'][$value['weight']] = $discountFieldIDs[$j][$value['weight']];
751 unset($discountFieldIDs[$j][$value['weight']]);
754 //create discount priceset
755 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
756 if (!empty($discountFieldIDs[$j])) {
757 foreach ($discountFieldIDs[$j] as $fID) {
758 CRM_Price_BAO_PriceFieldValue
::setIsActive($fID, '0');
762 $discountParams = array(
763 'entity_table' => 'civicrm_event',
764 'entity_id' => $this->_id
,
765 'price_set_id' => $priceSetID,
766 'start_date' => CRM_Utils_Date
::processDate($params['discount_start_date'][$j]),
767 'end_date' => CRM_Utils_Date
::processDate($params['discount_end_date'][$j]),
769 CRM_Core_BAO_Discount
::add($discountParams);
774 if (!empty($discountPriceSets)) {
775 foreach ($discountPriceSets as $setId) {
776 CRM_Price_BAO_PriceSet
::setIsQuickConfig($setId, 0);
782 if (!empty($params['price_field_id'])) {
783 $priceSetID = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $params['price_field_id'], 'price_set_id');
784 CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetID, 0);
786 $params['financial_type_id'] = '';
787 $params['is_pay_later'] = 0;
788 $params['is_billing_required'] = 0;
791 //update 'is_billing_required'
792 if (empty($params['is_pay_later'])) {
793 $params['is_billing_required'] = FALSE;
796 //update events table
797 $params['id'] = $this->_id
;
798 // skip update of financial type in price set
799 $params['skipFinancialType'] = TRUE;
800 CRM_Event_BAO_Event
::add($params);
802 // Update tab "disabled" css class
803 $this->ajaxResponse
['tabValid'] = !empty($params['is_monetary']);
804 parent
::endPostProcess();
808 * Return a descriptive name for the page, used in wizard header
812 public function getTitle() {
813 return ts('Event Fees');