CRM/Event add comments
[civicrm-core.git] / CRM / Event / Cart / Form / Cart.php
1 <?php
2
3 /**
4 * Class CRM_Event_Cart_Form_Cart
5 */
6 class CRM_Event_Cart_Form_Cart extends CRM_Core_Form {
7 public $cart;
8
9 public $_action;
10 public $contact;
11 public $event_cart_id = NULL;
12 public $_mode;
13 public $participants;
14
15 public function preProcess() {
16 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
17 $this->_mode = 'live';
18 $this->loadCart();
19
20 $this->checkWaitingList();
21
22 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
23 $this->_bltID = array_search('Billing', $locationTypes);
24 $this->assign('bltID', $this->_bltID);
25
26 $event_titles = array();
27 foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
28 $event_titles[] = $event_in_cart->event->title;
29 }
30 $this->description = ts("Online Registration for %1", array(1 => implode(", ", $event_titles)));
31 if (!isset($this->discounts)) {
32 $this->discounts = array();
33 }
34 }
35
36 function loadCart() {
37 if ($this->event_cart_id == NULL) {
38 $this->cart = CRM_Event_Cart_BAO_Cart::find_or_create_for_current_session();
39 }
40 else {
41 $this->cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->event_cart_id);
42 }
43 $this->cart->load_associations();
44 $this->stub_out_and_inherit();
45 }
46
47 function stub_out_and_inherit() {
48 $transaction = new CRM_Core_Transaction();
49
50 foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
51 if (empty($event_in_cart->participants)) {
52 $participant_params = array(
53 'cart_id' => $this->cart->id,
54 'event_id' => $event_in_cart->event_id,
55 'contact_id' => self::find_or_create_contact($this->getContactID()),
56 );
57 $participant = CRM_Event_Cart_BAO_MerParticipant::create($participant_params);
58 $participant->save();
59 $event_in_cart->add_participant($participant);
60 }
61 $event_in_cart->save();
62 }
63 $transaction->commit();
64 }
65
66 function checkWaitingList() {
67 foreach ($this->cart->events_in_carts as $event_in_cart) {
68 $empty_seats = $this->checkEventCapacity($event_in_cart->event_id);
69 if ($empty_seats === NULL) {
70 continue;
71 }
72 foreach ($event_in_cart->participants as $participant) {
73 $participant->must_wait = ($empty_seats <= 0);
74 $empty_seats--;
75 }
76 }
77 }
78
79 /**
80 * @param $event_id
81 *
82 * @return bool|int|null|string
83 */
84 function checkEventCapacity($event_id) {
85 $empty_seats = CRM_Event_BAO_Participant::eventFull($event_id, TRUE);
86 if (is_numeric($empty_seats)) {
87 return $empty_seats;
88 }
89 if (is_string($empty_seats)) {
90 return 0;
91 }
92 else {
93 return NULL;
94 }
95 }
96
97 /**
98 * @return bool
99 */
100 static function is_administrator() {
101 global $user;
102 return CRM_Core_Permission::check('administer CiviCRM');
103 }
104
105 /**
106 * @return mixed
107 */
108 function getContactID() {
109 //XXX when do we query 'cid' ?
110 $tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
111
112 //check if this is a checksum authentication
113 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
114 if ($userChecksum) {
115 //check for anonymous user.
116 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($tempID, $userChecksum);
117 if ($validUser) {
118 return $tempID;
119 }
120 }
121
122 // check if the user is registered and we have a contact ID
123 $session = CRM_Core_Session::singleton();
124 return $session->get('userID');
125 }
126
127 /**
128 * @param $fields
129 *
130 * @return mixed|null
131 */
132 static function find_contact($fields) {
133 $dedupe_params = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
134 $dedupe_params['check_permission'] = FALSE;
135 $ids = CRM_Dedupe_Finder::dupesByParams($dedupe_params, 'Individual');
136 if (is_array($ids)) {
137 return array_pop($ids);
138 }
139 else return NULL;
140 }
141
142 /**
143 * @param null $registeringContactID
144 * @param array $fields
145 *
146 * @return int|mixed|null
147 */
148 static function find_or_create_contact($registeringContactID = NULL, $fields = array(
149 )) {
150 $contact_id = self::find_contact($fields);
151
152 if ($contact_id) {
153 return $contact_id;
154 }
155 $contact_params = array(
156 'email-Primary' => CRM_Utils_Array::value('email', $fields, NULL),
157 'first_name' => CRM_Utils_Array::value('first_name', $fields, NULL),
158 'last_name' => CRM_Utils_Array::value('last_name', $fields, NULL),
159 'is_deleted' => CRM_Utils_Array::value('is_deleted', $fields, TRUE),
160 );
161 $no_fields = array();
162 $contact_id = CRM_Contact_BAO_Contact::createProfileContact($contact_params, $no_fields, NULL);
163 if (!$contact_id) {
164 CRM_Core_Error::displaySessionError("Could not create or match a contact with that email address. Please contact the webmaster.");
165 }
166 return $contact_id;
167 }
168
169 /**
170 * @param $page_name
171 *
172 * @return mixed
173 */
174 function getValuesForPage($page_name) {
175 $container = $this->controller->container();
176 return $container['values'][$page_name];
177 }
178 }
179