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