Merge pull request #17833 from totten/master-website-test
[civicrm-core.git] / ext / eventcart / CRM / Event / Cart / BAO / Cart.php
1 <?php
2
3 /**
4 * Class CRM_Event_Cart_BAO_Cart
5 */
6 class CRM_Event_Cart_BAO_Cart extends CRM_Event_Cart_DAO_Cart {
7 public $associations_loaded = FALSE;
8 /**
9 * event_in_cart_id => $event_in_cart
10 * @var array
11 */
12 public $events_in_carts = [];
13
14 /**
15 * @param array $params
16 *
17 * @return CRM_Event_Cart_BAO_Cart
18 */
19 public static function add(&$params) {
20 $cart = new CRM_Event_Cart_BAO_Cart();
21 $cart->copyValues($params);
22 $result = $cart->save();
23 return $result;
24 }
25
26 /**
27 * @param int $event_id
28 *
29 * @return mixed
30 */
31 public function add_event($event_id) {
32 $this->load_associations();
33 $event_in_cart = $this->get_event_in_cart_by_event_id($event_id);
34 if ($event_in_cart) {
35 return $event_in_cart;
36 }
37
38 $params = [
39 'event_id' => $event_id,
40 'event_cart_id' => $this->id,
41 ];
42 $event_in_cart = CRM_Event_Cart_BAO_EventInCart::create($params);
43 $event_in_cart->load_associations($this);
44 $this->events_in_carts[$event_in_cart->event_id] = $event_in_cart;
45 return $this->events_in_carts[$event_in_cart->event_id];
46 }
47
48 /**
49 * @param $participant
50 */
51 public function add_participant_to_cart($participant) {
52 $event_in_cart = $this->get_event_in_cart_by_event_id($participant->event_id);
53 if (!$event_in_cart) {
54 $event_in_cart = $this->add_event($participant->event_id);
55 }
56 $event_in_cart->add_participant($participant);
57 $event_in_cart->save();
58 }
59
60 /**
61 * @param array $params
62 *
63 * @return CRM_Event_Cart_BAO_Cart
64 * @throws Exception
65 */
66 public static function create($params) {
67 $transaction = new CRM_Core_Transaction();
68
69 $cart = self::add($params);
70
71 if (is_a($cart, 'CRM_Core_Error')) {
72 $transaction->rollback();
73 throw new CRM_Core_Exception(ts('There was an error creating an event cart'));
74 }
75
76 $transaction->commit();
77
78 return $cart;
79 }
80
81 /**
82 * @param int $id
83 *
84 * @return bool|CRM_Event_Cart_BAO_Cart
85 */
86 public static function find_by_id($id) {
87 return self::find_by_params(['id' => $id]);
88 }
89
90 /**
91 * @param array $params
92 *
93 * @return bool|CRM_Event_Cart_BAO_Cart
94 */
95 public static function find_by_params($params) {
96 $cart = new CRM_Event_Cart_BAO_Cart();
97 $cart->copyValues($params);
98 if ($cart->find(TRUE)) {
99 return $cart;
100 }
101 else {
102 return FALSE;
103 }
104 }
105
106 /**
107 * @return self|bool|CRM_Event_Cart_BAO_Cart
108 */
109 public static function find_or_create_for_current_session() {
110 $session = CRM_Core_Session::singleton();
111 $event_cart_id = $session->get('event_cart_id');
112 $userID = $session->get('userID');
113 $cart = FALSE;
114 if (!is_null($event_cart_id)) {
115 $cart = self::find_uncompleted_by_id($event_cart_id);
116 if ($cart && $userID) {
117 if (!$cart->user_id) {
118 $saved_cart = self::find_uncompleted_by_user_id($userID);
119 if ($saved_cart) {
120 $cart->adopt_participants($saved_cart->id);
121 $saved_cart->delete();
122 $cart->load_associations();
123 }
124 else {
125 $cart->user_id = $userID;
126 $cart->save();
127 }
128 }
129 }
130 }
131 if ($cart === FALSE) {
132 if (is_null($userID)) {
133 $cart = self::create([]);
134 }
135 else {
136 $cart = self::find_uncompleted_by_user_id($userID);
137 if ($cart === FALSE) {
138 $cart = self::create(['user_id' => $userID]);
139 }
140 }
141 $session->set('event_cart_id', $cart->id);
142 }
143 return $cart;
144 }
145
146 /**
147 * @param int $id
148 *
149 * @return bool|CRM_Event_Cart_BAO_Cart
150 */
151 public static function find_uncompleted_by_id($id) {
152 return self::find_by_params(['id' => $id, 'completed' => 0]);
153 }
154
155 /**
156 * @param int $user_id
157 *
158 * @return bool|CRM_Event_Cart_BAO_Cart
159 */
160 public static function find_uncompleted_by_user_id($user_id) {
161 return self::find_by_params(['user_id' => $user_id, 'completed' => 0]);
162 }
163
164 /**
165 * @return array
166 */
167 public function get_main_events_in_carts() {
168 //return CRM_Event_Cart_BAO_EventInCart::find_all_by_params( array('main_conference_event_id'
169 $all = [];
170 foreach ($this->events_in_carts as $event_in_cart) {
171 if (!$event_in_cart->is_child_event()) {
172 $all[] = $event_in_cart;
173 }
174 }
175 return $all;
176 }
177
178 /**
179 * @param int $main_conference_event_id
180 *
181 * @return array
182 */
183 public function get_events_in_carts_by_main_event_id($main_conference_event_id) {
184 $all = [];
185 if (!$main_conference_event_id) {
186 return $all;
187 }
188 foreach ($this->events_in_carts as $event_in_cart) {
189 if ($event_in_cart->event->parent_event_id == $main_conference_event_id) {
190 $all[] = $event_in_cart;
191 }
192 }
193 usort($all, "CRM_Event_Cart_BAO_Cart::compare_event_dates");
194 return $all;
195 }
196
197 /**
198 * @param $event_in_cart_1
199 * @param $event_in_cart_2
200 *
201 * @return int
202 */
203 public static function compare_event_dates($event_in_cart_1, $event_in_cart_2) {
204 $date_1 = CRM_Utils_Date::unixTime($event_in_cart_1->event->start_date);
205 $date_2 = CRM_Utils_Date::unixTime($event_in_cart_2->event->start_date);
206
207 if ($date_1 == $date_2) {
208
209 return 0;
210
211 }
212 return ($date_1 < $date_2) ? -1 : 1;
213 }
214
215 /**
216 * @param $main_participant
217 *
218 * @return array
219 */
220 public function get_subparticipants($main_participant) {
221 $subparticipants = [];
222 foreach ($this->events_in_carts as $event_in_cart) {
223 if ($event_in_cart->is_child_event($main_participant->event_id)) {
224 foreach ($event_in_cart->participants as $participant) {
225 if ($participant->contact_id == $main_participant->contact_id) {
226 $subparticipants[] = $participant;
227 continue;
228 }
229 }
230 }
231 }
232 return $subparticipants;
233 }
234
235 /**
236 * @param int $event_id
237 *
238 * @return mixed
239 */
240 public function get_event_in_cart_by_event_id($event_id) {
241 return $this->events_in_carts[$event_id] ?? NULL;
242 }
243
244 /**
245 * @param int $event_in_cart_id
246 *
247 * @return null
248 */
249 public function &get_event_in_cart_by_id($event_in_cart_id) {
250 foreach ($this->events_in_carts as $event_in_cart) {
251 if ($event_in_cart->id == $event_in_cart_id) {
252 return $event_in_cart;
253 }
254 }
255 return NULL;
256 }
257
258 /**
259 * @return array
260 */
261 public function get_main_event_participants() {
262 $participants = [];
263 foreach ($this->get_main_events_in_carts() as $event_in_cart) {
264 $participants = array_merge($participants, $event_in_cart->participants);
265 }
266 return $participants;
267 }
268
269 public function load_associations() {
270 if ($this->associations_loaded) {
271 return;
272 }
273 $this->associations_loaded = TRUE;
274 $this->events_in_carts = CRM_Event_Cart_BAO_EventInCart::find_all_by_event_cart_id($this->id);
275 foreach ($this->events_in_carts as $event_in_cart) {
276 $event_in_cart->load_associations($this);
277 }
278 $this->save();
279 }
280
281 /**
282 * @param int $event_in_cart_id
283 *
284 * @return bool|CRM_Event_Cart_BAO_EventInCart
285 */
286 public function remove_event_in_cart($event_in_cart_id) {
287 $event_in_cart = CRM_Event_Cart_BAO_EventInCart::find_by_id($event_in_cart_id);
288 if ($event_in_cart) {
289 $sessions_to_remove = $this->get_events_in_carts_by_main_event_id($event_in_cart->event_id);
290 foreach ($sessions_to_remove as $session) {
291 $this->remove_event_in_cart($session->id);
292 }
293 unset($this->events_in_carts[$event_in_cart->event_id]);
294 $event_in_cart->delete();
295 }
296 return $event_in_cart;
297 }
298
299 /**
300 * @param int $participant_id
301 *
302 * @return int
303 */
304 public function get_participant_index_from_id($participant_id) {
305 foreach ($this->events_in_carts as $event_in_cart) {
306 $index = 0;
307 foreach ($event_in_cart->participants as $participant) {
308 if ($participant->id == $participant_id) {
309 return $index;
310 }
311 $index++;
312 }
313 }
314 return -1;
315 }
316
317 /**
318 * @param array $params
319 * @param $values
320 *
321 * @return mixed
322 * @throws Exception
323 */
324 public static function retrieve(&$params, &$values) {
325 $cart = self::find_by_params($params);
326 if ($cart === FALSE) {
327 throw new CRM_Core_Exception(ts('Could not find cart matching %1', [1 => var_export($params, TRUE)]));
328 }
329 CRM_Core_DAO::storeValues($cart, $values);
330 return $values;
331 }
332
333 /**
334 * @param int $from_cart_id
335 */
336 public function adopt_participants($from_cart_id) {
337 $params = [
338 1 => [$this->id, 'Integer'],
339 2 => [$from_cart_id, 'Integer'],
340 ];
341 $sql = "UPDATE civicrm_participant SET cart_id='%1' WHERE cart_id='%2'";
342
343 CRM_Core_DAO::executeQuery($sql, $params);
344 }
345
346 }