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