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