3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
38 * This class generates form components for processing Event
41 class CRM_Event_Form_Registration
extends CRM_Core_Form
{
44 * how many locationBlocks should we display?
49 CONST LOCATION_BLOCKS
= 1;
52 * the id of the event we are proceessing
60 * the array of ids of all the participant we are proceessing
65 protected $_participantIDS = NULL;
68 * the id of the participant we are proceessing
73 protected $_participantId;
76 * is participant able to walk registration wizard.
81 public $_allowConfirmation;
84 * is participant requires approval
89 public $_requireApproval;
92 * is event configured for waitlist.
97 public $_allowWaitlist;
100 * store additional participant ids
101 * when there are pre-registered.
106 public $_additionalParticipantIds;
109 * the mode that we are in
117 * the values for the contribution db object
125 * the paymentProcessor attributes for this page
130 public $_paymentProcessor;
133 * The params submitted by the form and computed by the app
141 * The fields involved in this contribution page
149 * The billing location id for this contribiution page
157 * Price Set ID, if the new price set method is used
162 public $_priceSetId = NULL;
165 * Array of fields for the price set
176 /* Is event already full.
182 public $_isEventFull;
185 public $_lineItemParticipantsCount;
186 public $_availableRegistrations;
188 public $_forcePayement;
190 public $_isBillingAddressRequiredForPayLater;
193 * Function to set variables up before form is built
198 function preProcess() {
199 $this->_eventId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, TRUE);
200 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE);
203 $this->_participantId
= CRM_Utils_Request
::retrieve('participantId', 'Positive', $this);
206 $this->_mode
= ($this->_action
== 1024) ?
'test' : 'live';
208 $this->_values
= $this->get('values');
209 $this->_fields
= $this->get('fields');
210 $this->_bltID
= $this->get('bltID');
211 $this->_paymentProcessor
= $this->get('paymentProcessor');
212 $this->_priceSetId
= $this->get('priceSetId');
213 $this->_priceSet
= $this->get('priceSet');
214 $this->_lineItem
= $this->get('lineItem');
215 $this->_isEventFull
= $this->get('isEventFull');
216 $this->_lineItemParticipantsCount
= $this->get('lineItemParticipants');
217 if (!is_array($this->_lineItem
)) {
218 $this->_lineItem
= array();
220 if (!is_array($this->_lineItemParticipantsCount
)) {
221 $this->_lineItemParticipantsCount
= array();
223 $this->_availableRegistrations
= $this->get('availableRegistrations');
224 $this->_participantIDS
= $this->get('participantIDs');
226 //check if participant allow to walk registration wizard.
227 $this->_allowConfirmation
= $this->get('allowConfirmation');
229 // check for Approval
230 $this->_requireApproval
= $this->get('requireApproval');
232 // check for waitlisting.
233 $this->_allowWaitlist
= $this->get('allowWaitlist');
235 $this->_forcePayement
= $this->get('forcePayement');
237 //get the additional participant ids.
238 $this->_additionalParticipantIds
= $this->get('additionalParticipantIds');
239 $config = CRM_Core_Config
::singleton();
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('civicrm/event/info', "reset=1&id={$this->_eventId}",
244 FALSE, NULL, FALSE, TRUE
247 // this is the first time we are hitting this, so check for permissions here
248 if (!CRM_Core_Permission
::event(CRM_Core_Permission
::EDIT
, $this->_eventId
, 'register for events')) {
249 CRM_Core_Error
::statusBounce(ts('You do not have permission to register for this event'), $infoUrl);
252 // get all the values from the dao object
253 $this->_values
= $this->_fields
= array();
254 $this->_forcePayement
= FALSE;
256 //retrieve event information
257 $params = array('id' => $this->_eventId
);
258 CRM_Event_BAO_Event
::retrieve($params, $this->_values
['event']);
260 $this->checkValidEvent($infoUrl);
261 // get the participant values, CRM-4320
262 $this->_allowConfirmation
= FALSE;
263 if ($this->_participantId
) {
264 $this->processFirstParticipant($this->_participantId
);
266 //check for additional participants.
267 if ($this->_allowConfirmation
&& $this->_values
['event']['is_multiple_registrations']) {
268 $additionalParticipantIds = CRM_Event_BAO_Participant
::getAdditionalParticipantIds($this->_participantId
);
270 foreach ($additionalParticipantIds as $additionalParticipantId) {
271 $this->_additionalParticipantIds
[$cnt] = $additionalParticipantId;
274 $this->set('additionalParticipantIds', $this->_additionalParticipantIds
);
277 $eventFull = CRM_Event_BAO_Participant
::eventFull($this->_eventId
, FALSE,
278 CRM_Utils_Array
::value('has_waitlist', $this->_values
['event'])
281 $this->_allowWaitlist
= $this->_isEventFull
= FALSE;
282 if ($eventFull && !$this->_allowConfirmation
) {
283 $this->_isEventFull
= TRUE;
284 //lets redirecting to info only when to waiting list.
285 $this->_allowWaitlist
= CRM_Utils_Array
::value('has_waitlist', $this->_values
['event']);
286 if (!$this->_allowWaitlist
) {
287 CRM_Utils_System
::redirect($infoUrl);
290 $this->set('isEventFull', $this->_isEventFull
);
291 $this->set('allowWaitlist', $this->_allowWaitlist
);
293 //check for require requires approval.
294 $this->_requireApproval
= FALSE;
295 if (!empty($this->_values
['event']['requires_approval']) && !$this->_allowConfirmation
) {
296 $this->_requireApproval
= TRUE;
298 $this->set('requireApproval', $this->_requireApproval
);
300 if (isset($this->_values
['event']['default_role_id'])) {
301 $participant_role = CRM_Core_OptionGroup
::values('participant_role');
302 $this->_values
['event']['participant_role'] = $participant_role["{$this->_values['event']['default_role_id']}"];
304 // check for is_monetary status
305 $isMonetary = CRM_Utils_Array
::value('is_monetary', $this->_values
['event']);
306 $isPayLater = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $this->_eventId
, 'is_pay_later');
307 //check for variour combination for paylater, payment
308 //process with paid event.
309 if ($isMonetary && (!$isPayLater ||
!empty($this->_values
['event']['payment_processor']))) {
310 $ppID = CRM_Utils_Array
::value('payment_processor',
311 $this->_values
['event']
314 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);
316 $ppIds = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ppID);
317 $this->_paymentProcessors
= CRM_Financial_BAO_PaymentProcessor
::getPayments($ppIds,
320 $this->set('paymentProcessors', $this->_paymentProcessors
);
322 //set default payment processor
323 if (!empty($this->_paymentProcessors
) && empty($this->_paymentProcessor
)) {
324 foreach ($this->_paymentProcessors
as $ppId => $values) {
325 if ($values['is_default'] == 1 ||
(count($this->_paymentProcessors
) == 1)) {
326 $defaultProcessorId = $ppId;
332 if (isset($defaultProcessorId)) {
333 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($defaultProcessorId, $this->_mode
);
335 // make sure we have a valid payment class, else abort
336 if ($this->_values
['event']['is_monetary']) {
337 if (!CRM_Utils_System
::isNull($this->_paymentProcessors
)) {
338 foreach ($this->_paymentProcessors
as $eachPaymentProcessor) {
339 // check selected payment processor is active
340 if (!$eachPaymentProcessor) {
341 CRM_Core_Error
::fatal(ts('The site administrator must set a Payment Processor for this event in order to use online registration.'));
343 // ensure that processor has a valid config
344 $payment = CRM_Core_Payment
::singleton($this->_mode
, $eachPaymentProcessor, $this);
345 $error = $payment->checkConfig();
346 if (!empty($error)) {
347 CRM_Core_Error
::fatal($error);
354 self
::initEventFee($this, $this->_eventId
);
356 // get the profile ids
357 $ufJoinParams = array(
358 'entity_table' => 'civicrm_event',
359 // CRM-4377:Â CiviEvent for the main participant, CiviEvent_Additional for additional participants
360 'module' => 'CiviEvent',
361 'entity_id' => $this->_eventId
,
363 list($this->_values
['custom_pre_id'],
364 $this->_values
['custom_post_id']
365 ) = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
367 // set profiles for additional participants
368 if ($this->_values
['event']['is_multiple_registrations']) {
369 // CRM-4377:Â CiviEvent for the main participant, CiviEvent_Additional for additional participants
370 $ufJoinParams['module'] = 'CiviEvent_Additional';
372 list($this->_values
['additional_custom_pre_id'],
373 $this->_values
['additional_custom_post_id'], $preActive, $postActive
374 ) = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
376 // CRM-4377:Â we need to maintain backward compatibility, hence if there is profile for main contact
377 // set same profile for additional contacts.
378 if ($this->_values
['custom_pre_id'] && !$this->_values
['additional_custom_pre_id']) {
379 $this->_values
['additional_custom_pre_id'] = $this->_values
['custom_pre_id'];
382 if ($this->_values
['custom_post_id'] && !$this->_values
['additional_custom_post_id']) {
383 $this->_values
['additional_custom_post_id'] = $this->_values
['custom_post_id'];
385 // now check for no profile condition, in that case is_active = 0
386 if (isset($preActive) && !$preActive) {
387 unset($this->_values
['additional_custom_pre_id']);
389 if (isset($postActive) && !$postActive) {
390 unset($this->_values
['additional_custom_post_id']);
393 // get the billing location type
394 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
395 // CRM-8108 remove ts from Billing as the location type can not be translated in CiviCRM!
396 //$this->_bltID = array_search( ts('Billing'), $locationTypes );
397 $this->_bltID
= array_search('Billing', $locationTypes);
398 if (!$this->_bltID
) {
399 CRM_Core_Error
::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
401 $this->set('bltID', $this->_bltID
);
403 if ($this->_values
['event']['is_monetary'] &&
404 ($this->_paymentProcessor
['billing_mode'] & CRM_Core_Payment
::BILLING_MODE_FORM
)
406 CRM_Core_Payment_Form
::setCreditCardFields($this);
408 $params = array('entity_id' => $this->_eventId
, 'entity_table' => 'civicrm_event');
409 $this->_values
['location'] = CRM_Core_BAO_Location
::getValues($params, TRUE);
411 $this->set('values', $this->_values
);
412 $this->set('fields', $this->_fields
);
414 $this->_availableRegistrations
=
415 CRM_Event_BAO_Participant
::eventFull(
416 $this->_values
['event']['id'], TRUE,
417 CRM_Utils_Array
::value('has_waitlist', $this->_values
['event'])
419 $this->set('availableRegistrations', $this->_availableRegistrations
);
421 $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor
);
423 // check if this is a paypal auto return and redirect accordingly
424 if (CRM_Core_Payment
::paypalRedirect($this->_paymentProcessor
)) {
425 $url = CRM_Utils_System
::url('civicrm/event/register',
426 "_qf_ThankYou_display=1&qfKey={$this->controller->_key}"
428 CRM_Utils_System
::redirect($url);
430 $this->_contributeMode
= $this->get('contributeMode');
431 $this->assign('contributeMode', $this->_contributeMode
);
433 // setting CMS page title
434 CRM_Utils_System
::setTitle($this->_values
['event']['title']);
435 $this->assign('title', $this->_values
['event']['title']);
437 $this->assign('paidEvent', $this->_values
['event']['is_monetary']);
439 // we do not want to display recently viewed items on Registration pages
440 $this->assign('displayRecent', FALSE);
441 // Registration page values are cleared from session, so can't use normal Printer Friendly view.
442 // Use Browser Print instead.
443 $this->assign('browserPrint', TRUE);
445 $isShowLocation = CRM_Utils_Array
::value('is_show_location', $this->_values
['event']);
446 $this->assign('isShowLocation', $isShowLocation);
448 $pcpId = CRM_Utils_Request
::retrieve('pcpId', 'Positive', $this);
450 $pcp = CRM_PCP_BAO_PCP
::handlePcp($pcpId, 'event', $this->_values
['event']);
451 $this->_pcpId
= $pcp['pcpId'];
452 $this->_values
['event']['intro_text'] = CRM_Utils_Array
::value('intro_text', $pcp['pcpInfo']);
455 // assign all event properties so wizard templates can display event info.
456 $this->assign('event', $this->_values
['event']);
457 $this->assign('location', $this->_values
['location']);
458 $this->assign('bltID', $this->_bltID
);
459 $isShowLocation = CRM_Utils_Array
::value('is_show_location', $this->_values
['event']);
460 $this->assign('isShowLocation', $isShowLocation);
462 $config->defaultCurrency
= CRM_Utils_Array
::value('currency', $this->_values
['event'],
463 $config->defaultCurrency
466 //lets allow user to override campaign.
467 $campID = CRM_Utils_Request
::retrieve('campID', 'Positive', $this);
468 if ($campID && CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
469 $this->_values
['event']['campaign_id'] = $campID;
472 // check if billing block is required for pay later
473 if (CRM_Utils_Array
::value('is_pay_later', $this->_values
['event'])) {
474 $this->_isBillingAddressRequiredForPayLater
= CRM_Utils_Array
::value('is_billing_required', $this->_values
['event']);
475 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater
);
480 * assign the minimal set of variables to the template
485 function assignToTemplate() {
486 //process only primary participant params
487 $this->_params
= $this->get('params');
488 if (isset($this->_params
[0])) {
489 $params = $this->_params
[0];
492 if (!empty($params['billing_first_name'])) {
493 $name = $params['billing_first_name'];
496 if (!empty($params['billing_middle_name'])) {
497 $name .= " {$params['billing_middle_name']}";
500 if (!empty($params['billing_last_name'])) {
501 $name .= " {$params['billing_last_name']}";
503 $this->assign('billingName', $name);
504 $this->set('name', $name);
507 'amount', 'currencyID', 'credit_card_type',
508 'trxn_id', 'amount_level', 'receive_date',
511 foreach ($vars as $v) {
512 if (!empty($params[$v])) {
513 if ($v == 'receive_date') {
514 $this->assign($v, CRM_Utils_Date
::mysqlToIso($params[$v]));
517 $this->assign($v, $params[$v]);
520 elseif (CRM_Utils_Array
::value('amount', $params) == 0) {
521 $this->assign($v, CRM_Utils_Array
::value($v, $params));
525 // assign the address formatted up for display
526 $addressParts = array(
527 "street_address-{$this->_bltID}",
528 "city-{$this->_bltID}",
529 "postal_code-{$this->_bltID}",
530 "state_province-{$this->_bltID}",
531 "country-{$this->_bltID}",
533 $addressFields = array();
534 foreach ($addressParts as $part) {
535 list($n, $id) = explode('-', $part);
536 if (isset($params['billing_' . $part])) {
537 $addressFields[$n] = CRM_Utils_Array
::value('billing_' . $part, $params);
541 $this->assign('address', CRM_Utils_Address
::format($addressFields));
543 if ($this->_contributeMode
== 'direct' && empty($params['is_pay_later'])) {
544 $date = CRM_Utils_Date
::format(CRM_Utils_Array
::value('credit_card_exp_date', $params));
545 $date = CRM_Utils_Date
::mysqlToIso($date);
546 $this->assign('credit_card_exp_date', $date);
547 $this->assign('credit_card_number',
548 CRM_Utils_System
::mungeCreditCard(CRM_Utils_Array
::value('credit_card_number', $params))
552 // get the email that the confirmation would have been sent to
553 $session = CRM_Core_Session
::singleton();
555 // assign is_email_confirm to templates
556 if (isset($this->_values
['event']['is_email_confirm'])) {
557 $this->assign('is_email_confirm', $this->_values
['event']['is_email_confirm']);
560 // assign pay later stuff
561 $params['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, FALSE);
562 $this->assign('is_pay_later', $params['is_pay_later']);
563 if ($params['is_pay_later']) {
564 $this->assign('pay_later_text', $this->_values
['event']['pay_later_text']);
565 $this->assign('pay_later_receipt', $this->_values
['event']['pay_later_receipt']);
568 // also assign all participantIDs to the template
569 // useful in generating confirmation numbers if needed
570 $this->assign('participantIDs',
571 $this->_participantIDS
576 * Function to add the custom fields
580 * @param bool $viewOnly
585 function buildCustom($id, $name, $viewOnly = FALSE) {
587 $button = substr($this->controller
->getButtonName(), -4);
588 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
589 $session = CRM_Core_Session
::singleton();
590 $contactID = $session->get('userID');
592 // we don't allow conflicting fields to be
593 // configured via profile
594 $fieldsToIgnore = array(
595 'participant_fee_amount' => 1,
596 'participant_fee_level' => 1,
602 foreach ($id as $profileID) {
603 $field = CRM_Core_BAO_UFGroup
::getFields($profileID, FALSE, CRM_Core_Action
::ADD
,
604 NULL, NULL, FALSE, NULL,
605 FALSE, NULL, CRM_Core_Permission
::CREATE
,
608 $fields = array_merge($fields, $field);
612 if (CRM_Core_BAO_UFGroup
::filterUFGroups($id, $contactID)) {
613 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
,
614 NULL, NULL, FALSE, NULL,
615 FALSE, NULL, CRM_Core_Permission
::CREATE
,
622 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
,
623 NULL, NULL, FALSE, NULL,
624 FALSE, NULL, CRM_Core_Permission
::CREATE
,
629 if (array_intersect_key($fields, $fieldsToIgnore)) {
630 $fields = array_diff_key($fields, $fieldsToIgnore);
631 CRM_Core_Session
::setStatus(ts('Some of the profile fields cannot be configured for this page.'));
635 if (!empty($this->_fields
)) {
636 $fields = @array_diff_assoc
($fields, $this->_fields
);
639 if (empty($this->_params
[0]['additional_participants']) &&
642 CRM_Core_BAO_Address
::checkContactSharedAddressFields($fields, $contactID);
644 $this->assign($name, $fields);
645 if (is_array($fields)) {
646 foreach ($fields as $key => $field) {
648 isset($field['data_type']) &&
649 $field['data_type'] == 'File' ||
($viewOnly && $field['name'] == 'image_URL')
651 // ignore file upload fields
654 //make the field optional if primary participant
655 //have been skip the additional participant.
656 if ($button == 'skip') {
657 $field['is_required'] = FALSE;
659 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
660 elseif ($field['add_captcha'] && !$contactID) {
661 // only add captcha for first page
664 list($prefixName, $index) = CRM_Utils_System
::explode('-', $key, 2);
665 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, CRM_Profile_Form
::MODE_CREATE
, $contactID, TRUE);
667 $this->_fields
[$key] = $field;
671 if ($addCaptcha && !$viewOnly) {
672 $captcha = CRM_Utils_ReCAPTCHA
::singleton();
673 $captcha->add($this);
674 $this->assign('isCaptcha', TRUE);
685 static function initEventFee(&$form, $eventID) {
688 // retrive all active price set fields.
689 $discountId = CRM_Core_BAO_Discount
::findSet($eventID, 'civicrm_event');
690 if (property_exists($form, '_discountId') && $form->_discountId
) {
691 $discountId = $form->_discountId
;
694 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
695 $price = CRM_Price_BAO_PriceSet
::initSet($form, $eventID, 'civicrm_event', TRUE, $priceSetId);
698 $price = CRM_Price_BAO_PriceSet
::initSet($form, $eventID, 'civicrm_event', TRUE);
701 if (property_exists($form, '_context') && ($form->_context
== 'standalone'
702 ||
$form->_context
== 'participant')) {
703 $discountedEvent = CRM_Core_BAO_Discount
::getOptionGroup($eventID, 'civicrm_event');
704 if (is_array( $discountedEvent)) {
705 foreach ($discountedEvent as $key => $priceSetId) {
706 $priceSet = CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId);
707 $priceSet = CRM_Utils_Array
::value($priceSetId, $priceSet);
708 $form->_values
['discount'][$key] = CRM_Utils_Array
::value('fields', $priceSet);
709 $fieldID = key($form->_values
['discount'][$key]);
710 $form->_values
['discount'][$key][$fieldID]['name'] = CRM_Core_DAO
::getFieldValue(
711 'CRM_Price_DAO_PriceSet',
718 $eventFee = CRM_Utils_Array
::value('fee', $form->_values
);
719 if (!is_array($eventFee) ||
empty($eventFee)) {
720 $form->_values
['fee'] = array();
723 //fix for non-upgraded price sets.CRM-4256.
724 if (isset($form->_isPaidEvent
)) {
725 $isPaidEvent = $form->_isPaidEvent
;
728 $isPaidEvent = CRM_Utils_Array
::value('is_monetary', $form->_values
['event']);
730 if ($isPaidEvent && empty($form->_values
['fee'])) {
731 if (CRM_Utils_System
::getClassName($form) != 'CRM_Event_Form_Participant') {
732 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
))));
738 * Function to handle process after the confirmation of payment by User
740 * @param null $contactID
741 * @param null $contribution
742 * @param null $payment
747 function confirmPostProcess($contactID = NULL, $contribution = NULL, $payment = NULL) {
748 // add/update contact information
750 unset($this->_params
['note']);
752 //to avoid conflict overwrite $this->_params
753 $this->_params
= $this->get('value');
755 //get the amount of primary participant
756 if (!empty($this->_params
['is_primary'])) {
757 $this->_params
['fee_amount'] = $this->get('primaryParticipantAmount');
760 // add participant record
761 $participant = CRM_Event_Form_Registration
::addParticipant($this, $contactID);
762 $this->_participantIDS
[] = $participant->id
;
764 //setting register_by_id field and primaryContactId
765 if (!empty($this->_params
['is_primary'])) {
766 $this->set('registerByID', $participant->id
);
767 $this->set('primaryContactId', $contactID);
770 $this->processFirstParticipant($participant->id
);
773 CRM_Core_BAO_CustomValueTable
::postProcess($this->_params
,
774 CRM_Core_DAO
::$_nullArray,
775 'civicrm_participant',
780 $createPayment = (CRM_Utils_Array
::value('amount', $this->_params
, 0) != 0) ?
TRUE : FALSE;
782 // force to create zero amount payment, CRM-5095
783 // we know the amout is zero since createPayment is false
784 if (!$createPayment &&
785 (isset($contribution) && $contribution->id
) &&
786 $this->_priceSetId
&&
789 $createPayment = TRUE;
792 if ($createPayment && $this->_values
['event']['is_monetary'] && !empty($this->_params
['contributionID'])) {
793 $paymentParams = array(
794 'participant_id' => $participant->id
,
795 'contribution_id' => $contribution->id
,
798 $paymentPartcipant = CRM_Event_BAO_ParticipantPayment
::create($paymentParams, $ids);
801 //set only primary participant's params for transfer checkout.
802 if (($this->_contributeMode
== 'checkout' ||
$this->_contributeMode
== 'notify') && !empty($this->_params
['is_primary'])) {
803 $this->_params
['participantID'] = $participant->id
;
804 $this->set('primaryParticipant', $this->_params
);
807 $this->assign('action', $this->_action
);
810 if (!empty($this->_params
['cms_create_account'])) {
811 $this->_params
['contactID'] = $contactID;
813 if (array_key_exists('email-5', $this->_params
)) {
816 foreach ($this->_params
as $name => $dontCare) {
817 if (substr($name, 0, 5) == 'email') {
824 // we should use primary email for
825 // 1. free event registration.
826 // 2. pay later participant.
827 // 3. waiting list participant.
828 // 4. require approval participant.
829 if (!empty($this->_params
['is_pay_later']) ||
830 $this->_allowWaitlist ||
$this->_requireApproval ||
empty($this->_values
['event']['is_monetary'])) {
831 $mail = 'email-Primary';
834 if (!CRM_Core_BAO_CMSUser
::create($this->_params
, $mail)) {
835 CRM_Core_Error
::statusBounce(ts('Your profile is not saved and Account is not created.'));
841 * Process the participant
849 public static function addParticipant(&$form, $contactID) {
850 if (empty($form->_params
)) {
853 $params = $form->_params
;
854 $transaction = new CRM_Core_Transaction();
856 $groupName = 'participant_role';
858 SELECT v.label as label ,v.value as value
859 FROM civicrm_option_value v,
860 civicrm_option_group g
861 WHERE v.option_group_id = g.id
866 $p = array(1 => array($groupName, 'String'));
868 $dao = CRM_Core_DAO
::executeQuery($query, $p);
870 $roleID = $dao->value
;
873 // handle register date CRM-4320
874 $registerDate = NULL;
875 if (!empty($form->_allowConfirmation
) && $form->_participantId
) {
876 $registerDate = $params['participant_register_date'];
878 elseif (!empty($params['participant_register_date']) &&
879 is_array($params['participant_register_date']) &&
880 !empty($params['participant_register_date'])
882 $registerDate = CRM_Utils_Date
::format($params['participant_register_date']);
885 $participantFields = CRM_Event_DAO_Participant
::fields();
886 $participantParams = array('id' => CRM_Utils_Array
::value('participant_id', $params),
887 'contact_id' => $contactID,
888 'event_id' => $form->_eventId ?
$form->_eventId
: $params['event_id'],
889 'status_id' => CRM_Utils_Array
::value('participant_status',
892 'role_id' => CRM_Utils_Array
::value('participant_role_id',
895 'register_date' => ($registerDate) ?
$registerDate : date('YmdHis'),
896 'source' => CRM_Utils_String
::ellipsify(
897 isset($params['participant_source']) ?
898 CRM_Utils_Array
::value('participant_source', $params) : CRM_Utils_Array
::value('description', $params),
899 $participantFields['participant_source']['maxlength']
901 'fee_level' => CRM_Utils_Array
::value('amount_level', $params),
902 'is_pay_later' => CRM_Utils_Array
::value('is_pay_later', $params, 0),
903 'fee_amount' => CRM_Utils_Array
::value('fee_amount', $params),
904 'registered_by_id' => CRM_Utils_Array
::value('registered_by_id', $params),
905 'discount_id' => CRM_Utils_Array
::value('discount_id', $params),
906 'fee_currency' => CRM_Utils_Array
::value('currencyID', $params),
907 'campaign_id' => CRM_Utils_Array
::value('campaign_id', $params),
910 if ($form->_action
& CRM_Core_Action
::PREVIEW || CRM_Utils_Array
::value('mode', $params) == 'test') {
911 $participantParams['is_test'] = 1;
914 $participantParams['is_test'] = 0;
917 if (!empty($form->_params
['note'])) {
918 $participantParams['note'] = $form->_params
['note'];
920 elseif (!empty($form->_params
['participant_note'])) {
921 $participantParams['note'] = $form->_params
['participant_note'];
924 // reuse id if one already exists for this one (can happen
925 // with back button being hit etc)
926 if (!$participantParams['id'] && !empty($params['contributionID'])) {
927 $pID = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment',
928 $params['contributionID'],
932 $participantParams['id'] = $pID;
934 $participantParams['discount_id'] = CRM_Core_BAO_Discount
::findSet($form->_eventId
, 'civicrm_event');
936 if (!$participantParams['discount_id']) {
937 $participantParams['discount_id'] = "null";
940 $participant = CRM_Event_BAO_Participant
::create($participantParams);
942 $transaction->commit();
947 /* Calculate the total participant count as per params.
949 * @param array $params user params.
951 * @return $totalCount total participant count.
957 * @param bool $skipCurrent
961 public static function getParticipantCount(&$form, $params, $skipCurrent = FALSE) {
963 if (!is_array($params) ||
empty($params)) {
967 $priceSetId = $form->get('priceSetId');
968 $addParticipantNum = substr($form->_name
, 12);
969 $priceSetFields = $priceSetDetails = array();
970 $hasPriceFieldsCount = FALSE;
972 $priceSetDetails = $form->get('priceSet');
973 if (isset($priceSetDetails['optionsCountTotal'])
974 && $priceSetDetails['optionsCountTotal']
976 $hasPriceFieldsCount = TRUE;
977 $priceSetFields = $priceSetDetails['optionsCountDetails']['fields'];
981 $singleFormParams = FALSE;
982 foreach ($params as $key => $val) {
983 if (!is_numeric($key)) {
984 $singleFormParams = TRUE;
989 //first format the params.
990 if ($singleFormParams) {
991 $params = self
::formatPriceSetParams($form, $params);
992 $params = array($params);
995 foreach ($params as $key => $values) {
996 if (!is_numeric($key) ||
998 ($skipCurrent && ($addParticipantNum == $key))
1005 $cacheCount = CRM_Utils_Array
::value($key, $form->_lineItemParticipantsCount
);
1006 if ($cacheCount && is_numeric($cacheCount)) {
1007 $count = $cacheCount;
1011 if (!$usedCache && $hasPriceFieldsCount) {
1013 foreach ($values as $valKey => $value) {
1014 if (strpos($valKey, 'price_') === FALSE) {
1017 $priceFieldId = substr($valKey, 6);
1018 if (!$priceFieldId ||
1019 !is_array($value) ||
1020 !array_key_exists($priceFieldId, $priceSetFields)
1024 foreach ($value as $optId => $optVal) {
1025 $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
1026 if ($currentCount) {
1027 $count +
= $currentCount;
1035 $totalCount +
= $count;
1044 /* Format user submitted price set params.
1045 * Convert price set each param as an array.
1047 * @param $params an array of user submitted params.
1050 * @return array $formatted, formatted price set params.
1059 public static function formatPriceSetParams(&$form, $params) {
1060 if (!is_array($params) ||
empty($params)) {
1064 $priceSetId = $form->get('priceSetId');
1068 $priceSetDetails = $form->get('priceSet');
1070 foreach ($params as $key => & $value) {
1072 if (strpos($key, 'price_') !== FALSE) {
1073 $fieldId = substr($key, 6);
1074 if (!array_key_exists($fieldId, $priceSetDetails['fields']) ||
1080 $field = $priceSetDetails['fields'][$fieldId];
1081 if ($field['html_type'] == 'Text') {
1082 $fieldOption = current($field['options']);
1083 $value = array($fieldOption['id'] => $value);
1086 $value = array($value => TRUE);
1094 /* Calculate total count for each price set options.
1095 * those are currently selected by user.
1097 * @param $form form object.
1100 * @return array $optionsCount, array of each option w/ count total.
1108 public static function getPriceSetOptionCount(&$form) {
1109 $params = $form->get('params');
1110 $priceSet = $form->get('priceSet');
1111 $priceSetId = $form->get('priceSetId');
1113 $optionsCount = array();
1115 !is_array($priceSet) ||
1117 !is_array($params) ||
1120 return $optionsCount;
1123 $priceSetFields = $priceMaxFieldDetails = array();
1124 if (!empty($priceSet['optionsCountTotal'])) {
1125 $priceSetFields = $priceSet['optionsCountDetails']['fields'];
1128 if (!empty($priceSet['optionsMaxValueTotal'])) {
1129 $priceMaxFieldDetails = $priceSet['optionsMaxValueDetails']['fields'];
1132 $addParticipantNum = substr($form->_name
, 12);
1133 foreach ($params as $pCnt => $values) {
1134 if ($values == 'skip' ||
1135 $pCnt == $addParticipantNum
1140 foreach ($values as $valKey => $value) {
1141 if (strpos($valKey, 'price_') === FALSE) {
1145 $priceFieldId = substr($valKey, 6);
1146 if (!$priceFieldId ||
1147 !is_array($value) ||
1148 !(array_key_exists($priceFieldId, $priceSetFields) ||
array_key_exists($priceFieldId, $priceMaxFieldDetails))
1153 foreach ($value as $optId => $optVal) {
1154 if (CRM_Utils_Array
::value('html_type', $priceSet['fields'][$priceFieldId]) == 'Text') {
1155 $currentCount = $optVal;
1161 if (isset($priceSetFields[$priceFieldId]) && isset($priceSetFields[$priceFieldId]['options'][$optId])) {
1162 $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
1165 $optionsCount[$optId] = $currentCount + CRM_Utils_Array
::value($optId, $optionsCount, 0);
1170 return $optionsCount;
1174 * @param string $suffix
1176 * @return null|string
1178 function checkTemplateFileExists($suffix = '') {
1179 if ($this->_eventId
) {
1180 $templateName = $this->_name
;
1181 if (substr($templateName, 0, 12) == 'Participant_') {
1182 $templateName = 'AdditionalParticipant';
1185 $templateFile = "CRM/Event/Form/Registration/{$this->_eventId}/{$templateName}.{$suffix}tpl";
1186 $template = CRM_Core_Form
::getTemplate();
1187 if ($template->template_exists($templateFile)) {
1188 return $templateFile;
1195 * @return null|string
1197 function getTemplateFileName() {
1198 $fileName = $this->checkTemplateFileExists();
1199 return $fileName ?
$fileName : parent
::getTemplateFileName();
1203 * @return null|string
1205 function overrideExtraTemplateFileName() {
1206 $fileName = $this->checkTemplateFileExists('extra.');
1207 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();
1211 * Validate price set submitted params for price option limit,
1212 * as well as user should select at least one price field option.
1213 * @param unknown_type $form
1214 * @param unknown_type $params
1215 * @return multitype:|Ambigous <multitype:, string, string>
1217 static function validatePriceSet(&$form, $params) {
1219 $hasOptMaxValue = FALSE;
1220 if (!is_array($params) ||
empty($params)) {
1224 $currentParticipantNum = substr($form->_name
, 12);
1225 if (!$currentParticipantNum) {
1226 $currentParticipantNum = 0;
1229 $priceSetId = $form->get('priceSetId');
1230 $priceSetDetails = $form->get('priceSet');
1233 !is_array($priceSetDetails) ||
1234 empty($priceSetDetails)
1239 $optionsCountDetails = $optionsMaxValueDetails = array();
1241 isset($priceSetDetails['optionsMaxValueTotal'])
1242 && $priceSetDetails['optionsMaxValueTotal']
1244 $hasOptMaxValue = TRUE;
1245 $optionsMaxValueDetails = $priceSetDetails['optionsMaxValueDetails']['fields'];
1248 isset($priceSetDetails['optionsCountTotal'])
1249 && $priceSetDetails['optionsCountTotal']
1251 $hasOptCount = TRUE;
1252 $optionsCountDetails = $priceSetDetails['optionsCountDetails']['fields'];
1254 $feeBlock = $form->_feeBlock
;
1256 if (empty($feeBlock)) {
1257 $feeBlock = $priceSetDetails['fields'];
1260 $optionMaxValues = $fieldSelected = array();
1261 foreach ($params as $pNum => $values) {
1262 if (!is_array($values) ||
$values == 'skip') {
1266 foreach ($values as $valKey => $value) {
1267 if (strpos($valKey, 'price_') === FALSE) {
1270 $priceFieldId = substr($valKey, 6);
1271 $noneOptionValueSelected = FALSE;
1272 if (!$feeBlock[$priceFieldId]['is_required'] && $value == 0) {
1273 $noneOptionValueSelected = TRUE;
1278 (!$noneOptionValueSelected && !is_array($value))
1283 $fieldSelected[$pNum] = TRUE;
1285 if (!$hasOptMaxValue ||
!is_array($value)) {
1289 foreach ($value as $optId => $optVal) {
1290 if (CRM_Utils_Array
::value('html_type', $feeBlock[$priceFieldId]) == 'Text') {
1291 $currentMaxValue = $optVal;
1294 $currentMaxValue = 1;
1297 if (isset($optionsCountDetails[$priceFieldId]) && isset($optionsCountDetails[$priceFieldId]['options'][$optId])) {
1298 $currentMaxValue = $optionsCountDetails[$priceFieldId]['options'][$optId] * $optVal;
1300 if (empty($optionMaxValues)) {
1301 $optionMaxValues[$priceFieldId][$optId] = $currentMaxValue;
1304 $optionMaxValues[$priceFieldId][$optId] =
1305 $currentMaxValue + CRM_Utils_Array
::value($optId, CRM_Utils_Array
::value($priceFieldId, $optionMaxValues), 0);
1312 //validate for option max value.
1313 foreach ($optionMaxValues as $fieldId => $values) {
1314 $options = CRM_Utils_Array
::value('options', $feeBlock[$fieldId], array());
1315 foreach ($values as $optId => $total) {
1316 $optMax = $optionsMaxValueDetails[$fieldId]['options'][$optId];
1317 $opDbCount = CRM_Utils_Array
::value('db_total_count', $options[$optId], 0);
1318 $total +
= $opDbCount;
1319 if ($optMax && $total > $optMax) {
1320 if ($opDbCount && ($opDbCount >= $optMax)) {
1321 $errors[$currentParticipantNum]["price_{$fieldId}"] =
1322 ts('Sorry, this option is currently sold out.');
1324 elseif (($optMax - $opDbCount) == 1) {
1325 $errors[$currentParticipantNum]["price_{$fieldId}"] =
1326 ts('Sorry, currently only a single seat is available for this option.', array(1 => ($optMax - $opDbCount)));
1329 $errors[$currentParticipantNum]["price_{$fieldId}"] =
1330 ts('Sorry, currently only %1 seats are available for this option.', array(1 => ($optMax - $opDbCount)));
1336 //validate for price field selection.
1337 foreach ($params as $pNum => $values) {
1338 if (!is_array($values) ||
$values == 'skip') {
1341 if (empty($fieldSelected[$pNum])) {
1342 $errors[$pNum]['_qf_default'] = ts('Select at least one option from Event Fee(s).');
1349 // set the first participant ID if not set, CRM-10032
1351 * @param $participantID
1353 function processFirstParticipant($participantID) {
1354 $this->_participantId
= $participantID;
1355 $this->set('participantId', $this->_participantId
);
1357 $ids = $participantValues = array();
1358 $participantParams = array('id' => $this->_participantId
);
1359 CRM_Event_BAO_Participant
::getValues($participantParams, $participantValues, $ids);
1360 $this->_values
['participant'] = $participantValues[$this->_participantId
];
1361 $this->set('values', $this->_values
);
1363 // also set the allow confirmation stuff
1364 if (array_key_exists(
1365 $this->_values
['participant']['status_id'],
1366 CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Pending'")
1368 $this->_allowConfirmation
= TRUE;
1369 $this->set('allowConfirmation', TRUE);
1374 * @todo - combine this with CRM_Event_BAO_Event::validRegistrationRequest
1375 * (probably extract relevant values here & call that with them & handle bounces & redirects here -as
1376 * those belong in the form layer)
1378 * @param string $redirect
1380 function checkValidEvent($redirect = NULL) {
1381 // is the event active (enabled)?
1382 if (!$this->_values
['event']['is_active']) {
1383 // form is inactive, die a fatal death
1384 CRM_Core_Error
::statusBounce(ts('The event you requested is currently unavailable (contact the site administrator for assistance).'));
1387 // is online registration is enabled?
1388 if (!$this->_values
['event']['is_online_registration']) {
1389 CRM_Core_Error
::statusBounce(ts('Online registration is not currently available for this event (contact the site administrator for assistance).'), $redirect);
1392 // is this an event template ?
1393 if (!empty($this->_values
['event']['is_template'])) {
1394 CRM_Core_Error
::statusBounce(ts('Event templates are not meant to be registered.'), $redirect);
1397 $now = date('YmdHis');
1398 $startDate = CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('registration_start_date',
1399 $this->_values
['event']
1406 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);
1409 $endDate = CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('registration_end_date',
1410 $this->_values
['event']
1416 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);