4 * Class CRM_Event_Cart_BAO_EventInCart
6 class CRM_Event_Cart_BAO_EventInCart
extends CRM_Event_Cart_DAO_EventInCart
implements ArrayAccess
{
7 public $assocations_loaded = FALSE;
10 public $location = NULL;
11 public $participants = array();
16 public function __construct() {
17 parent
::__construct();
23 public function add_participant($participant) {
24 $this->participants
[$participant->id
] = $participant;
28 * @param array $params
30 * @return $this|CRM_Event_Cart_BAO_EventInCart
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();
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'));
44 $transaction->commit();
46 return $event_in_cart;
50 * @param bool $useWhere
52 public function delete($useWhere = FALSE) {
53 $this->load_associations();
54 $contacts_to_delete = array();
55 foreach ($this->participants
as $participant) {
57 $params = array('id' => $participant->contact_id
);
58 $temporary_contact = CRM_Contact_BAO_Contact
::retrieve($params, $defaults);
60 if ($temporary_contact->is_deleted
) {
61 $contacts_to_delete[$temporary_contact->id
] = 1;
63 $participant->delete();
65 foreach (array_keys($contacts_to_delete) as $contact_id) {
66 CRM_Contact_BAO_Contact
::deleteContact($contact_id);
72 * @param int $event_cart_id
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));
81 * @param array $params
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);
89 if ($event_in_cart->find()) {
90 while ($event_in_cart->fetch()) {
91 $result[$event_in_cart->event_id
] = clone($event_in_cart);
100 * @return bool|CRM_Event_Cart_BAO_EventInCart
102 public static function find_by_id($id) {
103 return self
::find_by_params(array('id' => $id));
107 * @param array $params
109 * @return bool|CRM_Event_Cart_BAO_EventInCart
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;
123 * @param int $contact_id
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();
133 $this->participants
= array_diff_key($this->participants
, $to_remove);
137 * @param int $participant_id
141 public function get_participant_by_id($participant_id) {
142 return $this->participants
[$participant_id];
146 * @param int $participant_id
148 public function remove_participant_by_id($participant_id) {
149 $this->get_participant_by_id($participant_id)->delete();
150 unset($this->participants
[$participant_id]);
154 * @param $participant
158 public static function part_key($participant) {
159 return $participant->id
;
163 * @param null $event_cart
165 public function load_associations($event_cart = NULL) {
166 if ($this->assocations_loaded
) {
169 $this->assocations_loaded
= TRUE;
170 $params = array('id' => $this->event_id
);
172 $this->event
= CRM_Event_BAO_Event
::retrieve($params, $defaults);
174 if ($event_cart != NULL) {
175 $this->event_cart
= $event_cart;
176 $this->event_cart_id
= $event_cart->id
;
179 $this->event_cart
= CRM_Event_Cart_BAO_Cart
::find_by_id($this->event_cart_id
);
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);
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);
199 public function not_waiting_participants() {
201 foreach ($this->participants
as $participant) {
202 if (!$participant->must_wait
) {
203 $result[] = $participant;
212 public function num_not_waiting_participants() {
213 return count($this->not_waiting_participants());
219 public function num_waiting_participants() {
220 return count($this->waiting_participants());
225 * @param mixed $offset
229 public function offsetExists($offset) {
230 return array_key_exists(array_merge($this->fields(), array('main_conference_event_id')), $offset);
234 * @param mixed $offset
238 public function offsetGet($offset) {
239 if ($offset == 'event') {
240 return $this->event
->toArray();
242 if ($offset == 'id') {
245 if ($offset == 'main_conference_event_id') {
246 return $this->main_conference_event_id
;
248 $fields = &$this->fields();
249 return $fields[$offset];
253 * @param mixed $offset
254 * @param mixed $value
256 public function offsetSet($offset, $value) {
260 * @param mixed $offset
262 public function offsetUnset($offset) {
268 public function waiting_participants() {
270 foreach ($this->participants
as $participant) {
271 if ($participant->must_wait
) {
272 $result[] = $participant;
279 * @param int $event_id
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);
288 if ($event_in_cart) {
290 'label' => "Remove from Cart",
291 'path' => 'civicrm/event/remove_from_cart',
292 'query' => "reset=1&id={$event_id}",
297 'label' => "Add to Cart",
298 'path' => 'civicrm/event/add_to_cart',
299 'query' => "reset=1&id={$event_id}",
307 public function is_parent_event() {
308 return (NULL !== (CRM_Event_BAO_Event
::get_sub_events($this->event_id
)));
312 * @param int $parent_event_id
316 public function is_child_event($parent_event_id = NULL) {
317 if ($parent_event_id == NULL) {
318 return $this->event
->parent_event_id
;
320 else { return $this->event
->parent_event_id
== $parent_event_id;