Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | class CRM_Event_Cart_BAO_EventInCart extends CRM_Event_Cart_DAO_EventInCart implements ArrayAccess { | |
3 | public $assocations_loaded = FALSE; | |
4 | public $event; | |
5 | public $event_cart; | |
6 | public $location = NULL; | |
7 | public $participants = array( | |
8 | ); | |
9 | ||
10 | function __construct() { | |
11 | parent::__construct(); | |
12 | } | |
13 | ||
14 | public function add_participant($participant) { | |
15 | $this->participants[$participant->id] = $participant; | |
16 | } | |
17 | ||
18 | public static function create(&$params) { | |
19 | $transaction = new CRM_Core_Transaction(); | |
20 | $event_in_cart = new CRM_Event_Cart_BAO_EventInCart(); | |
21 | $event_in_cart->copyValues($params); | |
22 | $event_in_cart = $event_in_cart->save(); | |
23 | ||
24 | if (is_a($event_in_cart, 'CRM_Core_Error')) { | |
25 | $transaction->rollback(); | |
26 | CRM_Core_Error::fatal(ts('There was an error creating an event_in_cart')); | |
27 | } | |
28 | ||
29 | $transaction->commit(); | |
30 | ||
31 | return $event_in_cart; | |
32 | } | |
33 | ||
34 | function delete($useWhere = false) { | |
35 | $this->load_associations(); | |
36 | $contacts_to_delete = array(); | |
37 | foreach ($this->participants as $participant) { | |
38 | $defaults = array(); | |
39 | $params = array('id' => $participant->contact_id); | |
40 | $temporary_contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults); | |
41 | ||
42 | if ($temporary_contact->is_deleted) { | |
43 | $contacts_to_delete[$temporary_contact->id] = 1; | |
44 | } | |
45 | $participant->delete(); | |
46 | } | |
47 | foreach (array_keys($contacts_to_delete) as $contact_id) { | |
48 | CRM_Contact_BAO_Contact::deleteContact($contact_id); | |
49 | } | |
50 | parent::delete(); | |
51 | } | |
52 | ||
53 | public static function find_all_by_event_cart_id($event_cart_id) { | |
54 | return self::find_all_by_params(array('event_cart_id' => $event_cart_id)); | |
55 | } | |
56 | ||
57 | public static function find_all_by_params($params) { | |
58 | $event_in_cart = new CRM_Event_Cart_BAO_EventInCart(); | |
59 | $event_in_cart->copyValues($params); | |
60 | $result = array(); | |
61 | if ($event_in_cart->find()) { | |
62 | while ($event_in_cart->fetch()) { | |
63 | $result[$event_in_cart->event_id] = clone($event_in_cart); | |
64 | } | |
65 | } | |
66 | return $result; | |
67 | } | |
68 | ||
69 | public static function find_by_id($id) { | |
70 | return self::find_by_params(array('id' => $id)); | |
71 | } | |
72 | ||
73 | public static function find_by_params($params) { | |
74 | $event_in_cart = new CRM_Event_Cart_BAO_EventInCart(); | |
75 | $event_in_cart->copyValues($params); | |
76 | if ($event_in_cart->find(TRUE)) { | |
77 | return $event_in_cart; | |
78 | } | |
79 | else { | |
80 | return FALSE; | |
81 | } | |
82 | } | |
83 | ||
84 | public function remove_participant_by_contact_id($contact_id) { | |
85 | $to_remove = array(); | |
86 | foreach ($this->participants as $participant) { | |
87 | if ($participant->contact_id == $contact_id) { | |
88 | $to_remove[$participant->id] = 1; | |
89 | $participant->delete(); | |
90 | } | |
91 | } | |
92 | $this->participants = array_diff_key($this->participants, $to_remove); | |
93 | } | |
94 | ||
95 | public function get_participant_by_id($participant_id) { | |
96 | return $this->participants[$participant_id]; | |
97 | } | |
98 | ||
99 | public function remove_participant_by_id($participant_id) { | |
100 | $this->get_participant_by_id($participant_id)->delete(); | |
101 | unset($this->participants[$participant_id]); | |
102 | } | |
103 | ||
104 | static function part_key($participant) { | |
105 | return $participant->id; | |
106 | } | |
107 | ||
108 | public function load_associations($event_cart = NULL) { | |
109 | if ($this->assocations_loaded) { | |
110 | return; | |
111 | } | |
112 | $this->assocations_loaded = TRUE; | |
113 | $params = array('id' => $this->event_id); | |
114 | $defaults = array(); | |
115 | $this->event = CRM_Event_BAO_Event::retrieve($params, $defaults); | |
116 | ||
117 | if ($event_cart != NULL) { | |
118 | $this->event_cart = $event_cart; | |
119 | $this->event_cart_id = $event_cart->id; | |
120 | } | |
121 | else { | |
122 | $this->event_cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->event_cart_id); | |
123 | } | |
124 | ||
125 | $participants = CRM_Event_Cart_BAO_MerParticipant::find_all_by_event_and_cart_id($this->event_id, $this->event_cart->id); | |
126 | foreach ($participants as $participant) { | |
127 | $participant->load_associations(); | |
128 | $this->add_participant($participant); | |
129 | } | |
130 | } | |
131 | ||
132 | public function load_location() { | |
133 | if ($this->location == NULL) { | |
134 | $location_params = array('entity_id' => $this->event_id, 'entity_table' => 'civicrm_event'); | |
135 | $this->location = CRM_Core_BAO_Location::getValues($location_params, TRUE); | |
136 | } | |
137 | } | |
138 | ||
139 | public function not_waiting_participants() { | |
140 | $result = array(); | |
141 | foreach ($this->participants as $participant) { | |
142 | if (!$participant->must_wait) { | |
143 | $result[] = $participant; | |
144 | } | |
145 | } | |
146 | return $result; | |
147 | } | |
148 | ||
149 | public function num_not_waiting_participants() { | |
150 | return count($this->not_waiting_participants()); | |
151 | } | |
152 | ||
153 | public function num_waiting_participants() { | |
154 | return count($this->waiting_participants()); | |
155 | } | |
156 | ||
157 | ||
158 | public function offsetExists($offset) { | |
159 | return array_key_exists(array_merge($this->fields(), array('main_conference_event_id')), $offset); | |
160 | } | |
161 | ||
162 | public function offsetGet($offset) { | |
163 | if ($offset == 'event') { | |
164 | return $this->event->toArray(); | |
165 | } | |
166 | if ($offset == 'id') { | |
167 | return $this->id; | |
168 | } | |
169 | if ($offset == 'main_conference_event_id') { | |
170 | return $this->main_conference_event_id; | |
171 | } | |
172 | $fields = &$this->fields(); | |
173 | return $fields[$offset]; | |
174 | } | |
175 | ||
176 | public function offsetSet($offset, $value) {} | |
177 | ||
178 | public function offsetUnset($offset) {} | |
179 | ||
180 | public function waiting_participants() { | |
181 | $result = array(); | |
182 | foreach ($this->participants as $participant) { | |
183 | if ($participant->must_wait) { | |
184 | $result[] = $participant; | |
185 | } | |
186 | } | |
187 | return $result; | |
188 | } | |
189 | ||
190 | static function get_registration_link($event_id) { | |
191 | $cart = CRM_Event_Cart_BAO_Cart::find_or_create_for_current_session(); | |
192 | $cart->load_associations(); | |
193 | $event_in_cart = $cart->get_event_in_cart_by_event_id($event_id); | |
194 | ||
195 | if ($event_in_cart) { | |
196 | return array( | |
197 | 'label' => "Remove from Cart", | |
198 | 'path' => 'civicrm/event/remove_from_cart', | |
199 | 'query' => "reset=1&id={$event_id}", | |
200 | ); | |
201 | } | |
202 | else { | |
203 | return array( | |
204 | 'label' => "Add to Cart", | |
205 | 'path' => 'civicrm/event/add_to_cart', | |
206 | 'query' => "reset=1&id={$event_id}", | |
207 | ); | |
208 | } | |
209 | } | |
210 | ||
211 | function is_parent_event() { | |
212 | return (NULL !== (CRM_Event_BAO_Event::get_sub_events($this->event_id))); | |
213 | } | |
214 | ||
215 | function is_child_event($parent_event_id = NULL) { | |
216 | if ($parent_event_id == NULL) { | |
217 | return $this->event->parent_event_id; | |
218 | } | |
219 | else return $this->event->parent_event_id == $parent_event_id; | |
220 | } | |
221 | } | |
222 |