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