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