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(
17 function __construct() {
18 parent
::__construct();
24 public function add_participant($participant) {
25 $this->participants
[$participant->id
] = $participant;
31 * @return $this|CRM_Event_Cart_BAO_EventInCart
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();
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'));
45 $transaction->commit();
47 return $event_in_cart;
51 * @param bool $useWhere
53 function delete($useWhere = false) {
54 $this->load_associations();
55 $contacts_to_delete = array();
56 foreach ($this->participants
as $participant) {
58 $params = array('id' => $participant->contact_id
);
59 $temporary_contact = CRM_Contact_BAO_Contact
::retrieve($params, $defaults);
61 if ($temporary_contact->is_deleted
) {
62 $contacts_to_delete[$temporary_contact->id
] = 1;
64 $participant->delete();
66 foreach (array_keys($contacts_to_delete) as $contact_id) {
67 CRM_Contact_BAO_Contact
::deleteContact($contact_id);
73 * @param $event_cart_id
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));
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);
90 if ($event_in_cart->find()) {
91 while ($event_in_cart->fetch()) {
92 $result[$event_in_cart->event_id
] = clone($event_in_cart);
101 * @return bool|CRM_Event_Cart_BAO_EventInCart
103 public static function find_by_id($id) {
104 return self
::find_by_params(array('id' => $id));
110 * @return bool|CRM_Event_Cart_BAO_EventInCart
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;
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();
134 $this->participants
= array_diff_key($this->participants
, $to_remove);
138 * @param $participant_id
142 public function get_participant_by_id($participant_id) {
143 return $this->participants
[$participant_id];
147 * @param $participant_id
149 public function remove_participant_by_id($participant_id) {
150 $this->get_participant_by_id($participant_id)->delete();
151 unset($this->participants
[$participant_id]);
155 * @param $participant
159 static function part_key($participant) {
160 return $participant->id
;
164 * @param null $event_cart
166 public function load_associations($event_cart = NULL) {
167 if ($this->assocations_loaded
) {
170 $this->assocations_loaded
= TRUE;
171 $params = array('id' => $this->event_id
);
173 $this->event
= CRM_Event_BAO_Event
::retrieve($params, $defaults);
175 if ($event_cart != NULL) {
176 $this->event_cart
= $event_cart;
177 $this->event_cart_id
= $event_cart->id
;
180 $this->event_cart
= CRM_Event_Cart_BAO_Cart
::find_by_id($this->event_cart_id
);
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);
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);
200 public function not_waiting_participants() {
202 foreach ($this->participants
as $participant) {
203 if (!$participant->must_wait
) {
204 $result[] = $participant;
213 public function num_not_waiting_participants() {
214 return count($this->not_waiting_participants());
220 public function num_waiting_participants() {
221 return count($this->waiting_participants());
226 * @param mixed $offset
230 public function offsetExists($offset) {
231 return array_key_exists(array_merge($this->fields(), array('main_conference_event_id')), $offset);
235 * @param mixed $offset
239 public function offsetGet($offset) {
240 if ($offset == 'event') {
241 return $this->event
->toArray();
243 if ($offset == 'id') {
246 if ($offset == 'main_conference_event_id') {
247 return $this->main_conference_event_id
;
249 $fields = &$this->fields();
250 return $fields[$offset];
254 * @param mixed $offset
255 * @param mixed $value
257 public function offsetSet($offset, $value) {}
260 * @param mixed $offset
262 public function offsetUnset($offset) {}
267 public function waiting_participants() {
269 foreach ($this->participants
as $participant) {
270 if ($participant->must_wait
) {
271 $result[] = $participant;
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);
287 if ($event_in_cart) {
289 'label' => "Remove from Cart",
290 'path' => 'civicrm/event/remove_from_cart',
291 'query' => "reset=1&id={$event_id}",
296 'label' => "Add to Cart",
297 'path' => 'civicrm/event/add_to_cart',
298 'query' => "reset=1&id={$event_id}",
306 function is_parent_event() {
307 return (NULL !== (CRM_Event_BAO_Event
::get_sub_events($this->event_id
)));
311 * @param null $parent_event_id
315 function is_child_event($parent_event_id = NULL) {
316 if ($parent_event_id == NULL) {
317 return $this->event
->parent_event_id
;
319 else return $this->event
->parent_event_id
== $parent_event_id;