Merge pull request #23982 from eileenmcnaughton/act_cont
[civicrm-core.git] / CRM / Event / Form / Registration.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
6a488035 13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
15 */
16
17/**
3bdf1f3a 18 * This class generates form components for processing Event.
6a488035
TO
19 */
20class CRM_Event_Form_Registration extends CRM_Core_Form {
bc66279e 21
573aa07e 22 use CRM_Financial_Form_FrontEndPaymentFormTrait;
6a488035
TO
23
24 /**
100fef9d 25 * How many locationBlocks should we display?
6a488035
TO
26 *
27 * @var int
28 * @const
29 */
7da04cde 30 const LOCATION_BLOCKS = 1;
6a488035
TO
31
32 /**
73543110 33 * The id of the event we are processing.
6a488035
TO
34 *
35 * @var int
6a488035
TO
36 */
37 public $_eventId;
38
427412ef
EM
39 /**
40 * Get the event it.
41 *
42 * @return int
43 */
44 protected function getEventID(): int {
45 return $this->_eventId;
46 }
47
6a488035 48 /**
73543110 49 * The array of ids of all the participant we are processing.
6a488035
TO
50 *
51 * @var int
6a488035
TO
52 */
53 protected $_participantIDS = NULL;
54
55 /**
73543110 56 * The id of the participant we are processing.
6a488035
TO
57 *
58 * @var int
6a488035
TO
59 */
60 protected $_participantId;
61
62 /**
100fef9d 63 * Is participant able to walk registration wizard.
6a488035 64 *
d51c6add 65 * @var bool
6a488035
TO
66 */
67 public $_allowConfirmation;
68
69 /**
66f9e52b 70 * Is participant requires approval.
6a488035 71 *
d51c6add 72 * @var bool
6a488035
TO
73 */
74 public $_requireApproval;
75
76 /**
100fef9d 77 * Is event configured for waitlist.
6a488035 78 *
d51c6add 79 * @var bool
6a488035
TO
80 */
81 public $_allowWaitlist;
82
83 /**
66f9e52b 84 * Store additional participant ids.
6a488035
TO
85 * when there are pre-registered.
86 *
87 * @var array
6a488035
TO
88 */
89 public $_additionalParticipantIds;
90
6a488035 91 /**
66f9e52b 92 * The values for the contribution db object.
6a488035
TO
93 *
94 * @var array
6a488035
TO
95 */
96 public $_values;
97
98 /**
66f9e52b 99 * The paymentProcessor attributes for this page.
6a488035
TO
100 *
101 * @var array
6a488035
TO
102 */
103 public $_paymentProcessor;
104
105 /**
66f9e52b 106 * The params submitted by the form and computed by the app.
6a488035
TO
107 *
108 * @var array
6a488035
TO
109 */
110 protected $_params;
111
112 /**
66f9e52b 113 * The fields involved in this contribution page.
6a488035
TO
114 *
115 * @var array
6a488035
TO
116 */
117 public $_fields;
118
119 /**
73543110 120 * The billing location id for this contribution page.
6a488035
TO
121 *
122 * @var int
6a488035
TO
123 */
124 public $_bltID;
125
126 /**
127 * Price Set ID, if the new price set method is used
128 *
129 * @var int
6a488035
TO
130 */
131 public $_priceSetId = NULL;
132
133 /**
66f9e52b 134 * Array of fields for the price set.
6a488035
TO
135 *
136 * @var array
6a488035
TO
137 */
138 public $_priceSet;
139
140 public $_action;
141
142 public $_pcpId;
143
90b461f1
SL
144 /**
145 * Is event already full.
e70a7fc0 146 *
d51c6add 147 * @var bool
90b461f1 148 *
e70a7fc0 149 */
6a488035
TO
150 public $_isEventFull;
151
152 public $_lineItem;
bc66279e 153
6a488035 154 public $_lineItemParticipantsCount;
bc66279e 155
6a488035
TO
156 public $_availableRegistrations;
157
18135422 158 /**
90b461f1 159 * @var bool
18135422 160 * @deprecated
18135422 161 */
8ae4d0d3 162 public $_isBillingAddressRequiredForPayLater;
163
18135422 164 /**
165 * Is this a back office form
166 *
167 * @var bool
168 */
169 public $isBackOffice = FALSE;
170
171 /**
172 * Payment instrument iD for the transaction.
173 *
174 * This will generally be drawn from the payment processor and is ignored for
175 * front end forms.
176 *
177 * @var int
178 */
179 public $paymentInstrumentID;
180
6a488035 181 /**
66f9e52b 182 * Set variables up before form is built.
6a488035 183 */
00be9182 184 public function preProcess() {
427412ef 185 $this->_eventId = (int) CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
0ebdca14 186 $this->_action = CRM_Utils_Request::retrieve('action', 'Alphanumeric', $this, FALSE, CRM_Core_Action::ADD);
6a488035
TO
187 //CRM-4320
188 $this->_participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
6a2b3da2 189 $this->setPaymentMode();
6a488035
TO
190
191 $this->_values = $this->get('values');
192 $this->_fields = $this->get('fields');
193 $this->_bltID = $this->get('bltID');
194 $this->_paymentProcessor = $this->get('paymentProcessor');
195 $this->_priceSetId = $this->get('priceSetId');
196 $this->_priceSet = $this->get('priceSet');
197 $this->_lineItem = $this->get('lineItem');
198 $this->_isEventFull = $this->get('isEventFull');
199 $this->_lineItemParticipantsCount = $this->get('lineItemParticipants');
200 if (!is_array($this->_lineItem)) {
affcc9d2 201 $this->_lineItem = [];
6a488035
TO
202 }
203 if (!is_array($this->_lineItemParticipantsCount)) {
affcc9d2 204 $this->_lineItemParticipantsCount = [];
6a488035
TO
205 }
206 $this->_availableRegistrations = $this->get('availableRegistrations');
6a488035
TO
207 $this->_participantIDS = $this->get('participantIDs');
208
209 //check if participant allow to walk registration wizard.
210 $this->_allowConfirmation = $this->get('allowConfirmation');
211
212 // check for Approval
213 $this->_requireApproval = $this->get('requireApproval');
214
215 // check for waitlisting.
216 $this->_allowWaitlist = $this->get('allowWaitlist');
217
6a488035
TO
218 //get the additional participant ids.
219 $this->_additionalParticipantIds = $this->get('additionalParticipantIds');
6a488035
TO
220
221 if (!$this->_values) {
6a488035 222 // this is the first time we are hitting this, so check for permissions here
e2d09ab4 223 if (!CRM_Core_Permission::event(CRM_Core_Permission::EDIT, $this->_eventId, 'register for events')) {
427412ef 224 CRM_Core_Error::statusBounce(ts('You do not have permission to register for this event'), $this->getInfoPageUrl());
6a488035
TO
225 }
226
227 // get all the values from the dao object
affcc9d2 228 $this->_values = $this->_fields = [];
6a488035
TO
229
230 //retrieve event information
bc66279e 231 $params = ['id' => $this->_eventId];
6a488035 232 CRM_Event_BAO_Event::retrieve($params, $this->_values['event']);
14fb2154 233
9ed3cd18 234 // check for is_monetary status
9c1bc317 235 $isMonetary = $this->_values['event']['is_monetary'] ?? NULL;
9ed3cd18 236 // check for ability to add contributions of type
a8b59c2c 237 if ($isMonetary
4323dc6c
PN
238 && CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
239 && !CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($this->_values['event']['financial_type_id']))
240 ) {
beb414cc 241 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
9ed3cd18 242 }
6a488035 243
427412ef 244 $this->checkValidEvent();
6a488035
TO
245 // get the participant values, CRM-4320
246 $this->_allowConfirmation = FALSE;
247 if ($this->_participantId) {
248 $this->processFirstParticipant($this->_participantId);
249 }
6a488035
TO
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
96f7c53e 261 $eventFull = CRM_Event_BAO_Participant::eventFull($this->_eventId, FALSE,
6a488035
TO
262 CRM_Utils_Array::value('has_waitlist', $this->_values['event'])
263 );
264
96f7c53e 265 $this->_allowWaitlist = $this->_isEventFull = FALSE;
6a488035
TO
266 if ($eventFull && !$this->_allowConfirmation) {
267 $this->_isEventFull = TRUE;
268 //lets redirecting to info only when to waiting list.
9c1bc317 269 $this->_allowWaitlist = $this->_values['event']['has_waitlist'] ?? NULL;
6a488035 270 if (!$this->_allowWaitlist) {
427412ef 271 CRM_Utils_System::redirect($this->getInfoPageUrl());
6a488035
TO
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;
a7488080 279 if (!empty($this->_values['event']['requires_approval']) && !$this->_allowConfirmation) {
6a488035
TO
280 $this->_requireApproval = TRUE;
281 }
282 $this->set('requireApproval', $this->_requireApproval);
283
6a488035
TO
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 }
96f7c53e 288 $isPayLater = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_eventId, 'is_pay_later');
55d21031 289 $this->setPayLaterLabel($isPayLater ? $this->_values['event']['pay_later_text'] : '');
2c5311b9 290 //check for various combinations for paylater, payment
6a488035 291 //process with paid event.
8cc574cf 292 if ($isMonetary && (!$isPayLater || !empty($this->_values['event']['payment_processor']))) {
2c5311b9 293 $this->_paymentProcessorIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('payment_processor',
6a488035 294 $this->_values['event']
2c5311b9 295 ));
cbcb5b49 296 $this->assignPaymentProcessor($isPayLater);
6a488035 297 }
6a488035 298 //init event fee.
96f7c53e 299 self::initEventFee($this, $this->_eventId);
6a488035
TO
300
301 // get the profile ids
bc66279e 302 $ufJoinParams = [
6a488035 303 'entity_table' => 'civicrm_event',
73543110 304 // CRM-4377: CiviEvent for the main participant, CiviEvent_Additional for additional participants
6a488035
TO
305 'module' => 'CiviEvent',
306 'entity_id' => $this->_eventId,
bc66279e 307 ];
427412ef 308 [$this->_values['custom_pre_id'], $this->_values['custom_post_id']] = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
6a488035
TO
309
310 // set profiles for additional participants
311 if ($this->_values['event']['is_multiple_registrations']) {
73543110 312 // CRM-4377: CiviEvent for the main participant, CiviEvent_Additional for additional participants
96f7c53e
PJ
313 $ufJoinParams['module'] = 'CiviEvent_Additional';
314
427412ef 315 [$this->_values['additional_custom_pre_id'], $this->_values['additional_custom_post_id'], $preActive, $postActive] = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
6a488035 316
73543110 317 // CRM-4377: we need to maintain backward compatibility, hence if there is profile for main contact
6a488035
TO
318 // set same profile for additional contacts.
319 if ($this->_values['custom_pre_id'] && !$this->_values['additional_custom_pre_id']) {
320 $this->_values['additional_custom_pre_id'] = $this->_values['custom_pre_id'];
321 }
322
323 if ($this->_values['custom_post_id'] && !$this->_values['additional_custom_post_id']) {
324 $this->_values['additional_custom_post_id'] = $this->_values['custom_post_id'];
325 }
6a488035
TO
326 // now check for no profile condition, in that case is_active = 0
327 if (isset($preActive) && !$preActive) {
328 unset($this->_values['additional_custom_pre_id']);
329 }
6a488035
TO
330 if (isset($postActive) && !$postActive) {
331 unset($this->_values['additional_custom_post_id']);
332 }
333 }
8345c9d3
EM
334
335 $this->assignBillingType();
6a488035 336
a6513ad5
EM
337 if ($this->_values['event']['is_monetary']) {
338 CRM_Core_Payment_Form::setPaymentFieldsByProcessor($this, $this->_paymentProcessor);
6a488035 339 }
bc66279e 340 $params = ['entity_id' => $this->_eventId, 'entity_table' => 'civicrm_event'];
6a488035
TO
341 $this->_values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE);
342
343 $this->set('values', $this->_values);
344 $this->set('fields', $this->_fields);
345
7c550ca0
WA
346 $this->_availableRegistrations
347 = CRM_Event_BAO_Participant::eventFull(
bc66279e 348 $this->_values['event']['id'], TRUE,
349 CRM_Utils_Array::value('has_waitlist', $this->_values['event'])
350 );
6a488035
TO
351 $this->set('availableRegistrations', $this->_availableRegistrations);
352 }
6a488035
TO
353 $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor);
354
355 // check if this is a paypal auto return and redirect accordingly
356 if (CRM_Core_Payment::paypalRedirect($this->_paymentProcessor)) {
357 $url = CRM_Utils_System::url('civicrm/event/register',
358 "_qf_ThankYou_display=1&qfKey={$this->controller->_key}"
359 );
360 CRM_Utils_System::redirect($url);
361 }
0f2b049e 362 // The concept of contributeMode is deprecated.
6a488035
TO
363 $this->_contributeMode = $this->get('contributeMode');
364 $this->assign('contributeMode', $this->_contributeMode);
365
d7188a5d 366 $this->setTitle($this->_values['event']['title']);
6a488035
TO
367
368 $this->assign('paidEvent', $this->_values['event']['is_monetary']);
369
370 // we do not want to display recently viewed items on Registration pages
371 $this->assign('displayRecent', FALSE);
6a488035 372
9c1bc317 373 $isShowLocation = $this->_values['event']['is_show_location'] ?? NULL;
6a488035 374 $this->assign('isShowLocation', $isShowLocation);
6a488035
TO
375 // Handle PCP
376 $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this);
377 if ($pcpId) {
353ffa53
TO
378 $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'event', $this->_values['event']);
379 $this->_pcpId = $pcp['pcpId'];
9c1bc317 380 $this->_values['event']['intro_text'] = $pcp['pcpInfo']['intro_text'] ?? NULL;
6a488035
TO
381 }
382
383 // assign all event properties so wizard templates can display event info.
384 $this->assign('event', $this->_values['event']);
385 $this->assign('location', $this->_values['location']);
386 $this->assign('bltID', $this->_bltID);
9c1bc317 387 $isShowLocation = $this->_values['event']['is_show_location'] ?? NULL;
6a488035 388 $this->assign('isShowLocation', $isShowLocation);
85939a77 389 CRM_Contribute_BAO_Contribution_Utils::overrideDefaultCurrency($this->_values['event']);
6a488035
TO
390
391 //lets allow user to override campaign.
392 $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this);
393 if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
394 $this->_values['event']['campaign_id'] = $campID;
395 }
8ae4d0d3 396
f48e6cf7 397 // Set the same value for is_billing_required as contribution page so code can be shared.
9c1bc317 398 $this->_values['is_billing_required'] = $this->_values['event']['is_billing_required'] ?? NULL;
8ae4d0d3 399 // check if billing block is required for pay later
f48e6cf7 400 // note that I have started removing the use of isBillingAddressRequiredForPayLater in favour of letting
401 // the CRM_Core_Payment_Manual class handle it - but there are ~300 references to it in the code base so only
402 // removing in very limited cases.
f3acfdd9 403 if (!empty($this->_values['event']['is_pay_later'])) {
9c1bc317 404 $this->_isBillingAddressRequiredForPayLater = $this->_values['event']['is_billing_required'] ?? NULL;
8ae4d0d3 405 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater);
406 }
6a488035
TO
407 }
408
409 /**
66f9e52b 410 * Assign the minimal set of variables to the template.
6a488035 411 */
00be9182 412 public function assignToTemplate() {
6a488035
TO
413 //process only primary participant params
414 $this->_params = $this->get('params');
415 if (isset($this->_params[0])) {
416 $params = $this->_params[0];
417 }
418 $name = '';
a7488080 419 if (!empty($params['billing_first_name'])) {
6a488035
TO
420 $name = $params['billing_first_name'];
421 }
422
a7488080 423 if (!empty($params['billing_middle_name'])) {
6a488035
TO
424 $name .= " {$params['billing_middle_name']}";
425 }
426
a7488080 427 if (!empty($params['billing_last_name'])) {
6a488035
TO
428 $name .= " {$params['billing_last_name']}";
429 }
430 $this->assign('billingName', $name);
431 $this->set('name', $name);
432
bc66279e 433 $vars = [
353ffa53
TO
434 'amount',
435 'currencyID',
436 'credit_card_type',
437 'trxn_id',
438 'amount_level',
439 'receive_date',
bc66279e 440 ];
6a488035
TO
441
442 foreach ($vars as $v) {
a7488080 443 if (!empty($params[$v])) {
427412ef 444 if ($v === 'receive_date') {
6a488035
TO
445 $this->assign($v, CRM_Utils_Date::mysqlToIso($params[$v]));
446 }
447 else {
448 $this->assign($v, $params[$v]);
449 }
450 }
de6c59ca 451 elseif (empty($params['amount'])) {
6a488035
TO
452 $this->assign($v, CRM_Utils_Array::value($v, $params));
453 }
454 }
455
0b50eca0 456 $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters($params, $this->_bltID));
6a488035 457
0f2b049e 458 // The concept of contributeMode is deprecated.
f634801a 459 if ($this->_contributeMode === 'direct' && empty($params['is_pay_later'])) {
6a488035
TO
460 $date = CRM_Utils_Date::format(CRM_Utils_Array::value('credit_card_exp_date', $params));
461 $date = CRM_Utils_Date::mysqlToIso($date);
462 $this->assign('credit_card_exp_date', $date);
463 $this->assign('credit_card_number',
464 CRM_Utils_System::mungeCreditCard(CRM_Utils_Array::value('credit_card_number', $params))
465 );
466 }
467
f634801a 468 $this->assign('is_email_confirm', $this->_values['event']['is_email_confirm'] ?? NULL);
6a488035 469 // assign pay later stuff
f634801a 470 $params['is_pay_later'] = $params['is_pay_later'] ?? FALSE;
6a488035 471 $this->assign('is_pay_later', $params['is_pay_later']);
f634801a
EM
472 $this->assign('pay_later_text', $params['is_pay_later'] ? $this->getPayLaterLabel() : FALSE);
473 $this->assign('pay_later_receipt', $params['is_pay_later'] ? $this->_values['event']['pay_later_receipt'] : NULL);
6a488035
TO
474
475 // also assign all participantIDs to the template
476 // useful in generating confirmation numbers if needed
f12392c8 477 $this->assign('participantIDs', $this->_participantIDS);
6a488035
TO
478 }
479
480 /**
66f9e52b 481 * Add the custom fields.
6a488035 482 *
100fef9d
CW
483 * @param int $id
484 * @param string $name
6a488035 485 */
fad3a3b0 486 public function buildCustom($id, $name) {
889a75d8
MW
487 if (!$id) {
488 return;
489 }
490
491 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
492 $contactID = CRM_Core_Session::getLoggedInContactID();
c60a5971 493 $fields = [];
889a75d8
MW
494
495 // we don't allow conflicting fields to be
496 // configured via profile
497 $fieldsToIgnore = [
498 'participant_fee_amount' => 1,
499 'participant_fee_level' => 1,
500 ];
501 if ($contactID) {
502 //FIX CRM-9653
503 if (is_array($id)) {
504 $fields = [];
505 foreach ($id as $profileID) {
506 $field = CRM_Core_BAO_UFGroup::getFields($profileID, FALSE, CRM_Core_Action::ADD,
507 NULL, NULL, FALSE, NULL,
508 FALSE, NULL, CRM_Core_Permission::CREATE,
509 'field_name', TRUE
510 );
511 $fields = array_merge($fields, $field);
6a488035
TO
512 }
513 }
514 else {
889a75d8
MW
515 if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) {
516 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD,
517 NULL, NULL, FALSE, NULL,
518 FALSE, NULL, CRM_Core_Permission::CREATE,
519 'field_name', TRUE
520 );
521 }
6a488035 522 }
889a75d8
MW
523 }
524 else {
525 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD,
526 NULL, NULL, FALSE, NULL,
527 FALSE, NULL, CRM_Core_Permission::CREATE,
528 'field_name', TRUE
529 );
530 }
6a488035 531
889a75d8
MW
532 if (array_intersect_key($fields, $fieldsToIgnore)) {
533 $fields = array_diff_key($fields, $fieldsToIgnore);
534 CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'));
535 }
a2149acc 536
889a75d8
MW
537 if (!empty($this->_fields)) {
538 $fields = @array_diff_assoc($fields, $this->_fields);
539 }
a2149acc 540
889a75d8
MW
541 if (empty($this->_params[0]['additional_participants']) &&
542 is_null($cid)
543 ) {
544 CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
545 }
546 $this->assign($name, $fields);
547 if (is_array($fields)) {
548 $button = substr($this->controller->getButtonName(), -4);
549 foreach ($fields as $key => $field) {
889a75d8
MW
550 //make the field optional if primary participant
551 //have been skip the additional participant.
552 if ($button == 'skip') {
553 $field['is_required'] = FALSE;
554 }
889a75d8 555 CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE, $contactID, TRUE);
4839c695 556
889a75d8 557 $this->_fields[$key] = $field;
6a488035
TO
558 }
559 }
560 }
561
0cf587a7 562 /**
3bdf1f3a 563 * Initiate event fee.
564 *
c490a46a 565 * @param CRM_Core_Form $form
100fef9d 566 * @param int $eventID
e5467560 567 * @param bool $doNotIncludeExpiredFields
1cb5e2d3 568 * See CRM-16456.
0cf587a7
EM
569 *
570 * @throws Exception
571 */
e5467560 572 public static function initEventFee(&$form, $eventID, $doNotIncludeExpiredFields = TRUE) {
6a488035
TO
573 // get price info
574
575 // retrive all active price set fields.
576 $discountId = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event');
8567d0f8
PN
577 if (property_exists($form, '_discountId') && $form->_discountId) {
578 $discountId = $form->_discountId;
579 }
d4acad97 580
6a488035
TO
581 if ($discountId) {
582 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
6a488035
TO
583 }
584 else {
d1d16083 585 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventID);
6a488035 586 }
e5467560 587 CRM_Price_BAO_PriceSet::initSet($form, 'civicrm_event', $doNotIncludeExpiredFields, $priceSetId);
8ef12e64 588
589 if (property_exists($form, '_context') && ($form->_context == 'standalone'
353ffa53
TO
590 || $form->_context == 'participant')
591 ) {
8567d0f8 592 $discountedEvent = CRM_Core_BAO_Discount::getOptionGroup($eventID, 'civicrm_event');
481a74f4 593 if (is_array($discountedEvent)) {
8567d0f8 594 foreach ($discountedEvent as $key => $priceSetId) {
9da8dc8c 595 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId);
9c1bc317
CW
596 $priceSet = $priceSet[$priceSetId] ?? NULL;
597 $form->_values['discount'][$key] = $priceSet['fields'] ?? NULL;
8567d0f8
PN
598 $fieldID = key($form->_values['discount'][$key]);
599 $form->_values['discount'][$key][$fieldID]['name'] = CRM_Core_DAO::getFieldValue(
9da8dc8c 600 'CRM_Price_DAO_PriceSet',
8567d0f8
PN
601 $priceSetId,
602 'title'
603 );
604 }
605 }
606 }
9c1bc317 607 $eventFee = $form->_values['fee'] ?? NULL;
6a488035 608 if (!is_array($eventFee) || empty($eventFee)) {
affcc9d2 609 $form->_values['fee'] = [];
6a488035
TO
610 }
611
612 //fix for non-upgraded price sets.CRM-4256.
613 if (isset($form->_isPaidEvent)) {
614 $isPaidEvent = $form->_isPaidEvent;
615 }
616 else {
9c1bc317 617 $isPaidEvent = $form->_values['event']['is_monetary'] ?? NULL;
6a488035 618 }
a8b59c2c 619 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
4323dc6c
PN
620 && !empty($form->_values['fee'])
621 ) {
b61d1bce 622 foreach ($form->_values['fee'] as $k => $fees) {
9ed3cd18
E
623 foreach ($fees['options'] as $options) {
624 if (!CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($options['financial_type_id']))) {
b61d1bce 625 unset($form->_values['fee'][$k]);
9ed3cd18
E
626 }
627 }
628 }
629 }
6a488035 630 if ($isPaidEvent && empty($form->_values['fee'])) {
8535cf1c 631 if (!in_array(CRM_Utils_System::getClassName($form), ['CRM_Event_Form_Participant', 'CRM_Event_Form_Task_Register'])) {
bc66279e 632 CRM_Core_Error::statusBounce(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.', [1 => CRM_Utils_System::url('civicrm/event/manage/fee', 'reset=1&action=update&id=' . $form->_eventId)]));
6a488035
TO
633 }
634 }
635 }
636
637 /**
66f9e52b 638 * Handle process after the confirmation of payment by User.
6a488035 639 *
100fef9d 640 * @param int $contactID
d5ce773d 641 * @param \CRM_Contribute_BAO_Contribution $contribution
f55a8ac8
MWMC
642 *
643 * @throws \CiviCRM_API3_Exception
6a488035 644 */
d5ce773d 645 public function confirmPostProcess($contactID = NULL, $contribution = NULL) {
6a488035 646 // add/update contact information
6a488035
TO
647 unset($this->_params['note']);
648
649 //to avoid conflict overwrite $this->_params
650 $this->_params = $this->get('value');
651
652 //get the amount of primary participant
a7488080 653 if (!empty($this->_params['is_primary'])) {
6a488035
TO
654 $this->_params['fee_amount'] = $this->get('primaryParticipantAmount');
655 }
656
657 // add participant record
44237a29 658 $participant = $this->addParticipant($this, $contactID);
6a488035
TO
659 $this->_participantIDS[] = $participant->id;
660
661 //setting register_by_id field and primaryContactId
a7488080 662 if (!empty($this->_params['is_primary'])) {
6a488035
TO
663 $this->set('registerByID', $participant->id);
664 $this->set('primaryContactId', $contactID);
665
666 // CRM-10032
667 $this->processFirstParticipant($participant->id);
668 }
2fda3ece
SL
669
670 if (!empty($this->_params['is_primary'])) {
671 $this->_params['participantID'] = $participant->id;
672 $this->set('primaryParticipant', $this->_params);
673 }
6a488035 674
6a488035
TO
675 $createPayment = (CRM_Utils_Array::value('amount', $this->_params, 0) != 0) ? TRUE : FALSE;
676
677 // force to create zero amount payment, CRM-5095
678 // we know the amout is zero since createPayment is false
679 if (!$createPayment &&
680 (isset($contribution) && $contribution->id) &&
681 $this->_priceSetId &&
682 $this->_lineItem
683 ) {
684 $createPayment = TRUE;
685 }
686
8cc574cf 687 if ($createPayment && $this->_values['event']['is_monetary'] && !empty($this->_params['contributionID'])) {
f55a8ac8 688 $paymentParams = [
6a488035
TO
689 'participant_id' => $participant->id,
690 'contribution_id' => $contribution->id,
f55a8ac8
MWMC
691 ];
692 civicrm_api3('ParticipantPayment', 'create', $paymentParams);
6a488035
TO
693 }
694
6a488035
TO
695 $this->assign('action', $this->_action);
696
697 // create CMS user
a7488080 698 if (!empty($this->_params['cms_create_account'])) {
6a488035
TO
699 $this->_params['contactID'] = $contactID;
700
701 if (array_key_exists('email-5', $this->_params)) {
353ffa53 702 $mail = 'email-5';
0db6c3e1
TO
703 }
704 else {
6a488035
TO
705 foreach ($this->_params as $name => $dontCare) {
706 if (substr($name, 0, 5) == 'email') {
707 $mail = $name;
708 break;
709 }
710 }
711 }
712
713 // we should use primary email for
e13b1333 714 // 1. pay later participant.
715 // 2. waiting list participant.
716 // 3. require approval participant.
a7488080 717 if (!empty($this->_params['is_pay_later']) ||
e13b1333 718 $this->_allowWaitlist || $this->_requireApproval
353ffa53 719 ) {
6a488035
TO
720 $mail = 'email-Primary';
721 }
722
723 if (!CRM_Core_BAO_CMSUser::create($this->_params, $mail)) {
724 CRM_Core_Error::statusBounce(ts('Your profile is not saved and Account is not created.'));
725 }
726 }
727 }
728
729 /**
66f9e52b 730 * Process the participant.
6a488035 731 *
3ab5efa9 732 * @param CRM_Core_Form $form
100fef9d 733 * @param int $contactID
3bdf1f3a 734 *
3ab5efa9 735 * @return \CRM_Event_BAO_Participant
f55a8ac8 736 * @throws \CiviCRM_API3_Exception
6a488035 737 */
44237a29 738 protected function addParticipant(&$form, $contactID) {
bf2c70af 739 if (empty($form->_params)) {
7c550ca0 740 return NULL;
bf2c70af 741 }
44237a29 742 // Note this used to be shared with the backoffice form & no longer is, some code may no longer be required.
bf2c70af 743 $params = $form->_params;
6a488035
TO
744 $transaction = new CRM_Core_Transaction();
745
6a488035
TO
746 // handle register date CRM-4320
747 $registerDate = NULL;
bf2c70af 748 if (!empty($form->_allowConfirmation) && $form->_participantId) {
6a488035
TO
749 $registerDate = $params['participant_register_date'];
750 }
a7488080 751 elseif (!empty($params['participant_register_date']) &&
6a488035
TO
752 is_array($params['participant_register_date']) &&
753 !empty($params['participant_register_date'])
754 ) {
755 $registerDate = CRM_Utils_Date::format($params['participant_register_date']);
756 }
757
6441bcc8 758 $participantFields = CRM_Event_DAO_Participant::fields();
bc66279e 759 $participantParams = [
6b409353 760 'id' => $params['participant_id'] ?? NULL,
6a488035 761 'contact_id' => $contactID,
bf2c70af 762 'event_id' => $form->_eventId ? $form->_eventId : $params['event_id'],
6a488035
TO
763 'status_id' => CRM_Utils_Array::value('participant_status',
764 $params, 1
765 ),
fc076fe3 766 'role_id' => CRM_Utils_Array::value('participant_role_id', $params) ?: CRM_Event_BAO_Participant::getDefaultRoleID(),
6a488035 767 'register_date' => ($registerDate) ? $registerDate : date('YmdHis'),
6441bcc8 768 'source' => CRM_Utils_String::ellipsify(
7c550ca0 769 isset($params['participant_source']) ? CRM_Utils_Array::value('participant_source', $params) : CRM_Utils_Array::value('description', $params),
6441bcc8
NG
770 $participantFields['participant_source']['maxlength']
771 ),
6b409353 772 'fee_level' => $params['amount_level'] ?? NULL,
6a488035 773 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0),
6b409353
CW
774 'fee_amount' => $params['fee_amount'] ?? NULL,
775 'registered_by_id' => $params['registered_by_id'] ?? NULL,
776 'discount_id' => $params['discount_id'] ?? NULL,
777 'fee_currency' => $params['currencyID'] ?? NULL,
778 'campaign_id' => $params['campaign_id'] ?? NULL,
bc66279e 779 ];
6a488035 780
bf2c70af 781 if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') {
6a488035
TO
782 $participantParams['is_test'] = 1;
783 }
784 else {
785 $participantParams['is_test'] = 0;
786 }
787
bf2c70af 788 if (!empty($form->_params['note'])) {
789 $participantParams['note'] = $form->_params['note'];
6a488035 790 }
bf2c70af 791 elseif (!empty($form->_params['participant_note'])) {
792 $participantParams['note'] = $form->_params['participant_note'];
6a488035
TO
793 }
794
795 // reuse id if one already exists for this one (can happen
796 // with back button being hit etc)
8cc574cf 797 if (!$participantParams['id'] && !empty($params['contributionID'])) {
6a488035
TO
798 $pID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
799 $params['contributionID'],
800 'participant_id',
801 'contribution_id'
802 );
803 $participantParams['id'] = $pID;
804 }
bf2c70af 805 $participantParams['discount_id'] = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event');
6a488035
TO
806
807 if (!$participantParams['discount_id']) {
808 $participantParams['discount_id'] = "null";
809 }
810
b21026cb
JJ
811 $participantParams['custom'] = [];
812 foreach ($form->_params as $paramName => $paramValue) {
813 if (strpos($paramName, 'custom_') === 0) {
814 list($customFieldID, $customValueID) = CRM_Core_BAO_CustomField::getKeyID($paramName, TRUE);
815 CRM_Core_BAO_CustomField::formatCustomField($customFieldID, $participantParams['custom'], $paramValue, 'Participant', $customValueID);
816
817 }
818 }
819
6a488035
TO
820 $participant = CRM_Event_BAO_Participant::create($participantParams);
821
822 $transaction->commit();
823
824 return $participant;
825 }
826
0cf587a7 827 /**
3bdf1f3a 828 * Calculate the total participant count as per params.
829 *
c490a46a
CW
830 * @param CRM_Core_Form $form
831 * @param array $params
3bdf1f3a 832 * User params.
0cf587a7
EM
833 * @param bool $skipCurrent
834 *
3bdf1f3a 835 * @return int
0cf587a7 836 */
6a488035
TO
837 public static function getParticipantCount(&$form, $params, $skipCurrent = FALSE) {
838 $totalCount = 0;
839 if (!is_array($params) || empty($params)) {
840 return $totalCount;
841 }
842
353ffa53
TO
843 $priceSetId = $form->get('priceSetId');
844 $addParticipantNum = substr($form->_name, 12);
affcc9d2 845 $priceSetFields = $priceSetDetails = [];
6a488035
TO
846 $hasPriceFieldsCount = FALSE;
847 if ($priceSetId) {
848 $priceSetDetails = $form->get('priceSet');
849 if (isset($priceSetDetails['optionsCountTotal'])
850 && $priceSetDetails['optionsCountTotal']
851 ) {
852 $hasPriceFieldsCount = TRUE;
853 $priceSetFields = $priceSetDetails['optionsCountDetails']['fields'];
854 }
855 }
856
857 $singleFormParams = FALSE;
858 foreach ($params as $key => $val) {
859 if (!is_numeric($key)) {
860 $singleFormParams = TRUE;
861 break;
862 }
863 }
864
865 //first format the params.
866 if ($singleFormParams) {
867 $params = self::formatPriceSetParams($form, $params);
bc66279e 868 $params = [$params];
6a488035
TO
869 }
870
871 foreach ($params as $key => $values) {
872 if (!is_numeric($key) ||
873 $values == 'skip' ||
874 ($skipCurrent && ($addParticipantNum == $key))
875 ) {
876 continue;
877 }
878 $count = 1;
879
880 $usedCache = FALSE;
9c1bc317 881 $cacheCount = $form->_lineItemParticipantsCount[$key] ?? NULL;
6a488035
TO
882 if ($cacheCount && is_numeric($cacheCount)) {
883 $count = $cacheCount;
884 $usedCache = TRUE;
885 }
886
887 if (!$usedCache && $hasPriceFieldsCount) {
888 $count = 0;
889 foreach ($values as $valKey => $value) {
890 if (strpos($valKey, 'price_') === FALSE) {
891 continue;
892 }
893 $priceFieldId = substr($valKey, 6);
894 if (!$priceFieldId ||
895 !is_array($value) ||
896 !array_key_exists($priceFieldId, $priceSetFields)
897 ) {
898 continue;
899 }
900 foreach ($value as $optId => $optVal) {
901 $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
902 if ($currentCount) {
903 $count += $currentCount;
904 }
905 }
906 }
907 if (!$count) {
908 $count = 1;
909 }
910 }
911 $totalCount += $count;
912 }
913 if (!$totalCount) {
914 $totalCount = 1;
915 }
916
917 return $totalCount;
918 }
919
3bdf1f3a 920 /**
921 * Format user submitted price set params.
922 *
6a488035
TO
923 * Convert price set each param as an array.
924 *
3bdf1f3a 925 * @param CRM_Core_Form $form
5a4f6742 926 * @param array $params
d4dd1e85 927 * An array of user submitted params.
6a488035 928 *
a6c01b45 929 * @return array
3bdf1f3a 930 * Formatted price set params.
0cf587a7 931 */
6a488035
TO
932 public static function formatPriceSetParams(&$form, $params) {
933 if (!is_array($params) || empty($params)) {
934 return $params;
935 }
936
937 $priceSetId = $form->get('priceSetId');
938 if (!$priceSetId) {
939 return $params;
940 }
941 $priceSetDetails = $form->get('priceSet');
942
943 foreach ($params as $key => & $value) {
affcc9d2 944 $vals = [];
6a488035
TO
945 if (strpos($key, 'price_') !== FALSE) {
946 $fieldId = substr($key, 6);
947 if (!array_key_exists($fieldId, $priceSetDetails['fields']) ||
948 is_array($value) ||
949 !$value
950 ) {
951 continue;
952 }
953 $field = $priceSetDetails['fields'][$fieldId];
954 if ($field['html_type'] == 'Text') {
955 $fieldOption = current($field['options']);
bc66279e 956 $value = [$fieldOption['id'] => $value];
6a488035
TO
957 }
958 else {
bc66279e 959 $value = [$value => TRUE];
6a488035
TO
960 }
961 }
962 }
963
964 return $params;
965 }
966
3bdf1f3a 967 /**
968 * Calculate total count for each price set options.
6a488035 969 *
3bdf1f3a 970 * - currently selected by user.
6a488035 971 *
3bdf1f3a 972 * @param CRM_Core_Form $form
973 * Form object.
0cf587a7
EM
974 *
975 * @return array
3bdf1f3a 976 * array of each option w/ count total.
0cf587a7 977 */
169be3c1 978 public static function getPriceSetOptionCount(&$form) {
353ffa53
TO
979 $params = $form->get('params');
980 $priceSet = $form->get('priceSet');
6a488035
TO
981 $priceSetId = $form->get('priceSetId');
982
affcc9d2 983 $optionsCount = [];
6a488035
TO
984 if (!$priceSetId ||
985 !is_array($priceSet) ||
986 empty($priceSet) ||
987 !is_array($params) ||
988 empty($params)
989 ) {
990 return $optionsCount;
991 }
992
affcc9d2 993 $priceSetFields = $priceMaxFieldDetails = [];
a7488080 994 if (!empty($priceSet['optionsCountTotal'])) {
6a488035
TO
995 $priceSetFields = $priceSet['optionsCountDetails']['fields'];
996 }
997
a7488080 998 if (!empty($priceSet['optionsMaxValueTotal'])) {
6a488035
TO
999 $priceMaxFieldDetails = $priceSet['optionsMaxValueDetails']['fields'];
1000 }
1001
1002 $addParticipantNum = substr($form->_name, 12);
1003 foreach ($params as $pCnt => $values) {
1004 if ($values == 'skip' ||
79b152ac 1005 $pCnt === $addParticipantNum
6a488035
TO
1006 ) {
1007 continue;
1008 }
1009
1010 foreach ($values as $valKey => $value) {
1011 if (strpos($valKey, 'price_') === FALSE) {
1012 continue;
1013 }
1014
1015 $priceFieldId = substr($valKey, 6);
1016 if (!$priceFieldId ||
1017 !is_array($value) ||
1018 !(array_key_exists($priceFieldId, $priceSetFields) || array_key_exists($priceFieldId, $priceMaxFieldDetails))
1019 ) {
1020 continue;
1021 }
1022
1023 foreach ($value as $optId => $optVal) {
1024 if (CRM_Utils_Array::value('html_type', $priceSet['fields'][$priceFieldId]) == 'Text') {
1025 $currentCount = $optVal;
1026 }
1027 else {
1028 $currentCount = 1;
1029 }
1030
1031 if (isset($priceSetFields[$priceFieldId]) && isset($priceSetFields[$priceFieldId]['options'][$optId])) {
1032 $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
1033 }
1034
1035 $optionsCount[$optId] = $currentCount + CRM_Utils_Array::value($optId, $optionsCount, 0);
1036 }
1037 }
1038 }
1039
1040 return $optionsCount;
1041 }
1042
0cf587a7 1043 /**
d7f0cce6 1044 * Check template file exists.
3bdf1f3a 1045 *
d7f0cce6 1046 * @param string|null $suffix
0cf587a7 1047 *
d7f0cce6
BT
1048 * @return string|null
1049 * Template file path, else null
0cf587a7 1050 */
d7f0cce6 1051 public function checkTemplateFileExists($suffix = NULL) {
6a488035
TO
1052 if ($this->_eventId) {
1053 $templateName = $this->_name;
1054 if (substr($templateName, 0, 12) == 'Participant_') {
1055 $templateName = 'AdditionalParticipant';
1056 }
1057
1058 $templateFile = "CRM/Event/Form/Registration/{$this->_eventId}/{$templateName}.{$suffix}tpl";
1059 $template = CRM_Core_Form::getTemplate();
1060 if ($template->template_exists($templateFile)) {
1061 return $templateFile;
1062 }
1063 }
1064 return NULL;
1065 }
1066
0cf587a7 1067 /**
3bdf1f3a 1068 * Get template file name.
1069 *
0cf587a7
EM
1070 * @return null|string
1071 */
00be9182 1072 public function getTemplateFileName() {
6a488035
TO
1073 $fileName = $this->checkTemplateFileExists();
1074 return $fileName ? $fileName : parent::getTemplateFileName();
1075 }
1076
0cf587a7 1077 /**
3bdf1f3a 1078 * Override extra template name.
1079 *
0cf587a7
EM
1080 * @return null|string
1081 */
00be9182 1082 public function overrideExtraTemplateFileName() {
6a488035
TO
1083 $fileName = $this->checkTemplateFileExists('extra.');
1084 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
1085 }
1086
0dc0b759 1087 /**
1088 * Reset values for all options those are full.
1089 *
780422d7 1090 * @param array $optionFullIds
3bdf1f3a 1091 * @param CRM_Core_Form $form
0dc0b759 1092 */
0efafdb0 1093 public static function resetElementValue($optionFullIds, &$form) {
0dc0b759 1094 if (!is_array($optionFullIds) ||
1095 empty($optionFullIds) ||
1096 !$form->isSubmitted()
1097 ) {
1098 return;
1099 }
1100
1101 foreach ($optionFullIds as $fldId => $optIds) {
1102 $name = "price_$fldId";
1103 if (!$form->elementExists($name)) {
1104 continue;
1105 }
1106
1107 $element = $form->getElement($name);
1108 $eleType = $element->getType();
1109
1110 $resetSubmitted = FALSE;
1111 switch ($eleType) {
1112 case 'text':
1113 if ($element->getValue() && $element->isFrozen()) {
1114 $label = "{$element->getLabel()}<tt>(x)</tt>";
1115 $element->setLabel($label);
1116 $element->setPersistantFreeze();
1117 $resetSubmitted = TRUE;
1118 }
1119 break;
1120
1121 case 'group':
1122 if (is_array($element->_elements)) {
1123 foreach ($element->_elements as $child) {
1124 $childType = $child->getType();
1125 $methodName = 'getName';
1126 if ($childType) {
1127 $methodName = 'getValue';
1128 }
1129 if (in_array($child->{$methodName}(), $optIds) && $child->isFrozen()) {
1130 $resetSubmitted = TRUE;
1131 $child->setPersistantFreeze();
1132 }
1133 }
1134 }
1135 break;
1136
1137 case 'select':
9d8d8fd0 1138 $value = $element->getValue();
1139 if (in_array($value[0], $optIds)) {
0dc0b759 1140 foreach ($element->_options as $option) {
d0ebccea 1141 if ($option['attr']['value'] === "crm_disabled_opt-{$value[0]}") {
2ae4d103 1142 $placeholder = html_entity_decode($option['text'], ENT_QUOTES, "UTF-8");
bc66279e 1143 $element->updateAttributes(['placeholder' => $placeholder]);
0dc0b759 1144 break;
1145 }
1146 }
1147 $resetSubmitted = TRUE;
1148 }
1149 break;
1150 }
1151
1152 //finally unset values from submitted.
1153 if ($resetSubmitted) {
1154 self::resetSubmittedValue($name, $optIds, $form);
1155 }
1156 }
1157 }
1158
1159 /**
3bdf1f3a 1160 * Reset submitted value.
1161 *
0dc0b759 1162 * @param string $elementName
1163 * @param array $optionIds
c91b34a5 1164 * @param CRM_Core_Form $form
0dc0b759 1165 */
0efafdb0 1166 public static function resetSubmittedValue($elementName, $optionIds, &$form) {
0dc0b759 1167 if (empty($elementName) ||
1168 !$form->elementExists($elementName) ||
1169 !$form->getSubmitValue($elementName)
1170 ) {
1171 return;
1172 }
bc66279e 1173 foreach (['constantValues', 'submitValues', 'defaultValues'] as $val) {
0dc0b759 1174 $values = $form->{"_$val"};
1175 if (!is_array($values) || empty($values)) {
1176 continue;
1177 }
9c1bc317 1178 $eleVal = $values[$elementName] ?? NULL;
0dc0b759 1179 if (empty($eleVal)) {
1180 continue;
1181 }
1182 if (is_array($eleVal)) {
1183 $found = FALSE;
1184 foreach ($eleVal as $keyId => $ignore) {
1185 if (in_array($keyId, $optionIds)) {
1186 $found = TRUE;
1187 unset($values[$elementName][$keyId]);
1188 }
1189 }
1190 if ($found && empty($values[$elementName][$keyId])) {
1191 $values[$elementName][$keyId] = NULL;
1192 }
1193 }
1194 else {
1195 if (!empty($keyId)) {
1196 $values[$elementName][$keyId] = NULL;
1197 }
1198 }
1199 }
1200 }
1201
da8d9879 1202 /**
3bdf1f3a 1203 * Validate price set submitted params for price option limit.
1204 *
1205 * User should select at least one price field option.
72b3a70c
CW
1206 *
1207 * @param CRM_Core_Form $form
1208 * @param array $params
1209 *
1210 * @return array
6a488035 1211 */
00be9182 1212 public static function validatePriceSet(&$form, $params) {
affcc9d2 1213 $errors = [];
6a488035
TO
1214 $hasOptMaxValue = FALSE;
1215 if (!is_array($params) || empty($params)) {
1216 return $errors;
1217 }
1218
1219 $currentParticipantNum = substr($form->_name, 12);
1220 if (!$currentParticipantNum) {
1221 $currentParticipantNum = 0;
1222 }
1223
1224 $priceSetId = $form->get('priceSetId');
1225 $priceSetDetails = $form->get('priceSet');
1226 if (
1227 !$priceSetId ||
1228 !is_array($priceSetDetails) ||
1229 empty($priceSetDetails)
1230 ) {
1231 return $errors;
1232 }
1233
affcc9d2 1234 $optionsCountDetails = $optionsMaxValueDetails = [];
6a488035
TO
1235 if (
1236 isset($priceSetDetails['optionsMaxValueTotal'])
1237 && $priceSetDetails['optionsMaxValueTotal']
1238 ) {
1239 $hasOptMaxValue = TRUE;
1240 $optionsMaxValueDetails = $priceSetDetails['optionsMaxValueDetails']['fields'];
1241 }
1242 if (
1243 isset($priceSetDetails['optionsCountTotal'])
1244 && $priceSetDetails['optionsCountTotal']
1245 ) {
1246 $hasOptCount = TRUE;
1247 $optionsCountDetails = $priceSetDetails['optionsCountDetails']['fields'];
1248 }
1249 $feeBlock = $form->_feeBlock;
1250
1251 if (empty($feeBlock)) {
1252 $feeBlock = $priceSetDetails['fields'];
1253 }
1254
affcc9d2 1255 $optionMaxValues = $fieldSelected = [];
6a488035
TO
1256 foreach ($params as $pNum => $values) {
1257 if (!is_array($values) || $values == 'skip') {
1258 continue;
1259 }
1260
1261 foreach ($values as $valKey => $value) {
1262 if (strpos($valKey, 'price_') === FALSE) {
1263 continue;
1264 }
1265 $priceFieldId = substr($valKey, 6);
1266 $noneOptionValueSelected = FALSE;
1267 if (!$feeBlock[$priceFieldId]['is_required'] && $value == 0) {
1268 $noneOptionValueSelected = TRUE;
1269 }
1270
1271 if (
1272 !$priceFieldId ||
1273 (!$noneOptionValueSelected && !is_array($value))
1274 ) {
1275 continue;
1276 }
1277
1278 $fieldSelected[$pNum] = TRUE;
1279
1280 if (!$hasOptMaxValue || !is_array($value)) {
1281 continue;
1282 }
1283
1284 foreach ($value as $optId => $optVal) {
1285 if (CRM_Utils_Array::value('html_type', $feeBlock[$priceFieldId]) == 'Text') {
1286 $currentMaxValue = $optVal;
1287 }
1288 else {
1289 $currentMaxValue = 1;
1290 }
1291
1292 if (isset($optionsCountDetails[$priceFieldId]) && isset($optionsCountDetails[$priceFieldId]['options'][$optId])) {
1293 $currentMaxValue = $optionsCountDetails[$priceFieldId]['options'][$optId] * $optVal;
1294 }
1295 if (empty($optionMaxValues)) {
1296 $optionMaxValues[$priceFieldId][$optId] = $currentMaxValue;
1297 }
1298 else {
7c550ca0
WA
1299 $optionMaxValues[$priceFieldId][$optId]
1300 = $currentMaxValue + CRM_Utils_Array::value($optId, CRM_Utils_Array::value($priceFieldId, $optionMaxValues), 0);
6a488035 1301 }
0dc0b759 1302 $soldOutPnum[$optId] = $pNum;
6a488035
TO
1303 }
1304 }
0dc0b759 1305
1306 //validate for price field selection.
db7f1024 1307 if (empty($fieldSelected[$pNum])) {
bc66279e 1308 $errors[$pNum]['_qf_default'] = ts('SELECT at least one OPTION FROM EVENT Fee(s).');
0dc0b759 1309 }
6a488035
TO
1310 }
1311
1312 //validate for option max value.
1313 foreach ($optionMaxValues as $fieldId => $values) {
affcc9d2 1314 $options = CRM_Utils_Array::value('options', $feeBlock[$fieldId], []);
6a488035
TO
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;
0dc0b759 1319 if ($optMax && ($total > $optMax)) {
6a488035 1320 if ($opDbCount && ($opDbCount >= $optMax)) {
0dc0b759 1321 $errors[$soldOutPnum[$optId]]["price_{$fieldId}"]
7c550ca0 1322 = ts('Sorry, this option is currently sold out.');
6a488035
TO
1323 }
1324 elseif (($optMax - $opDbCount) == 1) {
0dc0b759 1325 $errors[$soldOutPnum[$optId]]["price_{$fieldId}"]
bc66279e 1326 = ts('Sorry, currently only a single space is available for this option.', [1 => ($optMax - $opDbCount)]);
6a488035
TO
1327 }
1328 else {
0dc0b759 1329 $errors[$soldOutPnum[$optId]]["price_{$fieldId}"]
bc66279e 1330 = ts('Sorry, currently only %1 spaces are available for this option.', [1 => ($optMax - $opDbCount)]);
6a488035
TO
1331 }
1332 }
1333 }
1334 }
6a488035
TO
1335 return $errors;
1336 }
1337
0cf587a7 1338 /**
3bdf1f3a 1339 * Set the first participant ID if not set.
1340 *
1341 * CRM-10032.
72b3a70c 1342 *
100fef9d 1343 * @param int $participantID
0cf587a7 1344 */
00be9182 1345 public function processFirstParticipant($participantID) {
6a488035
TO
1346 $this->_participantId = $participantID;
1347 $this->set('participantId', $this->_participantId);
1348
affcc9d2 1349 $ids = $participantValues = [];
bc66279e 1350 $participantParams = ['id' => $this->_participantId];
6a488035
TO
1351 CRM_Event_BAO_Participant::getValues($participantParams, $participantValues, $ids);
1352 $this->_values['participant'] = $participantValues[$this->_participantId];
1353 $this->set('values', $this->_values);
1354
1355 // also set the allow confirmation stuff
1356 if (array_key_exists(
353ffa53
TO
1357 $this->_values['participant']['status_id'],
1358 CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'")
1359 )) {
6a488035
TO
1360 $this->_allowConfirmation = TRUE;
1361 $this->set('allowConfirmation', TRUE);
1362 }
1363 }
1364
e2d09ab4 1365 /**
3bdf1f3a 1366 * Check if event is valid.
1367 *
e2d09ab4
EM
1368 * @todo - combine this with CRM_Event_BAO_Event::validRegistrationRequest
1369 * (probably extract relevant values here & call that with them & handle bounces & redirects here -as
1370 * those belong in the form layer)
1371 *
e2d09ab4 1372 */
427412ef 1373 protected function checkValidEvent(): void {
6a488035
TO
1374 // is the event active (enabled)?
1375 if (!$this->_values['event']['is_active']) {
1376 // form is inactive, die a fatal death
1377 CRM_Core_Error::statusBounce(ts('The event you requested is currently unavailable (contact the site administrator for assistance).'));
1378 }
1379
1380 // is online registration is enabled?
1381 if (!$this->_values['event']['is_online_registration']) {
427412ef 1382 CRM_Core_Error::statusBounce(ts('Online registration is not currently available for this event (contact the site administrator for assistance).'), $this->getInfoPageUrl());
6a488035
TO
1383 }
1384
1385 // is this an event template ?
a7488080 1386 if (!empty($this->_values['event']['is_template'])) {
427412ef 1387 CRM_Core_Error::statusBounce(ts('Event templates are not meant to be registered.'), $this->getInfoPageUrl());
6a488035
TO
1388 }
1389
1390 $now = date('YmdHis');
1391 $startDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_start_date',
353ffa53
TO
1392 $this->_values['event']
1393 ));
6a488035
TO
1394
1395 if (
1396 $startDate &&
1397 $startDate >= $now
1398 ) {
427412ef 1399 CRM_Core_Error::statusBounce(ts('Registration for this event begins on %1', [1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('registration_start_date', $this->_values['event']))]), $this->getInfoPageUrl(), ts('Sorry'));
6a488035
TO
1400 }
1401
ce5694b5 1402 $regEndDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_end_date',
353ffa53
TO
1403 $this->_values['event']
1404 ));
4bd9b7a9 1405 $eventEndDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('event_end_date', $this->_values['event']));
b612d89b 1406 if (($regEndDate && ($regEndDate < $now)) || (empty($regEndDate) && !empty($eventEndDate) && ($eventEndDate < $now))) {
ce5694b5 1407 $endDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('registration_end_date', $this->_values['event']));
1408 if (empty($regEndDate)) {
1409 $endDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('event_end_date', $this->_values['event']));
1410 }
427412ef 1411 CRM_Core_Error::statusBounce(ts('Registration for this event ended on %1', [1 => $endDate]), $this->getInfoPageUrl(), ts('Sorry'));
6a488035
TO
1412 }
1413 }
96025800 1414
c039f658 1415 /**
1416 * Get the amount level for the event payment.
1417 *
1418 * The amount level is the string stored on the contribution record that describes the purchase.
1419 *
1420 * @param array $params
1421 * @param int|null $discountID
1422 *
1423 * @return string
1424 */
1425 protected function getAmountLevel($params, $discountID) {
1426 // @todo move handling of discount ID to the BAO function - preferably by converting it to a price_set with
1427 // time settings.
1428 if (!empty($this->_values['discount'][$discountID])) {
1429 return $this->_values['discount'][$discountID][$params['amount']]['label'];
1430 }
e173279c 1431 if (empty($params['priceSetId'])) {
1432 // CRM-17509 An example of this is where the person is being waitlisted & there is no payment.
1433 // ideally we would have calculated amount first & only call this is there is an
1434 // amount but the flow needs more changes for that.
1435 return '';
1436 }
c039f658 1437 return CRM_Price_BAO_PriceSet::getAmountLevelText($params);
1438 }
1439
3033e657 1440 /**
1441 * Process Registration of free event.
1442 *
1443 * @param array $params
1444 * Form values.
1445 * @param int $contactID
d7ade6fe 1446 *
1447 * @throws \CiviCRM_API3_Exception
3033e657 1448 */
1449 public function processRegistration($params, $contactID = NULL) {
1450 $session = CRM_Core_Session::singleton();
affcc9d2 1451 $this->_participantInfo = [];
3033e657 1452
1453 // CRM-4320, lets build array of cancelled additional participant ids
1454 // those are drop or skip by primary at the time of confirmation.
1455 // get all in and then unset those are confirmed.
1456 $cancelledIds = $this->_additionalParticipantIds;
1457
affcc9d2 1458 $participantCount = [];
3033e657 1459 foreach ($params as $participantNum => $record) {
1460 if ($record == 'skip') {
1461 $participantCount[$participantNum] = 'skip';
1462 }
1463 elseif ($participantNum) {
1464 $participantCount[$participantNum] = 'participant';
1465 }
1466 }
1467
1468 $registerByID = NULL;
1469 foreach ($params as $key => $value) {
1470 if ($value != 'skip') {
1471 $fields = NULL;
1472
1473 // setting register by Id and unset contactId.
1474 if (empty($value['is_primary'])) {
1475 $contactID = NULL;
1476 $registerByID = $this->get('registerByID');
1477 if ($registerByID) {
1478 $value['registered_by_id'] = $registerByID;
1479 }
1480 // get an email if one exists for the participant
1481 $participantEmail = '';
1482 foreach (array_keys($value) as $valueName) {
1483 if (substr($valueName, 0, 6) == 'email-') {
1484 $participantEmail = $value[$valueName];
1485 }
1486 }
1487 if ($participantEmail) {
1488 $this->_participantInfo[] = $participantEmail;
1489 }
1490 else {
1491 $this->_participantInfo[] = $value['first_name'] . ' ' . $value['last_name'];
1492 }
1493 }
1494 elseif (!empty($value['contact_id'])) {
1495 $contactID = $value['contact_id'];
1496 }
1497 else {
1498 $contactID = $this->getContactID();
1499 }
1500
1501 CRM_Event_Form_Registration_Confirm::fixLocationFields($value, $fields, $this);
1502 //for free event or additional participant, dont create billing email address.
1503 if (empty($value['is_primary']) || !$this->_values['event']['is_monetary']) {
1504 unset($value["email-{$this->_bltID}"]);
1505 }
1506
1507 $contactID = CRM_Event_Form_Registration_Confirm::updateContactFields($contactID, $value, $fields, $this);
1508
1509 // lets store the contactID in the session
1510 // we dont store in userID in case the user is doing multiple
1511 // transactions etc
1512 // for things like tell a friend
1513 if (!$this->getContactID() && !empty($value['is_primary'])) {
1514 $session->set('transaction.userID', $contactID);
1515 }
1516
1517 //lets get the status if require approval or waiting.
1518
1519 $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
1520 if ($this->_allowWaitlist && !$this->_allowConfirmation) {
1521 $value['participant_status_id'] = $value['participant_status'] = array_search('On waitlist', $waitingStatuses);
1522 }
1523 elseif ($this->_requireApproval && !$this->_allowConfirmation) {
1524 $value['participant_status_id'] = $value['participant_status'] = array_search('Awaiting approval', $waitingStatuses);
1525 }
1526
1527 $this->set('value', $value);
d5ce773d 1528 $this->confirmPostProcess($contactID, NULL);
3033e657 1529
1530 //lets get additional participant id to cancel.
1531 if ($this->_allowConfirmation && is_array($cancelledIds)) {
3b4c7acc
JF
1532 $additionalId = $value['participant_id'] ?? NULL;
1533 if ($additionalId && $key = array_search($additionalId, $cancelledIds)) {
3033e657 1534 unset($cancelledIds[$key]);
1535 }
1536 }
1537 }
1538 }
1539
1540 // update status and send mail to cancelled additional participants, CRM-4320
1541 if ($this->_allowConfirmation && is_array($cancelledIds) && !empty($cancelledIds)) {
1542 $cancelledId = array_search('Cancelled',
1543 CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'")
1544 );
1545 CRM_Event_BAO_Participant::transitionParticipants($cancelledIds, $cancelledId);
1546 }
1547
1548 //set information about additional participants if exists
1549 if (count($this->_participantInfo)) {
1550 $this->set('participantInfo', $this->_participantInfo);
1551 }
1552
1553 //send mail Confirmation/Receipt
1554 if ($this->_contributeMode != 'checkout' ||
1555 $this->_contributeMode != 'notify'
1556 ) {
d7ade6fe 1557 $this->sendMails($params, $registerByID, $participantCount);
1558 }
1559 }
3033e657 1560
d7ade6fe 1561 /**
1562 * Send Mail to participants.
1563 *
1564 * @param $params
1565 * @param $registerByID
1566 * @param array $participantCount
1567 *
1568 * @throws \CiviCRM_API3_Exception
1569 */
1570 private function sendMails($params, $registerByID, array $participantCount) {
1571 $isTest = FALSE;
1572 if ($this->_action & CRM_Core_Action::PREVIEW) {
1573 $isTest = TRUE;
1574 }
3033e657 1575
d7ade6fe 1576 //handle if no additional participant.
1577 if (!$registerByID) {
1578 $registerByID = $this->get('registerByID');
1579 }
1580 $primaryContactId = $this->get('primaryContactId');
3033e657 1581
d7ade6fe 1582 //build an array of custom profile and assigning it to template.
1583 $additionalIDs = CRM_Event_BAO_Event::buildCustomProfile($registerByID, NULL,
1584 $primaryContactId, $isTest, TRUE
1585 );
3033e657 1586
d7ade6fe 1587 //lets carry all participant params w/ values.
1588 foreach ($additionalIDs as $participantID => $contactId) {
1589 $participantNum = NULL;
1590 if ($participantID == $registerByID) {
1591 $participantNum = 0;
1592 }
1593 else {
1594 if ($participantNum = array_search('participant', $participantCount)) {
1595 unset($participantCount[$participantNum]);
3033e657 1596 }
d7ade6fe 1597 }
3033e657 1598
d7ade6fe 1599 if ($participantNum === NULL) {
1600 break;
3033e657 1601 }
1602
d7ade6fe 1603 //carry the participant submitted values.
1604 $this->_values['params'][$participantID] = $params[$participantNum];
1605 }
1606
1607 //lets send mails to all with meanigful text, CRM-4320.
1608 $this->assign('isOnWaitlist', $this->_allowWaitlist);
1609 $this->assign('isRequireApproval', $this->_requireApproval);
3033e657 1610
d7ade6fe 1611 foreach ($additionalIDs as $participantID => $contactId) {
1612 if ($participantID == $registerByID) {
1613 //set as Primary Participant
1614 $this->assign('isPrimary', 1);
3033e657 1615
d7ade6fe 1616 $customProfile = CRM_Event_BAO_Event::buildCustomProfile($participantID, $this->_values, NULL, $isTest);
3033e657 1617
d7ade6fe 1618 if (count($customProfile)) {
1619 $this->assign('customProfile', $customProfile);
1620 $this->set('customProfile', $customProfile);
3033e657 1621 }
3033e657 1622 }
d7ade6fe 1623 else {
1624 $this->assign('isPrimary', 0);
1625 $this->assign('customProfile', NULL);
1626 }
1627
1628 //send Confirmation mail to Primary & additional Participants if exists
1629 CRM_Event_BAO_Event::sendMail($contactId, $this->_values, $participantID, $isTest);
3033e657 1630 }
1631 }
1632
427412ef
EM
1633 /**
1634 * Get redirect URL to send folks back to event info page is registration not available.
1635 *
1636 * @return string
1637 */
1638 private function getInfoPageUrl(): string {
1639 return CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $this->getEventID(),
1640 FALSE, NULL, FALSE, TRUE
1641 );
1642 }
1643
6a488035 1644}