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