commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / 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 /* event_in_cart_id => $event_in_cart */
9 public $events_in_carts = array();
10
11 /**
12 * @param array $params
13 *
14 * @return $this
15 */
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
23 /**
24 * @param int $event_id
25 *
26 * @return mixed
27 */
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
45 /**
46 * @param $participant
47 */
48 public function add_participant_to_cart($participant) {
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
57 /**
58 * @param array $params
59 *
60 * @return $this
61 * @throws Exception
62 */
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
78 /**
79 * @param int $id
80 *
81 * @return bool|CRM_Event_Cart_BAO_Cart
82 */
83 public static function find_by_id($id) {
84 return self::find_by_params(array('id' => $id));
85 }
86
87 /**
88 * @param array $params
89 *
90 * @return bool|CRM_Event_Cart_BAO_Cart
91 */
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
103 /**
104 * @return self|bool|CRM_Event_Cart_BAO_Cart
105 */
106 public static function find_or_create_for_current_session() {
107 $session = CRM_Core_Session::singleton();
108 $event_cart_id = $session->get('event_cart_id');
109 $userID = $session->get('userID');
110 $cart = FALSE;
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
143 /**
144 * @param int $id
145 *
146 * @return bool|CRM_Event_Cart_BAO_Cart
147 */
148 public static function find_uncompleted_by_id($id) {
149 return self::find_by_params(array('id' => $id, 'completed' => 0));
150 }
151
152 /**
153 * @param int $user_id
154 *
155 * @return bool|CRM_Event_Cart_BAO_Cart
156 */
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
161 /**
162 * @return array
163 */
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
175 /**
176 * @param int $main_conference_event_id
177 *
178 * @return array
179 */
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
194 /**
195 * @param $event_in_cart_1
196 * @param $event_in_cart_2
197 *
198 * @return int
199 */
200 public static function compare_event_dates($event_in_cart_1, $event_in_cart_2) {
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
212 /**
213 * @param $main_participant
214 *
215 * @return array
216 */
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
232 /**
233 * @param int $event_id
234 *
235 * @return mixed
236 */
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
241 /**
242 * @param int $event_in_cart_id
243 *
244 * @return null
245 */
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
255 /**
256 * @return array
257 */
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
278 /**
279 * @param int $event_in_cart_id
280 *
281 * @return bool|CRM_Event_Cart_BAO_EventInCart
282 */
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
296 /**
297 * @param int $participant_id
298 *
299 * @return int
300 */
301 public function get_participant_index_from_id($participant_id) {
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 }
311 return -1;
312 }
313
314 /**
315 * @param array $params
316 * @param $values
317 *
318 * @return mixed
319 * @throws Exception
320 */
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
331 /**
332 * @param int $from_cart_id
333 */
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 }
343
344 }