Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
0cf587a7 EM |
2 | |
3 | /** | |
4 | * Class CRM_Event_Cart_BAO_Cart | |
5 | */ | |
6a488035 TO |
6 | class CRM_Event_Cart_BAO_Cart extends CRM_Event_Cart_DAO_Cart { |
7 | public $associations_loaded = FALSE; | |
90b461f1 SL |
8 | /** |
9 | * event_in_cart_id => $event_in_cart | |
10 | * @var array | |
11 | */ | |
be2fb01f | 12 | public $events_in_carts = []; |
6a488035 | 13 | |
0cf587a7 | 14 | /** |
c490a46a | 15 | * @param array $params |
0cf587a7 | 16 | * |
14069c56 | 17 | * @return CRM_Event_Cart_BAO_Cart |
0cf587a7 | 18 | */ |
6a488035 TO |
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 | ||
0cf587a7 | 26 | /** |
100fef9d | 27 | * @param int $event_id |
0cf587a7 EM |
28 | * |
29 | * @return mixed | |
30 | */ | |
6a488035 TO |
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 | ||
be2fb01f | 38 | $params = [ |
6a488035 TO |
39 | 'event_id' => $event_id, |
40 | 'event_cart_id' => $this->id, | |
be2fb01f | 41 | ]; |
6a488035 TO |
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 | ||
0cf587a7 EM |
48 | /** |
49 | * @param $participant | |
50 | */ | |
00be9182 | 51 | public function add_participant_to_cart($participant) { |
6a488035 TO |
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 | ||
0cf587a7 | 60 | /** |
c490a46a | 61 | * @param array $params |
0cf587a7 | 62 | * |
14069c56 | 63 | * @return CRM_Event_Cart_BAO_Cart |
0cf587a7 EM |
64 | * @throws Exception |
65 | */ | |
6a488035 TO |
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 | CRM_Core_Error::fatal(ts('There was an error creating an event cart')); | |
74 | } | |
75 | ||
76 | $transaction->commit(); | |
77 | ||
78 | return $cart; | |
79 | } | |
80 | ||
0cf587a7 | 81 | /** |
100fef9d | 82 | * @param int $id |
0cf587a7 EM |
83 | * |
84 | * @return bool|CRM_Event_Cart_BAO_Cart | |
85 | */ | |
6a488035 | 86 | public static function find_by_id($id) { |
be2fb01f | 87 | return self::find_by_params(['id' => $id]); |
6a488035 TO |
88 | } |
89 | ||
0cf587a7 | 90 | /** |
c490a46a | 91 | * @param array $params |
0cf587a7 EM |
92 | * |
93 | * @return bool|CRM_Event_Cart_BAO_Cart | |
94 | */ | |
6a488035 TO |
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 | ||
0cf587a7 | 106 | /** |
6c552737 | 107 | * @return self|bool|CRM_Event_Cart_BAO_Cart |
0cf587a7 | 108 | */ |
6a488035 | 109 | public static function find_or_create_for_current_session() { |
353ffa53 | 110 | $session = CRM_Core_Session::singleton(); |
6a488035 | 111 | $event_cart_id = $session->get('event_cart_id'); |
353ffa53 TO |
112 | $userID = $session->get('userID'); |
113 | $cart = FALSE; | |
6a488035 TO |
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)) { | |
be2fb01f | 133 | $cart = self::create([]); |
6a488035 TO |
134 | } |
135 | else { | |
136 | $cart = self::find_uncompleted_by_user_id($userID); | |
137 | if ($cart === FALSE) { | |
be2fb01f | 138 | $cart = self::create(['user_id' => $userID]); |
6a488035 TO |
139 | } |
140 | } | |
141 | $session->set('event_cart_id', $cart->id); | |
142 | } | |
143 | return $cart; | |
144 | } | |
145 | ||
0cf587a7 | 146 | /** |
100fef9d | 147 | * @param int $id |
0cf587a7 EM |
148 | * |
149 | * @return bool|CRM_Event_Cart_BAO_Cart | |
150 | */ | |
6a488035 | 151 | public static function find_uncompleted_by_id($id) { |
be2fb01f | 152 | return self::find_by_params(['id' => $id, 'completed' => 0]); |
6a488035 TO |
153 | } |
154 | ||
0cf587a7 | 155 | /** |
100fef9d | 156 | * @param int $user_id |
0cf587a7 EM |
157 | * |
158 | * @return bool|CRM_Event_Cart_BAO_Cart | |
159 | */ | |
6a488035 | 160 | public static function find_uncompleted_by_user_id($user_id) { |
be2fb01f | 161 | return self::find_by_params(['user_id' => $user_id, 'completed' => 0]); |
6a488035 TO |
162 | } |
163 | ||
0cf587a7 EM |
164 | /** |
165 | * @return array | |
166 | */ | |
6a488035 TO |
167 | public function get_main_events_in_carts() { |
168 | //return CRM_Event_Cart_BAO_EventInCart::find_all_by_params( array('main_conference_event_id' | |
be2fb01f | 169 | $all = []; |
6a488035 TO |
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 | ||
0cf587a7 | 178 | /** |
100fef9d | 179 | * @param int $main_conference_event_id |
0cf587a7 EM |
180 | * |
181 | * @return array | |
182 | */ | |
6a488035 | 183 | public function get_events_in_carts_by_main_event_id($main_conference_event_id) { |
be2fb01f | 184 | $all = []; |
6a488035 TO |
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 | ||
0cf587a7 EM |
197 | /** |
198 | * @param $event_in_cart_1 | |
199 | * @param $event_in_cart_2 | |
200 | * | |
201 | * @return int | |
202 | */ | |
00be9182 | 203 | public static function compare_event_dates($event_in_cart_1, $event_in_cart_2) { |
6a488035 TO |
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 | ||
0cf587a7 EM |
215 | /** |
216 | * @param $main_participant | |
217 | * | |
218 | * @return array | |
219 | */ | |
6a488035 | 220 | public function get_subparticipants($main_participant) { |
be2fb01f | 221 | $subparticipants = []; |
6a488035 TO |
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 | ||
0cf587a7 | 235 | /** |
100fef9d | 236 | * @param int $event_id |
0cf587a7 EM |
237 | * |
238 | * @return mixed | |
239 | */ | |
6a488035 TO |
240 | public function get_event_in_cart_by_event_id($event_id) { |
241 | return CRM_Utils_Array::value($event_id, $this->events_in_carts); | |
242 | } | |
243 | ||
0cf587a7 | 244 | /** |
100fef9d | 245 | * @param int $event_in_cart_id |
0cf587a7 EM |
246 | * |
247 | * @return null | |
248 | */ | |
6a488035 TO |
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 | ||
0cf587a7 EM |
258 | /** |
259 | * @return array | |
260 | */ | |
6a488035 | 261 | public function get_main_event_participants() { |
be2fb01f | 262 | $participants = []; |
6a488035 TO |
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 | ||
0cf587a7 | 281 | /** |
100fef9d | 282 | * @param int $event_in_cart_id |
0cf587a7 EM |
283 | * |
284 | * @return bool|CRM_Event_Cart_BAO_EventInCart | |
285 | */ | |
6a488035 TO |
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 | ||
0cf587a7 | 299 | /** |
100fef9d | 300 | * @param int $participant_id |
0cf587a7 EM |
301 | * |
302 | * @return int | |
303 | */ | |
00be9182 | 304 | public function get_participant_index_from_id($participant_id) { |
6a488035 TO |
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 | } | |
353ffa53 | 314 | return -1; |
6a488035 TO |
315 | } |
316 | ||
0cf587a7 | 317 | /** |
c490a46a | 318 | * @param array $params |
0cf587a7 EM |
319 | * @param $values |
320 | * | |
321 | * @return mixed | |
322 | * @throws Exception | |
323 | */ | |
6a488035 TO |
324 | public static function retrieve(&$params, &$values) { |
325 | $cart = self::find_by_params($params); | |
326 | if ($cart === FALSE) { | |
be2fb01f | 327 | CRM_Core_Error::fatal(ts('Could not find cart matching %1', [1 => var_export($params, TRUE)])); |
6a488035 TO |
328 | } |
329 | CRM_Core_DAO::storeValues($cart, $values); | |
330 | return $values; | |
331 | } | |
332 | ||
0cf587a7 | 333 | /** |
100fef9d | 334 | * @param int $from_cart_id |
0cf587a7 | 335 | */ |
6a488035 | 336 | public function adopt_participants($from_cart_id) { |
be2fb01f CW |
337 | $params = [ |
338 | 1 => [$this->id, 'Integer'], | |
339 | 2 => [$from_cart_id, 'Integer'], | |
340 | ]; | |
6a488035 TO |
341 | $sql = "UPDATE civicrm_participant SET cart_id='%1' WHERE cart_id='%2'"; |
342 | ||
343 | CRM_Core_DAO::executeQuery($sql, $params); | |
344 | } | |
96025800 | 345 | |
6a488035 | 346 | } |