3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
59 * The array of ids of all the participant we are proceessing
63 protected $_participantIDS = NULL;
66 * The id of the participant we are proceessing
70 protected $_participantId;
73 * Is participant able to walk registration wizard.
77 public $_allowConfirmation;
80 * Is participant requires approval
84 public $_requireApproval;
87 * Is event configured for waitlist.
91 public $_allowWaitlist;
94 * Store additional participant ids
95 * when there are pre-registered.
99 public $_additionalParticipantIds;
102 * The mode that we are in
110 * The values for the contribution db object
117 * The paymentProcessor attributes for this page
121 public $_paymentProcessor;
124 * The params submitted by the form and computed by the app
131 * The fields involved in this contribution page
138 * The billing location id for this contribiution page
145 * Price Set ID, if the new price set method is used
149 public $_priceSetId = NULL;
152 * Array of fields for the price set
162 /* Is event already full.
167 public $_isEventFull;
170 public $_lineItemParticipantsCount;
171 public $_availableRegistrations;
173 public $_forcePayement;
175 public $_isBillingAddressRequiredForPayLater;
178 * Set variables up before form is built
183 public function preProcess() {
184 $this->_eventId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, TRUE);
185 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE);
188 $this->_participantId
= CRM_Utils_Request
::retrieve('participantId', 'Positive', $this);
191 $this->_mode
= ($this->_action
== 1024) ?
'test' : 'live';
193 $this->_values
= $this->get('values');
194 $this->_fields
= $this->get('fields');
195 $this->_bltID
= $this->get('bltID');
196 $this->_paymentProcessor
= $this->get('paymentProcessor');
197 $this->_priceSetId
= $this->get('priceSetId');
198 $this->_priceSet
= $this->get('priceSet');
199 $this->_lineItem
= $this->get('lineItem');
200 $this->_isEventFull
= $this->get('isEventFull');
201 $this->_lineItemParticipantsCount
= $this->get('lineItemParticipants');
202 if (!is_array($this->_lineItem
)) {
203 $this->_lineItem
= array();
205 if (!is_array($this->_lineItemParticipantsCount
)) {
206 $this->_lineItemParticipantsCount
= array();
208 $this->_availableRegistrations
= $this->get('availableRegistrations');
209 $this->_participantIDS
= $this->get('participantIDs');
211 //check if participant allow to walk registration wizard.
212 $this->_allowConfirmation
= $this->get('allowConfirmation');
214 // check for Approval
215 $this->_requireApproval
= $this->get('requireApproval');
217 // check for waitlisting.
218 $this->_allowWaitlist
= $this->get('allowWaitlist');
220 $this->_forcePayement
= $this->get('forcePayement');
222 //get the additional participant ids.
223 $this->_additionalParticipantIds
= $this->get('additionalParticipantIds');
224 $config = CRM_Core_Config
::singleton();
226 if (!$this->_values
) {
227 // create redirect URL to send folks back to event info page is registration not available
228 $infoUrl = CRM_Utils_System
::url('civicrm/event/info', "reset=1&id={$this->_eventId}",
229 FALSE, NULL, FALSE, TRUE
232 // this is the first time we are hitting this, so check for permissions here
233 if (!CRM_Core_Permission
::event(CRM_Core_Permission
::EDIT
, $this->_eventId
, 'register for events')) {
234 CRM_Core_Error
::statusBounce(ts('You do not have permission to register for this event'), $infoUrl);
237 // get all the values from the dao object
238 $this->_values
= $this->_fields
= array();
239 $this->_forcePayement
= FALSE;
241 //retrieve event information
242 $params = array('id' => $this->_eventId
);
243 CRM_Event_BAO_Event
::retrieve($params, $this->_values
['event']);
245 $this->checkValidEvent($infoUrl);
246 // get the participant values, CRM-4320
247 $this->_allowConfirmation
= FALSE;
248 if ($this->_participantId
) {
249 $this->processFirstParticipant($this->_participantId
);
251 //check for additional participants.
252 if ($this->_allowConfirmation
&& $this->_values
['event']['is_multiple_registrations']) {
253 $additionalParticipantIds = CRM_Event_BAO_Participant
::getAdditionalParticipantIds($this->_participantId
);
255 foreach ($additionalParticipantIds as $additionalParticipantId) {
256 $this->_additionalParticipantIds
[$cnt] = $additionalParticipantId;
259 $this->set('additionalParticipantIds', $this->_additionalParticipantIds
);
262 $eventFull = CRM_Event_BAO_Participant
::eventFull($this->_eventId
, FALSE,
263 CRM_Utils_Array
::value('has_waitlist', $this->_values
['event'])
266 $this->_allowWaitlist
= $this->_isEventFull
= FALSE;
267 if ($eventFull && !$this->_allowConfirmation
) {
268 $this->_isEventFull
= TRUE;
269 //lets redirecting to info only when to waiting list.
270 $this->_allowWaitlist
= CRM_Utils_Array
::value('has_waitlist', $this->_values
['event']);
271 if (!$this->_allowWaitlist
) {
272 CRM_Utils_System
::redirect($infoUrl);
275 $this->set('isEventFull', $this->_isEventFull
);
276 $this->set('allowWaitlist', $this->_allowWaitlist
);
278 //check for require requires approval.
279 $this->_requireApproval
= FALSE;
280 if (!empty($this->_values
['event']['requires_approval']) && !$this->_allowConfirmation
) {
281 $this->_requireApproval
= TRUE;
283 $this->set('requireApproval', $this->_requireApproval
);
285 if (isset($this->_values
['event']['default_role_id'])) {
286 $participant_role = CRM_Core_OptionGroup
::values('participant_role');
287 $this->_values
['event']['participant_role'] = $participant_role["{$this->_values['event']['default_role_id']}"];
289 // check for is_monetary status
290 $isMonetary = CRM_Utils_Array
::value('is_monetary', $this->_values
['event']);
291 $isPayLater = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $this->_eventId
, 'is_pay_later');
292 //check for variour combination for paylater, payment
293 //process with paid event.
294 if ($isMonetary && (!$isPayLater ||
!empty($this->_values
['event']['payment_processor']))) {
295 $ppID = CRM_Utils_Array
::value('payment_processor',
296 $this->_values
['event']
299 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);
301 $ppIds = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ppID);
302 $this->_paymentProcessors
= CRM_Financial_BAO_PaymentProcessor
::getPayments($ppIds,
305 $this->set('paymentProcessors', $this->_paymentProcessors
);
307 //set default payment processor
308 if (!empty($this->_paymentProcessors
) && empty($this->_paymentProcessor
)) {
309 foreach ($this->_paymentProcessors
as $ppId => $values) {
310 if ($values['is_default'] == 1 ||
(count($this->_paymentProcessors
) == 1)) {
311 $defaultProcessorId = $ppId;
317 if (isset($defaultProcessorId)) {
318 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($defaultProcessorId, $this->_mode
);
320 // make sure we have a valid payment class, else abort
321 if ($this->_values
['event']['is_monetary']) {
322 if (!CRM_Utils_System
::isNull($this->_paymentProcessors
)) {
323 foreach ($this->_paymentProcessors
as $eachPaymentProcessor) {
324 // check selected payment processor is active
325 if (!$eachPaymentProcessor) {
326 CRM_Core_Error
::fatal(ts('The site administrator must set a Payment Processor for this event in order to use online registration.'));
328 // ensure that processor has a valid config
329 $payment = CRM_Core_Payment
::singleton($this->_mode
, $eachPaymentProcessor, $this);
330 $error = $payment->checkConfig();
331 if (!empty($error)) {
332 CRM_Core_Error
::fatal($error);
339 self
::initEventFee($this, $this->_eventId
);
341 // get the profile ids
342 $ufJoinParams = array(
343 'entity_table' => 'civicrm_event',
344 // CRM-4377:Â CiviEvent for the main participant, CiviEvent_Additional for additional participants
345 'module' => 'CiviEvent',
346 'entity_id' => $this->_eventId
,
348 list($this->_values
['custom_pre_id'],
349 $this->_values
['custom_post_id']
350 ) = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
352 // set profiles for additional participants
353 if ($this->_values
['event']['is_multiple_registrations']) {
354 // CRM-4377:Â CiviEvent for the main participant, CiviEvent_Additional for additional participants
355 $ufJoinParams['module'] = 'CiviEvent_Additional';
357 list($this->_values
['additional_custom_pre_id'],
358 $this->_values
['additional_custom_post_id'], $preActive, $postActive
359 ) = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
361 // CRM-4377:Â we need to maintain backward compatibility, hence if there is profile for main contact
362 // set same profile for additional contacts.
363 if ($this->_values
['custom_pre_id'] && !$this->_values
['additional_custom_pre_id']) {
364 $this->_values
['additional_custom_pre_id'] = $this->_values
['custom_pre_id'];
367 if ($this->_values
['custom_post_id'] && !$this->_values
['additional_custom_post_id']) {
368 $this->_values
['additional_custom_post_id'] = $this->_values
['custom_post_id'];
370 // now check for no profile condition, in that case is_active = 0
371 if (isset($preActive) && !$preActive) {
372 unset($this->_values
['additional_custom_pre_id']);
374 if (isset($postActive) && !$postActive) {
375 unset($this->_values
['additional_custom_post_id']);
378 // get the billing location type
379 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
380 // CRM-8108 remove ts from Billing as the location type can not be translated in CiviCRM!
381 //$this->_bltID = array_search( ts('Billing'), $locationTypes );
382 $this->_bltID
= array_search('Billing', $locationTypes);
383 if (!$this->_bltID
) {
384 CRM_Core_Error
::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
386 $this->set('bltID', $this->_bltID
);
388 if ($this->_values
['event']['is_monetary']) {
389 CRM_Core_Payment_Form
::setPaymentFieldsByProcessor($this, $this->_paymentProcessor
);
391 $params = array('entity_id' => $this->_eventId
, 'entity_table' => 'civicrm_event');
392 $this->_values
['location'] = CRM_Core_BAO_Location
::getValues($params, TRUE);
394 $this->set('values', $this->_values
);
395 $this->set('fields', $this->_fields
);
397 $this->_availableRegistrations
=
398 CRM_Event_BAO_Participant
::eventFull(
399 $this->_values
['event']['id'], TRUE,
400 CRM_Utils_Array
::value('has_waitlist', $this->_values
['event'])
402 $this->set('availableRegistrations', $this->_availableRegistrations
);
404 $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor
);
406 // check if this is a paypal auto return and redirect accordingly
407 if (CRM_Core_Payment
::paypalRedirect($this->_paymentProcessor
)) {
408 $url = CRM_Utils_System
::url('civicrm/event/register',
409 "_qf_ThankYou_display=1&qfKey={$this->controller->_key}"
411 CRM_Utils_System
::redirect($url);
413 $this->_contributeMode
= $this->get('contributeMode');
414 $this->assign('contributeMode', $this->_contributeMode
);
416 // setting CMS page title
417 CRM_Utils_System
::setTitle($this->_values
['event']['title']);
418 $this->assign('title', $this->_values
['event']['title']);
420 $this->assign('paidEvent', $this->_values
['event']['is_monetary']);
422 // we do not want to display recently viewed items on Registration pages
423 $this->assign('displayRecent', FALSE);
424 // Registration page values are cleared from session, so can't use normal Printer Friendly view.
425 // Use Browser Print instead.
426 $this->assign('browserPrint', TRUE);
428 $isShowLocation = CRM_Utils_Array
::value('is_show_location', $this->_values
['event']);
429 $this->assign('isShowLocation', $isShowLocation);
431 $pcpId = CRM_Utils_Request
::retrieve('pcpId', 'Positive', $this);
433 $pcp = CRM_PCP_BAO_PCP
::handlePcp($pcpId, 'event', $this->_values
['event']);
434 $this->_pcpId
= $pcp['pcpId'];
435 $this->_values
['event']['intro_text'] = CRM_Utils_Array
::value('intro_text', $pcp['pcpInfo']);
438 // assign all event properties so wizard templates can display event info.
439 $this->assign('event', $this->_values
['event']);
440 $this->assign('location', $this->_values
['location']);
441 $this->assign('bltID', $this->_bltID
);
442 $isShowLocation = CRM_Utils_Array
::value('is_show_location', $this->_values
['event']);
443 $this->assign('isShowLocation', $isShowLocation);
445 $config->defaultCurrency
= CRM_Utils_Array
::value('currency', $this->_values
['event'],
446 $config->defaultCurrency
449 //lets allow user to override campaign.
450 $campID = CRM_Utils_Request
::retrieve('campID', 'Positive', $this);
451 if ($campID && CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
452 $this->_values
['event']['campaign_id'] = $campID;
455 // check if billing block is required for pay later
456 if (CRM_Utils_Array
::value('is_pay_later', $this->_values
['event'])) {
457 $this->_isBillingAddressRequiredForPayLater
= CRM_Utils_Array
::value('is_billing_required', $this->_values
['event']);
458 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater
);
463 * Assign the minimal set of variables to the template
468 public function assignToTemplate() {
469 //process only primary participant params
470 $this->_params
= $this->get('params');
471 if (isset($this->_params
[0])) {
472 $params = $this->_params
[0];
475 if (!empty($params['billing_first_name'])) {
476 $name = $params['billing_first_name'];
479 if (!empty($params['billing_middle_name'])) {
480 $name .= " {$params['billing_middle_name']}";
483 if (!empty($params['billing_last_name'])) {
484 $name .= " {$params['billing_last_name']}";
486 $this->assign('billingName', $name);
487 $this->set('name', $name);
490 'amount', 'currencyID', 'credit_card_type',
491 'trxn_id', 'amount_level', 'receive_date',
494 foreach ($vars as $v) {
495 if (!empty($params[$v])) {
496 if ($v == 'receive_date') {
497 $this->assign($v, CRM_Utils_Date
::mysqlToIso($params[$v]));
500 $this->assign($v, $params[$v]);
503 elseif (CRM_Utils_Array
::value('amount', $params) == 0) {
504 $this->assign($v, CRM_Utils_Array
::value($v, $params));
508 // assign the address formatted up for display
509 $addressParts = array(
510 "street_address-{$this->_bltID}",
511 "city-{$this->_bltID}",
512 "postal_code-{$this->_bltID}",
513 "state_province-{$this->_bltID}",
514 "country-{$this->_bltID}",
516 $addressFields = array();
517 foreach ($addressParts as $part) {
518 list($n, $id) = explode('-', $part);
519 if (isset($params['billing_' . $part])) {
520 $addressFields[$n] = CRM_Utils_Array
::value('billing_' . $part, $params);
524 $this->assign('address', CRM_Utils_Address
::format($addressFields));
526 if ($this->_contributeMode
== 'direct' && empty($params['is_pay_later'])) {
527 $date = CRM_Utils_Date
::format(CRM_Utils_Array
::value('credit_card_exp_date', $params));
528 $date = CRM_Utils_Date
::mysqlToIso($date);
529 $this->assign('credit_card_exp_date', $date);
530 $this->assign('credit_card_number',
531 CRM_Utils_System
::mungeCreditCard(CRM_Utils_Array
::value('credit_card_number', $params))
535 // get the email that the confirmation would have been sent to
536 $session = CRM_Core_Session
::singleton();
538 // assign is_email_confirm to templates
539 if (isset($this->_values
['event']['is_email_confirm'])) {
540 $this->assign('is_email_confirm', $this->_values
['event']['is_email_confirm']);
543 // assign pay later stuff
544 $params['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, FALSE);
545 $this->assign('is_pay_later', $params['is_pay_later']);
546 if ($params['is_pay_later']) {
547 $this->assign('pay_later_text', $this->_values
['event']['pay_later_text']);
548 $this->assign('pay_later_receipt', $this->_values
['event']['pay_later_receipt']);
551 // also assign all participantIDs to the template
552 // useful in generating confirmation numbers if needed
553 $this->assign('participantIDs',
554 $this->_participantIDS
559 * Add the custom fields
562 * @param string $name
563 * @param bool $viewOnly
568 public function buildCustom($id, $name, $viewOnly = FALSE) {
570 $button = substr($this->controller
->getButtonName(), -4);
571 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
572 $session = CRM_Core_Session
::singleton();
573 $contactID = $session->get('userID');
575 // we don't allow conflicting fields to be
576 // configured via profile
577 $fieldsToIgnore = array(
578 'participant_fee_amount' => 1,
579 'participant_fee_level' => 1,
585 foreach ($id as $profileID) {
586 $field = CRM_Core_BAO_UFGroup
::getFields($profileID, FALSE, CRM_Core_Action
::ADD
,
587 NULL, NULL, FALSE, NULL,
588 FALSE, NULL, CRM_Core_Permission
::CREATE
,
591 $fields = array_merge($fields, $field);
595 if (CRM_Core_BAO_UFGroup
::filterUFGroups($id, $contactID)) {
596 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
,
597 NULL, NULL, FALSE, NULL,
598 FALSE, NULL, CRM_Core_Permission
::CREATE
,
605 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
,
606 NULL, NULL, FALSE, NULL,
607 FALSE, NULL, CRM_Core_Permission
::CREATE
,
612 if (array_intersect_key($fields, $fieldsToIgnore)) {
613 $fields = array_diff_key($fields, $fieldsToIgnore);
614 CRM_Core_Session
::setStatus(ts('Some of the profile fields cannot be configured for this page.'));
618 if (!empty($this->_fields
)) {
619 $fields = @array_diff_assoc
($fields, $this->_fields
);
622 if (empty($this->_params
[0]['additional_participants']) &&
625 CRM_Core_BAO_Address
::checkContactSharedAddressFields($fields, $contactID);
627 $this->assign($name, $fields);
628 if (is_array($fields)) {
629 foreach ($fields as $key => $field) {
631 isset($field['data_type']) &&
632 $field['data_type'] == 'File' ||
($viewOnly && $field['name'] == 'image_URL')
634 // ignore file upload fields
637 //make the field optional if primary participant
638 //have been skip the additional participant.
639 if ($button == 'skip') {
640 $field['is_required'] = FALSE;
642 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
643 elseif ($field['add_captcha'] && !$contactID) {
644 // only add captcha for first page
647 list($prefixName, $index) = CRM_Utils_System
::explode('-', $key, 2);
648 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, CRM_Profile_Form
::MODE_CREATE
, $contactID, TRUE);
650 $this->_fields
[$key] = $field;
654 if ($addCaptcha && !$viewOnly) {
655 $captcha = CRM_Utils_ReCAPTCHA
::singleton();
656 $captcha->add($this);
657 $this->assign('isCaptcha', TRUE);
663 * @param CRM_Core_Form $form
664 * @param int $eventID
668 public static function initEventFee(&$form, $eventID) {
671 // retrive all active price set fields.
672 $discountId = CRM_Core_BAO_Discount
::findSet($eventID, 'civicrm_event');
673 if (property_exists($form, '_discountId') && $form->_discountId
) {
674 $discountId = $form->_discountId
;
677 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
678 $price = CRM_Price_BAO_PriceSet
::initSet($form, $eventID, 'civicrm_event', TRUE, $priceSetId);
681 $price = CRM_Price_BAO_PriceSet
::initSet($form, $eventID, 'civicrm_event', TRUE);
684 if (property_exists($form, '_context') && ($form->_context
== 'standalone'
685 ||
$form->_context
== 'participant')) {
686 $discountedEvent = CRM_Core_BAO_Discount
::getOptionGroup($eventID, 'civicrm_event');
687 if (is_array( $discountedEvent)) {
688 foreach ($discountedEvent as $key => $priceSetId) {
689 $priceSet = CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId);
690 $priceSet = CRM_Utils_Array
::value($priceSetId, $priceSet);
691 $form->_values
['discount'][$key] = CRM_Utils_Array
::value('fields', $priceSet);
692 $fieldID = key($form->_values
['discount'][$key]);
693 $form->_values
['discount'][$key][$fieldID]['name'] = CRM_Core_DAO
::getFieldValue(
694 'CRM_Price_DAO_PriceSet',
701 $eventFee = CRM_Utils_Array
::value('fee', $form->_values
);
702 if (!is_array($eventFee) ||
empty($eventFee)) {
703 $form->_values
['fee'] = array();
706 //fix for non-upgraded price sets.CRM-4256.
707 if (isset($form->_isPaidEvent
)) {
708 $isPaidEvent = $form->_isPaidEvent
;
711 $isPaidEvent = CRM_Utils_Array
::value('is_monetary', $form->_values
['event']);
713 if ($isPaidEvent && empty($form->_values
['fee'])) {
714 if (CRM_Utils_System
::getClassName($form) != 'CRM_Event_Form_Participant') {
715 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
))));
721 * Handle process after the confirmation of payment by User
723 * @param int $contactID
724 * @param null $contribution
725 * @param null $payment
730 public function confirmPostProcess($contactID = NULL, $contribution = NULL, $payment = NULL) {
731 // add/update contact information
733 unset($this->_params
['note']);
735 //to avoid conflict overwrite $this->_params
736 $this->_params
= $this->get('value');
738 //get the amount of primary participant
739 if (!empty($this->_params
['is_primary'])) {
740 $this->_params
['fee_amount'] = $this->get('primaryParticipantAmount');
743 // add participant record
744 $participant = CRM_Event_Form_Registration
::addParticipant($this, $contactID);
745 $this->_participantIDS
[] = $participant->id
;
747 //setting register_by_id field and primaryContactId
748 if (!empty($this->_params
['is_primary'])) {
749 $this->set('registerByID', $participant->id
);
750 $this->set('primaryContactId', $contactID);
753 $this->processFirstParticipant($participant->id
);
756 CRM_Core_BAO_CustomValueTable
::postProcess($this->_params
,
757 CRM_Core_DAO
::$_nullArray,
758 'civicrm_participant',
763 $createPayment = (CRM_Utils_Array
::value('amount', $this->_params
, 0) != 0) ?
TRUE : FALSE;
765 // force to create zero amount payment, CRM-5095
766 // we know the amout is zero since createPayment is false
767 if (!$createPayment &&
768 (isset($contribution) && $contribution->id
) &&
769 $this->_priceSetId
&&
772 $createPayment = TRUE;
775 if ($createPayment && $this->_values
['event']['is_monetary'] && !empty($this->_params
['contributionID'])) {
776 $paymentParams = array(
777 'participant_id' => $participant->id
,
778 'contribution_id' => $contribution->id
,
781 $paymentPartcipant = CRM_Event_BAO_ParticipantPayment
::create($paymentParams, $ids);
784 //set only primary participant's params for transfer checkout.
785 if (($this->_contributeMode
== 'checkout' ||
$this->_contributeMode
== 'notify') && !empty($this->_params
['is_primary'])) {
786 $this->_params
['participantID'] = $participant->id
;
787 $this->set('primaryParticipant', $this->_params
);
790 $this->assign('action', $this->_action
);
793 if (!empty($this->_params
['cms_create_account'])) {
794 $this->_params
['contactID'] = $contactID;
796 if (array_key_exists('email-5', $this->_params
)) {
799 foreach ($this->_params
as $name => $dontCare) {
800 if (substr($name, 0, 5) == 'email') {
807 // we should use primary email for
808 // 1. free event registration.
809 // 2. pay later participant.
810 // 3. waiting list participant.
811 // 4. require approval participant.
812 if (!empty($this->_params
['is_pay_later']) ||
813 $this->_allowWaitlist ||
$this->_requireApproval ||
empty($this->_values
['event']['is_monetary'])) {
814 $mail = 'email-Primary';
817 if (!CRM_Core_BAO_CMSUser
::create($this->_params
, $mail)) {
818 CRM_Core_Error
::statusBounce(ts('Your profile is not saved and Account is not created.'));
824 * Process the participant
826 * @param array $params
827 * @param int $contactID
832 public static function addParticipant(&$form, $contactID) {
833 if (empty($form->_params
)) {
836 $params = $form->_params
;
837 $transaction = new CRM_Core_Transaction();
839 $groupName = 'participant_role';
841 SELECT v.label as label ,v.value as value
842 FROM civicrm_option_value v,
843 civicrm_option_group g
844 WHERE v.option_group_id = g.id
849 $p = array(1 => array($groupName, 'String'));
851 $dao = CRM_Core_DAO
::executeQuery($query, $p);
853 $roleID = $dao->value
;
856 // handle register date CRM-4320
857 $registerDate = NULL;
858 if (!empty($form->_allowConfirmation
) && $form->_participantId
) {
859 $registerDate = $params['participant_register_date'];
861 elseif (!empty($params['participant_register_date']) &&
862 is_array($params['participant_register_date']) &&
863 !empty($params['participant_register_date'])
865 $registerDate = CRM_Utils_Date
::format($params['participant_register_date']);
868 $participantFields = CRM_Event_DAO_Participant
::fields();
869 $participantParams = array('id' => CRM_Utils_Array
::value('participant_id', $params),
870 'contact_id' => $contactID,
871 'event_id' => $form->_eventId ?
$form->_eventId
: $params['event_id'],
872 'status_id' => CRM_Utils_Array
::value('participant_status',
875 'role_id' => CRM_Utils_Array
::value('participant_role_id',
878 'register_date' => ($registerDate) ?
$registerDate : date('YmdHis'),
879 'source' => CRM_Utils_String
::ellipsify(
880 isset($params['participant_source']) ?
881 CRM_Utils_Array
::value('participant_source', $params) : CRM_Utils_Array
::value('description', $params),
882 $participantFields['participant_source']['maxlength']
884 'fee_level' => CRM_Utils_Array
::value('amount_level', $params),
885 'is_pay_later' => CRM_Utils_Array
::value('is_pay_later', $params, 0),
886 'fee_amount' => CRM_Utils_Array
::value('fee_amount', $params),
887 'registered_by_id' => CRM_Utils_Array
::value('registered_by_id', $params),
888 'discount_id' => CRM_Utils_Array
::value('discount_id', $params),
889 'fee_currency' => CRM_Utils_Array
::value('currencyID', $params),
890 'campaign_id' => CRM_Utils_Array
::value('campaign_id', $params),
893 if ($form->_action
& CRM_Core_Action
::PREVIEW || CRM_Utils_Array
::value('mode', $params) == 'test') {
894 $participantParams['is_test'] = 1;
897 $participantParams['is_test'] = 0;
900 if (!empty($form->_params
['note'])) {
901 $participantParams['note'] = $form->_params
['note'];
903 elseif (!empty($form->_params
['participant_note'])) {
904 $participantParams['note'] = $form->_params
['participant_note'];
907 // reuse id if one already exists for this one (can happen
908 // with back button being hit etc)
909 if (!$participantParams['id'] && !empty($params['contributionID'])) {
910 $pID = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment',
911 $params['contributionID'],
915 $participantParams['id'] = $pID;
917 $participantParams['discount_id'] = CRM_Core_BAO_Discount
::findSet($form->_eventId
, 'civicrm_event');
919 if (!$participantParams['discount_id']) {
920 $participantParams['discount_id'] = "null";
923 $participant = CRM_Event_BAO_Participant
::create($participantParams);
925 $transaction->commit();
930 /* Calculate the total participant count as per params.
932 * @param array $params user params.
934 * @return $totalCount total participant count.
938 * @param CRM_Core_Form $form
939 * @param array $params
940 * @param bool $skipCurrent
944 public static function getParticipantCount(&$form, $params, $skipCurrent = FALSE) {
946 if (!is_array($params) ||
empty($params)) {
950 $priceSetId = $form->get('priceSetId');
951 $addParticipantNum = substr($form->_name
, 12);
952 $priceSetFields = $priceSetDetails = array();
953 $hasPriceFieldsCount = FALSE;
955 $priceSetDetails = $form->get('priceSet');
956 if (isset($priceSetDetails['optionsCountTotal'])
957 && $priceSetDetails['optionsCountTotal']
959 $hasPriceFieldsCount = TRUE;
960 $priceSetFields = $priceSetDetails['optionsCountDetails']['fields'];
964 $singleFormParams = FALSE;
965 foreach ($params as $key => $val) {
966 if (!is_numeric($key)) {
967 $singleFormParams = TRUE;
972 //first format the params.
973 if ($singleFormParams) {
974 $params = self
::formatPriceSetParams($form, $params);
975 $params = array($params);
978 foreach ($params as $key => $values) {
979 if (!is_numeric($key) ||
981 ($skipCurrent && ($addParticipantNum == $key))
988 $cacheCount = CRM_Utils_Array
::value($key, $form->_lineItemParticipantsCount
);
989 if ($cacheCount && is_numeric($cacheCount)) {
990 $count = $cacheCount;
994 if (!$usedCache && $hasPriceFieldsCount) {
996 foreach ($values as $valKey => $value) {
997 if (strpos($valKey, 'price_') === FALSE) {
1000 $priceFieldId = substr($valKey, 6);
1001 if (!$priceFieldId ||
1002 !is_array($value) ||
1003 !array_key_exists($priceFieldId, $priceSetFields)
1007 foreach ($value as $optId => $optVal) {
1008 $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
1009 if ($currentCount) {
1010 $count +
= $currentCount;
1018 $totalCount +
= $count;
1027 /* Format user submitted price set params.
1028 * Convert price set each param as an array.
1030 * @param $params an array of user submitted params.
1033 * @return array $formatted, formatted price set params.
1037 * @param CRM_Core_Form $form
1038 * @param array $params
1042 public static function formatPriceSetParams(&$form, $params) {
1043 if (!is_array($params) ||
empty($params)) {
1047 $priceSetId = $form->get('priceSetId');
1051 $priceSetDetails = $form->get('priceSet');
1053 foreach ($params as $key => & $value) {
1055 if (strpos($key, 'price_') !== FALSE) {
1056 $fieldId = substr($key, 6);
1057 if (!array_key_exists($fieldId, $priceSetDetails['fields']) ||
1063 $field = $priceSetDetails['fields'][$fieldId];
1064 if ($field['html_type'] == 'Text') {
1065 $fieldOption = current($field['options']);
1066 $value = array($fieldOption['id'] => $value);
1069 $value = array($value => TRUE);
1077 /* Calculate total count for each price set options.
1078 * those are currently selected by user.
1080 * @param $form form object.
1083 * @return array $optionsCount, array of each option w/ count total.
1091 public static function getPriceSetOptionCount(&$form) {
1092 $params = $form->get('params');
1093 $priceSet = $form->get('priceSet');
1094 $priceSetId = $form->get('priceSetId');
1096 $optionsCount = array();
1098 !is_array($priceSet) ||
1100 !is_array($params) ||
1103 return $optionsCount;
1106 $priceSetFields = $priceMaxFieldDetails = array();
1107 if (!empty($priceSet['optionsCountTotal'])) {
1108 $priceSetFields = $priceSet['optionsCountDetails']['fields'];
1111 if (!empty($priceSet['optionsMaxValueTotal'])) {
1112 $priceMaxFieldDetails = $priceSet['optionsMaxValueDetails']['fields'];
1115 $addParticipantNum = substr($form->_name
, 12);
1116 foreach ($params as $pCnt => $values) {
1117 if ($values == 'skip' ||
1118 $pCnt === $addParticipantNum
1123 foreach ($values as $valKey => $value) {
1124 if (strpos($valKey, 'price_') === FALSE) {
1128 $priceFieldId = substr($valKey, 6);
1129 if (!$priceFieldId ||
1130 !is_array($value) ||
1131 !(array_key_exists($priceFieldId, $priceSetFields) ||
array_key_exists($priceFieldId, $priceMaxFieldDetails))
1136 foreach ($value as $optId => $optVal) {
1137 if (CRM_Utils_Array
::value('html_type', $priceSet['fields'][$priceFieldId]) == 'Text') {
1138 $currentCount = $optVal;
1144 if (isset($priceSetFields[$priceFieldId]) && isset($priceSetFields[$priceFieldId]['options'][$optId])) {
1145 $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
1148 $optionsCount[$optId] = $currentCount + CRM_Utils_Array
::value($optId, $optionsCount, 0);
1153 return $optionsCount;
1157 * @param string $suffix
1159 * @return null|string
1161 public function checkTemplateFileExists($suffix = '') {
1162 if ($this->_eventId
) {
1163 $templateName = $this->_name
;
1164 if (substr($templateName, 0, 12) == 'Participant_') {
1165 $templateName = 'AdditionalParticipant';
1168 $templateFile = "CRM/Event/Form/Registration/{$this->_eventId}/{$templateName}.{$suffix}tpl";
1169 $template = CRM_Core_Form
::getTemplate();
1170 if ($template->template_exists($templateFile)) {
1171 return $templateFile;
1178 * @return null|string
1180 public function getTemplateFileName() {
1181 $fileName = $this->checkTemplateFileExists();
1182 return $fileName ?
$fileName : parent
::getTemplateFileName();
1186 * @return null|string
1188 public function overrideExtraTemplateFileName() {
1189 $fileName = $this->checkTemplateFileExists('extra.');
1190 return $fileName ?
$fileName : parent
::overrideExtraTemplateFileName();
1194 * Validate price set submitted params for price option limit,
1195 * as well as user should select at least one price field option.
1196 * @param unknown_type $form
1197 * @param unknown_type $params
1198 * @return multitype:|Ambigous <multitype:, string, string>
1200 public static function validatePriceSet(&$form, $params) {
1202 $hasOptMaxValue = FALSE;
1203 if (!is_array($params) ||
empty($params)) {
1207 $currentParticipantNum = substr($form->_name
, 12);
1208 if (!$currentParticipantNum) {
1209 $currentParticipantNum = 0;
1212 $priceSetId = $form->get('priceSetId');
1213 $priceSetDetails = $form->get('priceSet');
1216 !is_array($priceSetDetails) ||
1217 empty($priceSetDetails)
1222 $optionsCountDetails = $optionsMaxValueDetails = array();
1224 isset($priceSetDetails['optionsMaxValueTotal'])
1225 && $priceSetDetails['optionsMaxValueTotal']
1227 $hasOptMaxValue = TRUE;
1228 $optionsMaxValueDetails = $priceSetDetails['optionsMaxValueDetails']['fields'];
1231 isset($priceSetDetails['optionsCountTotal'])
1232 && $priceSetDetails['optionsCountTotal']
1234 $hasOptCount = TRUE;
1235 $optionsCountDetails = $priceSetDetails['optionsCountDetails']['fields'];
1237 $feeBlock = $form->_feeBlock
;
1239 if (empty($feeBlock)) {
1240 $feeBlock = $priceSetDetails['fields'];
1243 $optionMaxValues = $fieldSelected = array();
1244 foreach ($params as $pNum => $values) {
1245 if (!is_array($values) ||
$values == 'skip') {
1249 foreach ($values as $valKey => $value) {
1250 if (strpos($valKey, 'price_') === FALSE) {
1253 $priceFieldId = substr($valKey, 6);
1254 $noneOptionValueSelected = FALSE;
1255 if (!$feeBlock[$priceFieldId]['is_required'] && $value == 0) {
1256 $noneOptionValueSelected = TRUE;
1261 (!$noneOptionValueSelected && !is_array($value))
1266 $fieldSelected[$pNum] = TRUE;
1268 if (!$hasOptMaxValue ||
!is_array($value)) {
1272 foreach ($value as $optId => $optVal) {
1273 if (CRM_Utils_Array
::value('html_type', $feeBlock[$priceFieldId]) == 'Text') {
1274 $currentMaxValue = $optVal;
1277 $currentMaxValue = 1;
1280 if (isset($optionsCountDetails[$priceFieldId]) && isset($optionsCountDetails[$priceFieldId]['options'][$optId])) {
1281 $currentMaxValue = $optionsCountDetails[$priceFieldId]['options'][$optId] * $optVal;
1283 if (empty($optionMaxValues)) {
1284 $optionMaxValues[$priceFieldId][$optId] = $currentMaxValue;
1287 $optionMaxValues[$priceFieldId][$optId] =
1288 $currentMaxValue + CRM_Utils_Array
::value($optId, CRM_Utils_Array
::value($priceFieldId, $optionMaxValues), 0);
1295 //validate for option max value.
1296 foreach ($optionMaxValues as $fieldId => $values) {
1297 $options = CRM_Utils_Array
::value('options', $feeBlock[$fieldId], array());
1298 foreach ($values as $optId => $total) {
1299 $optMax = $optionsMaxValueDetails[$fieldId]['options'][$optId];
1300 $opDbCount = CRM_Utils_Array
::value('db_total_count', $options[$optId], 0);
1301 $total +
= $opDbCount;
1302 if ($optMax && $total > $optMax) {
1303 $errors['soldOutOptions'][] = ts('Option %1 has sold out.', array(1 => $feeBlock[$fieldId]['options'][$optId]['label']));
1304 if ($opDbCount && ($opDbCount >= $optMax)) {
1305 $errors[$currentParticipantNum]["price_{$fieldId}"] =
1306 ts('Sorry, this option is currently sold out.');
1308 elseif (($optMax - $opDbCount) == 1) {
1309 $errors[$currentParticipantNum]["price_{$fieldId}"] =
1310 ts('Sorry, currently only a single seat is available for this option.', array(1 => ($optMax - $opDbCount)));
1313 $errors[$currentParticipantNum]["price_{$fieldId}"] =
1314 ts('Sorry, currently only %1 seats are available for this option.', array(1 => ($optMax - $opDbCount)));
1320 //validate for price field selection.
1321 foreach ($params as $pNum => $values) {
1322 if (!is_array($values) ||
$values == 'skip') {
1325 if (empty($fieldSelected[$pNum])) {
1326 $errors[$pNum]['_qf_default'] = ts('Select at least one option from Event Fee(s).');
1333 // set the first participant ID if not set, CRM-10032
1335 * @param int $participantID
1337 public function processFirstParticipant($participantID) {
1338 $this->_participantId
= $participantID;
1339 $this->set('participantId', $this->_participantId
);
1341 $ids = $participantValues = array();
1342 $participantParams = array('id' => $this->_participantId
);
1343 CRM_Event_BAO_Participant
::getValues($participantParams, $participantValues, $ids);
1344 $this->_values
['participant'] = $participantValues[$this->_participantId
];
1345 $this->set('values', $this->_values
);
1347 // also set the allow confirmation stuff
1348 if (array_key_exists(
1349 $this->_values
['participant']['status_id'],
1350 CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Pending'")
1352 $this->_allowConfirmation
= TRUE;
1353 $this->set('allowConfirmation', TRUE);
1358 * @todo - combine this with CRM_Event_BAO_Event::validRegistrationRequest
1359 * (probably extract relevant values here & call that with them & handle bounces & redirects here -as
1360 * those belong in the form layer)
1362 * @param string $redirect
1364 public function checkValidEvent($redirect = NULL) {
1365 // is the event active (enabled)?
1366 if (!$this->_values
['event']['is_active']) {
1367 // form is inactive, die a fatal death
1368 CRM_Core_Error
::statusBounce(ts('The event you requested is currently unavailable (contact the site administrator for assistance).'));
1371 // is online registration is enabled?
1372 if (!$this->_values
['event']['is_online_registration']) {
1373 CRM_Core_Error
::statusBounce(ts('Online registration is not currently available for this event (contact the site administrator for assistance).'), $redirect);
1376 // is this an event template ?
1377 if (!empty($this->_values
['event']['is_template'])) {
1378 CRM_Core_Error
::statusBounce(ts('Event templates are not meant to be registered.'), $redirect);
1381 $now = date('YmdHis');
1382 $startDate = CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('registration_start_date',
1383 $this->_values
['event']
1390 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);
1393 $endDate = CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('registration_end_date',
1394 $this->_values
['event']
1396 $eventEndDate = CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('event_end_date', $this->_values
['event']));
1401 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);
1403 if (!empty($eventEndDate) && $eventEndDate < $now) {
1404 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);