2 class CRM_Event_Cart_BAO_EventInCart
extends CRM_Event_Cart_DAO_EventInCart
implements ArrayAccess
{
3 public $assocations_loaded = FALSE;
6 public $location = NULL;
7 public $participants = array(
10 function __construct() {
11 parent
::__construct();
14 public function add_participant($participant) {
15 $this->participants
[$participant->id
] = $participant;
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();
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'));
29 $transaction->commit();
31 return $event_in_cart;
34 function delete($useWhere = false) {
35 $this->load_associations();
36 $contacts_to_delete = array();
37 foreach ($this->participants
as $participant) {
39 $params = array('id' => $participant->contact_id
);
40 $temporary_contact = CRM_Contact_BAO_Contact
::retrieve($params, $defaults);
42 if ($temporary_contact->is_deleted
) {
43 $contacts_to_delete[$temporary_contact->id
] = 1;
45 $participant->delete();
47 foreach (array_keys($contacts_to_delete) as $contact_id) {
48 CRM_Contact_BAO_Contact
::deleteContact($contact_id);
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));
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);
61 if ($event_in_cart->find()) {
62 while ($event_in_cart->fetch()) {
63 $result[$event_in_cart->event_id
] = clone($event_in_cart);
69 public static function find_by_id($id) {
70 return self
::find_by_params(array('id' => $id));
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;
84 public function remove_participant_by_contact_id($contact_id) {
86 foreach ($this->participants
as $participant) {
87 if ($participant->contact_id
== $contact_id) {
88 $to_remove[$participant->id
] = 1;
89 $participant->delete();
92 $this->participants
= array_diff_key($this->participants
, $to_remove);
95 public function get_participant_by_id($participant_id) {
96 return $this->participants
[$participant_id];
99 public function remove_participant_by_id($participant_id) {
100 $this->get_participant_by_id($participant_id)->delete();
101 unset($this->participants
[$participant_id]);
104 static function part_key($participant) {
105 return $participant->id
;
108 public function load_associations($event_cart = NULL) {
109 if ($this->assocations_loaded
) {
112 $this->assocations_loaded
= TRUE;
113 $params = array('id' => $this->event_id
);
115 $this->event
= CRM_Event_BAO_Event
::retrieve($params, $defaults);
117 if ($event_cart != NULL) {
118 $this->event_cart
= $event_cart;
119 $this->event_cart_id
= $event_cart->id
;
122 $this->event_cart
= CRM_Event_Cart_BAO_Cart
::find_by_id($this->event_cart_id
);
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);
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);
139 public function not_waiting_participants() {
141 foreach ($this->participants
as $participant) {
142 if (!$participant->must_wait
) {
143 $result[] = $participant;
149 public function num_not_waiting_participants() {
150 return count($this->not_waiting_participants());
153 public function num_waiting_participants() {
154 return count($this->waiting_participants());
158 public function offsetExists($offset) {
159 return array_key_exists(array_merge($this->fields(), array('main_conference_event_id')), $offset);
162 public function offsetGet($offset) {
163 if ($offset == 'event') {
164 return $this->event
->toArray();
166 if ($offset == 'id') {
169 if ($offset == 'main_conference_event_id') {
170 return $this->main_conference_event_id
;
172 $fields = &$this->fields();
173 return $fields[$offset];
176 public function offsetSet($offset, $value) {}
178 public function offsetUnset($offset) {}
180 public function waiting_participants() {
182 foreach ($this->participants
as $participant) {
183 if ($participant->must_wait
) {
184 $result[] = $participant;
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);
195 if ($event_in_cart) {
197 'label' => "Remove from Cart",
198 'path' => 'civicrm/event/remove_from_cart',
199 'query' => "reset=1&id={$event_id}",
204 'label' => "Add to Cart",
205 'path' => 'civicrm/event/add_to_cart',
206 'query' => "reset=1&id={$event_id}",
211 function is_parent_event() {
212 return (NULL !== (CRM_Event_BAO_Event
::get_sub_events($this->event_id
)));
215 function is_child_event($parent_event_id = NULL) {
216 if ($parent_event_id == NULL) {
217 return $this->event
->parent_event_id
;
219 else return $this->event
->parent_event_id
== $parent_event_id;