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