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