commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / 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 public 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 public 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 public 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 int $event_id
81 *
82 * @return bool|int|null|string
83 */
84 public 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 public static function is_administrator() {
101 global $user;
102 return CRM_Core_Permission::check('administer CiviCRM');
103 }
104
105 /**
106 * @return mixed
107 */
108 public 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 public 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 {
140 return NULL;
141 }
142 }
143
144 /**
145 * @param int $registeringContactID
146 * @param array $fields
147 *
148 * @return int|mixed|null
149 */
150 public static function find_or_create_contact($registeringContactID = NULL, $fields = array()) {
151 $contact_id = self::find_contact($fields);
152
153 if ($contact_id) {
154 return $contact_id;
155 }
156 $contact_params = array(
157 'email-Primary' => CRM_Utils_Array::value('email', $fields, NULL),
158 'first_name' => CRM_Utils_Array::value('first_name', $fields, NULL),
159 'last_name' => CRM_Utils_Array::value('last_name', $fields, NULL),
160 'is_deleted' => CRM_Utils_Array::value('is_deleted', $fields, TRUE),
161 );
162 $no_fields = array();
163 $contact_id = CRM_Contact_BAO_Contact::createProfileContact($contact_params, $no_fields, NULL);
164 if (!$contact_id) {
165 CRM_Core_Error::displaySessionError("Could not create or match a contact with that email address. Please contact the webmaster.");
166 }
167 return $contact_id;
168 }
169
170 /**
171 * @param string $page_name
172 *
173 * @return mixed
174 */
175 public function getValuesForPage($page_name) {
176 $container = $this->controller->container();
177 return $container['values'][$page_name];
178 }
179
180 }