Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * This class generates form components for processing Event | |
39 | * | |
40 | */ | |
41 | class CRM_Event_Form_Registration extends CRM_Core_Form { | |
42 | ||
43 | /** | |
44 | * how many locationBlocks should we display? | |
45 | * | |
46 | * @var int | |
47 | * @const | |
48 | */ | |
49 | CONST LOCATION_BLOCKS = 1; | |
50 | ||
51 | /** | |
52 | * the id of the event we are proceessing | |
53 | * | |
54 | * @var int | |
55 | * @protected | |
56 | */ | |
57 | public $_eventId; | |
58 | ||
59 | /** | |
60 | * the array of ids of all the participant we are proceessing | |
61 | * | |
62 | * @var int | |
63 | * @protected | |
64 | */ | |
65 | protected $_participantIDS = NULL; | |
66 | ||
67 | /** | |
68 | * the id of the participant we are proceessing | |
69 | * | |
70 | * @var int | |
71 | * @protected | |
72 | */ | |
73 | protected $_participantId; | |
74 | ||
75 | /** | |
76 | * is participant able to walk registration wizard. | |
77 | * | |
78 | * @var Boolean | |
79 | * @protected | |
80 | */ | |
81 | public $_allowConfirmation; | |
82 | ||
83 | /** | |
84 | * is participant requires approval | |
85 | * | |
86 | * @var Boolean | |
87 | * @public | |
88 | */ | |
89 | public $_requireApproval; | |
90 | ||
91 | /** | |
92 | * is event configured for waitlist. | |
93 | * | |
94 | * @var Boolean | |
95 | * @public | |
96 | */ | |
97 | public $_allowWaitlist; | |
98 | ||
99 | /** | |
100 | * store additional participant ids | |
101 | * when there are pre-registered. | |
102 | * | |
103 | * @var array | |
104 | * @public | |
105 | */ | |
106 | public $_additionalParticipantIds; | |
107 | ||
108 | /** | |
109 | * the mode that we are in | |
110 | * | |
111 | * @var string | |
112 | * @protect | |
113 | */ | |
114 | public $_mode; | |
115 | ||
116 | /** | |
117 | * the values for the contribution db object | |
118 | * | |
119 | * @var array | |
120 | * @protected | |
121 | */ | |
122 | public $_values; | |
123 | ||
124 | /** | |
125 | * the paymentProcessor attributes for this page | |
126 | * | |
127 | * @var array | |
128 | * @protected | |
129 | */ | |
130 | public $_paymentProcessor; | |
131 | ||
132 | /** | |
133 | * The params submitted by the form and computed by the app | |
134 | * | |
135 | * @var array | |
136 | * @protected | |
137 | */ | |
138 | protected $_params; | |
139 | ||
140 | /** | |
141 | * The fields involved in this contribution page | |
142 | * | |
143 | * @var array | |
144 | * @protected | |
145 | */ | |
146 | public $_fields; | |
147 | ||
148 | /** | |
149 | * The billing location id for this contribiution page | |
150 | * | |
151 | * @var int | |
152 | * @protected | |
153 | */ | |
154 | public $_bltID; | |
155 | ||
156 | /** | |
157 | * Price Set ID, if the new price set method is used | |
158 | * | |
159 | * @var int | |
160 | * @protected | |
161 | */ | |
162 | public $_priceSetId = NULL; | |
163 | ||
164 | /** | |
165 | * Array of fields for the price set | |
166 | * | |
167 | * @var array | |
168 | * @protected | |
169 | */ | |
170 | public $_priceSet; | |
171 | ||
172 | public $_action; | |
173 | ||
174 | public $_pcpId; | |
175 | ||
176 | /* Is event already full. | |
177 | * | |
178 | * @var boolean | |
179 | * @protected | |
180 | */ | |
181 | ||
182 | public $_isEventFull; | |
183 | ||
184 | public $_lineItem; | |
185 | public $_lineItemParticipantsCount; | |
186 | public $_availableRegistrations; | |
187 | ||
188 | public $_forcePayement; | |
189 | ||
190 | /** | |
191 | * Function to set variables up before form is built | |
192 | * | |
193 | * @return void | |
194 | * @access public | |
195 | */ | |
196 | function preProcess() { | |
197 | $this->_eventId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); | |
198 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE); | |
199 | ||
200 | //CRM-4320 | |
201 | $this->_participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this); | |
202 | ||
203 | // current mode | |
204 | $this->_mode = ($this->_action == 1024) ? 'test' : 'live'; | |
205 | ||
206 | $this->_values = $this->get('values'); | |
207 | $this->_fields = $this->get('fields'); | |
208 | $this->_bltID = $this->get('bltID'); | |
209 | $this->_paymentProcessor = $this->get('paymentProcessor'); | |
210 | $this->_priceSetId = $this->get('priceSetId'); | |
211 | $this->_priceSet = $this->get('priceSet'); | |
212 | $this->_lineItem = $this->get('lineItem'); | |
213 | $this->_isEventFull = $this->get('isEventFull'); | |
214 | $this->_lineItemParticipantsCount = $this->get('lineItemParticipants'); | |
215 | if (!is_array($this->_lineItem)) { | |
216 | $this->_lineItem = array(); | |
217 | } | |
218 | if (!is_array($this->_lineItemParticipantsCount)) { | |
219 | $this->_lineItemParticipantsCount = array(); | |
220 | } | |
221 | $this->_availableRegistrations = $this->get('availableRegistrations'); | |
6a488035 TO |
222 | $this->_participantIDS = $this->get('participantIDs'); |
223 | ||
224 | //check if participant allow to walk registration wizard. | |
225 | $this->_allowConfirmation = $this->get('allowConfirmation'); | |
226 | ||
227 | // check for Approval | |
228 | $this->_requireApproval = $this->get('requireApproval'); | |
229 | ||
230 | // check for waitlisting. | |
231 | $this->_allowWaitlist = $this->get('allowWaitlist'); | |
232 | ||
233 | $this->_forcePayement = $this->get('forcePayement'); | |
234 | ||
235 | //get the additional participant ids. | |
236 | $this->_additionalParticipantIds = $this->get('additionalParticipantIds'); | |
6a488035 TO |
237 | $config = CRM_Core_Config::singleton(); |
238 | ||
239 | if (!$this->_values) { | |
240 | // create redirect URL to send folks back to event info page is registration not available | |
96f7c53e | 241 | $infoUrl = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_eventId}", |
6a488035 TO |
242 | FALSE, NULL, FALSE, TRUE |
243 | ); | |
244 | ||
245 | // this is the first time we are hitting this, so check for permissions here | |
e2d09ab4 | 246 | if (!CRM_Core_Permission::event(CRM_Core_Permission::EDIT, $this->_eventId, 'register for events')) { |
6a488035 TO |
247 | CRM_Core_Error::statusBounce(ts('You do not have permission to register for this event'), $infoUrl); |
248 | } | |
249 | ||
250 | // get all the values from the dao object | |
96f7c53e | 251 | $this->_values = $this->_fields = array(); |
6a488035 TO |
252 | $this->_forcePayement = FALSE; |
253 | ||
254 | //retrieve event information | |
255 | $params = array('id' => $this->_eventId); | |
256 | CRM_Event_BAO_Event::retrieve($params, $this->_values['event']); | |
257 | ||
258 | $this->checkValidEvent($infoUrl); | |
6a488035 TO |
259 | // get the participant values, CRM-4320 |
260 | $this->_allowConfirmation = FALSE; | |
261 | if ($this->_participantId) { | |
262 | $this->processFirstParticipant($this->_participantId); | |
263 | } | |
6a488035 TO |
264 | //check for additional participants. |
265 | if ($this->_allowConfirmation && $this->_values['event']['is_multiple_registrations']) { | |
266 | $additionalParticipantIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_participantId); | |
267 | $cnt = 1; | |
268 | foreach ($additionalParticipantIds as $additionalParticipantId) { | |
269 | $this->_additionalParticipantIds[$cnt] = $additionalParticipantId; | |
270 | $cnt++; | |
271 | } | |
272 | $this->set('additionalParticipantIds', $this->_additionalParticipantIds); | |
273 | } | |
274 | ||
96f7c53e | 275 | $eventFull = CRM_Event_BAO_Participant::eventFull($this->_eventId, FALSE, |
6a488035 TO |
276 | CRM_Utils_Array::value('has_waitlist', $this->_values['event']) |
277 | ); | |
278 | ||
96f7c53e | 279 | $this->_allowWaitlist = $this->_isEventFull = FALSE; |
6a488035 TO |
280 | if ($eventFull && !$this->_allowConfirmation) { |
281 | $this->_isEventFull = TRUE; | |
282 | //lets redirecting to info only when to waiting list. | |
283 | $this->_allowWaitlist = CRM_Utils_Array::value('has_waitlist', $this->_values['event']); | |
284 | if (!$this->_allowWaitlist) { | |
285 | CRM_Utils_System::redirect($infoUrl); | |
286 | } | |
287 | } | |
288 | $this->set('isEventFull', $this->_isEventFull); | |
289 | $this->set('allowWaitlist', $this->_allowWaitlist); | |
290 | ||
291 | //check for require requires approval. | |
292 | $this->_requireApproval = FALSE; | |
a7488080 | 293 | if (!empty($this->_values['event']['requires_approval']) && !$this->_allowConfirmation) { |
6a488035 TO |
294 | $this->_requireApproval = TRUE; |
295 | } | |
296 | $this->set('requireApproval', $this->_requireApproval); | |
297 | ||
6a488035 TO |
298 | if (isset($this->_values['event']['default_role_id'])) { |
299 | $participant_role = CRM_Core_OptionGroup::values('participant_role'); | |
300 | $this->_values['event']['participant_role'] = $participant_role["{$this->_values['event']['default_role_id']}"]; | |
301 | } | |
6a488035 TO |
302 | // check for is_monetary status |
303 | $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values['event']); | |
96f7c53e | 304 | $isPayLater = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_eventId, 'is_pay_later'); |
6a488035 TO |
305 | //check for variour combination for paylater, payment |
306 | //process with paid event. | |
8cc574cf | 307 | if ($isMonetary && (!$isPayLater || !empty($this->_values['event']['payment_processor']))) { |
6a488035 TO |
308 | $ppID = CRM_Utils_Array::value('payment_processor', |
309 | $this->_values['event'] | |
310 | ); | |
311 | if (!$ppID) { | |
312 | CRM_Core_Error::statusBounce(ts('A payment processor must be selected for this event registration page, or the event must be configured to give users the option to pay later (contact the site administrator for assistance).'), $infoUrl); | |
313 | } | |
6a488035 TO |
314 | $ppIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ppID); |
315 | $this->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPayments($ppIds, | |
316 | $this->_mode | |
317 | ); | |
6a488035 TO |
318 | $this->set('paymentProcessors', $this->_paymentProcessors); |
319 | ||
320 | //set default payment processor | |
321 | if (!empty($this->_paymentProcessors) && empty($this->_paymentProcessor)) { | |
322 | foreach ($this->_paymentProcessors as $ppId => $values) { | |
323 | if ($values['is_default'] == 1 || (count($this->_paymentProcessors) == 1)) { | |
324 | $defaultProcessorId = $ppId; | |
325 | break; | |
326 | } | |
327 | } | |
328 | } | |
329 | ||
330 | if (isset($defaultProcessorId)) { | |
331 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($defaultProcessorId, $this->_mode); | |
6a488035 | 332 | } |
6a488035 TO |
333 | // make sure we have a valid payment class, else abort |
334 | if ($this->_values['event']['is_monetary']) { | |
6a488035 TO |
335 | if (!CRM_Utils_System::isNull($this->_paymentProcessors)) { |
336 | foreach ($this->_paymentProcessors as $eachPaymentProcessor) { | |
6a488035 TO |
337 | // check selected payment processor is active |
338 | if (!$eachPaymentProcessor) { | |
339 | CRM_Core_Error::fatal(ts('The site administrator must set a Payment Processor for this event in order to use online registration.')); | |
340 | } | |
6a488035 TO |
341 | // ensure that processor has a valid config |
342 | $payment = CRM_Core_Payment::singleton($this->_mode, $eachPaymentProcessor, $this); | |
343 | $error = $payment->checkConfig(); | |
344 | if (!empty($error)) { | |
345 | CRM_Core_Error::fatal($error); | |
346 | } | |
347 | } | |
348 | } | |
349 | } | |
350 | } | |
6a488035 | 351 | //init event fee. |
96f7c53e | 352 | self::initEventFee($this, $this->_eventId); |
6a488035 TO |
353 | |
354 | // get the profile ids | |
355 | $ufJoinParams = array( | |
356 | 'entity_table' => 'civicrm_event', | |
357 | // CRM-4377:Â CiviEvent for the main participant, CiviEvent_Additional for additional participants | |
358 | 'module' => 'CiviEvent', | |
359 | 'entity_id' => $this->_eventId, | |
360 | ); | |
361 | list($this->_values['custom_pre_id'], | |
362 | $this->_values['custom_post_id'] | |
363 | ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
364 | ||
365 | // set profiles for additional participants | |
366 | if ($this->_values['event']['is_multiple_registrations']) { | |
96f7c53e PJ |
367 | // CRM-4377:Â CiviEvent for the main participant, CiviEvent_Additional for additional participants |
368 | $ufJoinParams['module'] = 'CiviEvent_Additional'; | |
369 | ||
6a488035 TO |
370 | list($this->_values['additional_custom_pre_id'], |
371 | $this->_values['additional_custom_post_id'], $preActive, $postActive | |
372 | ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
373 | ||
374 | // CRM-4377:Â we need to maintain backward compatibility, hence if there is profile for main contact | |
375 | // set same profile for additional contacts. | |
376 | if ($this->_values['custom_pre_id'] && !$this->_values['additional_custom_pre_id']) { | |
377 | $this->_values['additional_custom_pre_id'] = $this->_values['custom_pre_id']; | |
378 | } | |
379 | ||
380 | if ($this->_values['custom_post_id'] && !$this->_values['additional_custom_post_id']) { | |
381 | $this->_values['additional_custom_post_id'] = $this->_values['custom_post_id']; | |
382 | } | |
6a488035 TO |
383 | // now check for no profile condition, in that case is_active = 0 |
384 | if (isset($preActive) && !$preActive) { | |
385 | unset($this->_values['additional_custom_pre_id']); | |
386 | } | |
6a488035 TO |
387 | if (isset($postActive) && !$postActive) { |
388 | unset($this->_values['additional_custom_post_id']); | |
389 | } | |
390 | } | |
6a488035 | 391 | // get the billing location type |
180409a4 | 392 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate'); |
6a488035 TO |
393 | // CRM-8108 remove ts from Billing as the location type can not be translated in CiviCRM! |
394 | //$this->_bltID = array_search( ts('Billing'), $locationTypes ); | |
395 | $this->_bltID = array_search('Billing', $locationTypes); | |
6a488035 TO |
396 | if (!$this->_bltID) { |
397 | CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing'))); | |
398 | } | |
399 | $this->set('bltID', $this->_bltID); | |
400 | ||
401 | if ($this->_values['event']['is_monetary'] && | |
402 | ($this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM) | |
403 | ) { | |
404 | CRM_Core_Payment_Form::setCreditCardFields($this); | |
405 | } | |
6a488035 TO |
406 | $params = array('entity_id' => $this->_eventId, 'entity_table' => 'civicrm_event'); |
407 | $this->_values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE); | |
408 | ||
409 | $this->set('values', $this->_values); | |
410 | $this->set('fields', $this->_fields); | |
411 | ||
412 | $this->_availableRegistrations = | |
413 | CRM_Event_BAO_Participant::eventFull( | |
96f7c53e | 414 | $this->_values['event']['id'], TRUE, |
6a488035 TO |
415 | CRM_Utils_Array::value('has_waitlist', $this->_values['event']) |
416 | ); | |
417 | $this->set('availableRegistrations', $this->_availableRegistrations); | |
418 | } | |
6a488035 TO |
419 | $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor); |
420 | ||
421 | // check if this is a paypal auto return and redirect accordingly | |
422 | if (CRM_Core_Payment::paypalRedirect($this->_paymentProcessor)) { | |
423 | $url = CRM_Utils_System::url('civicrm/event/register', | |
424 | "_qf_ThankYou_display=1&qfKey={$this->controller->_key}" | |
425 | ); | |
426 | CRM_Utils_System::redirect($url); | |
427 | } | |
6a488035 TO |
428 | $this->_contributeMode = $this->get('contributeMode'); |
429 | $this->assign('contributeMode', $this->_contributeMode); | |
430 | ||
431 | // setting CMS page title | |
432 | CRM_Utils_System::setTitle($this->_values['event']['title']); | |
433 | $this->assign('title', $this->_values['event']['title']); | |
434 | ||
435 | $this->assign('paidEvent', $this->_values['event']['is_monetary']); | |
436 | ||
437 | // we do not want to display recently viewed items on Registration pages | |
438 | $this->assign('displayRecent', FALSE); | |
439 | // Registration page values are cleared from session, so can't use normal Printer Friendly view. | |
440 | // Use Browser Print instead. | |
441 | $this->assign('browserPrint', TRUE); | |
442 | ||
443 | $isShowLocation = CRM_Utils_Array::value('is_show_location', $this->_values['event']); | |
444 | $this->assign('isShowLocation', $isShowLocation); | |
6a488035 TO |
445 | // Handle PCP |
446 | $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this); | |
447 | if ($pcpId) { | |
448 | $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'event', $this->_values['event']); | |
449 | $this->_pcpId = $pcp['pcpId']; | |
2fd5e617 | 450 | $this->_values['event']['intro_text'] = CRM_Utils_Array::value('intro_text', $pcp['pcpInfo']); |
6a488035 TO |
451 | } |
452 | ||
453 | // assign all event properties so wizard templates can display event info. | |
454 | $this->assign('event', $this->_values['event']); | |
455 | $this->assign('location', $this->_values['location']); | |
456 | $this->assign('bltID', $this->_bltID); | |
457 | $isShowLocation = CRM_Utils_Array::value('is_show_location', $this->_values['event']); | |
458 | $this->assign('isShowLocation', $isShowLocation); | |
6a488035 | 459 | //CRM-6907 |
96f7c53e | 460 | $config->defaultCurrency = CRM_Utils_Array::value('currency', $this->_values['event'], |
6a488035 TO |
461 | $config->defaultCurrency |
462 | ); | |
463 | ||
464 | //lets allow user to override campaign. | |
465 | $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this); | |
466 | if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) { | |
467 | $this->_values['event']['campaign_id'] = $campID; | |
468 | } | |
469 | } | |
470 | ||
471 | /** | |
472 | * assign the minimal set of variables to the template | |
473 | * | |
474 | * @return void | |
475 | * @access public | |
476 | */ | |
477 | function assignToTemplate() { | |
478 | //process only primary participant params | |
479 | $this->_params = $this->get('params'); | |
480 | if (isset($this->_params[0])) { | |
481 | $params = $this->_params[0]; | |
482 | } | |
483 | $name = ''; | |
a7488080 | 484 | if (!empty($params['billing_first_name'])) { |
6a488035 TO |
485 | $name = $params['billing_first_name']; |
486 | } | |
487 | ||
a7488080 | 488 | if (!empty($params['billing_middle_name'])) { |
6a488035 TO |
489 | $name .= " {$params['billing_middle_name']}"; |
490 | } | |
491 | ||
a7488080 | 492 | if (!empty($params['billing_last_name'])) { |
6a488035 TO |
493 | $name .= " {$params['billing_last_name']}"; |
494 | } | |
495 | $this->assign('billingName', $name); | |
496 | $this->set('name', $name); | |
497 | ||
498 | $vars = array( | |
499 | 'amount', 'currencyID', 'credit_card_type', | |
500 | 'trxn_id', 'amount_level', 'receive_date', | |
501 | ); | |
502 | ||
503 | foreach ($vars as $v) { | |
a7488080 | 504 | if (!empty($params[$v])) { |
6a488035 TO |
505 | if ($v == 'receive_date') { |
506 | $this->assign($v, CRM_Utils_Date::mysqlToIso($params[$v])); | |
507 | } | |
508 | else { | |
509 | $this->assign($v, $params[$v]); | |
510 | } | |
511 | } | |
512 | elseif (CRM_Utils_Array::value('amount', $params) == 0) { | |
513 | $this->assign($v, CRM_Utils_Array::value($v, $params)); | |
514 | } | |
515 | } | |
516 | ||
517 | // assign the address formatted up for display | |
518 | $addressParts = array( | |
519 | "street_address-{$this->_bltID}", | |
520 | "city-{$this->_bltID}", | |
521 | "postal_code-{$this->_bltID}", | |
522 | "state_province-{$this->_bltID}", | |
523 | "country-{$this->_bltID}", | |
524 | ); | |
525 | $addressFields = array(); | |
526 | foreach ($addressParts as $part) { | |
527 | list($n, $id) = explode('-', $part); | |
528 | if (isset($params['billing_' . $part])) { | |
529 | $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $params); | |
530 | } | |
531 | } | |
532 | ||
533 | $this->assign('address', CRM_Utils_Address::format($addressFields)); | |
534 | ||
8cc574cf | 535 | if ($this->_contributeMode == 'direct' && empty($params['is_pay_later'])) { |
6a488035 TO |
536 | $date = CRM_Utils_Date::format(CRM_Utils_Array::value('credit_card_exp_date', $params)); |
537 | $date = CRM_Utils_Date::mysqlToIso($date); | |
538 | $this->assign('credit_card_exp_date', $date); | |
539 | $this->assign('credit_card_number', | |
540 | CRM_Utils_System::mungeCreditCard(CRM_Utils_Array::value('credit_card_number', $params)) | |
541 | ); | |
542 | } | |
543 | ||
544 | // get the email that the confirmation would have been sent to | |
545 | $session = CRM_Core_Session::singleton(); | |
546 | ||
547 | // assign is_email_confirm to templates | |
548 | if (isset($this->_values['event']['is_email_confirm'])) { | |
549 | $this->assign('is_email_confirm', $this->_values['event']['is_email_confirm']); | |
550 | } | |
551 | ||
552 | // assign pay later stuff | |
553 | $params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE); | |
554 | $this->assign('is_pay_later', $params['is_pay_later']); | |
555 | if ($params['is_pay_later']) { | |
556 | $this->assign('pay_later_text', $this->_values['event']['pay_later_text']); | |
557 | $this->assign('pay_later_receipt', $this->_values['event']['pay_later_receipt']); | |
558 | } | |
559 | ||
560 | // also assign all participantIDs to the template | |
561 | // useful in generating confirmation numbers if needed | |
562 | $this->assign('participantIDs', | |
563 | $this->_participantIDS | |
564 | ); | |
565 | } | |
566 | ||
567 | /** | |
568 | * Function to add the custom fields | |
569 | * | |
77b97be7 EM |
570 | * @param $id |
571 | * @param $name | |
572 | * @param bool $viewOnly | |
573 | * | |
355ba699 | 574 | * @return void |
6a488035 TO |
575 | * @access public |
576 | */ | |
577 | function buildCustom($id, $name, $viewOnly = FALSE) { | |
6a488035 TO |
578 | if ($id) { |
579 | $button = substr($this->controller->getButtonName(), -4); | |
580 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
581 | $session = CRM_Core_Session::singleton(); | |
582 | $contactID = $session->get('userID'); | |
583 | ||
584 | // we don't allow conflicting fields to be | |
585 | // configured via profile | |
586 | $fieldsToIgnore = array( | |
587 | 'participant_fee_amount' => 1, | |
588 | 'participant_fee_level' => 1, | |
589 | ); | |
590 | if ($contactID) { | |
591 | //FIX CRM-9653 | |
592 | if (is_array($id)) { | |
593 | $fields = array(); | |
594 | foreach ($id as $profileID) { | |
595 | $field = CRM_Core_BAO_UFGroup::getFields($profileID, FALSE, CRM_Core_Action::ADD, | |
596 | NULL, NULL, FALSE, NULL, | |
597 | FALSE, NULL, CRM_Core_Permission::CREATE, | |
598 | 'field_name', TRUE | |
599 | ); | |
600 | $fields = array_merge($fields, $field); | |
601 | } | |
602 | } | |
603 | else { | |
604 | if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) { | |
605 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, | |
606 | NULL, NULL, FALSE, NULL, | |
607 | FALSE, NULL, CRM_Core_Permission::CREATE, | |
608 | 'field_name', TRUE | |
609 | ); | |
610 | } | |
611 | } | |
612 | } | |
613 | else { | |
614 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, | |
615 | NULL, NULL, FALSE, NULL, | |
616 | FALSE, NULL, CRM_Core_Permission::CREATE, | |
617 | 'field_name', TRUE | |
618 | ); | |
619 | } | |
620 | ||
621 | if (array_intersect_key($fields, $fieldsToIgnore)) { | |
622 | $fields = array_diff_key($fields, $fieldsToIgnore); | |
623 | CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.')); | |
624 | } | |
625 | $addCaptcha = FALSE; | |
a2149acc | 626 | |
627 | if (!empty($this->_fields)) { | |
628 | $fields = @array_diff_assoc($fields, $this->_fields); | |
629 | } | |
630 | ||
a7488080 | 631 | if (empty($this->_params[0]['additional_participants']) && |
6a488035 TO |
632 | is_null($cid) |
633 | ) { | |
634 | CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID); | |
635 | } | |
636 | $this->assign($name, $fields); | |
6a488035 TO |
637 | if (is_array($fields)) { |
638 | foreach ($fields as $key => $field) { | |
639 | if ($viewOnly && | |
640 | isset($field['data_type']) && | |
641 | $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL') | |
642 | ) { | |
643 | // ignore file upload fields | |
644 | continue; | |
645 | } | |
646 | //make the field optional if primary participant | |
647 | //have been skip the additional participant. | |
648 | if ($button == 'skip') { | |
649 | $field['is_required'] = FALSE; | |
da8d9879 | 650 | } |
71fc6ea4 DG |
651 | // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor |
652 | elseif ($field['add_captcha'] && !$contactID) { | |
6a488035 TO |
653 | // only add captcha for first page |
654 | $addCaptcha = TRUE; | |
655 | } | |
6a488035 | 656 | list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2); |
6a488035 TO |
657 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE, $contactID, TRUE); |
658 | ||
659 | $this->_fields[$key] = $field; | |
660 | } | |
661 | } | |
4839c695 | 662 | |
6a488035 TO |
663 | if ($addCaptcha && !$viewOnly) { |
664 | $captcha = CRM_Utils_ReCAPTCHA::singleton(); | |
665 | $captcha->add($this); | |
666 | $this->assign('isCaptcha', TRUE); | |
667 | } | |
668 | } | |
669 | } | |
670 | ||
0cf587a7 EM |
671 | /** |
672 | * @param $form | |
673 | * @param $eventID | |
674 | * | |
675 | * @throws Exception | |
676 | */ | |
6a488035 TO |
677 | static function initEventFee(&$form, $eventID) { |
678 | // get price info | |
679 | ||
680 | // retrive all active price set fields. | |
681 | $discountId = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event'); | |
8567d0f8 PN |
682 | if (property_exists($form, '_discountId') && $form->_discountId) { |
683 | $discountId = $form->_discountId; | |
684 | } | |
6a488035 TO |
685 | if ($discountId) { |
686 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id'); | |
9da8dc8c | 687 | $price = CRM_Price_BAO_PriceSet::initSet($form, $eventID, 'civicrm_event', TRUE, $priceSetId); |
6a488035 TO |
688 | } |
689 | else { | |
9da8dc8c | 690 | $price = CRM_Price_BAO_PriceSet::initSet($form, $eventID, 'civicrm_event', TRUE); |
6a488035 | 691 | } |
8ef12e64 | 692 | |
693 | if (property_exists($form, '_context') && ($form->_context == 'standalone' | |
8567d0f8 PN |
694 | || $form->_context == 'participant')) { |
695 | $discountedEvent = CRM_Core_BAO_Discount::getOptionGroup($eventID, 'civicrm_event'); | |
696 | if (is_array( $discountedEvent)) { | |
697 | foreach ($discountedEvent as $key => $priceSetId) { | |
9da8dc8c | 698 | $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId); |
8567d0f8 PN |
699 | $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet); |
700 | $form->_values['discount'][$key] = CRM_Utils_Array::value('fields', $priceSet); | |
701 | $fieldID = key($form->_values['discount'][$key]); | |
702 | $form->_values['discount'][$key][$fieldID]['name'] = CRM_Core_DAO::getFieldValue( | |
9da8dc8c | 703 | 'CRM_Price_DAO_PriceSet', |
8567d0f8 PN |
704 | $priceSetId, |
705 | 'title' | |
706 | ); | |
707 | } | |
708 | } | |
709 | } | |
6a488035 TO |
710 | $eventFee = CRM_Utils_Array::value('fee', $form->_values); |
711 | if (!is_array($eventFee) || empty($eventFee)) { | |
712 | $form->_values['fee'] = array(); | |
713 | } | |
714 | ||
715 | //fix for non-upgraded price sets.CRM-4256. | |
716 | if (isset($form->_isPaidEvent)) { | |
717 | $isPaidEvent = $form->_isPaidEvent; | |
718 | } | |
719 | else { | |
720 | $isPaidEvent = CRM_Utils_Array::value('is_monetary', $form->_values['event']); | |
721 | } | |
722 | if ($isPaidEvent && empty($form->_values['fee'])) { | |
723 | if (CRM_Utils_System::getClassName($form) != 'CRM_Event_Form_Participant') { | |
724 | CRM_Core_Error::fatal(ts('No Fee Level(s) or Price Set is configured for this event.<br />Click <a href=\'%1\'>CiviEvent >> Manage Event >> Configure >> Event Fees</a> to configure the Fee Level(s) or Price Set for this event.', array(1 => CRM_Utils_System::url('civicrm/event/manage/fee', 'reset=1&action=update&id=' . $form->_eventId)))); | |
725 | } | |
726 | } | |
727 | } | |
728 | ||
729 | /** | |
6441bcc8 | 730 | * Function to handle process after the confirmation of payment by User |
6a488035 | 731 | * |
2a6da8d7 EM |
732 | * @param null $contactID |
733 | * @param null $contribution | |
734 | * @param null $payment | |
735 | * | |
355ba699 | 736 | * @return void |
6a488035 TO |
737 | * @access public |
738 | */ | |
739 | function confirmPostProcess($contactID = NULL, $contribution = NULL, $payment = NULL) { | |
740 | // add/update contact information | |
741 | $fields = array(); | |
742 | unset($this->_params['note']); | |
743 | ||
744 | //to avoid conflict overwrite $this->_params | |
745 | $this->_params = $this->get('value'); | |
746 | ||
747 | //get the amount of primary participant | |
a7488080 | 748 | if (!empty($this->_params['is_primary'])) { |
6a488035 TO |
749 | $this->_params['fee_amount'] = $this->get('primaryParticipantAmount'); |
750 | } | |
751 | ||
752 | // add participant record | |
753 | $participant = $this->addParticipant($this->_params, $contactID); | |
754 | $this->_participantIDS[] = $participant->id; | |
755 | ||
756 | //setting register_by_id field and primaryContactId | |
a7488080 | 757 | if (!empty($this->_params['is_primary'])) { |
6a488035 TO |
758 | $this->set('registerByID', $participant->id); |
759 | $this->set('primaryContactId', $contactID); | |
760 | ||
761 | // CRM-10032 | |
762 | $this->processFirstParticipant($participant->id); | |
763 | } | |
764 | ||
765 | CRM_Core_BAO_CustomValueTable::postProcess($this->_params, | |
766 | CRM_Core_DAO::$_nullArray, | |
767 | 'civicrm_participant', | |
768 | $participant->id, | |
769 | 'Participant' | |
770 | ); | |
771 | ||
772 | $createPayment = (CRM_Utils_Array::value('amount', $this->_params, 0) != 0) ? TRUE : FALSE; | |
773 | ||
774 | // force to create zero amount payment, CRM-5095 | |
775 | // we know the amout is zero since createPayment is false | |
776 | if (!$createPayment && | |
777 | (isset($contribution) && $contribution->id) && | |
778 | $this->_priceSetId && | |
779 | $this->_lineItem | |
780 | ) { | |
781 | $createPayment = TRUE; | |
782 | } | |
783 | ||
8cc574cf | 784 | if ($createPayment && $this->_values['event']['is_monetary'] && !empty($this->_params['contributionID'])) { |
6a488035 TO |
785 | $paymentParams = array( |
786 | 'participant_id' => $participant->id, | |
787 | 'contribution_id' => $contribution->id, | |
788 | ); | |
789 | $ids = array(); | |
790 | $paymentPartcipant = CRM_Event_BAO_ParticipantPayment::create($paymentParams, $ids); | |
791 | } | |
792 | ||
793 | //set only primary participant's params for transfer checkout. | |
8cc574cf | 794 | if (($this->_contributeMode == 'checkout' || $this->_contributeMode == 'notify') && !empty($this->_params['is_primary'])) { |
6a488035 TO |
795 | $this->_params['participantID'] = $participant->id; |
796 | $this->set('primaryParticipant', $this->_params); | |
797 | } | |
798 | ||
799 | $this->assign('action', $this->_action); | |
800 | ||
801 | // create CMS user | |
a7488080 | 802 | if (!empty($this->_params['cms_create_account'])) { |
6a488035 TO |
803 | $this->_params['contactID'] = $contactID; |
804 | ||
805 | if (array_key_exists('email-5', $this->_params)) { | |
806 | $mail = 'email-5'; | |
807 | } else { | |
808 | foreach ($this->_params as $name => $dontCare) { | |
809 | if (substr($name, 0, 5) == 'email') { | |
810 | $mail = $name; | |
811 | break; | |
812 | } | |
813 | } | |
814 | } | |
815 | ||
816 | // we should use primary email for | |
817 | // 1. free event registration. | |
818 | // 2. pay later participant. | |
819 | // 3. waiting list participant. | |
820 | // 4. require approval participant. | |
a7488080 | 821 | if (!empty($this->_params['is_pay_later']) || |
8cc574cf | 822 | $this->_allowWaitlist || $this->_requireApproval || empty($this->_values['event']['is_monetary'])) { |
6a488035 TO |
823 | $mail = 'email-Primary'; |
824 | } | |
825 | ||
826 | if (!CRM_Core_BAO_CMSUser::create($this->_params, $mail)) { | |
827 | CRM_Core_Error::statusBounce(ts('Your profile is not saved and Account is not created.')); | |
828 | } | |
829 | } | |
830 | } | |
831 | ||
832 | /** | |
833 | * Process the participant | |
834 | * | |
dd244018 EM |
835 | * @param $params |
836 | * @param $contactID | |
837 | * | |
6a488035 TO |
838 | * @return void |
839 | * @access public | |
840 | */ | |
841 | public function addParticipant($params, $contactID) { | |
842 | ||
843 | $transaction = new CRM_Core_Transaction(); | |
844 | ||
845 | $groupName = 'participant_role'; | |
846 | $query = " | |
847 | SELECT v.label as label ,v.value as value | |
848 | FROM civicrm_option_value v, | |
849 | civicrm_option_group g | |
850 | WHERE v.option_group_id = g.id | |
851 | AND g.name = %1 | |
852 | AND v.is_active = 1 | |
853 | AND g.is_active = 1 | |
854 | "; | |
855 | $p = array(1 => array($groupName, 'String')); | |
856 | ||
857 | $dao = CRM_Core_DAO::executeQuery($query, $p); | |
858 | if ($dao->fetch()) { | |
859 | $roleID = $dao->value; | |
860 | } | |
861 | ||
862 | // handle register date CRM-4320 | |
863 | $registerDate = NULL; | |
864 | if ($this->_allowConfirmation && $this->_participantId) { | |
865 | $registerDate = $params['participant_register_date']; | |
866 | } | |
a7488080 | 867 | elseif (!empty($params['participant_register_date']) && |
6a488035 TO |
868 | is_array($params['participant_register_date']) && |
869 | !empty($params['participant_register_date']) | |
870 | ) { | |
871 | $registerDate = CRM_Utils_Date::format($params['participant_register_date']); | |
872 | } | |
873 | ||
6441bcc8 | 874 | $participantFields = CRM_Event_DAO_Participant::fields(); |
6a488035 TO |
875 | $participantParams = array('id' => CRM_Utils_Array::value('participant_id', $params), |
876 | 'contact_id' => $contactID, | |
877 | 'event_id' => $this->_eventId ? $this->_eventId : $params['event_id'], | |
878 | 'status_id' => CRM_Utils_Array::value('participant_status', | |
879 | $params, 1 | |
880 | ), | |
881 | 'role_id' => CRM_Utils_Array::value('participant_role_id', | |
882 | $params, $roleID | |
883 | ), | |
884 | 'register_date' => ($registerDate) ? $registerDate : date('YmdHis'), | |
6441bcc8 NG |
885 | 'source' => CRM_Utils_String::ellipsify( |
886 | isset($params['participant_source']) ? | |
887 | CRM_Utils_Array::value('participant_source', $params) : CRM_Utils_Array::value('description', $params), | |
888 | $participantFields['participant_source']['maxlength'] | |
889 | ), | |
6a488035 TO |
890 | 'fee_level' => CRM_Utils_Array::value('amount_level', $params), |
891 | 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0), | |
892 | 'fee_amount' => CRM_Utils_Array::value('fee_amount', $params), | |
893 | 'registered_by_id' => CRM_Utils_Array::value('registered_by_id', $params), | |
894 | 'discount_id' => CRM_Utils_Array::value('discount_id', $params), | |
895 | 'fee_currency' => CRM_Utils_Array::value('currencyID', $params), | |
896 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params), | |
897 | ); | |
898 | ||
899 | if ($this->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') { | |
900 | $participantParams['is_test'] = 1; | |
901 | } | |
902 | else { | |
903 | $participantParams['is_test'] = 0; | |
904 | } | |
905 | ||
a7488080 | 906 | if (!empty($this->_params['note'])) { |
6a488035 TO |
907 | $participantParams['note'] = $this->_params['note']; |
908 | } | |
a7488080 | 909 | elseif (!empty($this->_params['participant_note'])) { |
6a488035 TO |
910 | $participantParams['note'] = $this->_params['participant_note']; |
911 | } | |
912 | ||
913 | // reuse id if one already exists for this one (can happen | |
914 | // with back button being hit etc) | |
8cc574cf | 915 | if (!$participantParams['id'] && !empty($params['contributionID'])) { |
6a488035 TO |
916 | $pID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', |
917 | $params['contributionID'], | |
918 | 'participant_id', | |
919 | 'contribution_id' | |
920 | ); | |
921 | $participantParams['id'] = $pID; | |
922 | } | |
923 | $participantParams['discount_id'] = CRM_Core_BAO_Discount::findSet($this->_eventId, 'civicrm_event'); | |
924 | ||
925 | if (!$participantParams['discount_id']) { | |
926 | $participantParams['discount_id'] = "null"; | |
927 | } | |
928 | ||
929 | $participant = CRM_Event_BAO_Participant::create($participantParams); | |
930 | ||
931 | $transaction->commit(); | |
932 | ||
933 | return $participant; | |
934 | } | |
935 | ||
936 | /* Calculate the total participant count as per params. | |
937 | * | |
938 | * @param array $params user params. | |
939 | * | |
940 | * @return $totalCount total participant count. | |
941 | * @access public | |
942 | */ | |
0cf587a7 EM |
943 | /** |
944 | * @param $form | |
945 | * @param $params | |
946 | * @param bool $skipCurrent | |
947 | * | |
948 | * @return int|string | |
949 | */ | |
6a488035 TO |
950 | public static function getParticipantCount(&$form, $params, $skipCurrent = FALSE) { |
951 | $totalCount = 0; | |
952 | if (!is_array($params) || empty($params)) { | |
953 | return $totalCount; | |
954 | } | |
955 | ||
956 | $priceSetId = $form->get('priceSetId'); | |
957 | $addParticipantNum = substr($form->_name, 12); | |
958 | $priceSetFields = $priceSetDetails = array(); | |
959 | $hasPriceFieldsCount = FALSE; | |
960 | if ($priceSetId) { | |
961 | $priceSetDetails = $form->get('priceSet'); | |
962 | if (isset($priceSetDetails['optionsCountTotal']) | |
963 | && $priceSetDetails['optionsCountTotal'] | |
964 | ) { | |
965 | $hasPriceFieldsCount = TRUE; | |
966 | $priceSetFields = $priceSetDetails['optionsCountDetails']['fields']; | |
967 | } | |
968 | } | |
969 | ||
970 | $singleFormParams = FALSE; | |
971 | foreach ($params as $key => $val) { | |
972 | if (!is_numeric($key)) { | |
973 | $singleFormParams = TRUE; | |
974 | break; | |
975 | } | |
976 | } | |
977 | ||
978 | //first format the params. | |
979 | if ($singleFormParams) { | |
980 | $params = self::formatPriceSetParams($form, $params); | |
981 | $params = array($params); | |
982 | } | |
983 | ||
984 | foreach ($params as $key => $values) { | |
985 | if (!is_numeric($key) || | |
986 | $values == 'skip' || | |
987 | ($skipCurrent && ($addParticipantNum == $key)) | |
988 | ) { | |
989 | continue; | |
990 | } | |
991 | $count = 1; | |
992 | ||
993 | $usedCache = FALSE; | |
994 | $cacheCount = CRM_Utils_Array::value($key, $form->_lineItemParticipantsCount); | |
995 | if ($cacheCount && is_numeric($cacheCount)) { | |
996 | $count = $cacheCount; | |
997 | $usedCache = TRUE; | |
998 | } | |
999 | ||
1000 | if (!$usedCache && $hasPriceFieldsCount) { | |
1001 | $count = 0; | |
1002 | foreach ($values as $valKey => $value) { | |
1003 | if (strpos($valKey, 'price_') === FALSE) { | |
1004 | continue; | |
1005 | } | |
1006 | $priceFieldId = substr($valKey, 6); | |
1007 | if (!$priceFieldId || | |
1008 | !is_array($value) || | |
1009 | !array_key_exists($priceFieldId, $priceSetFields) | |
1010 | ) { | |
1011 | continue; | |
1012 | } | |
1013 | foreach ($value as $optId => $optVal) { | |
1014 | $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal; | |
1015 | if ($currentCount) { | |
1016 | $count += $currentCount; | |
1017 | } | |
1018 | } | |
1019 | } | |
1020 | if (!$count) { | |
1021 | $count = 1; | |
1022 | } | |
1023 | } | |
1024 | $totalCount += $count; | |
1025 | } | |
1026 | if (!$totalCount) { | |
1027 | $totalCount = 1; | |
1028 | } | |
1029 | ||
1030 | return $totalCount; | |
1031 | } | |
1032 | ||
1033 | /* Format user submitted price set params. | |
1034 | * Convert price set each param as an array. | |
1035 | * | |
1036 | * @param $params an array of user submitted params. | |
1037 | * | |
1038 | * | |
1039 | * @return array $formatted, formatted price set params. | |
1040 | * @access public | |
1041 | */ | |
0cf587a7 EM |
1042 | /** |
1043 | * @param $form | |
1044 | * @param $params | |
1045 | * | |
1046 | * @return mixed | |
1047 | */ | |
6a488035 TO |
1048 | public static function formatPriceSetParams(&$form, $params) { |
1049 | if (!is_array($params) || empty($params)) { | |
1050 | return $params; | |
1051 | } | |
1052 | ||
1053 | $priceSetId = $form->get('priceSetId'); | |
1054 | if (!$priceSetId) { | |
1055 | return $params; | |
1056 | } | |
1057 | $priceSetDetails = $form->get('priceSet'); | |
1058 | ||
1059 | foreach ($params as $key => & $value) { | |
1060 | $vals = array(); | |
1061 | if (strpos($key, 'price_') !== FALSE) { | |
1062 | $fieldId = substr($key, 6); | |
1063 | if (!array_key_exists($fieldId, $priceSetDetails['fields']) || | |
1064 | is_array($value) || | |
1065 | !$value | |
1066 | ) { | |
1067 | continue; | |
1068 | } | |
1069 | $field = $priceSetDetails['fields'][$fieldId]; | |
1070 | if ($field['html_type'] == 'Text') { | |
1071 | $fieldOption = current($field['options']); | |
1072 | $value = array($fieldOption['id'] => $value); | |
1073 | } | |
1074 | else { | |
1075 | $value = array($value => TRUE); | |
1076 | } | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | return $params; | |
1081 | } | |
1082 | ||
1083 | /* Calculate total count for each price set options. | |
1084 | * those are currently selected by user. | |
1085 | * | |
1086 | * @param $form form object. | |
1087 | * | |
1088 | * | |
1089 | * @return array $optionsCount, array of each option w/ count total. | |
1090 | * @access public | |
1091 | */ | |
0cf587a7 EM |
1092 | /** |
1093 | * @param $form | |
1094 | * | |
1095 | * @return array | |
1096 | */ | |
169be3c1 | 1097 | public static function getPriceSetOptionCount(&$form) { |
6a488035 TO |
1098 | $params = $form->get('params'); |
1099 | $priceSet = $form->get('priceSet'); | |
1100 | $priceSetId = $form->get('priceSetId'); | |
1101 | ||
1102 | $optionsCount = array(); | |
1103 | if (!$priceSetId || | |
1104 | !is_array($priceSet) || | |
1105 | empty($priceSet) || | |
1106 | !is_array($params) || | |
1107 | empty($params) | |
1108 | ) { | |
1109 | return $optionsCount; | |
1110 | } | |
1111 | ||
1112 | $priceSetFields = $priceMaxFieldDetails = array(); | |
a7488080 | 1113 | if (!empty($priceSet['optionsCountTotal'])) { |
6a488035 TO |
1114 | $priceSetFields = $priceSet['optionsCountDetails']['fields']; |
1115 | } | |
1116 | ||
a7488080 | 1117 | if (!empty($priceSet['optionsMaxValueTotal'])) { |
6a488035 TO |
1118 | $priceMaxFieldDetails = $priceSet['optionsMaxValueDetails']['fields']; |
1119 | } | |
1120 | ||
1121 | $addParticipantNum = substr($form->_name, 12); | |
1122 | foreach ($params as $pCnt => $values) { | |
1123 | if ($values == 'skip' || | |
1124 | $pCnt == $addParticipantNum | |
1125 | ) { | |
1126 | continue; | |
1127 | } | |
1128 | ||
1129 | foreach ($values as $valKey => $value) { | |
1130 | if (strpos($valKey, 'price_') === FALSE) { | |
1131 | continue; | |
1132 | } | |
1133 | ||
1134 | $priceFieldId = substr($valKey, 6); | |
1135 | if (!$priceFieldId || | |
1136 | !is_array($value) || | |
1137 | !(array_key_exists($priceFieldId, $priceSetFields) || array_key_exists($priceFieldId, $priceMaxFieldDetails)) | |
1138 | ) { | |
1139 | continue; | |
1140 | } | |
1141 | ||
1142 | foreach ($value as $optId => $optVal) { | |
1143 | if (CRM_Utils_Array::value('html_type', $priceSet['fields'][$priceFieldId]) == 'Text') { | |
1144 | $currentCount = $optVal; | |
1145 | } | |
1146 | else { | |
1147 | $currentCount = 1; | |
1148 | } | |
1149 | ||
1150 | if (isset($priceSetFields[$priceFieldId]) && isset($priceSetFields[$priceFieldId]['options'][$optId])) { | |
1151 | $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal; | |
1152 | } | |
1153 | ||
1154 | $optionsCount[$optId] = $currentCount + CRM_Utils_Array::value($optId, $optionsCount, 0); | |
1155 | } | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | return $optionsCount; | |
1160 | } | |
1161 | ||
0cf587a7 EM |
1162 | /** |
1163 | * @param string $suffix | |
1164 | * | |
1165 | * @return null|string | |
1166 | */ | |
6a488035 TO |
1167 | function checkTemplateFileExists($suffix = '') { |
1168 | if ($this->_eventId) { | |
1169 | $templateName = $this->_name; | |
1170 | if (substr($templateName, 0, 12) == 'Participant_') { | |
1171 | $templateName = 'AdditionalParticipant'; | |
1172 | } | |
1173 | ||
1174 | $templateFile = "CRM/Event/Form/Registration/{$this->_eventId}/{$templateName}.{$suffix}tpl"; | |
1175 | $template = CRM_Core_Form::getTemplate(); | |
1176 | if ($template->template_exists($templateFile)) { | |
1177 | return $templateFile; | |
1178 | } | |
1179 | } | |
1180 | return NULL; | |
1181 | } | |
1182 | ||
0cf587a7 EM |
1183 | /** |
1184 | * @return null|string | |
1185 | */ | |
6a488035 TO |
1186 | function getTemplateFileName() { |
1187 | $fileName = $this->checkTemplateFileExists(); | |
1188 | return $fileName ? $fileName : parent::getTemplateFileName(); | |
1189 | } | |
1190 | ||
0cf587a7 EM |
1191 | /** |
1192 | * @return null|string | |
1193 | */ | |
6a488035 TO |
1194 | function overrideExtraTemplateFileName() { |
1195 | $fileName = $this->checkTemplateFileExists('extra.'); | |
1196 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); | |
1197 | } | |
1198 | ||
da8d9879 DG |
1199 | /** |
1200 | * Validate price set submitted params for price option limit, | |
6a488035 | 1201 | * as well as user should select at least one price field option. |
da8d9879 DG |
1202 | * @param unknown_type $form |
1203 | * @param unknown_type $params | |
1204 | * @return multitype:|Ambigous <multitype:, string, string> | |
6a488035 TO |
1205 | */ |
1206 | static function validatePriceSet(&$form, $params) { | |
1207 | $errors = array(); | |
1208 | $hasOptMaxValue = FALSE; | |
1209 | if (!is_array($params) || empty($params)) { | |
1210 | return $errors; | |
1211 | } | |
1212 | ||
1213 | $currentParticipantNum = substr($form->_name, 12); | |
1214 | if (!$currentParticipantNum) { | |
1215 | $currentParticipantNum = 0; | |
1216 | } | |
1217 | ||
1218 | $priceSetId = $form->get('priceSetId'); | |
1219 | $priceSetDetails = $form->get('priceSet'); | |
1220 | if ( | |
1221 | !$priceSetId || | |
1222 | !is_array($priceSetDetails) || | |
1223 | empty($priceSetDetails) | |
1224 | ) { | |
1225 | return $errors; | |
1226 | } | |
1227 | ||
1228 | $optionsCountDetails = $optionsMaxValueDetails = array(); | |
1229 | if ( | |
1230 | isset($priceSetDetails['optionsMaxValueTotal']) | |
1231 | && $priceSetDetails['optionsMaxValueTotal'] | |
1232 | ) { | |
1233 | $hasOptMaxValue = TRUE; | |
1234 | $optionsMaxValueDetails = $priceSetDetails['optionsMaxValueDetails']['fields']; | |
1235 | } | |
1236 | if ( | |
1237 | isset($priceSetDetails['optionsCountTotal']) | |
1238 | && $priceSetDetails['optionsCountTotal'] | |
1239 | ) { | |
1240 | $hasOptCount = TRUE; | |
1241 | $optionsCountDetails = $priceSetDetails['optionsCountDetails']['fields']; | |
1242 | } | |
1243 | $feeBlock = $form->_feeBlock; | |
1244 | ||
1245 | if (empty($feeBlock)) { | |
1246 | $feeBlock = $priceSetDetails['fields']; | |
1247 | } | |
1248 | ||
1249 | $optionMaxValues = $fieldSelected = array(); | |
1250 | foreach ($params as $pNum => $values) { | |
1251 | if (!is_array($values) || $values == 'skip') { | |
1252 | continue; | |
1253 | } | |
1254 | ||
1255 | foreach ($values as $valKey => $value) { | |
1256 | if (strpos($valKey, 'price_') === FALSE) { | |
1257 | continue; | |
1258 | } | |
1259 | $priceFieldId = substr($valKey, 6); | |
1260 | $noneOptionValueSelected = FALSE; | |
1261 | if (!$feeBlock[$priceFieldId]['is_required'] && $value == 0) { | |
1262 | $noneOptionValueSelected = TRUE; | |
1263 | } | |
1264 | ||
1265 | if ( | |
1266 | !$priceFieldId || | |
1267 | (!$noneOptionValueSelected && !is_array($value)) | |
1268 | ) { | |
1269 | continue; | |
1270 | } | |
1271 | ||
1272 | $fieldSelected[$pNum] = TRUE; | |
1273 | ||
1274 | if (!$hasOptMaxValue || !is_array($value)) { | |
1275 | continue; | |
1276 | } | |
1277 | ||
1278 | foreach ($value as $optId => $optVal) { | |
1279 | if (CRM_Utils_Array::value('html_type', $feeBlock[$priceFieldId]) == 'Text') { | |
1280 | $currentMaxValue = $optVal; | |
1281 | } | |
1282 | else { | |
1283 | $currentMaxValue = 1; | |
1284 | } | |
1285 | ||
1286 | if (isset($optionsCountDetails[$priceFieldId]) && isset($optionsCountDetails[$priceFieldId]['options'][$optId])) { | |
1287 | $currentMaxValue = $optionsCountDetails[$priceFieldId]['options'][$optId] * $optVal; | |
1288 | } | |
1289 | if (empty($optionMaxValues)) { | |
1290 | $optionMaxValues[$priceFieldId][$optId] = $currentMaxValue; | |
1291 | } | |
1292 | else { | |
1293 | $optionMaxValues[$priceFieldId][$optId] = | |
1294 | $currentMaxValue + CRM_Utils_Array::value($optId, CRM_Utils_Array::value($priceFieldId, $optionMaxValues), 0); | |
1295 | } | |
1296 | ||
1297 | } | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | //validate for option max value. | |
1302 | foreach ($optionMaxValues as $fieldId => $values) { | |
1303 | $options = CRM_Utils_Array::value('options', $feeBlock[$fieldId], array()); | |
1304 | foreach ($values as $optId => $total) { | |
1305 | $optMax = $optionsMaxValueDetails[$fieldId]['options'][$optId]; | |
1306 | $opDbCount = CRM_Utils_Array::value('db_total_count', $options[$optId], 0); | |
1307 | $total += $opDbCount; | |
1308 | if ($optMax && $total > $optMax) { | |
1309 | if ($opDbCount && ($opDbCount >= $optMax)) { | |
1310 | $errors[$currentParticipantNum]["price_{$fieldId}"] = | |
1311 | ts('Sorry, this option is currently sold out.'); | |
1312 | } | |
1313 | elseif (($optMax - $opDbCount) == 1) { | |
1314 | $errors[$currentParticipantNum]["price_{$fieldId}"] = | |
1315 | ts('Sorry, currently only a single seat is available for this option.', array(1 => ($optMax - $opDbCount))); | |
1316 | } | |
1317 | else { | |
1318 | $errors[$currentParticipantNum]["price_{$fieldId}"] = | |
1319 | ts('Sorry, currently only %1 seats are available for this option.', array(1 => ($optMax - $opDbCount))); | |
1320 | } | |
1321 | } | |
1322 | } | |
1323 | } | |
1324 | ||
1325 | //validate for price field selection. | |
1326 | foreach ($params as $pNum => $values) { | |
1327 | if (!is_array($values) || $values == 'skip') { | |
1328 | continue; | |
1329 | } | |
a7488080 | 1330 | if (empty($fieldSelected[$pNum])) { |
6a488035 TO |
1331 | $errors[$pNum]['_qf_default'] = ts('Select at least one option from Event Fee(s).'); |
1332 | } | |
1333 | } | |
1334 | ||
1335 | return $errors; | |
1336 | } | |
1337 | ||
1338 | // set the first participant ID if not set, CRM-10032 | |
0cf587a7 EM |
1339 | /** |
1340 | * @param $participantID | |
1341 | */ | |
6a488035 TO |
1342 | function processFirstParticipant($participantID) { |
1343 | $this->_participantId = $participantID; | |
1344 | $this->set('participantId', $this->_participantId); | |
1345 | ||
1346 | $ids = $participantValues = array(); | |
1347 | $participantParams = array('id' => $this->_participantId); | |
1348 | CRM_Event_BAO_Participant::getValues($participantParams, $participantValues, $ids); | |
1349 | $this->_values['participant'] = $participantValues[$this->_participantId]; | |
1350 | $this->set('values', $this->_values); | |
1351 | ||
1352 | // also set the allow confirmation stuff | |
1353 | if (array_key_exists( | |
1354 | $this->_values['participant']['status_id'], | |
1355 | CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'") | |
1356 | )) { | |
1357 | $this->_allowConfirmation = TRUE; | |
1358 | $this->set('allowConfirmation', TRUE); | |
1359 | } | |
1360 | } | |
1361 | ||
e2d09ab4 EM |
1362 | /** |
1363 | * @todo - combine this with CRM_Event_BAO_Event::validRegistrationRequest | |
1364 | * (probably extract relevant values here & call that with them & handle bounces & redirects here -as | |
1365 | * those belong in the form layer) | |
1366 | * | |
1367 | * @param string $redirect | |
1368 | */ | |
6a488035 TO |
1369 | function checkValidEvent($redirect = NULL) { |
1370 | // is the event active (enabled)? | |
1371 | if (!$this->_values['event']['is_active']) { | |
1372 | // form is inactive, die a fatal death | |
1373 | CRM_Core_Error::statusBounce(ts('The event you requested is currently unavailable (contact the site administrator for assistance).')); | |
1374 | } | |
1375 | ||
1376 | // is online registration is enabled? | |
1377 | if (!$this->_values['event']['is_online_registration']) { | |
1378 | CRM_Core_Error::statusBounce(ts('Online registration is not currently available for this event (contact the site administrator for assistance).'), $redirect); | |
1379 | } | |
1380 | ||
1381 | // is this an event template ? | |
a7488080 | 1382 | if (!empty($this->_values['event']['is_template'])) { |
6a488035 TO |
1383 | CRM_Core_Error::statusBounce(ts('Event templates are not meant to be registered.'), $redirect); |
1384 | } | |
1385 | ||
1386 | $now = date('YmdHis'); | |
1387 | $startDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_start_date', | |
1388 | $this->_values['event'] | |
1389 | )); | |
1390 | ||
1391 | if ( | |
1392 | $startDate && | |
1393 | $startDate >= $now | |
1394 | ) { | |
1395 | CRM_Core_Error::statusBounce(ts('Registration for this event begins on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('registration_start_date', $this->_values['event'])))), $redirect); | |
1396 | } | |
1397 | ||
1398 | $endDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_end_date', | |
1399 | $this->_values['event'] | |
1400 | )); | |
1401 | if ( | |
1402 | $endDate && | |
1403 | $endDate < $now | |
1404 | ) { | |
1405 | CRM_Core_Error::statusBounce(ts('Registration for this event ended on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('registration_end_date', $this->_values['event'])))), $redirect); | |
1406 | } | |
1407 | } | |
1408 | } | |
1409 |