4 * Class CRM_Event_Cart_Form_Cart
6 class CRM_Event_Cart_Form_Cart
extends CRM_Core_Form
{
9 * @var \CRM_Event_Cart_BAO_Cart
15 public $event_cart_id = NULL;
19 public function preProcess() {
20 $this->_action
= CRM_Utils_Request
::retrieveValue('action', 'String');
21 $this->_mode
= 'live';
24 $this->checkWaitingList();
26 $this->assignBillingType();
29 foreach ($this->cart
->get_main_events_in_carts() as $event_in_cart) {
30 $event_titles[] = $event_in_cart->event
->title
;
32 $this->description
= ts("Online Registration for %1", [1 => implode(", ", $event_titles)]);
33 if (!isset($this->discounts
)) {
34 $this->discounts
= [];
38 public function loadCart() {
39 if ($this->event_cart_id
== NULL) {
40 $this->cart
= CRM_Event_Cart_BAO_Cart
::find_or_create_for_current_session();
43 $this->cart
= CRM_Event_Cart_BAO_Cart
::find_by_id($this->event_cart_id
);
45 $this->cart
->load_associations();
46 $this->stub_out_and_inherit();
49 public function stub_out_and_inherit() {
50 $transaction = new CRM_Core_Transaction();
52 foreach ($this->cart
->get_main_events_in_carts() as $event_in_cart) {
53 if (empty($event_in_cart->participants
)) {
54 $participant_params = [
55 'cart_id' => $this->cart
->id
,
56 'event_id' => $event_in_cart->event_id
,
57 'contact_id' => self
::find_or_create_contact(),
59 $participant = CRM_Event_Cart_BAO_MerParticipant
::create($participant_params);
61 $event_in_cart->add_participant($participant);
63 $event_in_cart->save();
65 $transaction->commit();
68 public function checkWaitingList() {
69 foreach ($this->cart
->events_in_carts
as $event_in_cart) {
70 $empty_seats = $this->checkEventCapacity($event_in_cart->event_id
);
71 if ($empty_seats === NULL) {
74 foreach ($event_in_cart->participants
as $participant) {
75 $participant->must_wait
= ($empty_seats <= 0);
82 * @param int $event_id
84 * @return bool|int|null|string
86 public function checkEventCapacity($event_id) {
87 $empty_seats = CRM_Event_BAO_Participant
::eventFull($event_id, TRUE);
88 if (is_numeric($empty_seats)) {
91 if (is_string($empty_seats)) {
101 * @throws \CRM_Core_Exception
103 public function getContactID() {
104 $tempID = CRM_Utils_Request
::retrieveValue('cid', 'Positive');
106 //check if this is a checksum authentication
107 $userChecksum = CRM_Utils_Request
::retrieveValue('cs', 'String');
109 //check for anonymous user.
110 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($tempID, $userChecksum);
116 // check if the user is registered and we have a contact ID
117 $session = CRM_Core_Session
::singleton();
118 return $session->get('userID');
126 public static function find_contact($fields) {
127 return CRM_Contact_BAO_Contact
::getFirstDuplicateContact($fields, 'Individual', 'Unsupervised', [], FALSE);
131 * @param array $fields
133 * @return int|mixed|null
135 public static function find_or_create_contact($fields = []) {
136 $contact_id = self
::find_contact($fields);
142 'email-Primary' => $fields['email'] ??
NULL,
143 'first_name' => $fields['first_name'] ??
NULL,
144 'last_name' => $fields['last_name'] ??
NULL,
145 'is_deleted' => CRM_Utils_Array
::value('is_deleted', $fields, TRUE),
148 $contact_id = CRM_Contact_BAO_Contact
::createProfileContact($contact_params, $no_fields, NULL);
150 CRM_Core_Session
::setStatus(ts("Could not create or match a contact with that email address. Please contact the webmaster."), '', 'error');
156 * @param string $page_name
160 public function getValuesForPage($page_name) {
161 $container = $this->controller
->container();
162 return $container['values'][$page_name];