Merge pull request #16759 from eileenmcnaughton/fatal2
[civicrm-core.git] / CRM / Event / Cart / Form / Cart.php
1 <?php
2
3 /**
4 * Class CRM_Event_Cart_Form_Cart
5 */
6 class CRM_Event_Cart_Form_Cart extends CRM_Core_Form {
7
8 /**
9 * @var \CRM_Event_Cart_BAO_Cart
10 */
11 public $cart;
12
13 public $_action;
14 public $contact;
15 public $event_cart_id = NULL;
16 public $_mode;
17 public $participants;
18
19 public function preProcess() {
20 $this->_action = CRM_Utils_Request::retrieveValue('action', 'String');
21 $this->_mode = 'live';
22 $this->loadCart();
23
24 $this->checkWaitingList();
25
26 $this->assignBillingType();
27
28 $event_titles = [];
29 foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
30 $event_titles[] = $event_in_cart->event->title;
31 }
32 $this->description = ts("Online Registration for %1", [1 => implode(", ", $event_titles)]);
33 if (!isset($this->discounts)) {
34 $this->discounts = [];
35 }
36 }
37
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();
41 }
42 else {
43 $this->cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->event_cart_id);
44 }
45 $this->cart->load_associations();
46 $this->stub_out_and_inherit();
47 }
48
49 public function stub_out_and_inherit() {
50 $transaction = new CRM_Core_Transaction();
51
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(),
58 ];
59 $participant = CRM_Event_Cart_BAO_MerParticipant::create($participant_params);
60 $participant->save();
61 $event_in_cart->add_participant($participant);
62 }
63 $event_in_cart->save();
64 }
65 $transaction->commit();
66 }
67
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) {
72 continue;
73 }
74 foreach ($event_in_cart->participants as $participant) {
75 $participant->must_wait = ($empty_seats <= 0);
76 $empty_seats--;
77 }
78 }
79 }
80
81 /**
82 * @param int $event_id
83 *
84 * @return bool|int|null|string
85 */
86 public function checkEventCapacity($event_id) {
87 $empty_seats = CRM_Event_BAO_Participant::eventFull($event_id, TRUE);
88 if (is_numeric($empty_seats)) {
89 return $empty_seats;
90 }
91 if (is_string($empty_seats)) {
92 return 0;
93 }
94 else {
95 return NULL;
96 }
97 }
98
99 /**
100 * @return int
101 * @throws \CRM_Core_Exception
102 */
103 public function getContactID() {
104 $tempID = CRM_Utils_Request::retrieveValue('cid', 'Positive');
105
106 //check if this is a checksum authentication
107 $userChecksum = CRM_Utils_Request::retrieveValue('cs', 'String');
108 if ($userChecksum) {
109 //check for anonymous user.
110 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($tempID, $userChecksum);
111 if ($validUser) {
112 return $tempID;
113 }
114 }
115
116 // check if the user is registered and we have a contact ID
117 $session = CRM_Core_Session::singleton();
118 return $session->get('userID');
119 }
120
121 /**
122 * @param $fields
123 *
124 * @return mixed|null
125 */
126 public static function find_contact($fields) {
127 return CRM_Contact_BAO_Contact::getFirstDuplicateContact($fields, 'Individual', 'Unsupervised', [], FALSE);
128 }
129
130 /**
131 * @param array $fields
132 *
133 * @return int|mixed|null
134 */
135 public static function find_or_create_contact($fields = []) {
136 $contact_id = self::find_contact($fields);
137
138 if ($contact_id) {
139 return $contact_id;
140 }
141 $contact_params = [
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),
146 ];
147 $no_fields = [];
148 $contact_id = CRM_Contact_BAO_Contact::createProfileContact($contact_params, $no_fields, NULL);
149 if (!$contact_id) {
150 CRM_Core_Session::setStatus(ts("Could not create or match a contact with that email address. Please contact the webmaster."), '', 'error');
151 }
152 return $contact_id;
153 }
154
155 /**
156 * @param string $page_name
157 *
158 * @return mixed
159 */
160 public function getValuesForPage($page_name) {
161 $container = $this->controller->container();
162 return $container['values'][$page_name];
163 }
164
165 }