Merge pull request #3179 from webpartners/master
[civicrm-core.git] / CRM / Event / Cart / BAO / EventInCart.php
CommitLineData
6a488035 1<?php
0cf587a7
EM
2
3/**
4 * Class CRM_Event_Cart_BAO_EventInCart
5 */
6a488035
TO
6class CRM_Event_Cart_BAO_EventInCart extends CRM_Event_Cart_DAO_EventInCart implements ArrayAccess {
7 public $assocations_loaded = FALSE;
8 public $event;
9 public $event_cart;
10 public $location = NULL;
11 public $participants = array(
12 );
13
0cf587a7
EM
14 /**
15 *
16 */
6a488035
TO
17 function __construct() {
18 parent::__construct();
19 }
20
0cf587a7
EM
21 /**
22 * @param $participant
23 */
6a488035
TO
24 public function add_participant($participant) {
25 $this->participants[$participant->id] = $participant;
26 }
27
0cf587a7
EM
28 /**
29 * @param $params
30 *
31 * @return $this|CRM_Event_Cart_BAO_EventInCart
32 * @throws Exception
33 */
6a488035
TO
34 public static function create(&$params) {
35 $transaction = new CRM_Core_Transaction();
36 $event_in_cart = new CRM_Event_Cart_BAO_EventInCart();
37 $event_in_cart->copyValues($params);
38 $event_in_cart = $event_in_cart->save();
39
40 if (is_a($event_in_cart, 'CRM_Core_Error')) {
41 $transaction->rollback();
42 CRM_Core_Error::fatal(ts('There was an error creating an event_in_cart'));
43 }
44
45 $transaction->commit();
46
47 return $event_in_cart;
48 }
49
0cf587a7
EM
50 /**
51 * @param bool $useWhere
52 */
6a488035
TO
53 function delete($useWhere = false) {
54 $this->load_associations();
55 $contacts_to_delete = array();
56 foreach ($this->participants as $participant) {
57 $defaults = array();
58 $params = array('id' => $participant->contact_id);
59 $temporary_contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults);
60
61 if ($temporary_contact->is_deleted) {
62 $contacts_to_delete[$temporary_contact->id] = 1;
63 }
64 $participant->delete();
65 }
66 foreach (array_keys($contacts_to_delete) as $contact_id) {
67 CRM_Contact_BAO_Contact::deleteContact($contact_id);
68 }
69 parent::delete();
70 }
71
0cf587a7
EM
72 /**
73 * @param $event_cart_id
74 *
75 * @return array
76 */
6a488035
TO
77 public static function find_all_by_event_cart_id($event_cart_id) {
78 return self::find_all_by_params(array('event_cart_id' => $event_cart_id));
79 }
80
0cf587a7
EM
81 /**
82 * @param $params
83 *
84 * @return array
85 */
6a488035
TO
86 public static function find_all_by_params($params) {
87 $event_in_cart = new CRM_Event_Cart_BAO_EventInCart();
88 $event_in_cart->copyValues($params);
89 $result = array();
90 if ($event_in_cart->find()) {
91 while ($event_in_cart->fetch()) {
92 $result[$event_in_cart->event_id] = clone($event_in_cart);
93 }
94 }
95 return $result;
96 }
97
0cf587a7
EM
98 /**
99 * @param $id
100 *
101 * @return bool|CRM_Event_Cart_BAO_EventInCart
102 */
6a488035
TO
103 public static function find_by_id($id) {
104 return self::find_by_params(array('id' => $id));
105 }
106
0cf587a7
EM
107 /**
108 * @param $params
109 *
110 * @return bool|CRM_Event_Cart_BAO_EventInCart
111 */
6a488035
TO
112 public static function find_by_params($params) {
113 $event_in_cart = new CRM_Event_Cart_BAO_EventInCart();
114 $event_in_cart->copyValues($params);
115 if ($event_in_cart->find(TRUE)) {
116 return $event_in_cart;
117 }
118 else {
119 return FALSE;
120 }
121 }
122
0cf587a7
EM
123 /**
124 * @param $contact_id
125 */
6a488035
TO
126 public function remove_participant_by_contact_id($contact_id) {
127 $to_remove = array();
128 foreach ($this->participants as $participant) {
129 if ($participant->contact_id == $contact_id) {
130 $to_remove[$participant->id] = 1;
131 $participant->delete();
132 }
133 }
134 $this->participants = array_diff_key($this->participants, $to_remove);
135 }
136
0cf587a7
EM
137 /**
138 * @param $participant_id
139 *
140 * @return mixed
141 */
6a488035
TO
142 public function get_participant_by_id($participant_id) {
143 return $this->participants[$participant_id];
144 }
145
0cf587a7
EM
146 /**
147 * @param $participant_id
148 */
6a488035
TO
149 public function remove_participant_by_id($participant_id) {
150 $this->get_participant_by_id($participant_id)->delete();
151 unset($this->participants[$participant_id]);
152 }
153
0cf587a7
EM
154 /**
155 * @param $participant
156 *
157 * @return mixed
158 */
6a488035
TO
159 static function part_key($participant) {
160 return $participant->id;
161 }
162
0cf587a7
EM
163 /**
164 * @param null $event_cart
165 */
6a488035
TO
166 public function load_associations($event_cart = NULL) {
167 if ($this->assocations_loaded) {
168 return;
169 }
170 $this->assocations_loaded = TRUE;
171 $params = array('id' => $this->event_id);
172 $defaults = array();
173 $this->event = CRM_Event_BAO_Event::retrieve($params, $defaults);
174
175 if ($event_cart != NULL) {
176 $this->event_cart = $event_cart;
177 $this->event_cart_id = $event_cart->id;
178 }
179 else {
180 $this->event_cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->event_cart_id);
181 }
182
183 $participants = CRM_Event_Cart_BAO_MerParticipant::find_all_by_event_and_cart_id($this->event_id, $this->event_cart->id);
184 foreach ($participants as $participant) {
185 $participant->load_associations();
186 $this->add_participant($participant);
187 }
188 }
189
190 public function load_location() {
191 if ($this->location == NULL) {
192 $location_params = array('entity_id' => $this->event_id, 'entity_table' => 'civicrm_event');
193 $this->location = CRM_Core_BAO_Location::getValues($location_params, TRUE);
194 }
195 }
196
0cf587a7
EM
197 /**
198 * @return array
199 */
6a488035
TO
200 public function not_waiting_participants() {
201 $result = array();
202 foreach ($this->participants as $participant) {
203 if (!$participant->must_wait) {
204 $result[] = $participant;
205 }
206 }
207 return $result;
208 }
209
0cf587a7
EM
210 /**
211 * @return int
212 */
6a488035
TO
213 public function num_not_waiting_participants() {
214 return count($this->not_waiting_participants());
215 }
216
0cf587a7
EM
217 /**
218 * @return int
219 */
6a488035
TO
220 public function num_waiting_participants() {
221 return count($this->waiting_participants());
222 }
223
224
0cf587a7
EM
225 /**
226 * @param mixed $offset
227 *
228 * @return bool
229 */
6a488035
TO
230 public function offsetExists($offset) {
231 return array_key_exists(array_merge($this->fields(), array('main_conference_event_id')), $offset);
232 }
233
0cf587a7
EM
234 /**
235 * @param mixed $offset
236 *
237 * @return int
238 */
6a488035
TO
239 public function offsetGet($offset) {
240 if ($offset == 'event') {
241 return $this->event->toArray();
242 }
243 if ($offset == 'id') {
244 return $this->id;
245 }
246 if ($offset == 'main_conference_event_id') {
247 return $this->main_conference_event_id;
248 }
249 $fields = &$this->fields();
250 return $fields[$offset];
251 }
252
0cf587a7
EM
253 /**
254 * @param mixed $offset
255 * @param mixed $value
256 */
6a488035
TO
257 public function offsetSet($offset, $value) {}
258
0cf587a7
EM
259 /**
260 * @param mixed $offset
261 */
6a488035
TO
262 public function offsetUnset($offset) {}
263
0cf587a7
EM
264 /**
265 * @return array
266 */
6a488035
TO
267 public function waiting_participants() {
268 $result = array();
269 foreach ($this->participants as $participant) {
270 if ($participant->must_wait) {
271 $result[] = $participant;
272 }
273 }
274 return $result;
275 }
276
0cf587a7
EM
277 /**
278 * @param $event_id
279 *
280 * @return array
281 */
6a488035
TO
282 static function get_registration_link($event_id) {
283 $cart = CRM_Event_Cart_BAO_Cart::find_or_create_for_current_session();
284 $cart->load_associations();
285 $event_in_cart = $cart->get_event_in_cart_by_event_id($event_id);
286
287 if ($event_in_cart) {
288 return array(
289 'label' => "Remove from Cart",
290 'path' => 'civicrm/event/remove_from_cart',
291 'query' => "reset=1&id={$event_id}",
292 );
293 }
294 else {
295 return array(
296 'label' => "Add to Cart",
297 'path' => 'civicrm/event/add_to_cart',
298 'query' => "reset=1&id={$event_id}",
299 );
300 }
301 }
302
0cf587a7
EM
303 /**
304 * @return bool
305 */
6a488035
TO
306 function is_parent_event() {
307 return (NULL !== (CRM_Event_BAO_Event::get_sub_events($this->event_id)));
308 }
309
0cf587a7
EM
310 /**
311 * @param null $parent_event_id
312 *
313 * @return bool
314 */
6a488035
TO
315 function is_child_event($parent_event_id = NULL) {
316 if ($parent_event_id == NULL) {
317 return $this->event->parent_event_id;
318 }
319 else return $this->event->parent_event_id == $parent_event_id;
320 }
321}
322