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 / Ical.php

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

515 lines 20.5 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_iCal {
7
8 public $prefix = null;
9
10 public $pluginName = null;
11
12 public $setting = null;
13
14 public $currencies = array();
15
16 public function __construct($prefix, $pluginName, $currencies) {
17
18 $this->prefix = $prefix;
19 $this->pluginName = $pluginName;
20 $this->setting = new booking_package_setting($prefix, $pluginName);
21 $this->currencies = $currencies;
22
23 }
24
25 public function isValid($token, $siteToken, $id = 'all'){
26
27 $booking_sync = $this->setting->getBookingSyncList();
28 $booking_sync = $booking_sync['iCal'];
29 if (get_option('_' . $this->prefix . 'siteToken', false) != $siteToken) {
30
31 return false;
32
33 }
34
35 if ($id == 'all') {
36
37 if (intval($booking_sync[$this->prefix.'ical_active']['value']) == 1 && $booking_sync[$this->prefix.'ical_token']['value'] == $token) {
38
39 $ical_data = $this->getBookingSchedules($id, null);
40 print $ical_data;
41 exit;
42
43 } else {
44
45 return false;
46
47 }
48
49 } else {
50
51 $schedule = new booking_package_schedule($this->prefix, $this->pluginName, $this->currencies);
52 $calendarAccount = $schedule->getCalendarAccount($id);
53 if (intval($calendarAccount['ical']) == 1 && $calendarAccount['icalToken'] == $token) {
54
55 $ical_data = $this->getBookingSchedules($id, $calendarAccount);
56 print $ical_data;
57 exit;
58
59 } else {
60
61 return false;
62
63 }
64
65 }
66
67 }
68
69 public function getBookingSchedules($id = 'all', $calendarAccount = null){
70
71 #$timezone = get_option('timezone_string');
72 $timezone = get_option($this->prefix . "timezone", null);
73 $syncPastCustomersForIcal = intval(get_option($this->prefix . 'syncPastCustomersForIcal', 7));
74 if (is_null($timezone)) {
75
76 $timezone = get_option('timezone_string', 'UTC');
77
78 }
79
80 if ($id != 'all' && !empty($calendarAccount)) {
81
82 $timezone = $calendarAccount['timezone'];
83 $syncPastCustomersForIcal = intval($calendarAccount['syncPastCustomersForIcal']);
84
85 }
86
87 header('Content-type: text/calendar; charset=utf-8');
88 header('Content-Disposition: inline; filename=WP_BOOKING_PACKAGE-' . $id . '.ics');
89
90 date_default_timezone_set('UTC');
91 $unixTime = date('U') - ($syncPastCustomersForIcal * 1440 * 60);
92 $gmtOffset = get_option('gmt_offset');
93 $gmtOffset = date('P');
94 $url_parce = parse_url(get_home_url());
95 $host = $url_parce["host"];
96
97 $ical_data = "BEGIN:VCALENDAR\n";
98 $ical_data .= "PRODID:-//WP_BOOKING_APP/" . $id . '/' . $host . "\n";
99 $ical_data .= "VERSION:2.0\n";
100 $ical_data .= "CALSCALE:GREGORIAN\n";
101 $ical_data .= "METHOD:PUBLISH\n";
102 $ical_data .= "X-WR-CALNAME:WP_BOOKING_PACKAGE-" . $id . "\n";
103 $ical_data .= "X-WR-TIMEZONE:".$timezone."\n";
104 $ical_data .= "X-WR-CALDESC:\n";
105
106 $schedule = new booking_package_schedule($this->prefix, $this->pluginName, $this->currencies);
107
108 $month = date('m', $unixTime);
109 $year = date('Y', $unixTime);
110 if ($id == 'all') {
111
112 $calendarList = $schedule->getCalendarAccountListData("`key`, `name`, `type`, `timezone`, `guestsBool`");
113 foreach ((array) $calendarList as $key => $value) {
114
115 $calendarTimezone = $timezone;
116 if ($value['timezone'] != 'none') {
117
118 $calendarTimezone = $value['timezone'];
119
120 }
121
122 $month = date('m', $unixTime);
123 $year = date('Y', $unixTime);
124 $_POST['accountKey'] = $value['key'];
125 $response = $schedule->getReservationData($month, date('d', $unixTime), $year, true);
126 $ical_data .= $this->createCard($value, $value['key'], $value['name'], $value['type'], $schedule, $response, $calendarTimezone, $host);
127 $month++;
128 if ($month > 12) {
129
130 $month = 1;
131 $year++;
132
133 }
134
135 }
136
137 } else {
138
139 $calendar = $schedule->getCalendarAccount(intval($id));
140 $calendarTimezone = $timezone;
141 if ($calendar['timezone'] != 'none') {
142
143 $calendarTimezone = $calendar['timezone'];
144
145 }
146 $_POST['accountKey'] = $calendar['key'];
147 $response = $schedule->getReservationData($month, date('d', $unixTime), $year, true);
148 $ical_data .= $this->createCard($calendar, $calendar['key'], $calendar['name'], $calendar['type'], $schedule, $response, $calendarTimezone, $host);
149 $month++;
150 if ($month > 12) {
151
152 $month = 1;
153 $year++;
154
155 }
156
157 }
158
159 $ical_data .= "END:VCALENDAR\n";
160 return $ical_data;
161
162 }
163
164 public function createCard($calendarAccount, $calendarKey, $calendarName, $calendarType, $schedule, $response, $timezone, $host){
165
166 #date_default_timezone_set('UTC');
167 date_default_timezone_set($timezone);
168 $ical_data = '';
169 for ($i = 0; $i < count($response); $i++) {
170
171 $bookingDetail = $response[$i];
172 if (isset($bookingDetail['status']) && $bookingDetail['status'] == 'canceled') {
173
174 continue;
175
176 }
177 $summaryList = array();
178 //DESCRIPTION
179
180 $descriptionList = array();
181 $guestsList = array();
182 if ($calendarAccount['type'] == 'day') {
183
184 $guests = $bookingDetail['guests'];
185 if (empty($guests) || is_null($guests)) {
186
187 $guests = array();
188
189 } else {
190
191 $guests = json_decode($guests, true);
192
193 }
194
195
196 if (count($guests) > 0 && isset($guests['guests'])) {
197
198 $guestsList = $guests['guests'];
199 for ($guestKey = 0; $guestKey < count($guestsList); $guestKey++) {
200
201 $guest = $guestsList[$guestKey];
202 if (isset($guest['index'])) {
203
204 $index = intval($guest['index']);
205 if ($index > 0) {
206
207 array_push($descriptionList, $guest['name'] . ": " . $guest['json'][$index]['name']);
208
209 }
210
211 }
212
213 }
214
215 array_push($descriptionList, '');
216
217 }
218
219 } else {
220
221 $accommodationDetails = json_decode($bookingDetail['accommodationDetails'], true);
222 $descriptionList = $this->addGuestsInRooms($accommodationDetails, $descriptionList);
223
224 }
225
226
227
228 for ($a = 0; $a < count($bookingDetail['praivateData']); $a++) {
229
230 $praivateData = $bookingDetail['praivateData'][$a];
231 if (isset($praivateData['type']) && $praivateData['type'] == 'TEXTAREA') {
232
233 $praivateData['value'] = str_replace(array("\r\n", "\r", "\n"), "\\n", $praivateData['value']);
234
235 }
236
237 if (isset($praivateData['isName']) && $praivateData['isName'] == 'true') {
238
239 array_push($summaryList, $praivateData['value']);
240
241 }
242
243 if (isset($praivateData['active']) && $praivateData['active'] == 'true') {
244
245 if (is_string($praivateData['value'])) {
246
247 array_push($descriptionList, $praivateData['name'] . ": " . $praivateData['value']);
248
249 } else if (is_array($praivateData['value'])) {
250
251 array_push($descriptionList, $praivateData['name'] . ": " . implode(' ', $praivateData['value']));
252
253 }
254
255
256 }
257
258 }
259
260 $summary = "No title";
261 if (count($summaryList) != 0) {
262
263 $summary = implode(" ", $summaryList);
264
265 }
266
267 $description = implode('\n', $descriptionList);
268 $selectedOptionsObject = $schedule->getSelectedServices($calendarAccount, json_decode($bookingDetail['options'], true), $guestsList, "options", array(), 1, false);
269 $courseTime = intval($bookingDetail['courseTime']);
270 $courseTime += $selectedOptionsObject['time'];
271 $services = $selectedOptionsObject['object'];
272
273 $serviceDetails = '';
274 $detailsList = array();
275 if (is_array($services)) {
276
277 foreach ((array) $services as $key => $service) {
278
279 $responseCostInService = $schedule->getCostsInService($calendarAccount, $service, $guestsList);
280 $costs = $responseCostInService['costs'];
281 $details = $service['name'];
282
283 array_push($detailsList, $details);
284
285 $no = 0;
286 foreach ((array) $service['options'] as $option) {
287
288 if (intval($option['selected']) == 1) {
289
290 $no++;
291 $details = "#".$no." ".$option['name']." ";
292
293 array_push($detailsList, $details);
294
295 }
296
297 }
298
299 }
300
301 $serviceDetails = implode('\n', $detailsList) . '\n\n';
302
303 }
304
305
306
307 $unixTime = $bookingDetail['date']['unixTime'];
308 $ical_data .= "BEGIN:VEVENT\n";
309 $ical_data .= "UID:".$bookingDetail['key']."@".$host."\n";
310
311 if ($calendarType == 'day') {
312
313 $ical_data .= "DTSTART;TZID=".$timezone.":".date("Ymd\THi00", $unixTime)."\n";
314 if ($courseTime != 0) {
315
316 $ical_data .= "DTEND;TZID=".$timezone.":".date("Ymd\THi00", ($unixTime + ($courseTime * 60)))."\n";
317
318 } else {
319
320 $ical_data .= "DTEND;TZID=".$timezone.":".date("Ymd\THi00", $unixTime)."\n";
321
322 }
323
324 } else {
325
326 $accommodationDetails = json_decode($bookingDetail['accommodationDetails'], true);
327 $ical_data .= "DTSTART;TZID=".$timezone.":".date("Ymd\T12i00", $accommodationDetails['checkIn'])."\n";
328 $ical_data .= "DTEND;TZID=".$timezone.":".date("Ymd\T12i00", $accommodationDetails['checkOut'])."\n";
329
330 }
331
332 $ical_data .= "DTSTAMP:".date("Ymd\THi00\Z")."\n";
333 $ical_data .= "CREATED;TZID=".$timezone.":".date("Ymd\THi00", $bookingDetail['reserveTime'])."\n";
334 $ical_data .= "STATUS:CONFIRMED\n";
335 #$ical_data .= "SEQUENCE:".($i + 1)."\n";
336 $ical_data .= "SEQUENCE:0\n";
337 $ical_data .= "SUMMARY:".$summary."\n";
338 $ical_data .= "LOCATION:".$calendarName."\n";
339 $ical_data .= "DESCRIPTION:" . $serviceDetails . $description . "\n";
340 $ical_data .= "END:VEVENT\n";
341 #var_dump($bookingDetail['praivateData']);
342
343 }
344
345 return $ical_data;
346
347 }
348
349 public function addGuestsInRooms($accommodationDetails, $descriptionList) {
350
351 $rooms = array();
352 if (isset($accommodationDetails['rooms'])) {
353
354 $rooms = $accommodationDetails['rooms'];
355
356 }
357
358 for ($i = 0; $i < count($rooms); $i++) {
359
360 $room = $rooms[$i];
361 $guests = $room['guests'];
362 if (array_key_exists('guestsList', $room) === false) {
363
364 $room['guestsList'] = array();
365
366 }
367 array_push($descriptionList, __("Room", 'booking-package') . ' ' . ($i + 1) . ': ' . $room['person']);
368
369 $nameOfGuests = array();
370 foreach ($room['guestsList'] as $key => $guestData) {
371
372 $nameOfGuests[$key] = $guestData['name'];
373
374 }
375
376 foreach ($guests as $key => $guest) {
377
378 if (intval($guest['number']) > 0) {
379
380 array_push($descriptionList, $nameOfGuests[$key] . ": " . $guest['name']);
381
382 }
383
384 }
385
386 }
387
388 return $descriptionList;
389
390 }
391
392 public function attachICalendar($calendarAccount, $event_data, $email_id, $id, $token = null, $type = 'attach') {
393
394 global $wpdb;
395 $locationForIcalendar = '';
396 $emailData = array('subject' => null, 'body' => null);
397
398 /**
399 $table_name = $wpdb->prefix . "booking_package_email_settings";
400 for ($i = 0; $i < count($email_id); $i++) {
401
402 $sql = $wpdb->prepare(
403 "SELECT `subjectForIcalendar`, `locationForIcalendar`, `contentForIcalendar` FROM " . $table_name . " WHERE `accountKey` = %d AND `mail_id` = %s;",
404 array(intval($calendarAccount['key']), $email_id[$i])
405 );
406 $row = $wpdb->get_row($sql, ARRAY_A);
407 $locationForIcalendar = $row['locationForIcalendar'];
408 $emailData['subject'] = $row['subjectForIcalendar'];
409 $emailData['body'] = $row['contentForIcalendar'];
410 break;
411
412 }
413 **/
414
415 $locationForIcalendar = $event_data['ical_location'];
416 $emailData['subject'] = $event_data['ical_subject'];
417 $emailData['body'] = $event_data['ical_description'];
418
419 if (empty($emailData['subject']) === true) {
420
421 return array('temp_file' => null, 'temp_file_name' => null, 'status' => false);
422
423 }
424
425 $schedule = new booking_package_schedule($this->prefix, $this->pluginName, $this->currencies);
426 $customer = $schedule->getCustomer($id, $token);
427 $unixTime = $customer['scheduleUnixTime'];
428 $notificationContents = $schedule->getNotificationContents($calendarAccount, $customer, $email_id, 'visitor', $emailData, 'text');
429 $emailSubject = $notificationContents['emailSubject'];
430 $emailBody = str_replace("\n", '\n', $notificationContents['emailBody']);
431 $servicesDetails = $notificationContents['servicesDetails'];
432
433 $timezone = $calendarAccount['timezone'];
434 $url_parce = parse_url(get_home_url());
435 $host = $url_parce["host"];
436
437 $ical_data = "BEGIN:VCALENDAR\n";
438 $ical_data .= "PRODID:-//WP_BOOKING_PACKAGE_CUSTOMER//" . $calendarAccount['key'] . '//' . $host . "\n";
439 $ical_data .= "VERSION:2.0\n";
440 $ical_data .= "CALSCALE:GREGORIAN\n";
441 $ical_data .= "METHOD:PUBLISH\n";
442 $ical_data .= "X-WR-CALNAME:WP_BOOKING_PACKAGE-" . $calendarAccount['key'] . "\n";
443 $ical_data .= "X-WR-TIMEZONE:".$timezone."\n";
444
445 $ical_data .= "BEGIN:VEVENT\n";
446 $ical_data .= "UID:" . $id . "@" . $host . "\n";
447
448 if ($calendarAccount['type'] == 'day') {
449
450 $ical_data .= "DTSTART;TZID=" . $timezone . ":" . date("Ymd\THi00", $unixTime) . "\n";
451 #$ical_data .= "DTSTART:" . date("Ymd\THi00\Z", $unixTime) . "\n";
452 if (intval($servicesDetails['time']) != 0) {
453
454 $ical_data .= "DTEND;TZID=" . $timezone . ":" . date("Ymd\THi00", ($unixTime + (intval($servicesDetails['time']) * 60))) . "\n";
455 #$ical_data .= "DTEND:" . date("Ymd\THi00\Z", ($unixTime + ($serviceTime * 60))) . "\n";
456
457 } else {
458
459 $ical_data .= "DTEND;TZID=" . $timezone . ":" . date("Ymd\THi00", $unixTime) . "\n";
460 #$ical_data .= "DTEND:" . date("Ymd\THi00\Z", $unixTime) . "\n";
461
462 }
463
464 } else {
465
466 $accommodationDetails = json_decode($customer['accommodationDetails'], true);
467 #$ical_data .= "DTSTART;TZID=" . $timezone . ":" . date("Ymd\T12i00", $accommodationDetails['checkIn']) . "\n";
468 #$ical_data .= "DTEND;TZID=" . $timezone . ":" . date("Ymd\T12i00", $accommodationDetails['checkOut']) . "\n";
469 $ical_data .= "DTSTART;VALUE=DATE:" . date("Ymd", $accommodationDetails['checkIn']) . "\n";
470 $ical_data .= "DTEND;VALUE=DATE:" . date("Ymd", $accommodationDetails['checkOut']) . "\n";
471
472 }
473
474 $ical_data .= "DTSTAMP:" . date("Ymd\THi00\Z") . "\n";
475 #$ical_data .= "CREATED;TZID=" . $timezone . ":" . date("Ymd\THi00", $bookingDetail['reserveTime']) . "\n";
476 $ical_data .= "CREATED:" . date("Ymd\THi00\Z", $customer['reserveTime']) . "\n";
477 $ical_data .= "STATUS:CONFIRMED\n";
478 $ical_data .= "SEQUENCE:0\n";
479 $ical_data .= "SUMMARY:" . trim($emailSubject) . "\n";
480 $ical_data .= "LOCATION:" . trim($locationForIcalendar) . "\n";
481 $ical_data .= "DESCRIPTION:" . trim($emailBody) . "\n";
482 $ical_data .= "END:VEVENT\n";
483 $ical_data .= "END:VCALENDAR\n";
484
485 $temp_file = tempnam(sys_get_temp_dir(), 'ical_') . '.ics';
486 $temp_file_name = basename($temp_file);
487
488 file_put_contents($temp_file, $ical_data);
489
490 if ($type === 'attach') {
491
492 return array('temp_file' => $temp_file, 'temp_file_name' => $temp_file_name, 'status' => true);
493
494 } else if ($type === 'download') {
495
496 $file_mime_type = 'text/calendar';
497 header('Content-Type: ' . $file_mime_type);
498 header('Content-Disposition: attachment; filename="' . $temp_file_name . '"');
499 header('Content-Length: ' . filesize($temp_file));
500
501 readfile($temp_file);
502 unlink($temp_file);
503
504 }
505
506
507
508 }
509
510
511 }
512
513
514
515 ?>