-
Notifications
You must be signed in to change notification settings - Fork 0
/
booking.php
412 lines (339 loc) · 13.9 KB
/
booking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/*
* YOU WILL NEED YOUR GOOGLE API KEY FOR THIS TO WORK
*
* You will also notice many areas that are commented out which can be used for debugging by uncommenting them.
*
*/
function sendPostRequest($postargs,$token, $cal){
$APIKEY = 'AIzaSyCBgCHben7bTtLRf3TA0bAhBtGnBUtRI5Q';
$request = 'https://www.googleapis.com/calendar/v3/calendars/' . $cal . '/events?pp=1&key=' . $APIKEY;
//$auth = json_decode($_SESSION['oauth_access_token'],true);
//var_dump($auth);
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, true);
curl_setopt($session, CURLINFO_HEADER_OUT, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Bearer ' . $token,'X-JavaScript-User-Agent: Mount Pearl Tennis Club Bookings'));
$response = curl_exec($session);
//echo '<pre>';
//var_dump(curl_getinfo($session, CURLINFO_HEADER_OUT));
//echo '</pre>';
curl_close($session);
return $response;
}
function sendGetRequest($token,$request){
$APIKEY = 'AIzaSyCBgCHben7bTtLRf3TA0bAhBtGnBUtRI5Q';
//$request = 'https://www.googleapis.com/calendar/v3/calendars/' . $CAL . '/events?pp=1&key=' . $APIKEY;
//$auth = json_decode($_SESSION['oauth_access_token'],true);
//var_dump($auth);
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_HTTPGET, true);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLINFO_HEADER_OUT, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token,'X-JavaScript-User-Agent: Mount Pearl Tennis Club Bookings'));
$response = curl_exec($session);
//echo '<pre>';
//var_dump(curl_getinfo($session, CURLINFO_HEADER_OUT));
//echo '</pre>';
curl_close($session);
return $response;
}
function createPostArgsJSON($date,$starttime,$endtime,$title,$where,$request){
$arg_list = func_get_args();
foreach($arg_list as $key => $arg){
$arg_list[$key] = urlencode($arg);
}
$postargs = <<<JSON
{
"start": {
"dateTime": "{$date}T{$starttime}:00.000-08:00"
},
"end": {
"dateTime": "{$date}T{$endtime}:00.000-08:00"
},
"summary": "$title",
"description": "$request",
"location": "$where"
}
JSON;
return $postargs;
}
/*
* ----- INSTRUCTIONS FOR THIS SECTION -----
*
* Go to http://enarion.net/programming/php/google-client-api/google-client-api-php/<br>
* Follow the tutorial step by step and then come back and fill in the information below.
*
*/
function getAccessToken(){
$tokenURL = 'https://accounts.google.com/o/oauth2/token';
$postData = array(
'client_secret'=>'TbsQWnKPRUkhCdjYFMjrMKRX',
'grant_type'=>'refresh_token',
'refresh_token'=>'1/kdKV10PnkM7y0WeAmC9fF1Lznwn5MBhn1P3CwvYd0a252YlhgT6hM3tTWan-5bYj',
'client_id'=>'1096366798109-89j8o33jcnhb8id6hlcu51gvuhu6r0fd.apps.googleusercontent.com'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tokenURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tokenReturn = curl_exec($ch);
$token = json_decode($tokenReturn);
//var_dump($tokenReturn);
$accessToken = $token->access_token;
return $accessToken;
}
function isTimeBooked($date,$starttime,$endtime,$cal){
$APIKEY = 'AIzaSyCBgCHben7bTtLRf3TA0bAhBtGnBUtRI5Q';
$start = $date . 'T' . '09:00:00.000-08:00';
$end = $date . 'T' . '20:00:00.000-08:00';
$token = getAccessToken();
$result = sendGetRequest($token, 'https://www.googleapis.com/calendar/v3/calendars/' . $cal . '/events?timeMax=' . $end . '&timeMin=' . $start . '&fields=items(end%2Cstart)&key=' . $APIKEY);
//var_dump($result);
$json_obj = json_decode( $result );
//var_dump($json_obj);
foreach($json_obj as $obj)
{
//echo '<br>';
//print_r($obj);
//echo '<br>';
//echo '<br>';
$count = count($obj);
}
//echo $count;
if($count >= 1){
return $obj;
}
else{
return false;
}
}
function getEndLocation($date,$starttime,$endtime,$cal){
$APIKEY = 'AIzaSyCBgCHben7bTtLRf3TA0bAhBtGnBUtRI5Q';
$start = $date . 'T' . $starttime . ':00.000-08:00';
$end = $date . 'T' . $endtime . ':00.000-08:00';
$token = getAccessToken();
$result = sendGetRequest($token, 'https://www.googleapis.com/calendar/v3/calendars/' . $cal . '/events?timeMax=' . $end . '&timeMin=' . $start . '&fields=items%2Flocation&key=' . $APIKEY);
//var_dump($result);
$json_obj = json_decode( $result );
foreach($json_obj as $obj )
{
echo '<br>';
//print_r($obj);
echo '<br>';
//var_dump($obj);
//echo '<br>';
//echo $obj[0]->location;
$endLocation = $obj[0]->location;
//echo '<br>';
//echo '<br>';
$count = count($obj);
}
//echo $count;
if($count >= 1){
return $endLocation;
}
else{
return false;
}
}
function checkCourtRegistrations($startdate,$starttime,$enddate,$endtime,$cal){
$APIKEY = 'AIzaSyCBgCHben7bTtLRf3TA0bAhBtGnBUtRI5Q';
$start = $startdate . 'T' . $starttime . ':00-08:00';
$end = $enddate . 'T' . $endtime . ':00-08:00';
$token = getAccessToken();
$result = sendGetRequest($token, 'https://www.googleapis.com/calendar/v3/calendars/' . $cal . '/events?timeMax=' . $end . '&timeMin=' . $start . '&fields=items(end%2Cstart%2Csummary)%2Csummary&pp=1&key=' . $APIKEY);
if(strlen($result) > 5){
$result = json_decode($result,true);
//return array($result['summary'] => $result['items']);
return array('items' => $result['items'], 'court' => $result['summary']);
}
else{
return '';
}
}
// Haversine formula
function Haversine($start, $finish) {
$theta = $start[1] - $finish[1];
$distance = (sin(deg2rad($start[0])) * sin(deg2rad($finish[0]))) + (cos(deg2rad($start[0])) * cos(deg2rad($finish[0])) * cos(deg2rad($theta)));
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
return round($distance, 2);
}
// Get lat/long co-ords
function getLatLong($address) {
$address = str_replace(' ', '+', $address);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$geoloc = curl_exec($ch);
$json = json_decode($geoloc);
return array($json->results[0]->geometry->location->lat, $json->results[0]->geometry->location->lng);
}
/*
* End Core Calendar Functions (contents of the corecal.php file above)
*/
?>
<?php
//require_once('libs/cal/corecal.php');
$thecal = 'court1';
/*
* Advance is the amount of time in the future someone can book something.
* days
* weeks
* months
* If it is 0, it will allow unlimited future booking
*/
/*
* ------ Instructions ------
* Fill in where is says put calendar id with the id of your calendar which can be found by doing the following.
*
* 1. Access your Google Calendar at https://www.calendar.google.com.
* 2. Click on the drop down arrow next to the affected calendar and select 'Calendar settings'
* 3. Copy the string beside 'Calendar ID' and paste it in.
*
* You will also need the API Key for Google Calendar as well.
*/
$calendarId = "[email protected]";
$courts = array(
'court1' => array('cid' => 'court1', 'name' => 'court1', 'id' => '[email protected]', 'starttime' => '10:00:00', 'endtime' => '20:00:00', 'advance' => '52 weeks'),
);
$APIKEY = 'AIzaSyCBgCHben7bTtLRf3TA0bAhBtGnBUtRI5Q';
$message = "";
if(isset($_POST['submit']) && $_POST['submit'] == 'Book Court'){
/* This section is to set the date in the correct format
* and to set the ending time to 15 minutes past the time
* that they selected and set time to 15 minuts prior
* to the time they selected which is used to determine
* information about the location of each booking and if
* the driver can get to the two places within one trip.
*/
$date = $_POST['startdate'];
$startdate = date("Y-m-d", strtotime($date));
$From = $_POST['starttime'];
$Minutes = 15;
$endtime1 = date("H:i", strtotime($From)+($Minutes*60));
$locationStartTime = date("H:i", strtotime($From)-($Minutes*60));
$endtime;
/* This section gets the location that the user wants to be picked up
* and gets the location of any booking that is 15 minutes before or
* 15 minutes after the time that they selected. It then will get the
* distance between the two points in miles.
*/
//$startlocation = $_POST['location'];
//$endlocation = getEndLocation($startdate,$locationStartTime,$endtime1,$courts[$_POST['calendar']]['id']);
//$startLoc = $startlocation;
//$endLoc = $endlocation;
//if ($endlocation != ""){
//$start = getLatLong($startLoc);
//$finish = getLatLong($endLoc);
//$distance = Haversine($start, $finish);
//}else{
//$distance = 0;
//}
if ($_POST['passengers'] == '1'){
$titleDetails = $_POST['title'] .';'. $_POST['passengers'] .'per';
}else{
$titleDetails = $_POST['title'] .';'. $_POST['passengers'] .'ppl';
}
/*
* Check to see if everything was filled out properly.
*/
if(date('Ymd') > date('Ymd',strtotime($_POST['startdate']))){
$message = 'You cannot make a booking in the past. Please check your date.';
}
elseif($_POST['starttime'] == ''){
$message = 'You must enter a start time.';
?>
<script type="text/javascript">
<!--
window.document.write('<h2><span style="color: #ff0000;">Unfortunately, that time is already booked. <br></span></h2><p><em><strong>Please give us a call at (702) 485-3232 and we\'ll get you on the schedule...</strong></em> </p><p> </p><p>Thank you!</p><div style="width:100%; text-align: center;"><a href="https://www.therange702.com/parallax/busbooking.php" >Click Here to Start Over</a></div>');
//-->
</script>
<?php
}
elseif ($_POST['title'] == '') {
$message = 'You must enter your name';
?>
<script type="text/javascript">
<!--
window.document.write('<h2><span style="color: #ff0000;">There was an error in your submission please be sure to fill out all the required fields. <br>-Thank You</span></h2>');
//-->
</script>
<?php
}
elseif ($_POST['passengers'] == '') {
$message = 'You must enter your name';
?>
<script type="text/javascript">
<!--
window.document.write('<h2><span style="color: #ff0000;">There was an error in your submission please be sure to fill out all the required fields. <br>-Thank You</span></h2>');
//-->
</script>
<?php
}
elseif ($_POST['email'] == '') {
$message = 'You must enter your email address';
?>
<script type="text/javascript">
<!--
window.document.write('<h2><span style="color: #ff0000;">There was an error in your submission please be sure to fill out all the required fields. <br>-Thank You</span></h2>');
//-->
</script>
<?php
}
elseif ($_POST['phone'] == '') {
$message = 'You must enter your phone number';
?>
<script type="text/javascript">
<!--
window.document.write('<h2><span style="color: #ff0000;">There was an error in your submission please be sure to fill out all the required fields. <br>-Thank You</span></h2>');
//-->
</script>
<?php
}else{
$postargs = createPostArgsJSON($startdate,$_POST['starttime'],$endtime1,$titleDetails,$_POST['location'],$_POST['requests']);
$token = getAccessToken();
$result = sendPostRequest($postargs,$token,$courts[$_POST['calendar']]['id']);
echo '<pre>' . $result . '</pre>';
$emailaddress = "[email protected]";
$customeremail = $_POST['email'];
$subject = "New Online Bus Booking from The Range 702";
$email = $_POST['email'];
$military_time = $_POST['starttime'];
$standard_time = date('h:i A', strtotime($military_time));
$message="";
$message.="Name: ".$_POST['title']."\r\n";
$message.="E-Mail: ".$_POST['email']."\r\n";
$message.="Phone Number: ".$_POST['phone']."\r\n\r\n";
$message.="\r\n Booking Information:\r\n\r\n";
$message.="Passengers: ".$_POST['passengers']."\r\n";
$message.="Pick Up Date: ".$date."\r\n";
$message.="Pick Up Time: ".$standard_time."\r\n";
$message.="Pick Up Location: ".$_POST['location']."\r\n";
$message=$message."\r\nMessage:\r\n".$_POST['requests'];
//mail($emailaddress,$subject,$message,"From: $email" );
//mail($customeremail,$subject,$message,"From: [email protected]" );
?>
<script type="text/javascript">
<!--
window.document.write('<h2><span style="color: #339966;">The shuttle was booked. You will recieve an email confirmation shortly. <br>-Thank You</span></h2>');
//-->
</script>
<?php
}
}
?>