PluginProbe
Booking Package / trunk
Booking Package vtrunk
1.7.28 1.7.27 1.7.26 1.7.24 1.7.25 1.7.23 1.7.22 1.7.21 1.7.20 1.7.19 1.7.18 1.7.17 1.7.16 1.7.15 1.7.14 1.7.13 1.7.12 1.7.11 1.7.10 trunk 1.5.30 1.5.31 1.5.32 1.5.33 1.5.35 All 183 releases
booking-package / lib / Webhook.php

Webhook.php in Booking Package trunk, at lib/Webhook.php

454 lines 18.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if(!defined('ABSPATH')){
3 exit;
4 }
5
6 class booking_package_webhook {
7
8 public $prefix = null;
9
10 public $pluginName = null;
11
12 public $setting = null;
13
14 public $schedule = null;
15
16 public function __construct($prefix, $pluginName) {
17
18 $this->prefix = $prefix;
19 $this->pluginName = $pluginName;
20 $this->setting = new booking_package_setting($prefix, $pluginName);
21 $this->schedule = new booking_package_schedule($prefix, $pluginName, null);
22
23 }
24
25 public function catchWebhook($target, $server, $post, $body = false){
26
27 global $wpdb;
28
29 /** accountKey **/
30 $accountKey = 1;
31
32 $timeMin = date('U') - (7 * 24 * 60 * 60);
33 $schedule = $this->schedule;
34 $setting = $this->setting;
35 $list = array();
36 $eventList = array();
37 $calendarAccountList = array();
38 $table_name = $wpdb->prefix . "booking_package_calendar_accounts";
39 #$sql = $wpdb->prepare("SELECT `key`,`googleCalendarID` FROM `".$table_name."`;", array());
40 $rows = $wpdb->get_results("SELECT `key`,`googleCalendarID` FROM `".$table_name."`;", ARRAY_A);
41 foreach($rows as $row){
42
43 $calendarAccountList[$row['googleCalendarID']] = $row['key'];
44 $list = array_merge($list, $schedule->getUserList($timeMin, $row['key']));
45 $eventList = array_merge($eventList, $setting->listsGC($row['key'], $row['googleCalendarID'], $timeMin));
46
47 }
48
49
50
51 #$eventList = $setting->listsGC($timeMin);
52 #var_dump($eventList);
53 if(is_null($eventList)){
54
55 return null;
56
57 }
58
59
60
61 $timezone = get_option('timezone_string');
62 date_default_timezone_set($timezone);
63
64
65 #$list = $schedule->getUserList($timeMin, $accountKey);
66 $userLists = array();
67 for($i = 0; $i < count($list); $i++){
68
69 $key = $list[$i]["iCalIDforGoogleCalendar"];
70 $userLists[$key] = $list[$i];
71
72 }
73
74
75 #var_dump($calendarAccountList);
76 $googleCalendarID = null;
77 foreach($eventList as $key => $value){
78
79 $calendarAccount = array();
80 if(isset($calendarAccountList[$value['googleCalendarID']])){
81
82 $accountKey = $calendarAccountList[$value['googleCalendarID']];
83 $calendarAccount = $schedule->getCalendarAccount($accountKey);
84 $googleCalendarID = $value['googleCalendarID'];
85
86 }else{
87
88 continue;
89
90 }
91
92 #print "iCalIDforGoogleCalendar = ".$key."<br>";
93 $iCalIDforGoogleCalendar = $key;
94 $timeStart = date( "U", strtotime($value["timeStart"]));
95 $timeEnd = date( "U", strtotime($value["timeEnd"]));
96
97 $accommodationDetails = array();
98
99 if(isset($userLists[$key])){
100
101 #print "check update<br>";
102 $id = $userLists[$key]['key'];
103 $applicantCount = $userLists[$key]['applicantCount'];
104 $accommodationDetails = json_decode($userLists[$key]['accommodationDetails'], true);
105 $description = $eventList[$key]['description'];
106 $bookingTimeStart = $userLists[$key]["scheduleUnixTime"];
107 $bookingTimeEnd = $userLists[$key]["scheduleUnixTime"] + (intval($userLists[$key]["courseTime"]) * 60);
108
109 if($calendarAccount['type'] == 'hotel'){
110
111 #var_dump($value);
112 $bookingTimeStart = $userLists[$key]["checkIn"];
113 $bookingTimeEnd = $userLists[$key]["checkOut"] - (1440 * 60);
114
115 /**
116 $timeStart = date('U', mktime(0, 0, 0, date('n', $timeStart), date('j', $timeStart), date('Y', $timeStart)));
117 $timeEnd = date('U', mktime(0, 0, 0, date('n', $timeEnd), date('j', $timeEnd), date('Y', $timeEnd))) - (1440 * 60);
118 **/
119
120 if(!is_null($value['dateStart']) && !is_null($value['dateEnd'])){
121
122 $dateStartParse = date_parse($value['dateStart']);
123 $dateEndParse = date_parse($value['dateEnd']);
124 $timeStart = date('U', mktime(0, 0, 0, $dateStartParse['month'], $dateStartParse['day'], $dateStartParse['year']));
125 $timeEnd = date('U', mktime(0, 0, 0, $dateEndParse['month'], $dateEndParse['day'], $dateEndParse['year'])) - (1440 * 60);
126
127 }else{
128
129 $timeStart = date('U', mktime(0, 0, 0, date('n', $timeStart), date('j', $timeStart), date('Y', $timeStart)));
130 $timeEnd = date('U', mktime(0, 0, 0, date('n', $timeEnd), date('j', $timeEnd), date('Y', $timeEnd))) - (1440 * 60);
131
132 }
133
134 if($bookingTimeStart != $timeStart || $bookingTimeEnd != $timeEnd){
135
136 #print $bookingTimeStart." ".$timeStart." - ".$bookingTimeEnd." ".$timeEnd."<br>";
137 $scheduleResponse = $schedule->serachSchedule($timeStart, $accountKey);
138 if($scheduleResponse['status'] != "error"){
139
140 $accommodationDetails['list'][(string) $timeStart] = $scheduleResponse;
141
142 }
143
144 $scheduleResponse = $schedule->serachSchedule($timeEnd, $accountKey);
145 if($scheduleResponse['status'] != "error"){
146
147 $accommodationDetails['list'][(string) $timeEnd] = $scheduleResponse;
148
149 }
150
151 #$guestsList = $setting->getGuestsList($accountKey, true);
152 $guestsList = $accommodationDetails['guestsList'];
153 #var_dump($guestsList);
154 foreach($guestsList as $guestsKey => $guestsValue){
155
156 for($i = 0; $i < count($guestsValue['json']); $i++){
157
158 if($guestsValue['json'][$i]['selected'] == 1){
159
160 $accommodationDetails['guestsList'][$guestsValue['key']] = $guestsValue['json'][$i];
161
162 }
163
164 }
165
166 }
167
168 $accommodationDetails = $schedule->createAccommodationDetails($calendarAccount, $accommodationDetails, $timeStart, $applicantCount, $accommodationDetails);
169 unset($accommodationDetails['list']);
170 #$bookingTimeEnd += 1440 * 60;
171 $timeEnd++;
172 #var_dump($accommodationDetails);
173 #var_dump($userLists[$key]);
174
175 }
176
177 }
178
179 $form = $userLists[$key]["praivateData"];
180 #print "bookingTimeStart = ".$bookingTimeStart."<br>";
181 #print "bookingTimeEnd = ".$bookingTimeEnd."<br>";
182
183 if(intval($userLists[$key]['resultOfGoogleCalendar']) != 0){
184
185 $update = $this->prepareFrom("update", $id, $eventList[$key]['name'], $eventList[$key]['address'], $eventList[$key]['description'], json_decode($form, true));
186 if($bookingTimeStart != $timeStart || $bookingTimeEnd != $timeEnd){
187
188 $response = $schedule->changeBookingTime("update", $id, $userLists[$key]["scheduleKey"], $userLists[$key]["status"], $userLists[$key]['applicantCount'], $timeStart, $timeEnd, $bookingTimeStart, $bookingTimeEnd, $accommodationDetails, $accountKey);
189 if($response['event'] == 'return'){
190
191 $setting->pushGC('update', $accountKey, $calendarAccount['type'], $id, $googleCalendarID, $bookingTimeStart, $bookingTimeEnd, json_decode($form), $key);
192
193 }
194
195 }
196
197 }
198
199 unset($userLists[$key]);
200
201 }else{
202
203 $googleCalendarIDofVisitor = $schedule->serachGoogleCalendarIdOfVisitor($iCalIDforGoogleCalendar);
204 if($googleCalendarIDofVisitor != false){
205
206 continue;
207
208 }
209
210 if($calendarAccount['type'] == 'hotel'){
211
212 #var_dump($value);
213 if(!is_null($value['dateStart']) && !is_null($value['dateEnd'])){
214
215 $dateStartParse = date_parse($value['dateStart']);
216 $dateEndParse = date_parse($value['dateEnd']);
217 $timeStart = date('U', mktime(0, 0, 0, $dateStartParse['month'], $dateStartParse['day'], $dateStartParse['year']));
218 $timeEnd = date('U', mktime(0, 0, 0, $dateEndParse['month'], $dateEndParse['day'], $dateEndParse['year'])) - (1440 * 60);
219
220 }else{
221
222 $timeStart = date('U', mktime(0, 0, 0, date('n', $timeStart), date('j', $timeStart), date('Y', $timeStart)));
223 $timeEnd = date('U', mktime(0, 0, 0, date('n', $timeEnd), date('j', $timeEnd), date('Y', $timeEnd))) - (1440 * 60);
224
225
226 }
227
228
229 $scheduleResponse = $schedule->serachSchedule($timeStart, $accountKey);
230 if($scheduleResponse['status'] != "error"){
231
232 $accommodationDetails['list'][(string) $timeStart] = $scheduleResponse;
233
234 }else{
235
236 $setting->deleteGC($accountKey, $iCalIDforGoogleCalendar, $googleCalendarID);
237 continue;
238
239 }
240 $scheduleResponse = $schedule->serachSchedule($timeEnd, $accountKey);
241 if($scheduleResponse['status'] != "error"){
242
243 $accommodationDetails['list'][(string) $timeEnd] = $scheduleResponse;
244
245 }else{
246
247 $setting->deleteGC($accountKey, $iCalIDforGoogleCalendar, $googleCalendarID);
248 continue;
249
250 }
251
252 $guestsList = $setting->getGuestsList($accountKey, true);
253 foreach($guestsList as $guestsKey => $guestsValue){
254
255 #$guestsJson = json_decode($guestsValue['json'], true);
256 $accommodationDetails['guestsList'][$guestsValue['key']] = $guestsJson[0];
257
258 }
259
260 $accommodationDetails = $schedule->createAccommodationDetails($calendarAccount, $accommodationDetails, $timeStart, $applicantCount, null);
261 unset($accommodationDetails['list']);
262 $schedule->setAccommodationDetails($accommodationDetails);
263
264
265 }
266
267
268
269 $bookingSchedule = $schedule->serachSchedule($timeStart, $accountKey);
270 if($bookingSchedule['status'] != 'error' && $bookingSchedule['stop'] == 'false'){
271
272 $applicantCount = 1;
273 $time = ($timeEnd - $timeStart) / 60;
274 $course = $this->prepareCourses($time, $accountKey);
275 /** $course arrays exchange to $courseKey, $courseName, $courseTime, $courseCost. **/
276 extract($course, EXTR_SKIP);
277
278 $bool = $schedule->changeBookingTime("insert", null, null, null, $applicantCount, $timeStart, $timeEnd, null, null, $accommodationDetails, $accountKey);
279 if($bool === true){
280
281 $form = json_encode($setting->getForm($accountKey));
282 $form = $this->prepareFrom("insert", $id, $eventList[$key]['name'], $eventList[$key]['address'], $eventList[$key]['description'], json_decode($form, true));
283
284 $table_name = $wpdb->prefix . "booking_package_schedules";
285 $sql = "SELECT * FROM `".$table_name."` WHERE `accountKey` = %d AND (`unixTime` >= %d AND `unixTime` < %d) ORDER BY `unixTime` ASC ;";
286 $values = array(intval($accountKey), intval($timeStart), intval($timeEnd));
287 if($timeStart == $timeEnd){
288
289 $sql = "SELECT * FROM `".$table_name."` WHERE `accountKey` = %d AND `unixTime` = %d;";
290 $values = array(intval($accountKey), intval($timeStart));
291
292 }
293
294 if($calendarAccount['type'] == 'hotel'){
295
296 $sql = "SELECT * FROM `".$table_name."` WHERE `accountKey` = %d AND (`unixTime` >= %d AND `unixTime` <= %d) ORDER BY `unixTime` ASC ;";
297 $values = array(intval($accountKey), intval($timeStart), intval($timeEnd));
298
299 }
300
301 $currency = get_option($this->prefix."currency", 'usd');
302 $status = $schedule->getStatus();
303 $schedule->updateRemainderSeart("increase", $sql, $values, $applicantCount);
304 $lastID = $schedule->insertPrivateData(date('U'), 'google', $status, $bookingSchedule['key'], $bookingSchedule['unixTime'], $bookingSchedule['title'], $bookingSchedule['cost'], $courseKey, $courseName, $courseTime, $courseCost, $form, $currency, null, null, $accountKey, $applicantCount);
305 $schedule->updateICalIDforGoogleCalendar($lastID, $iCalIDforGoogleCalendar);
306
307 }else{
308
309 /** Delete events sent from Google Calendar. **/
310 #var_dump("Delete events sent from Google Calendar.");
311 $setting->deleteGC($accountKey, $iCalIDforGoogleCalendar, $googleCalendarID);
312
313 }
314
315 }else{
316
317 $setting->deleteGC($accountKey, $iCalIDforGoogleCalendar, $googleCalendarID);
318 #print "Google Calendar から送られてきたイベントを削除<br>";
319
320 }
321
322 }
323
324 #print "<hr>";
325
326 }
327
328 foreach($userLists as $key => $value){
329
330 $schedule->deleteBookingData($value['key'], false);
331 #var_dump($value);
332
333 }
334
335
336 }
337
338 public function prepareCourses($time, $accountKey){
339
340 $schedule = $this->schedule;
341 $response = array('courseKey' => null, 'courseName' => null, 'courseTime' => null, 'courseCost' => null);
342 $course = $schedule->serachCourse($accountKey, false, $time);
343 if($course['status'] != 'error'){
344
345 $response['courseKey'] = $course['key'];
346 $response['courseName'] = $course['name'];
347 $response['courseTime'] = $course['time'];
348 $response['courseCost'] = $course['cost'];
349
350 }else if($time != 0){
351
352 $response['courseKey'] = "exception";
353 $response['courseName'] = $time." min";
354 $response['courseTime'] = $time;
355 $response['courseCost'] = null;
356
357 }
358
359 return $response;
360
361 }
362
363 public function prepareFrom($mode, $id, $name, $address, $description, $form){
364
365 $name = preg_replace("/ /", " ", $name);
366 $address = preg_replace("/ /", " ", $address);
367 $descriptionList = explode('<br>', $description);
368 #var_dump($name);
369 #var_dump($address);
370
371 $nameList = array();
372 $addressList = array();
373 foreach($form as $key => $value){
374
375 if($value['isName'] == 'true' && $value['active'] == 'true'){
376
377 array_push($nameList, $value['value']);
378
379 }
380
381 if($value['isAddress'] == 'true' && $value['active'] == 'true'){
382
383 array_push($addressList, $value['value']);
384
385 }
386
387 }
388
389 $update = false;
390 if($name != implode(' ', $nameList)){
391
392 $form = $this->prepareValues($name, 'isName', count($nameList), $form);
393 $update = true;
394
395 }
396
397 if($address != implode(' ', $addressList)){
398
399 $form = $this->prepareValues($address, 'isAddress', count($addressList), $form);
400 $update = true;
401
402 }
403
404 if($mode == 'update'){
405
406 if($update === true){
407
408 $schedule = $this->schedule;
409 $schedule->updatePraivateData($id, $form);
410
411 }
412
413 return $update;
414
415 }else{
416
417 return $form;
418
419 }
420
421 }
422
423 public function prepareValues($values, $isKey, $count, $form){
424
425 $valueList = explode(' ', $values);
426 $i = $count;
427 foreach($form as $key => $value){
428
429 if($value[$isKey] == 'true' && $value['active'] == 'true'){
430
431 $i--;
432 if($i == 0){
433
434 $form[$key]['value'] = implode(' ', $valueList);
435 break;
436
437 }else{
438
439 $form[$key]['value'] = array_shift($valueList);
440
441 }
442
443 }
444
445 }
446
447 return $form;
448
449 }
450
451
452 }
453
454 ?>