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