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