CRM-16808 follow up - finally remove all references to calling doTransferPayment...
[civicrm-core.git] / CRM / Event / Form / Registration.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 *
31 * @package CRM
e7112fa7 32 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
33 * $Id$
34 *
35 */
36
37/**
38 * This class generates form components for processing Event
39 *
40 */
41class CRM_Event_Form_Registration extends CRM_Core_Form {
42
43 /**
100fef9d 44 * How many locationBlocks should we display?
6a488035
TO
45 *
46 * @var int
47 * @const
48 */
7da04cde 49 const LOCATION_BLOCKS = 1;
6a488035
TO
50
51 /**
66f9e52b 52 * The id of the event we are proceessing.
6a488035
TO
53 *
54 * @var int
6a488035
TO
55 */
56 public $_eventId;
57
58 /**
66f9e52b 59 * The array of ids of all the participant we are proceessing.
6a488035
TO
60 *
61 * @var int
6a488035
TO
62 */
63 protected $_participantIDS = NULL;
64
65 /**
66f9e52b 66 * The id of the participant we are proceessing.
6a488035
TO
67 *
68 * @var int
6a488035
TO
69 */
70 protected $_participantId;
71
72 /**
100fef9d 73 * Is participant able to walk registration wizard.
6a488035
TO
74 *
75 * @var Boolean
6a488035
TO
76 */
77 public $_allowConfirmation;
78
79 /**
66f9e52b 80 * Is participant requires approval.
6a488035
TO
81 *
82 * @var Boolean
6a488035
TO
83 */
84 public $_requireApproval;
85
86 /**
100fef9d 87 * Is event configured for waitlist.
6a488035
TO
88 *
89 * @var Boolean
6a488035
TO
90 */
91 public $_allowWaitlist;
92
93 /**
66f9e52b 94 * Store additional participant ids.
6a488035
TO
95 * when there are pre-registered.
96 *
97 * @var array
6a488035
TO
98 */
99 public $_additionalParticipantIds;
100
101 /**
66f9e52b 102 * The mode that we are in.
6a488035
TO
103 *
104 * @var string
105 * @protect
106 */
107 public $_mode;
108
109 /**
66f9e52b 110 * The values for the contribution db object.
6a488035
TO
111 *
112 * @var array
6a488035
TO
113 */
114 public $_values;
115
116 /**
66f9e52b 117 * The paymentProcessor attributes for this page.
6a488035
TO
118 *
119 * @var array
6a488035
TO
120 */
121 public $_paymentProcessor;
122
123 /**
66f9e52b 124 * The params submitted by the form and computed by the app.
6a488035
TO
125 *
126 * @var array
6a488035
TO
127 */
128 protected $_params;
129
130 /**
66f9e52b 131 * The fields involved in this contribution page.
6a488035
TO
132 *
133 * @var array
6a488035
TO
134 */
135 public $_fields;
136
137 /**
66f9e52b 138 * The billing location id for this contribiution page.
6a488035
TO
139 *
140 * @var int
6a488035
TO
141 */
142 public $_bltID;
143
144 /**
145 * Price Set ID, if the new price set method is used
146 *
147 * @var int
6a488035
TO
148 */
149 public $_priceSetId = NULL;
150
151 /**
66f9e52b 152 * Array of fields for the price set.
6a488035
TO
153 *
154 * @var array
6a488035
TO
155 */
156 public $_priceSet;
157
158 public $_action;
159
160 public $_pcpId;
161
162 /* Is event already full.
e70a7fc0
TO
163 *
164 * @var boolean
165 */
6a488035
TO
166
167 public $_isEventFull;
168
169 public $_lineItem;
170 public $_lineItemParticipantsCount;
171 public $_availableRegistrations;
172
173 public $_forcePayement;
174
8ae4d0d3 175 public $_isBillingAddressRequiredForPayLater;
176
6a488035 177 /**
66f9e52b 178 * Set variables up before form is built.
6a488035
TO
179 *
180 * @return void
6a488035 181 */
00be9182 182 public function preProcess() {
6a488035
TO
183 $this->_eventId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
184 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
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
219 $this->_forcePayement = $this->get('forcePayement');
220
221 //get the additional participant ids.
222 $this->_additionalParticipantIds = $this->get('additionalParticipantIds');
6a488035
TO
223 $config = CRM_Core_Config::singleton();
224
225 if (!$this->_values) {
226 // create redirect URL to send folks back to event info page is registration not available
96f7c53e 227 $infoUrl = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_eventId}",
6a488035
TO
228 FALSE, NULL, FALSE, TRUE
229 );
230
231 // this is the first time we are hitting this, so check for permissions here
e2d09ab4 232 if (!CRM_Core_Permission::event(CRM_Core_Permission::EDIT, $this->_eventId, 'register for events')) {
6a488035
TO
233 CRM_Core_Error::statusBounce(ts('You do not have permission to register for this event'), $infoUrl);
234 }
235
236 // get all the values from the dao object
96f7c53e 237 $this->_values = $this->_fields = array();
6a488035
TO
238 $this->_forcePayement = FALSE;
239
240 //retrieve event information
241 $params = array('id' => $this->_eventId);
242 CRM_Event_BAO_Event::retrieve($params, $this->_values['event']);
243
244 $this->checkValidEvent($infoUrl);
6a488035
TO
245 // get the participant values, CRM-4320
246 $this->_allowConfirmation = FALSE;
247 if ($this->_participantId) {
248 $this->processFirstParticipant($this->_participantId);
249 }
6a488035
TO
250 //check for additional participants.
251 if ($this->_allowConfirmation && $this->_values['event']['is_multiple_registrations']) {
252 $additionalParticipantIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_participantId);
253 $cnt = 1;
254 foreach ($additionalParticipantIds as $additionalParticipantId) {
255 $this->_additionalParticipantIds[$cnt] = $additionalParticipantId;
256 $cnt++;
257 }
258 $this->set('additionalParticipantIds', $this->_additionalParticipantIds);
259 }
260
96f7c53e 261 $eventFull = CRM_Event_BAO_Participant::eventFull($this->_eventId, FALSE,
6a488035
TO
262 CRM_Utils_Array::value('has_waitlist', $this->_values['event'])
263 );
264
96f7c53e 265 $this->_allowWaitlist = $this->_isEventFull = FALSE;
6a488035
TO
266 if ($eventFull && !$this->_allowConfirmation) {
267 $this->_isEventFull = TRUE;
268 //lets redirecting to info only when to waiting list.
269 $this->_allowWaitlist = CRM_Utils_Array::value('has_waitlist', $this->_values['event']);
270 if (!$this->_allowWaitlist) {
271 CRM_Utils_System::redirect($infoUrl);
272 }
273 }
274 $this->set('isEventFull', $this->_isEventFull);
275 $this->set('allowWaitlist', $this->_allowWaitlist);
276
277 //check for require requires approval.
278 $this->_requireApproval = FALSE;
a7488080 279 if (!empty($this->_values['event']['requires_approval']) && !$this->_allowConfirmation) {
6a488035
TO
280 $this->_requireApproval = TRUE;
281 }
282 $this->set('requireApproval', $this->_requireApproval);
283
6a488035
TO
284 if (isset($this->_values['event']['default_role_id'])) {
285 $participant_role = CRM_Core_OptionGroup::values('participant_role');
286 $this->_values['event']['participant_role'] = $participant_role["{$this->_values['event']['default_role_id']}"];
287 }
6a488035
TO
288 // check for is_monetary status
289 $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values['event']);
96f7c53e 290 $isPayLater = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_eventId, 'is_pay_later');
2c5311b9 291 //check for various combinations for paylater, payment
6a488035 292 //process with paid event.
8cc574cf 293 if ($isMonetary && (!$isPayLater || !empty($this->_values['event']['payment_processor']))) {
2c5311b9 294 $this->_paymentProcessorIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('payment_processor',
6a488035 295 $this->_values['event']
2c5311b9
EM
296 ));
297 $this->assignPaymentProcessor();
6a488035 298
6a488035 299 }
6a488035 300 //init event fee.
96f7c53e 301 self::initEventFee($this, $this->_eventId);
6a488035
TO
302
303 // get the profile ids
304 $ufJoinParams = array(
305 'entity_table' => 'civicrm_event',
306 // CRM-4377: CiviEvent for the main participant, CiviEvent_Additional for additional participants
307 'module' => 'CiviEvent',
308 'entity_id' => $this->_eventId,
309 );
310 list($this->_values['custom_pre_id'],
311 $this->_values['custom_post_id']
353ffa53 312 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
6a488035
TO
313
314 // set profiles for additional participants
315 if ($this->_values['event']['is_multiple_registrations']) {
96f7c53e
PJ
316 // CRM-4377: CiviEvent for the main participant, CiviEvent_Additional for additional participants
317 $ufJoinParams['module'] = 'CiviEvent_Additional';
318
6a488035
TO
319 list($this->_values['additional_custom_pre_id'],
320 $this->_values['additional_custom_post_id'], $preActive, $postActive
353ffa53 321 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
6a488035
TO
322
323 // CRM-4377: we need to maintain backward compatibility, hence if there is profile for main contact
324 // set same profile for additional contacts.
325 if ($this->_values['custom_pre_id'] && !$this->_values['additional_custom_pre_id']) {
326 $this->_values['additional_custom_pre_id'] = $this->_values['custom_pre_id'];
327 }
328
329 if ($this->_values['custom_post_id'] && !$this->_values['additional_custom_post_id']) {
330 $this->_values['additional_custom_post_id'] = $this->_values['custom_post_id'];
331 }
6a488035
TO
332 // now check for no profile condition, in that case is_active = 0
333 if (isset($preActive) && !$preActive) {
334 unset($this->_values['additional_custom_pre_id']);
335 }
6a488035
TO
336 if (isset($postActive) && !$postActive) {
337 unset($this->_values['additional_custom_post_id']);
338 }
339 }
8345c9d3
EM
340
341 $this->assignBillingType();
6a488035 342
a6513ad5
EM
343 if ($this->_values['event']['is_monetary']) {
344 CRM_Core_Payment_Form::setPaymentFieldsByProcessor($this, $this->_paymentProcessor);
6a488035 345 }
6a488035
TO
346 $params = array('entity_id' => $this->_eventId, 'entity_table' => 'civicrm_event');
347 $this->_values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE);
348
349 $this->set('values', $this->_values);
350 $this->set('fields', $this->_fields);
351
7c550ca0
WA
352 $this->_availableRegistrations
353 = CRM_Event_BAO_Participant::eventFull(
96f7c53e 354 $this->_values['event']['id'], TRUE,
6a488035
TO
355 CRM_Utils_Array::value('has_waitlist', $this->_values['event'])
356 );
357 $this->set('availableRegistrations', $this->_availableRegistrations);
358 }
6a488035
TO
359 $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor);
360
361 // check if this is a paypal auto return and redirect accordingly
362 if (CRM_Core_Payment::paypalRedirect($this->_paymentProcessor)) {
363 $url = CRM_Utils_System::url('civicrm/event/register',
364 "_qf_ThankYou_display=1&qfKey={$this->controller->_key}"
365 );
366 CRM_Utils_System::redirect($url);
367 }
6a488035
TO
368 $this->_contributeMode = $this->get('contributeMode');
369 $this->assign('contributeMode', $this->_contributeMode);
370
371 // setting CMS page title
372 CRM_Utils_System::setTitle($this->_values['event']['title']);
373 $this->assign('title', $this->_values['event']['title']);
374
375 $this->assign('paidEvent', $this->_values['event']['is_monetary']);
376
377 // we do not want to display recently viewed items on Registration pages
378 $this->assign('displayRecent', FALSE);
379 // Registration page values are cleared from session, so can't use normal Printer Friendly view.
380 // Use Browser Print instead.
381 $this->assign('browserPrint', TRUE);
382
383 $isShowLocation = CRM_Utils_Array::value('is_show_location', $this->_values['event']);
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'];
2fd5e617 390 $this->_values['event']['intro_text'] = CRM_Utils_Array::value('intro_text', $pcp['pcpInfo']);
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);
397 $isShowLocation = CRM_Utils_Array::value('is_show_location', $this->_values['event']);
398 $this->assign('isShowLocation', $isShowLocation);
6a488035 399 //CRM-6907
96f7c53e 400 $config->defaultCurrency = CRM_Utils_Array::value('currency', $this->_values['event'],
6a488035
TO
401 $config->defaultCurrency
402 );
403
404 //lets allow user to override campaign.
405 $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this);
406 if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
407 $this->_values['event']['campaign_id'] = $campID;
408 }
8ae4d0d3 409
410 // check if billing block is required for pay later
411 if (CRM_Utils_Array::value('is_pay_later', $this->_values['event'])) {
412 $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values['event']);
413 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater);
414 }
6a488035
TO
415 }
416
417 /**
66f9e52b 418 * Assign the minimal set of variables to the template.
6a488035
TO
419 *
420 * @return void
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 }
461 elseif (CRM_Utils_Array::value('amount', $params) == 0) {
462 $this->assign($v, CRM_Utils_Array::value($v, $params));
463 }
464 }
465
466 // assign the address formatted up for display
467 $addressParts = array(
468 "street_address-{$this->_bltID}",
469 "city-{$this->_bltID}",
470 "postal_code-{$this->_bltID}",
471 "state_province-{$this->_bltID}",
472 "country-{$this->_bltID}",
473 );
474 $addressFields = array();
475 foreach ($addressParts as $part) {
476 list($n, $id) = explode('-', $part);
477 if (isset($params['billing_' . $part])) {
478 $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $params);
479 }
480 }
481
482 $this->assign('address', CRM_Utils_Address::format($addressFields));
483
8cc574cf 484 if ($this->_contributeMode == 'direct' && empty($params['is_pay_later'])) {
6a488035
TO
485 $date = CRM_Utils_Date::format(CRM_Utils_Array::value('credit_card_exp_date', $params));
486 $date = CRM_Utils_Date::mysqlToIso($date);
487 $this->assign('credit_card_exp_date', $date);
488 $this->assign('credit_card_number',
489 CRM_Utils_System::mungeCreditCard(CRM_Utils_Array::value('credit_card_number', $params))
490 );
491 }
492
493 // get the email that the confirmation would have been sent to
494 $session = CRM_Core_Session::singleton();
495
496 // assign is_email_confirm to templates
497 if (isset($this->_values['event']['is_email_confirm'])) {
498 $this->assign('is_email_confirm', $this->_values['event']['is_email_confirm']);
499 }
500
501 // assign pay later stuff
502 $params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE);
503 $this->assign('is_pay_later', $params['is_pay_later']);
504 if ($params['is_pay_later']) {
505 $this->assign('pay_later_text', $this->_values['event']['pay_later_text']);
506 $this->assign('pay_later_receipt', $this->_values['event']['pay_later_receipt']);
507 }
508
509 // also assign all participantIDs to the template
510 // useful in generating confirmation numbers if needed
511 $this->assign('participantIDs',
512 $this->_participantIDS
513 );
514 }
515
516 /**
66f9e52b 517 * Add the custom fields.
6a488035 518 *
100fef9d
CW
519 * @param int $id
520 * @param string $name
77b97be7
EM
521 * @param bool $viewOnly
522 *
355ba699 523 * @return void
6a488035 524 */
00be9182 525 public function buildCustom($id, $name, $viewOnly = FALSE) {
6a488035 526 if ($id) {
353ffa53
TO
527 $button = substr($this->controller->getButtonName(), -4);
528 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
529 $session = CRM_Core_Session::singleton();
6a488035
TO
530 $contactID = $session->get('userID');
531
532 // we don't allow conflicting fields to be
533 // configured via profile
534 $fieldsToIgnore = array(
535 'participant_fee_amount' => 1,
536 'participant_fee_level' => 1,
537 );
538 if ($contactID) {
539 //FIX CRM-9653
540 if (is_array($id)) {
541 $fields = array();
542 foreach ($id as $profileID) {
543 $field = CRM_Core_BAO_UFGroup::getFields($profileID, FALSE, CRM_Core_Action::ADD,
544 NULL, NULL, FALSE, NULL,
545 FALSE, NULL, CRM_Core_Permission::CREATE,
546 'field_name', TRUE
547 );
548 $fields = array_merge($fields, $field);
549 }
550 }
551 else {
552 if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) {
553 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD,
554 NULL, NULL, FALSE, NULL,
555 FALSE, NULL, CRM_Core_Permission::CREATE,
556 'field_name', TRUE
557 );
558 }
559 }
560 }
561 else {
562 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD,
563 NULL, NULL, FALSE, NULL,
564 FALSE, NULL, CRM_Core_Permission::CREATE,
565 'field_name', TRUE
566 );
567 }
568
569 if (array_intersect_key($fields, $fieldsToIgnore)) {
570 $fields = array_diff_key($fields, $fieldsToIgnore);
571 CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'));
572 }
573 $addCaptcha = FALSE;
a2149acc 574
575 if (!empty($this->_fields)) {
576 $fields = @array_diff_assoc($fields, $this->_fields);
577 }
578
a7488080 579 if (empty($this->_params[0]['additional_participants']) &&
6a488035
TO
580 is_null($cid)
581 ) {
582 CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
583 }
584 $this->assign($name, $fields);
6a488035
TO
585 if (is_array($fields)) {
586 foreach ($fields as $key => $field) {
587 if ($viewOnly &&
588 isset($field['data_type']) &&
589 $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL')
590 ) {
591 // ignore file upload fields
592 continue;
593 }
594 //make the field optional if primary participant
595 //have been skip the additional participant.
596 if ($button == 'skip') {
597 $field['is_required'] = FALSE;
da8d9879 598 }
71fc6ea4 599 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
353ffa53 600 elseif ($field['add_captcha'] && !$contactID) {
6a488035
TO
601 // only add captcha for first page
602 $addCaptcha = TRUE;
603 }
6a488035 604 list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2);
6a488035
TO
605 CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE, $contactID, TRUE);
606
607 $this->_fields[$key] = $field;
608 }
609 }
4839c695 610
6a488035
TO
611 if ($addCaptcha && !$viewOnly) {
612 $captcha = CRM_Utils_ReCAPTCHA::singleton();
613 $captcha->add($this);
614 $this->assign('isCaptcha', TRUE);
615 }
616 }
617 }
618
0cf587a7 619 /**
c490a46a 620 * @param CRM_Core_Form $form
100fef9d 621 * @param int $eventID
0cf587a7
EM
622 *
623 * @throws Exception
624 */
00be9182 625 public static function initEventFee(&$form, $eventID) {
6a488035
TO
626 // get price info
627
628 // retrive all active price set fields.
629 $discountId = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event');
8567d0f8
PN
630 if (property_exists($form, '_discountId') && $form->_discountId) {
631 $discountId = $form->_discountId;
632 }
d4acad97
SB
633
634 //CRM-16456 get all price field including expired one.
635 $getAllPriceField = TRUE;
636 $className = CRM_Utils_System::getClassName($form);
637 if ($className == 'CRM_Event_Form_ParticipantFeeSelection' && $form->_action == CRM_Core_Action::UPDATE) {
638 $getAllPriceField = FALSE;
639 }
640
6a488035
TO
641 if ($discountId) {
642 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
d4acad97 643 $price = CRM_Price_BAO_PriceSet::initSet($form, $eventID, 'civicrm_event', $getAllPriceField, $priceSetId);
6a488035
TO
644 }
645 else {
d4acad97 646 $price = CRM_Price_BAO_PriceSet::initSet($form, $eventID, 'civicrm_event', $getAllPriceField);
6a488035 647 }
8ef12e64 648
649 if (property_exists($form, '_context') && ($form->_context == 'standalone'
353ffa53
TO
650 || $form->_context == 'participant')
651 ) {
8567d0f8 652 $discountedEvent = CRM_Core_BAO_Discount::getOptionGroup($eventID, 'civicrm_event');
481a74f4 653 if (is_array($discountedEvent)) {
8567d0f8 654 foreach ($discountedEvent as $key => $priceSetId) {
9da8dc8c 655 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId);
8567d0f8
PN
656 $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
657 $form->_values['discount'][$key] = CRM_Utils_Array::value('fields', $priceSet);
658 $fieldID = key($form->_values['discount'][$key]);
659 $form->_values['discount'][$key][$fieldID]['name'] = CRM_Core_DAO::getFieldValue(
9da8dc8c 660 'CRM_Price_DAO_PriceSet',
8567d0f8
PN
661 $priceSetId,
662 'title'
663 );
664 }
665 }
666 }
6a488035
TO
667 $eventFee = CRM_Utils_Array::value('fee', $form->_values);
668 if (!is_array($eventFee) || empty($eventFee)) {
669 $form->_values['fee'] = array();
670 }
671
672 //fix for non-upgraded price sets.CRM-4256.
673 if (isset($form->_isPaidEvent)) {
674 $isPaidEvent = $form->_isPaidEvent;
675 }
676 else {
677 $isPaidEvent = CRM_Utils_Array::value('is_monetary', $form->_values['event']);
678 }
679 if ($isPaidEvent && empty($form->_values['fee'])) {
680 if (CRM_Utils_System::getClassName($form) != 'CRM_Event_Form_Participant') {
681 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))));
682 }
683 }
684 }
685
686 /**
66f9e52b 687 * Handle process after the confirmation of payment by User.
6a488035 688 *
100fef9d 689 * @param int $contactID
2a6da8d7
EM
690 * @param null $contribution
691 * @param null $payment
692 *
355ba699 693 * @return void
6a488035 694 */
00be9182 695 public function confirmPostProcess($contactID = NULL, $contribution = NULL, $payment = NULL) {
6a488035
TO
696 // add/update contact information
697 $fields = array();
698 unset($this->_params['note']);
699
700 //to avoid conflict overwrite $this->_params
701 $this->_params = $this->get('value');
702
703 //get the amount of primary participant
a7488080 704 if (!empty($this->_params['is_primary'])) {
6a488035
TO
705 $this->_params['fee_amount'] = $this->get('primaryParticipantAmount');
706 }
707
708 // add participant record
bf2c70af 709 $participant = CRM_Event_Form_Registration::addParticipant($this, $contactID);
6a488035
TO
710 $this->_participantIDS[] = $participant->id;
711
712 //setting register_by_id field and primaryContactId
a7488080 713 if (!empty($this->_params['is_primary'])) {
6a488035
TO
714 $this->set('registerByID', $participant->id);
715 $this->set('primaryContactId', $contactID);
716
717 // CRM-10032
718 $this->processFirstParticipant($participant->id);
719 }
720
721 CRM_Core_BAO_CustomValueTable::postProcess($this->_params,
6a488035
TO
722 'civicrm_participant',
723 $participant->id,
724 'Participant'
725 );
726
727 $createPayment = (CRM_Utils_Array::value('amount', $this->_params, 0) != 0) ? TRUE : FALSE;
728
729 // force to create zero amount payment, CRM-5095
730 // we know the amout is zero since createPayment is false
731 if (!$createPayment &&
732 (isset($contribution) && $contribution->id) &&
733 $this->_priceSetId &&
734 $this->_lineItem
735 ) {
736 $createPayment = TRUE;
737 }
738
8cc574cf 739 if ($createPayment && $this->_values['event']['is_monetary'] && !empty($this->_params['contributionID'])) {
6a488035
TO
740 $paymentParams = array(
741 'participant_id' => $participant->id,
742 'contribution_id' => $contribution->id,
743 );
744 $ids = array();
745 $paymentPartcipant = CRM_Event_BAO_ParticipantPayment::create($paymentParams, $ids);
746 }
747
748 //set only primary participant's params for transfer checkout.
8cc574cf 749 if (($this->_contributeMode == 'checkout' || $this->_contributeMode == 'notify') && !empty($this->_params['is_primary'])) {
6a488035
TO
750 $this->_params['participantID'] = $participant->id;
751 $this->set('primaryParticipant', $this->_params);
752 }
753
754 $this->assign('action', $this->_action);
755
756 // create CMS user
a7488080 757 if (!empty($this->_params['cms_create_account'])) {
6a488035
TO
758 $this->_params['contactID'] = $contactID;
759
760 if (array_key_exists('email-5', $this->_params)) {
353ffa53 761 $mail = 'email-5';
0db6c3e1
TO
762 }
763 else {
6a488035
TO
764 foreach ($this->_params as $name => $dontCare) {
765 if (substr($name, 0, 5) == 'email') {
766 $mail = $name;
767 break;
768 }
769 }
770 }
771
772 // we should use primary email for
773 // 1. free event registration.
774 // 2. pay later participant.
775 // 3. waiting list participant.
776 // 4. require approval participant.
a7488080 777 if (!empty($this->_params['is_pay_later']) ||
353ffa53
TO
778 $this->_allowWaitlist || $this->_requireApproval || empty($this->_values['event']['is_monetary'])
779 ) {
6a488035
TO
780 $mail = 'email-Primary';
781 }
782
783 if (!CRM_Core_BAO_CMSUser::create($this->_params, $mail)) {
784 CRM_Core_Error::statusBounce(ts('Your profile is not saved and Account is not created.'));
785 }
786 }
787 }
788
789 /**
66f9e52b 790 * Process the participant.
6a488035 791 *
3ab5efa9 792 * @param CRM_Core_Form $form
100fef9d 793 * @param int $contactID
3ab5efa9 794 * @return \CRM_Event_BAO_Participant
6a488035 795 */
bf2c70af 796 public static function addParticipant(&$form, $contactID) {
797 if (empty($form->_params)) {
7c550ca0 798 return NULL;
bf2c70af 799 }
800 $params = $form->_params;
6a488035
TO
801 $transaction = new CRM_Core_Transaction();
802
803 $groupName = 'participant_role';
804 $query = "
805SELECT v.label as label ,v.value as value
806FROM civicrm_option_value v,
807 civicrm_option_group g
808WHERE v.option_group_id = g.id
809 AND g.name = %1
810 AND v.is_active = 1
811 AND g.is_active = 1
812";
813 $p = array(1 => array($groupName, 'String'));
814
815 $dao = CRM_Core_DAO::executeQuery($query, $p);
816 if ($dao->fetch()) {
817 $roleID = $dao->value;
818 }
819
820 // handle register date CRM-4320
821 $registerDate = NULL;
bf2c70af 822 if (!empty($form->_allowConfirmation) && $form->_participantId) {
6a488035
TO
823 $registerDate = $params['participant_register_date'];
824 }
a7488080 825 elseif (!empty($params['participant_register_date']) &&
6a488035
TO
826 is_array($params['participant_register_date']) &&
827 !empty($params['participant_register_date'])
828 ) {
829 $registerDate = CRM_Utils_Date::format($params['participant_register_date']);
830 }
831
6441bcc8 832 $participantFields = CRM_Event_DAO_Participant::fields();
6ea503d4
TO
833 $participantParams = array(
834 'id' => CRM_Utils_Array::value('participant_id', $params),
6a488035 835 'contact_id' => $contactID,
bf2c70af 836 'event_id' => $form->_eventId ? $form->_eventId : $params['event_id'],
6a488035
TO
837 'status_id' => CRM_Utils_Array::value('participant_status',
838 $params, 1
839 ),
840 'role_id' => CRM_Utils_Array::value('participant_role_id',
841 $params, $roleID
842 ),
843 'register_date' => ($registerDate) ? $registerDate : date('YmdHis'),
6441bcc8 844 'source' => CRM_Utils_String::ellipsify(
7c550ca0 845 isset($params['participant_source']) ? CRM_Utils_Array::value('participant_source', $params) : CRM_Utils_Array::value('description', $params),
6441bcc8
NG
846 $participantFields['participant_source']['maxlength']
847 ),
6a488035
TO
848 'fee_level' => CRM_Utils_Array::value('amount_level', $params),
849 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0),
850 'fee_amount' => CRM_Utils_Array::value('fee_amount', $params),
851 'registered_by_id' => CRM_Utils_Array::value('registered_by_id', $params),
852 'discount_id' => CRM_Utils_Array::value('discount_id', $params),
853 'fee_currency' => CRM_Utils_Array::value('currencyID', $params),
854 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params),
855 );
856
bf2c70af 857 if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') {
6a488035
TO
858 $participantParams['is_test'] = 1;
859 }
860 else {
861 $participantParams['is_test'] = 0;
862 }
863
bf2c70af 864 if (!empty($form->_params['note'])) {
865 $participantParams['note'] = $form->_params['note'];
6a488035 866 }
bf2c70af 867 elseif (!empty($form->_params['participant_note'])) {
868 $participantParams['note'] = $form->_params['participant_note'];
6a488035
TO
869 }
870
871 // reuse id if one already exists for this one (can happen
872 // with back button being hit etc)
8cc574cf 873 if (!$participantParams['id'] && !empty($params['contributionID'])) {
6a488035
TO
874 $pID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
875 $params['contributionID'],
876 'participant_id',
877 'contribution_id'
878 );
879 $participantParams['id'] = $pID;
880 }
bf2c70af 881 $participantParams['discount_id'] = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event');
6a488035
TO
882
883 if (!$participantParams['discount_id']) {
884 $participantParams['discount_id'] = "null";
885 }
886
887 $participant = CRM_Event_BAO_Participant::create($participantParams);
888
889 $transaction->commit();
890
891 return $participant;
892 }
893
894 /* Calculate the total participant count as per params.
895 *
d4dd1e85
TO
896 * @param array $params
897 * User params.
6a488035
TO
898 *
899 * @return $totalCount total participant count.
6a488035 900 */
0cf587a7 901 /**
c490a46a
CW
902 * @param CRM_Core_Form $form
903 * @param array $params
0cf587a7
EM
904 * @param bool $skipCurrent
905 *
906 * @return int|string
907 */
6a488035
TO
908 public static function getParticipantCount(&$form, $params, $skipCurrent = FALSE) {
909 $totalCount = 0;
910 if (!is_array($params) || empty($params)) {
911 return $totalCount;
912 }
913
353ffa53
TO
914 $priceSetId = $form->get('priceSetId');
915 $addParticipantNum = substr($form->_name, 12);
916 $priceSetFields = $priceSetDetails = array();
6a488035
TO
917 $hasPriceFieldsCount = FALSE;
918 if ($priceSetId) {
919 $priceSetDetails = $form->get('priceSet');
920 if (isset($priceSetDetails['optionsCountTotal'])
921 && $priceSetDetails['optionsCountTotal']
922 ) {
923 $hasPriceFieldsCount = TRUE;
924 $priceSetFields = $priceSetDetails['optionsCountDetails']['fields'];
925 }
926 }
927
928 $singleFormParams = FALSE;
929 foreach ($params as $key => $val) {
930 if (!is_numeric($key)) {
931 $singleFormParams = TRUE;
932 break;
933 }
934 }
935
936 //first format the params.
937 if ($singleFormParams) {
938 $params = self::formatPriceSetParams($form, $params);
939 $params = array($params);
940 }
941
942 foreach ($params as $key => $values) {
943 if (!is_numeric($key) ||
944 $values == 'skip' ||
945 ($skipCurrent && ($addParticipantNum == $key))
946 ) {
947 continue;
948 }
949 $count = 1;
950
951 $usedCache = FALSE;
952 $cacheCount = CRM_Utils_Array::value($key, $form->_lineItemParticipantsCount);
953 if ($cacheCount && is_numeric($cacheCount)) {
954 $count = $cacheCount;
955 $usedCache = TRUE;
956 }
957
958 if (!$usedCache && $hasPriceFieldsCount) {
959 $count = 0;
960 foreach ($values as $valKey => $value) {
961 if (strpos($valKey, 'price_') === FALSE) {
962 continue;
963 }
964 $priceFieldId = substr($valKey, 6);
965 if (!$priceFieldId ||
966 !is_array($value) ||
967 !array_key_exists($priceFieldId, $priceSetFields)
968 ) {
969 continue;
970 }
971 foreach ($value as $optId => $optVal) {
972 $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
973 if ($currentCount) {
974 $count += $currentCount;
975 }
976 }
977 }
978 if (!$count) {
979 $count = 1;
980 }
981 }
982 $totalCount += $count;
983 }
984 if (!$totalCount) {
985 $totalCount = 1;
986 }
987
988 return $totalCount;
989 }
990
991 /* Format user submitted price set params.
992 * Convert price set each param as an array.
993 *
5a4f6742 994 * @param array $params
d4dd1e85 995 * An array of user submitted params.
6a488035
TO
996 *
997 *
a6c01b45
CW
998 * @return array
999 * , formatted price set params.
6a488035 1000 */
0cf587a7 1001 /**
c490a46a
CW
1002 * @param CRM_Core_Form $form
1003 * @param array $params
0cf587a7
EM
1004 *
1005 * @return mixed
1006 */
6a488035
TO
1007 public static function formatPriceSetParams(&$form, $params) {
1008 if (!is_array($params) || empty($params)) {
1009 return $params;
1010 }
1011
1012 $priceSetId = $form->get('priceSetId');
1013 if (!$priceSetId) {
1014 return $params;
1015 }
1016 $priceSetDetails = $form->get('priceSet');
1017
1018 foreach ($params as $key => & $value) {
1019 $vals = array();
1020 if (strpos($key, 'price_') !== FALSE) {
1021 $fieldId = substr($key, 6);
1022 if (!array_key_exists($fieldId, $priceSetDetails['fields']) ||
1023 is_array($value) ||
1024 !$value
1025 ) {
1026 continue;
1027 }
1028 $field = $priceSetDetails['fields'][$fieldId];
1029 if ($field['html_type'] == 'Text') {
1030 $fieldOption = current($field['options']);
1031 $value = array($fieldOption['id'] => $value);
1032 }
1033 else {
1034 $value = array($value => TRUE);
1035 }
1036 }
1037 }
1038
1039 return $params;
1040 }
1041
1042 /* Calculate total count for each price set options.
1043 * those are currently selected by user.
1044 *
d4dd1e85
TO
1045 * @param $form
1046 * Form object.
6a488035
TO
1047 *
1048 *
a6c01b45
CW
1049 * @return array
1050 * , array of each option w/ count total.
6a488035 1051 */
0cf587a7
EM
1052 /**
1053 * @param $form
1054 *
1055 * @return array
1056 */
169be3c1 1057 public static function getPriceSetOptionCount(&$form) {
353ffa53
TO
1058 $params = $form->get('params');
1059 $priceSet = $form->get('priceSet');
6a488035
TO
1060 $priceSetId = $form->get('priceSetId');
1061
1062 $optionsCount = array();
1063 if (!$priceSetId ||
1064 !is_array($priceSet) ||
1065 empty($priceSet) ||
1066 !is_array($params) ||
1067 empty($params)
1068 ) {
1069 return $optionsCount;
1070 }
1071
1072 $priceSetFields = $priceMaxFieldDetails = array();
a7488080 1073 if (!empty($priceSet['optionsCountTotal'])) {
6a488035
TO
1074 $priceSetFields = $priceSet['optionsCountDetails']['fields'];
1075 }
1076
a7488080 1077 if (!empty($priceSet['optionsMaxValueTotal'])) {
6a488035
TO
1078 $priceMaxFieldDetails = $priceSet['optionsMaxValueDetails']['fields'];
1079 }
1080
1081 $addParticipantNum = substr($form->_name, 12);
1082 foreach ($params as $pCnt => $values) {
1083 if ($values == 'skip' ||
79b152ac 1084 $pCnt === $addParticipantNum
6a488035
TO
1085 ) {
1086 continue;
1087 }
1088
1089 foreach ($values as $valKey => $value) {
1090 if (strpos($valKey, 'price_') === FALSE) {
1091 continue;
1092 }
1093
1094 $priceFieldId = substr($valKey, 6);
1095 if (!$priceFieldId ||
1096 !is_array($value) ||
1097 !(array_key_exists($priceFieldId, $priceSetFields) || array_key_exists($priceFieldId, $priceMaxFieldDetails))
1098 ) {
1099 continue;
1100 }
1101
1102 foreach ($value as $optId => $optVal) {
1103 if (CRM_Utils_Array::value('html_type', $priceSet['fields'][$priceFieldId]) == 'Text') {
1104 $currentCount = $optVal;
1105 }
1106 else {
1107 $currentCount = 1;
1108 }
1109
1110 if (isset($priceSetFields[$priceFieldId]) && isset($priceSetFields[$priceFieldId]['options'][$optId])) {
1111 $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
1112 }
1113
1114 $optionsCount[$optId] = $currentCount + CRM_Utils_Array::value($optId, $optionsCount, 0);
1115 }
1116 }
1117 }
1118
1119 return $optionsCount;
1120 }
1121
0cf587a7
EM
1122 /**
1123 * @param string $suffix
1124 *
1125 * @return null|string
1126 */
00be9182 1127 public function checkTemplateFileExists($suffix = '') {
6a488035
TO
1128 if ($this->_eventId) {
1129 $templateName = $this->_name;
1130 if (substr($templateName, 0, 12) == 'Participant_') {
1131 $templateName = 'AdditionalParticipant';
1132 }
1133
1134 $templateFile = "CRM/Event/Form/Registration/{$this->_eventId}/{$templateName}.{$suffix}tpl";
1135 $template = CRM_Core_Form::getTemplate();
1136 if ($template->template_exists($templateFile)) {
1137 return $templateFile;
1138 }
1139 }
1140 return NULL;
1141 }
1142
0cf587a7
EM
1143 /**
1144 * @return null|string
1145 */
00be9182 1146 public function getTemplateFileName() {
6a488035
TO
1147 $fileName = $this->checkTemplateFileExists();
1148 return $fileName ? $fileName : parent::getTemplateFileName();
1149 }
1150
0cf587a7
EM
1151 /**
1152 * @return null|string
1153 */
00be9182 1154 public function overrideExtraTemplateFileName() {
6a488035
TO
1155 $fileName = $this->checkTemplateFileExists('extra.');
1156 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
1157 }
1158
0dc0b759 1159 /**
1160 * Reset values for all options those are full.
1161 *
780422d7
EM
1162 * @param array $optionFullIds
1163 * @param $form
0dc0b759 1164 */
1165 public static function resetElementValue($optionFullIds = array(), &$form) {
1166 if (!is_array($optionFullIds) ||
1167 empty($optionFullIds) ||
1168 !$form->isSubmitted()
1169 ) {
1170 return;
1171 }
1172
1173 foreach ($optionFullIds as $fldId => $optIds) {
1174 $name = "price_$fldId";
1175 if (!$form->elementExists($name)) {
1176 continue;
1177 }
1178
1179 $element = $form->getElement($name);
1180 $eleType = $element->getType();
1181
1182 $resetSubmitted = FALSE;
1183 switch ($eleType) {
1184 case 'text':
1185 if ($element->getValue() && $element->isFrozen()) {
1186 $label = "{$element->getLabel()}<tt>(x)</tt>";
1187 $element->setLabel($label);
1188 $element->setPersistantFreeze();
1189 $resetSubmitted = TRUE;
1190 }
1191 break;
1192
1193 case 'group':
1194 if (is_array($element->_elements)) {
1195 foreach ($element->_elements as $child) {
1196 $childType = $child->getType();
1197 $methodName = 'getName';
1198 if ($childType) {
1199 $methodName = 'getValue';
1200 }
1201 if (in_array($child->{$methodName}(), $optIds) && $child->isFrozen()) {
1202 $resetSubmitted = TRUE;
1203 $child->setPersistantFreeze();
1204 }
1205 }
1206 }
1207 break;
1208
1209 case 'select':
9d8d8fd0 1210 $value = $element->getValue();
1211 if (in_array($value[0], $optIds)) {
0dc0b759 1212 foreach ($element->_options as $option) {
d0ebccea 1213 if ($option['attr']['value'] === "crm_disabled_opt-{$value[0]}") {
2ae4d103 1214 $placeholder = html_entity_decode($option['text'], ENT_QUOTES, "UTF-8");
0dc0b759 1215 $element->updateAttributes(array('placeholder' => $placeholder));
1216 break;
1217 }
1218 }
1219 $resetSubmitted = TRUE;
1220 }
1221 break;
1222 }
1223
1224 //finally unset values from submitted.
1225 if ($resetSubmitted) {
1226 self::resetSubmittedValue($name, $optIds, $form);
1227 }
1228 }
1229 }
1230
1231 /**
1232 * @param string $elementName
1233 * @param array $optionIds
780422d7 1234 * @param CRM_Core_form $form
0dc0b759 1235 */
1236 public static function resetSubmittedValue($elementName, $optionIds = array(), &$form) {
1237 if (empty($elementName) ||
1238 !$form->elementExists($elementName) ||
1239 !$form->getSubmitValue($elementName)
1240 ) {
1241 return;
1242 }
1243 foreach (array(
1244 'constantValues',
1245 'submitValues',
1246 'defaultValues',
1247 ) as $val) {
1248 $values = $form->{"_$val"};
1249 if (!is_array($values) || empty($values)) {
1250 continue;
1251 }
1252 $eleVal = CRM_Utils_Array::value($elementName, $values);
1253 if (empty($eleVal)) {
1254 continue;
1255 }
1256 if (is_array($eleVal)) {
1257 $found = FALSE;
1258 foreach ($eleVal as $keyId => $ignore) {
1259 if (in_array($keyId, $optionIds)) {
1260 $found = TRUE;
1261 unset($values[$elementName][$keyId]);
1262 }
1263 }
1264 if ($found && empty($values[$elementName][$keyId])) {
1265 $values[$elementName][$keyId] = NULL;
1266 }
1267 }
1268 else {
1269 if (!empty($keyId)) {
1270 $values[$elementName][$keyId] = NULL;
1271 }
1272 }
1273 }
1274 }
1275
da8d9879
DG
1276 /**
1277 * Validate price set submitted params for price option limit,
6a488035 1278 * as well as user should select at least one price field option.
72b3a70c
CW
1279 *
1280 * @param CRM_Core_Form $form
1281 * @param array $params
1282 *
1283 * @return array
6a488035 1284 */
00be9182 1285 public static function validatePriceSet(&$form, $params) {
6a488035
TO
1286 $errors = array();
1287 $hasOptMaxValue = FALSE;
1288 if (!is_array($params) || empty($params)) {
1289 return $errors;
1290 }
1291
1292 $currentParticipantNum = substr($form->_name, 12);
1293 if (!$currentParticipantNum) {
1294 $currentParticipantNum = 0;
1295 }
1296
1297 $priceSetId = $form->get('priceSetId');
1298 $priceSetDetails = $form->get('priceSet');
1299 if (
1300 !$priceSetId ||
1301 !is_array($priceSetDetails) ||
1302 empty($priceSetDetails)
1303 ) {
1304 return $errors;
1305 }
1306
1307 $optionsCountDetails = $optionsMaxValueDetails = array();
1308 if (
1309 isset($priceSetDetails['optionsMaxValueTotal'])
1310 && $priceSetDetails['optionsMaxValueTotal']
1311 ) {
1312 $hasOptMaxValue = TRUE;
1313 $optionsMaxValueDetails = $priceSetDetails['optionsMaxValueDetails']['fields'];
1314 }
1315 if (
1316 isset($priceSetDetails['optionsCountTotal'])
1317 && $priceSetDetails['optionsCountTotal']
1318 ) {
1319 $hasOptCount = TRUE;
1320 $optionsCountDetails = $priceSetDetails['optionsCountDetails']['fields'];
1321 }
1322 $feeBlock = $form->_feeBlock;
1323
1324 if (empty($feeBlock)) {
1325 $feeBlock = $priceSetDetails['fields'];
1326 }
1327
1328 $optionMaxValues = $fieldSelected = array();
1329 foreach ($params as $pNum => $values) {
1330 if (!is_array($values) || $values == 'skip') {
1331 continue;
1332 }
1333
1334 foreach ($values as $valKey => $value) {
1335 if (strpos($valKey, 'price_') === FALSE) {
1336 continue;
1337 }
1338 $priceFieldId = substr($valKey, 6);
1339 $noneOptionValueSelected = FALSE;
1340 if (!$feeBlock[$priceFieldId]['is_required'] && $value == 0) {
1341 $noneOptionValueSelected = TRUE;
1342 }
1343
1344 if (
1345 !$priceFieldId ||
1346 (!$noneOptionValueSelected && !is_array($value))
1347 ) {
1348 continue;
1349 }
1350
1351 $fieldSelected[$pNum] = TRUE;
1352
1353 if (!$hasOptMaxValue || !is_array($value)) {
1354 continue;
1355 }
1356
1357 foreach ($value as $optId => $optVal) {
1358 if (CRM_Utils_Array::value('html_type', $feeBlock[$priceFieldId]) == 'Text') {
1359 $currentMaxValue = $optVal;
1360 }
1361 else {
1362 $currentMaxValue = 1;
1363 }
1364
1365 if (isset($optionsCountDetails[$priceFieldId]) && isset($optionsCountDetails[$priceFieldId]['options'][$optId])) {
1366 $currentMaxValue = $optionsCountDetails[$priceFieldId]['options'][$optId] * $optVal;
1367 }
1368 if (empty($optionMaxValues)) {
1369 $optionMaxValues[$priceFieldId][$optId] = $currentMaxValue;
1370 }
1371 else {
7c550ca0
WA
1372 $optionMaxValues[$priceFieldId][$optId]
1373 = $currentMaxValue + CRM_Utils_Array::value($optId, CRM_Utils_Array::value($priceFieldId, $optionMaxValues), 0);
6a488035 1374 }
0dc0b759 1375 $soldOutPnum[$optId] = $pNum;
6a488035
TO
1376 }
1377 }
0dc0b759 1378
1379 //validate for price field selection.
1380 if (empty($fieldSelected[$pNum])) {
1381 $errors[$pNum]['_qf_default'] = ts('Select at least one option from Event Fee(s).');
1382 }
6a488035
TO
1383 }
1384
1385 //validate for option max value.
1386 foreach ($optionMaxValues as $fieldId => $values) {
1387 $options = CRM_Utils_Array::value('options', $feeBlock[$fieldId], array());
1388 foreach ($values as $optId => $total) {
1389 $optMax = $optionsMaxValueDetails[$fieldId]['options'][$optId];
1390 $opDbCount = CRM_Utils_Array::value('db_total_count', $options[$optId], 0);
1391 $total += $opDbCount;
0dc0b759 1392 if ($optMax && ($total > $optMax)) {
6a488035 1393 if ($opDbCount && ($opDbCount >= $optMax)) {
0dc0b759 1394 $errors[$soldOutPnum[$optId]]["price_{$fieldId}"]
7c550ca0 1395 = ts('Sorry, this option is currently sold out.');
6a488035
TO
1396 }
1397 elseif (($optMax - $opDbCount) == 1) {
0dc0b759 1398 $errors[$soldOutPnum[$optId]]["price_{$fieldId}"]
7c550ca0 1399 = ts('Sorry, currently only a single seat is available for this option.', array(1 => ($optMax - $opDbCount)));
6a488035
TO
1400 }
1401 else {
0dc0b759 1402 $errors[$soldOutPnum[$optId]]["price_{$fieldId}"]
7c550ca0 1403 = ts('Sorry, currently only %1 seats are available for this option.', array(1 => ($optMax - $opDbCount)));
6a488035
TO
1404 }
1405 }
1406 }
1407 }
6a488035
TO
1408 return $errors;
1409 }
1410
0cf587a7 1411 /**
72b3a70c
CW
1412 * set the first participant ID if not set, CRM-10032
1413 *
100fef9d 1414 * @param int $participantID
0cf587a7 1415 */
00be9182 1416 public function processFirstParticipant($participantID) {
6a488035
TO
1417 $this->_participantId = $participantID;
1418 $this->set('participantId', $this->_participantId);
1419
1420 $ids = $participantValues = array();
1421 $participantParams = array('id' => $this->_participantId);
1422 CRM_Event_BAO_Participant::getValues($participantParams, $participantValues, $ids);
1423 $this->_values['participant'] = $participantValues[$this->_participantId];
1424 $this->set('values', $this->_values);
1425
1426 // also set the allow confirmation stuff
1427 if (array_key_exists(
353ffa53
TO
1428 $this->_values['participant']['status_id'],
1429 CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'")
1430 )) {
6a488035
TO
1431 $this->_allowConfirmation = TRUE;
1432 $this->set('allowConfirmation', TRUE);
1433 }
1434 }
1435
e2d09ab4
EM
1436 /**
1437 * @todo - combine this with CRM_Event_BAO_Event::validRegistrationRequest
1438 * (probably extract relevant values here & call that with them & handle bounces & redirects here -as
1439 * those belong in the form layer)
1440 *
1441 * @param string $redirect
1442 */
00be9182 1443 public function checkValidEvent($redirect = NULL) {
6a488035
TO
1444 // is the event active (enabled)?
1445 if (!$this->_values['event']['is_active']) {
1446 // form is inactive, die a fatal death
1447 CRM_Core_Error::statusBounce(ts('The event you requested is currently unavailable (contact the site administrator for assistance).'));
1448 }
1449
1450 // is online registration is enabled?
1451 if (!$this->_values['event']['is_online_registration']) {
1452 CRM_Core_Error::statusBounce(ts('Online registration is not currently available for this event (contact the site administrator for assistance).'), $redirect);
1453 }
1454
1455 // is this an event template ?
a7488080 1456 if (!empty($this->_values['event']['is_template'])) {
6a488035
TO
1457 CRM_Core_Error::statusBounce(ts('Event templates are not meant to be registered.'), $redirect);
1458 }
1459
1460 $now = date('YmdHis');
1461 $startDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_start_date',
353ffa53
TO
1462 $this->_values['event']
1463 ));
6a488035
TO
1464
1465 if (
1466 $startDate &&
1467 $startDate >= $now
1468 ) {
1469 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);
1470 }
1471
1472 $endDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_end_date',
353ffa53
TO
1473 $this->_values['event']
1474 ));
4bd9b7a9 1475 $eventEndDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('event_end_date', $this->_values['event']));
6a488035
TO
1476 if (
1477 $endDate &&
1478 $endDate < $now
1479 ) {
1480 CRM_Core_Error::statusBounce(ts('Registration for this event ended on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('registration_end_date', $this->_values['event'])))), $redirect);
1481 }
4bd9b7a9
RK
1482 if (!empty($eventEndDate) && $eventEndDate < $now) {
1483 CRM_Core_Error::statusBounce(ts('Event ended on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('event_end_date', $this->_values['event'])))), $redirect);
1484 }
6a488035 1485 }
96025800 1486
6a488035 1487}