PluginProbe
Booking Calendar / 10.1.3
Booking Calendar v10.1.3
11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 All 203 releases
booking / core / sync / wpbc-gcal-class.php

wpbc-gcal-class.php in Booking Calendar 10.1.3, at core/sync/wpbc-gcal-class.php

820 lines 43.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.0
4 * @package Booking Calendar
5 * @subpackage Google Calendar Sync
6 * @category Data Sync
7 *
8 * @author wpdevelop
9 * @link https://wpbookingcalendar.com/
10 * @email info@wpbookingcalendar.com
11 *
12 * @modified 2014.06.27
13 * @since 5.2.0
14 */
15
16 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
17
18 // TODO:
19 // 1: Auto import
20 // 1.1 Write description about the Cron working only on opening some page.
21 // 2. Set assignment of form fields between events and bookings
22 // 3. Test MultiUser conception - During import of all resources - its will require to import only booking resources of the actual user
23 // 3.1 Test auto import paramters for multiuser version, which paramters, like maximum number of events is taken during auto import process... etc...
24 // 4. Test everything and refactor if required.
25
26
27 // Membership - create WP users from the bookins
28 // Unlimited number of bookings
29 // Bookings for specific times
30 // Show cost of specific date inside of the date(s).
31
32 class WPBC_Google_Calendar {
33
34 public $events;
35
36 public $messages_arr;
37
38 private $feed_url;
39 private $booking_gcal_events_from;
40 private $booking_gcal_events_until;
41 private $booking_gcal_events_max;
42 private $booking_gcal_timezone;
43 private $start_of_week;
44 private $error;
45 private $bktype;
46 private $user_id;
47 private $is_silent;
48 function __construct() {
49
50 $this->messages_arr = array();
51 $this->error = '';
52 $this->bktype = 1;
53 $this->is_silent = false;
54 $this->events = array();
55 $user = wpbc_get_current_user();
56 $this->setUserID( $user->ID );
57
58 if ( ! $this->start_of_week = get_option('start_of_week') )
59 $this->start_of_week = 0;
60 }
61
62 public function show_message( $message , $is_spin = false, $is_error = false ) {
63
64 $this->messages_arr[] = $message;
65
66 if ( $this->is_silent )
67 return;
68
69 ?><script type="text/javascript">
70 var my_message = '<?php echo html_entity_decode( esc_js( $message ),ENT_QUOTES) ; ?>';
71 wpbc_admin_show_message( my_message, '<?php echo ( $is_error ? 'error' : 'info' ); ?>', <?php echo ( $is_error ? '60000' : '3000' ); ?> );
72 </script><?php
73
74 return;
75 //Old:
76 //$message = str_replace("'", '&#039;', $message);
77 $message = esc_js($message);
78
79 ?> <script type="text/javascript"> if (jQuery('#ajax_message').length > 0 ) {
80 <?php if ($is_spin ) { ?>
81 jQuery('#ajax_message').html('<div class="updated ajax_message<?php echo ($is_error?' error':''); ?>" id="ajax_message"><div style="float:left;"><?php echo $message; ?></div><div class="wpbc_spin_loader"><img style="vertical-align:middle;box-shadow:none;width:14px;" src="'+_wpbc.get_other_param( 'url_plugin' )+'/assets/img/ajax-loader.gif"></div></div>');
82 <?php } else { ?>
83 jQuery('#ajax_message').html('<div class="updated ajax_message<?php echo ($is_error?' error':''); ?>" id="ajax_message"><?php echo $message; ?></div>');
84 <?php } ?>
85 }</script> <?php
86 }
87
88
89 public function setSilent() {
90 $this->is_silent = true;
91 }
92
93 public function setUserID($user_id) {
94 $this->user_id = $user_id;
95 }
96
97 public function getUserID() {
98 return $this->user_id;
99 }
100
101 public function setUrl( $relative_url ) {
102 $this->feed_url = $relative_url ;
103 // $this->feed_url = 'https://www.google.com' . $relative_url ;
104 }
105
106 public function setResource($param) {
107 $this->bktype = $param;
108 }
109
110 public function getResource() {
111 return $this->bktype;
112 }
113
114 public function getErrorMessage(){
115 return $this->error;
116 }
117 public function set_events_from_with_array( $booking_gcal_events_from ) { // array( 'from type', 'offset', 'offset type' );
118
119 if ($booking_gcal_events_from[0]=='date') {
120 $booking_gcal_events_from_offset = $booking_gcal_events_from[1] ;
121 } else {
122 switch ($booking_gcal_events_from[2]) {
123 case "second":
124 $booking_gcal_events_from_offset = intval( $booking_gcal_events_from[1] );
125 break;
126 case "minute":
127 $booking_gcal_events_from_offset = intval( $booking_gcal_events_from[1] ) * 60 ; //FixIn: 9.7.3.15
128 break;
129 case "hour":
130 $booking_gcal_events_from_offset = intval( $booking_gcal_events_from[1] ) * 3600 ;
131 break;
132 case "day":
133 $booking_gcal_events_from_offset = intval( $booking_gcal_events_from[1] ) * 86400 ;
134 break;
135 default:
136 $booking_gcal_events_from_offset = intval( $booking_gcal_events_from[1] );
137 }
138 }
139 $booking_gcal_events_from = $booking_gcal_events_from[0];
140
141 $this->set_events_from( $booking_gcal_events_from, $booking_gcal_events_from_offset );
142 }
143
144
145 public function set_events_from( $booking_gcal_events_from, $offset = 0 ){
146
147 switch ( $booking_gcal_events_from ) {
148 //Don't just use time() for 'now', as this will effectively make cache duration 1 second. Instead set to previous minute. Events in Google Calendar cannot be set to precision of seconds anyway
149 case 'now':
150 $this->booking_gcal_events_from = mktime( intval( date( 'H' ) ), intval( date( 'i' ) ), 0, intval( date( 'm' ) ), intval( date( 'j' ) ), intval( date( 'Y' ) ) ) + $offset;
151 break;
152 case 'today':
153 $this->booking_gcal_events_from = mktime( 0, 0, 0, intval( date( 'm' ) ), intval( date( 'j' ) ), intval( date( 'Y' ) ) ) + $offset;
154 break;
155 case 'week':
156 $this->booking_gcal_events_from = mktime( 0, 0, 0, intval( date( 'm' ) ), ( intval( date( 'j' ) ) - intval( date( 'w' ) ) + intval( $this->start_of_week ) ), intval( date( 'Y' ) ) ) + $offset;
157 break;
158 case 'month-start':
159 $this->booking_gcal_events_from = mktime( 0, 0, 0, intval( date( 'm' ) ), 1, intval( date( 'Y' ) ) ) + $offset;
160 break;
161 case 'month-end':
162 $this->booking_gcal_events_from = mktime( 0, 0, 0, intval( date( 'm' ) ) + 1, 1, intval( date( 'Y' ) ) ) + $offset;
163 break;
164 case 'date':
165 $offset = explode('-', $offset);
166 $this->booking_gcal_events_from = mktime( 0, 0, 0, intval( $offset[1] ), intval( $offset[2] ), intval( $offset[0] ) );
167 break;
168 default:
169 $this->booking_gcal_events_from = 0 ; //any - 1970-01-01 00:00
170 }
171 }
172
173
174 public function set_events_until_with_array( $booking_gcal_events_until ) { // array( 'from type', 'offset', 'offset type' );
175
176 if ($booking_gcal_events_until[0]=='date') {
177 $booking_gcal_events_until_offset = $booking_gcal_events_until[1] ;
178 } else {
179 switch ($booking_gcal_events_until[2]) {
180 case "second":
181 $booking_gcal_events_until_offset = intval( $booking_gcal_events_until[1] );
182 break;
183 case "minute":
184 $booking_gcal_events_until_offset = intval( $booking_gcal_events_until[1] ) * 60; //FixIn: 9.8.15.1
185 break;
186 case "hour":
187 $booking_gcal_events_until_offset = intval( $booking_gcal_events_until[1] ) * 3600;
188 break;
189 case "day":
190 $booking_gcal_events_until_offset = intval( $booking_gcal_events_until[1] ) * 86400;
191 break;
192 default:
193 $booking_gcal_events_until_offset = intval( $booking_gcal_events_until[1] );
194 }
195 }
196 $booking_gcal_events_until = $booking_gcal_events_until[0];
197
198 $this->set_events_until( $booking_gcal_events_until, $booking_gcal_events_until_offset );
199 }
200
201 public function set_events_until( $booking_gcal_events_until, $offset = 0 ){
202
203 switch ( $booking_gcal_events_until ) {
204 case 'now':
205 $this->booking_gcal_events_until = mktime( intval( date( 'H' ) ), intval( date( 'i' ) ), 0, intval( date( 'm' ) ), intval( date( 'j' ) ), intval( date( 'Y' ) ) ) + $offset;
206 break;
207 case 'today':
208 $this->booking_gcal_events_until = mktime( 0, 0, 0, intval( date( 'm' ) ), intval( date( 'j' ) ), intval( date( 'Y' ) ) ) + $offset;
209 break;
210 case 'week':
211 $this->booking_gcal_events_until = mktime( 0, 0, 0, intval( date( 'm' ) ), ( intval( date( 'j' ) ) - intval( date( 'w' ) ) + intval( $this->start_of_week ) ), intval( date( 'Y' ) ) ) + $offset;
212 break;
213 case 'month-start':
214 $this->booking_gcal_events_until = mktime( 0, 0, 0, intval( date( 'm' ) ), 1, intval( date( 'Y' ) ) ) + $offset;
215 break;
216 case 'month-end':
217 $this->booking_gcal_events_until = mktime( 0, 0, 0, intval( date( 'm' ) ) + 1, 1, intval( date( 'Y' ) ) ) + $offset;
218 break;
219 case 'date':
220 $offset = explode('-', $offset);
221 $this->booking_gcal_events_until = mktime( 0, 0, 0, intval( $offset[1] ), intval( $offset[2] ), intval( $offset[0] ) );
222 break;
223 case 'any':
224 $this->booking_gcal_events_until = 2145916800; //any - 2038-01-01 00:00
225 }
226
227 }
228
229 public function set_events_max( $booking_gcal_events_max ){
230 $this->booking_gcal_events_max = intval( $booking_gcal_events_max );
231 }
232
233 public function set_timezone( $booking_gcal_timezone ){
234 $this->booking_gcal_timezone = $booking_gcal_timezone;
235 }
236
237
238 //Convert an ISO date/time to a UNIX timestamp
239 private function iso_to_ts( $iso ) {
240 sscanf( $iso, "%u-%u-%uT%u:%u:%uZ", $year, $month, $day, $hour, $minute, $second );
241 $year = intval((string) $year); //FixIn: 9.6.3.7
242 $month = intval((string) $month);
243 $day = intval((string) $day);
244 $hour = intval((string) $hour);
245 $minute = intval((string) $minute);
246 $second = intval((string) $second);
247 return mktime( $hour, $minute, $second, $month, $day, $year );
248 }
249
250
251 // Google Calendar Sync
252 function run() {
253
254 // Define some variables //////////////////////////////////////////////
255 $is_send_emeils = 0; // ( ( get_bk_option( 'booking_gcal_is_send_email') == 'On' ) ? 1 : 0 );
256
257 $this->events = array();
258
259 // $url = $this->feed_url;
260 //
261 // // Break the feed URL up into its parts (scheme, host, path, query)
262 // $url_parts = parse_url( $url );
263 //
264 // if (! isset($url_parts['path'])) // Something wrong with URL
265 // return false;
266 //
267 // $scheme_and_host = $url_parts['scheme'] . '://' . $url_parts['host'];
268 //
269 // // Remove the exisitng projection from the path, and replace it with '/full-noattendees'
270 // $path = substr( $url_parts['path'], 0, strrpos( $url_parts['path'], '/' ) ) . '/full-noattendees';
271 //
272 // // Add the default parameters to the querystring (retrieving JSON, not XML)
273 // $query = '?alt=json&singleevents=false&sortorder=ascending&orderby=starttime';
274
275 $gmt_offset = get_option( 'gmt_offset' ) * 3600;
276
277
278 //$this->feed_url = '2bfu44fmv0fu8or1duckjj11mo@group.calendar.google.com';
279
280 $url = 'https://www.googleapis.com/calendar/v3/calendars/' . $this->feed_url . '/events';
281
282 // Google API Key -- public Google API key shared across all plugin users. Currently the shared key is limited to 500,000 requests per day and 5 requests per second.
283 $api_key = get_bk_option( 'booking_gcal_api_key');
284
285 // Set API key
286 $url .= '?key=' . $api_key;
287
288
289 $args['timeMin'] = urlencode( date( 'c', $this->booking_gcal_events_from - $gmt_offset ) );
290
291 $args['timeMax'] = urlencode( date( 'c', $this->booking_gcal_events_until - $gmt_offset ) );
292
293 $args['maxResults'] = $this->booking_gcal_events_max;
294
295 $args['singleEvents'] = 'True'; //'False'; // Each recurrent event will be showing as separate booking. Google Description: Whether to expand recurring events into instances and only return single one-off events and instances of recurring events, but not the underlying recurring events themselves. Optional. The default is False.
296
297 if ( ! empty( $this->booking_gcal_timezone ) )
298 $args['timeZone'] = $this->booking_gcal_timezone;
299
300 $url = add_query_arg( $args, $url );
301
302
303 // //Append the feed specific parameters to the querystring
304 // $query .= '&start-min=' . date( 'Y-m-d\TH:i:s', $this->booking_gcal_events_from - $gmt_offset );
305 // $query .= '&start-max=' . date( 'Y-m-d\TH:i:s', $this->booking_gcal_events_until - $gmt_offset );
306 // $query .= '&max-results=' . $this->booking_gcal_events_max;
307 //
308 // if ( ! empty( $this->booking_gcal_timezone ) )
309 // $query .= '&ctz=' . $this->booking_gcal_timezone;
310 //
311 // //If enabled, use experimental 'fields' parameter of Google Data API, so that only necessary data is retrieved. This *significantly* reduces amount of data to retrieve and process
312 // // $query .= '&fields=entry(title,link[@rel="alternate"],content,gd:where,gd:when,gCal:uid)';
313 //
314 // $url = $scheme_and_host . $path . $query;
315
316 $this->show_message( __('Importing Feed' ,'booking') . ': ' . $url , true );
317 //debuge($url);
318 //Retrieve the feed data
319 $raw_data = wp_remote_get( $url, array(
320 'sslverify' => false, //sslverify is set to false to ensure https URLs work reliably. Data source is Google's servers, so is trustworthy
321 'timeout' => 10 //Increase timeout from the default 5 seconds to ensure even large feeds are retrieved successfully
322 ) );
323
324 //debuge($raw_data);
325
326 //If $raw_data is a WP_Error, something went wrong
327 if ( ! is_wp_error( $raw_data ) ) {
328
329 //If response code isn't 200, something went wrong
330 if ( 200 == $raw_data['response']['code'] ) {
331
332 $this->show_message( __('Data Parsing' ,'booking') , true );
333
334 //Attempt to convert the returned JSON into an array
335 $raw_data = json_decode( $raw_data['body'], true );
336 //debuge($raw_data);
337 //If decoding was successful
338 if ( ! empty( $raw_data ) ) {
339
340 //If there are some entries (events) to process
341 if ( isset( $raw_data['items'] ) ) {
342 //Loop through each event, extracting the relevant information
343 foreach ( $raw_data['items'] as $event ) {
344 //debuge($event);
345 $id = esc_html( $event['id'] );
346 $title = (isset($event['summary']))?esc_html( $event['summary'] ):'';
347 $description = (isset($event['description']))?esc_html( $event['description'] ):'';
348 //$link = esc_url( $event['link'][0]['href'] );
349 $location = (isset($event['location']))?esc_html( $event['location'] ):'';
350
351 if ( isset($event['creator']) && isset($event['creator']['email']) )
352 $event_author_email = esc_html( $event['creator']['email'] );
353 else $event_author_email = '';
354
355
356 if ( isset( $event['start'] ) && isset( $event['end'] ) )
357 list($range_dates, $range_time) = $this->getCommaSeparatedDates( $event['start'], $event['end'] );
358 else
359 continue; // Skip if we gave no dates
360
361 //debuge($range_dates, $range_time);
362 $bktype = $this->getResource();
363
364 //FixIn: 8.6.1.4
365 // if ( ( class_exists( 'wpdev_bk_biz_s' ) )
366 // && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
367 // && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
368 // && ( get_bk_option( 'booking_ics_import_append_checkout_day' ) !== 'Off' )
369 // ) {
370 if ( 'On' == get_bk_option( 'booking_ics_import_append_checkout_day' ) ) { //FixIn: 2.0.27.1 //FixIn: 9.5.4.1
371 // Add one additional day to .ics event (useful in some cases for bookings with change-over days),
372 // if the imported .ics dates is coming without check in/our times
373 // Later system is adding check in/out times from Booking Calendar to this event
374 $range_dates_arr = explode( ',', $range_dates );
375 $ics_event_check_out = trim( $range_dates_arr[ ( count( $range_dates_arr ) - 1 ) ] );
376 $ics_event_check_out = explode( '.', $ics_event_check_out );
377 $ics_event_check_out = $ics_event_check_out[2] . '-' . $ics_event_check_out[1] . '-' . $ics_event_check_out[0];
378
379 $ics_event_check_out = strtotime( $ics_event_check_out );
380 $ics_event_check_out = strtotime( '+1 day', $ics_event_check_out );
381 $ics_event_check_out = date_i18n( "d.m.Y", $ics_event_check_out );
382 $range_dates .= ', ' . $ics_event_check_out;
383 }
384
385 //FixIn: 8.6.1.1
386 if ( empty( $range_time ) ) {
387
388 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
389 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
390 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
391 ) { //FixIn: 2.0.5.1
392 //Add check in/out times to full day events
393 $wpbc_check_in = get_bk_option( 'booking_range_selection_start_time' );// . ':01'; // ' 14:00:01'
394 $wpbc_check_out = get_bk_option( 'booking_range_selection_end_time' ); // . ':02'; // ' 10:00:02';
395 $range_time = $wpbc_check_in . ' - ' . $wpbc_check_out;
396 $range_time = "selectbox-one^rangetime{$bktype}^{$range_time}~";
397 }
398 }
399 //debuge($range_dates, $range_time); // array( [0] => 07.08.2017, 08.08.2017, [1] => selectbox-one^rangetime4^03:00 - 07:00~ )
400
401 $previous_active_user = -1;
402 // MU
403 if ( class_exists('wpdev_bk_multiuser') ) {
404 // Get the owner of this booking resource
405 $user_bk_id = apply_bk_filter('get_user_of_this_bk_resource', false, $bktype );
406
407 // Check if its different user
408 if ( ($user_bk_id !== false) && ($this->getUserID() != $user_bk_id) ){
409 // Get possible other active user settings
410 $previous_active_user = apply_bk_filter('get_client_side_active_params_of_user');
411
412 // Set active user of that specific booking resource
413 make_bk_action('check_multiuser_params_for_client_side_by_user_id', $user_bk_id);
414
415 }
416 }
417
418 $booking_gcal_events_form_fields = get_bk_option( 'booking_gcal_events_form_fields');
419 if ( is_serialized( $booking_gcal_events_form_fields ) )
420 $booking_gcal_events_form_fields = unserialize( $booking_gcal_events_form_fields );
421
422 // MU
423 if ( $previous_active_user !== -1 ) {
424 // Reactivate the previous active user
425 make_bk_action('check_multiuser_params_for_client_side_by_user_id', $previous_active_user );
426 }
427
428
429
430 $booking_gcal_events_form_fields1 = explode('^', $booking_gcal_events_form_fields['title']);
431 $booking_gcal_events_form_fields1 = (empty($booking_gcal_events_form_fields1[1]))?false:true;
432
433 $booking_gcal_events_form_fields2 = explode('^', $booking_gcal_events_form_fields['description']);
434 $booking_gcal_events_form_fields2 = (empty($booking_gcal_events_form_fields2[1]))?false:true;
435
436 $booking_gcal_events_form_fields3 = explode('^', $booking_gcal_events_form_fields['where']);
437 $booking_gcal_events_form_fields3 = (empty($booking_gcal_events_form_fields3[1]))?false:true;
438
439
440 $submit_array = array(
441 'bktype' => $bktype
442 , 'dates' => $range_dates
443 , 'form' => $range_time
444 . (($booking_gcal_events_form_fields1)? trim($booking_gcal_events_form_fields['title'])."{$bktype}^{$title}~" :'')
445 . (($booking_gcal_events_form_fields2)? trim($booking_gcal_events_form_fields['description'])."{$bktype}^{$description}~" :'')
446 . (($booking_gcal_events_form_fields3)? trim($booking_gcal_events_form_fields['where'])."{$bktype}^{$location}" :'')
447
448 // . "text^".trim($booking_gcal_events_form_fields['title'])."{$bktype}^{$title}~"
449 // //. "text^secondname{$bktype}^{$title}~"
450 // //. "email^email{$bktype}^{$title}~"
451 // //."text^phone{$bktype}^{$title}~"
452 // ."textarea^".trim($booking_gcal_events_form_fields['description'])."{$bktype}^{$description}~"
453 // ."text^".trim($booking_gcal_events_form_fields['where'])."{$bktype}^{$location}"
454 , 'is_send_emeils' => $is_send_emeils
455 , 'sync_gid' => $id
456 );
457
458
459 // Add imported data to the array of events
460 $this->events[] = array(
461 // 'id'=>$booking_id
462 'sync_gid' => $id
463 , 'title'=> $title
464 , 'description' => $description
465 , 'location' => $location
466 , 'dates' => $range_dates
467 , 'times' => $range_time
468 , 'booking_submit_data'=>$submit_array
469 );
470 }
471 }
472
473 } else {
474 //json_decode failed
475 $this->error = __( 'Some data was retrieved, but could not be parsed successfully. Please ensure your feed URL is correct.', 'booking' );
476 }
477 } else {
478 //debuge($raw_data['response']['code']);
479 //The response code wasn't 200, so generate a helpful(ish) error message depending on error code
480 switch ( $raw_data['response']['code'] ) {
481 case 404:
482 $this->error = __( 'The feed could not be found (404). Please ensure your feed URL is correct.' ,'booking');
483 break;
484 case 403:
485 $this->error = __( 'Access to this feed was denied (403). Please ensure you have public sharing enabled for your calendar.' ,'booking');
486 break;
487 default:
488 $this->error = sprintf( __( 'The feed data could not be retrieved. Error code: %s. Please ensure your feed URL is correct.' ,'booking'), '<strong>' . $raw_data['response']['code'] . '</strong>' );
489
490 if (isset( $raw_data['body'] ))
491 $this->error .= '<br><br><strong>' . __( 'Info', 'booking' ) . '</strong>: <code>' . $raw_data['body'] . '</code>';
492
493 }
494 }
495 } else {
496
497 //Generate an error message from the returned WP_Error
498 $this->error = $raw_data->get_error_message() ;
499 }
500
501
502 if ( ! empty($this->error) ) {
503 $is_spin = false;
504 $is_error = true;
505 $this->show_message( $this->error , $is_spin, $is_error);
506 if ( ( isset( $_REQUEST['action'] ) ) && ( 'WPBC_AJX_BOOKING_ACTIONS' == $_REQUEST['action'] ) ) {
507 return false;
508 }
509 die;
510 } else
511 $this->show_message( __('Done' ,'booking') );
512
513
514
515 // Get Already exist same bookings
516 $exist_bookings_guid = $this->getExistBookings_gid( $this->events );
517
518
519 // Create New bookings
520 if (! empty($this->events) )
521 $this->createNewBookingsFromEvents( $exist_bookings_guid );
522
523
524 // Show imported Bookings Table
525 if ( (! empty($this->events) ) && ( ! $this->is_silent ) )
526 $this->showImportedEvents();
527
528
529 return true;
530 }
531
532
533 private function getCommaSeparatedDates( $event_dates_start, $event_dates_end ) {
534
535 if ( isset($event_dates_start['date']) && isset($event_dates_end['date']) ) {
536 //$start_date = date_i18n('Y-m-d', $this->iso_to_ts( $event_dates_start['date'] ) );
537 //$end_date = date_i18n('Y-m-d', ( $this->iso_to_ts( $event_dates_end['date'] ) - 86400 ) );
538 $start_date = $this->iso_to_ts( $event_dates_start['date'] ) ;
539 $end_date = $this->iso_to_ts( $event_dates_end['date'] ) - 86400 ;
540 $range_time = '00:00 - 00:00';
541 }
542
543 if ( isset($event_dates_start['dateTime']) && isset($event_dates_end['dateTime']) ) {
544
545
546 $start_date = $this->iso_to_ts( $event_dates_start['dateTime'] );
547 $end_date = $this->iso_to_ts( $event_dates_end['dateTime'] );
548
549 //FixIn: 8.9.4.2
550 /**
551 * Check for situation, when we are having times starting or ending at the midnight: 00:00:00
552 * Because in this "IF section" we are checking events dates with times (in the previous "IF section" was FULL day events,
553 * we need to be sure that the event do not start or end at 00:00 Otherwise Booking Calendar will
554 * show such date as fully booked!
555 */
556 $start_date_unix = $start_date;
557 $check_start_date = wpbc_datetime_localized( date( 'Y-m-d H:i:s', $start_date_unix ), 'Y-m-d H:i:s' );
558 if ( substr( $check_start_date, 11 ) === '00:00:00' ) {
559 $start_date_unix = $start_date_unix + 61;
560 $start_date = $start_date_unix;
561 }
562 $end_date_unix = $end_date;
563 $check_end_date = wpbc_datetime_localized( date( 'Y-m-d H:i:s', $end_date_unix ), 'Y-m-d H:i:s' );
564 if ( substr( $check_end_date, 11 ) === '00:00:00' ) {
565 $end_date_unix = $end_date_unix - 2;
566 $end_date = $end_date_unix;
567 }
568
569 $range_time = date_i18n('H:i', $start_date ) . ' - ' . date_i18n('H:i', $end_date );
570
571 //$start_date = date_i18n('Y-m-d', $start_date );
572 //$end_date = date_i18n('Y-m-d', $end_date );
573 }
574
575 $dates_comma = wpbc_get_comma_seprated_dates_from_to_day( date_i18n("d.m.Y", $start_date ), date_i18n("d.m.Y", $end_date ) ) ;
576 //$dates = wpbc_get_dates_array_from_start_end_days($start_date, $end_date );
577
578 //$dates_comma = implode(', ', $dates);
579
580
581 // // Get Times
582 // $start_time = $this->iso_to_ts( $event_dates[0]['startTime'] );
583 //
584 // if ( date_i18n('H:i', $this->iso_to_ts( $event_dates[ (count($event_dates)-1) ]['endTime'] ) ) == '00:00' )
585 // $end_time = $this->iso_to_ts( $event_dates[ (count($event_dates)-1) ]['endTime'] ) - 86400; // 24 hours - 60*60*24 = 86400
586 // else
587 // $end_time = $this->iso_to_ts( $event_dates[ (count($event_dates)-1) ]['endTime'] );
588 //
589 //
590 // $range_time = date_i18n('H:i', $start_time ) . ' - ' . date_i18n('H:i', $end_time );
591 if ( $range_time != '00:00 - 00:00' ) {
592 $bktype = $this->getResource();
593 $range_time = "selectbox-one^rangetime{$bktype}^{$range_time}~";
594 } else
595 $range_time = '';
596
597 return array($dates_comma, $range_time);
598 }
599
600
601 ////////////////////////////////////////////////////////////////////////////
602 // Create New bookings based on $this->events array.
603 ////////////////////////////////////////////////////////////////////////////
604 public function createNewBookingsFromEvents( $exist_bookings_guid = array() ) {
605
606 foreach ($this->events as $key => $event) {
607
608 if ( ! in_array( $event['sync_gid'], $exist_bookings_guid ) ) {
609
610 $submit_array = $event['booking_submit_data'];
611
612 // Create a new booking
613 $request_save_params = array(
614 'resource_id' => $submit_array['bktype'], // 2
615 'dates_ddmmyy_csv' => $submit_array['dates'], // '04.10.2023, 05.10.2023, 06.10.2023'
616 'form_data' => $submit_array['form'], // 'text^cost_hint2^150.00฿~selectbox-multiple^rangetime2[]^14:00...'
617 'booking_hash' => '', // 'sdfsf34534rf'
618 'custom_form' => '', // 'custom_form_name'
619 'is_emails_send' => $submit_array['is_send_emeils'], // 0 | 1
620 'is_show_payment_form' => 0, // 0 | 1
621 //'request_uri' => $_SERVER['HTTP_REFERER']
622 //'user_id' => wpbc_get_current_user_id()
623 'sync_gid' => $submit_array['sync_gid'],
624 'is_approve_booking' => intval( 'On' === get_bk_option( 'booking_auto_approve_bookings_when_import' ) ) // Auto approve booking if imported
625 );
626
627 if ( 'On' != get_bk_option( 'booking_condition_import_if_available' ) ) { //FixIn: 9.8.15.8 - 'Import only, if days are available'
628 $request_save_params['save_booking_even_if_unavailable'] = 1;
629 }
630
631 $booking_save_arr = wpbc_booking_save( $request_save_params );
632
633 if ( 'ok' === $booking_save_arr['ajx_data']['status'] ) { // Everything Cool :) - booking has been created
634 $booking_id = $booking_save_arr['booking_id'];
635 $this->events[$key]['id'] = $booking_id;
636
637 // Add notes to the booking relative source of imported booking
638 if ( class_exists( 'wpdev_bk_personal' ) ) {
639 $remark_text = '[' . wpbc_datetime_localized( date( 'Y-m-d H:i:s' ), 'Y-m-d H:i:s' ) . '] ' . sprintf( __( 'Imported from %s ', 'booking-manager' ), 'Google Calendar' );
640 $remark_text = str_replace( '%', '&#37;', $remark_text );
641 $remark_text = str_replace( array( '"', "'" ), '', $remark_text );
642 $remark_text = trim( $remark_text );
643 $is_append = true;
644 make_bk_action( 'wpdev_make_update_of_remark', $booking_id, $remark_text, $is_append );
645 }
646
647 } else { // Error
648 // Error message: $booking_save_arr['ajx_data']['ajx_after_action_message'];
649 $parsed_booking_data_arr = wpbc_get_parsed_booking_data_arr( $request_save_params['form_data'], $request_save_params['resource_id'], array( 'get' => 'value' ) );
650
651 $plain_form_data_show = wpbc_get__booking_form_data__show( $request_save_params['form_data'], $request_save_params['resource_id'], array( 'unknown_shortcodes_replace_by' => ' ... ' ) );
652
653 // Replace <p> | <br> to ' '
654 $plain_form_data_show = preg_replace( '/<(br|p)[\t\s]*[\/]?>/i', ' ', $plain_form_data_show );
655
656 // Replace \r \n \t
657 $plain_form_data_show = preg_replace( '/(\\r|\\n|\\t)/i', ' ', $plain_form_data_show );
658
659 $this->error .= '<div style="border:1px solid #ccc;padding:10px 1em;margin:1em 0;">'
660 . '<strong> Google Calendar ' . __( 'Event', 'booking' ) . '</strong> '. 'UID' . ': ' . $request_save_params['sync_gid']
661 . '<hr/>'
662 . ' <span style="color:#8b2122;">' . $booking_save_arr['ajx_data']['ajx_after_action_message'] . '</span>'
663 . '<hr/>'
664 . ' <strong>' . __( 'Resource', 'booking' ) . ' ID : ' . '</strong>' . $request_save_params['resource_id']
665 . ' <strong>' . __( 'Dates', 'booking' ) .': ' . '</strong>' . $request_save_params['dates_ddmmyy_csv']
666 . ( ( ! empty( $parsed_booking_data_arr['rangetime'] ) )
667 ? ( ' <strong>' . __( 'Times', 'booking' ) . ': ' . '</strong>' . $parsed_booking_data_arr['rangetime'] )
668 : ''
669 )
670 //. ' <strong>' . __( 'Data', 'booking' ) . ': ' . ' </strong>'
671 . $plain_form_data_show
672 . '</div>';
673
674 unset( $this->events[ $key ] );
675 }
676 } else {
677 unset($this->events[$key]);
678 }
679 }
680
681 return $this->events;
682 }
683
684
685 ////////////////////////////////////////////////////////////////////////////
686 // Get Already exist same bookings
687 ////////////////////////////////////////////////////////////////////////////
688 public function getExistBookings_gid( $events_array ) {
689
690 $sql_sync_gid = array();
691 foreach ($events_array as $event) {
692 $sql_sync_gid[] = $event['sync_gid'];
693 }
694 $sql_sync_gid= implode( "','",$sql_sync_gid );
695
696 $exist_bookings_guid = array();
697
698 $booking_condition_import_only_new = get_bk_option( 'booking_condition_import_only_new' ); //FixIn: 9.8.15.8
699 if ( ( ! empty( $sql_sync_gid ) ) && ( 'On' === $booking_condition_import_only_new ) ) {
700 global $wpdb;
701 $exist_bookings = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}booking WHERE sync_gid IN ('{$sql_sync_gid}') AND trash != 1" ); //FixIn: 9.8.15.8
702 foreach ($exist_bookings as $bk) {
703 $exist_bookings_guid[]=$bk->sync_gid;
704 }
705 }
706 return $exist_bookings_guid;
707 }
708
709
710 ////////////////////////////////////////////////////////////////////////////
711 // Show Table of imported Events
712 ////////////////////////////////////////////////////////////////////////////
713 public function showImportedEvents( ) {
714 ////////////////////////////////////////////////////////////////////////
715 ?>
716 <div id="gcal_imported_events<?php echo $this->getResource(); ?>" class="table-responsive">
717 <table style="width:99%;margin-top:45px;border:1px solid #ccc;"
718
719 class="resource_table0 booking_table0 table table-striped "
720 cellpadding="0" cellspacing="0">
721 <?php
722
723 if (function_exists ('wpbc_db__get_resource_title')) {
724 echo '<tr><td colspan="6" style="padding:5px 10px;font-style:italic;"> <h4>' , wpbc_get_resource_title( $this->getResource() ) , '</h4></td></tr>';
725 }
726
727 ?>
728 <?php // Headers ?>
729 <tr>
730 <th style="text-align:center;width:15px;"><input type="checkbox" onclick="javascript:jQuery('#gcal_imported_events<?php echo $this->getResource(); ?> .events_items').attr('checked', this.checked);" class="" id="events_items_all" name="events_items_all" /></th>
731 <th style="text-align:center;width:10px;height:35px;"> <?php _e('ID' ,'booking'); ?> </th>
732 <th style="text-align:center;height:35px;width:220px;" style="border-left: 1px solid #ccc;"> <?php _e('Title' ,'booking'); ?> </th>
733 <th style="text-align:center;"> <?php _e('Info' ,'booking'); ?> </th>
734 <th style="text-align:center;"> <?php _e('Dates' ,'booking'); ?> </th>
735 <th style="text-align:center;width:10px;height:35px;"> <?php _e('GID' ,'booking'); ?> </th>
736 </tr>
737 <?php
738 $alternative_color = '';
739 if (! empty($this->events))
740 foreach ($this->events as $bt) {
741
742 if ( $alternative_color == '')
743 $alternative_color = ' class="alternative_color" ';
744 else
745 $alternative_color = '';
746 ?>
747 <tr id="gcal_imported_events_id_<?php echo $bt['id']; ?>">
748 <td <?php echo $alternative_color; ?> style="border-left: 0;border-right: 1px solid #ccc;">
749 <!-- <span class="wpbc_mobile_legend"><?php _e('Selection' ,'booking'); ?>:</span>-->
750 <input type="checkbox" class="events_items" id="events_items_<?php echo $bt['id']; ?>" value="<?php echo $bt['id']; ?>" name="events_items_<?php echo $bt['id']; ?>" /></td>
751 <td style="border-right: 0;border-left: 1px solid #ccc;text-align: center;" <?php echo $alternative_color; ?> >
752 <!-- <span class="wpbc_mobile_legend"><?php _e('ID' ,'booking'); ?>:</span>-->
753 <?php echo $bt['id']; ?></td>
754 <td <?php echo $alternative_color; ?> style="border-right: 0;border-left: 1px solid #ccc;">
755 <!-- <span class="wpbc_mobile_legend"><?php _e('Title' ,'booking'); ?>:</span>-->
756 <span ><?php echo $bt['title']; ?></span>
757 </td>
758 <td style="border-right: 0;border-left: 1px solid #ccc;text-align: center;" <?php echo $alternative_color; ?> >
759 <!-- <span class="wpbc_mobile_legend"><?php _e('Info' ,'booking'); ?>:</span>-->
760 <span ><?php echo $bt['description']; ?></span><br/>
761 <?php
762 if (! empty($bt['location']) )
763 echo '<span >', __('Location:' ,'booking'), ': ' , $bt['location'], '</span>';
764 ?>
765 </td>
766
767 <td style="border-right: 0;border-left: 1px solid #ccc;text-align: center;" <?php echo $alternative_color; ?> >
768 <span class="wpbc-listing-collumn booking-dates field-dates field-booking-date">
769 <!-- <span class="wpbc_mobile_legend"><?php _e('Dates' ,'booking'); ?>:</span>-->
770 <div class="booking_dates_full" ><?php
771 $bt['dates'] = explode(', ', $bt['dates']);
772 foreach ($bt['dates'] as $keyd=>$valued) {
773
774 $valued = explode( '.', $valued );
775 $valued = wpbc_get_date_in_correct_format( sprintf("%04d-%02d-%02d" ,$valued[2], $valued[1], $valued[0] ) ) ;
776
777 $bt['dates'][$keyd] = '<a href="javascript:void(0)" class="field-booking-date">' . $valued[0] . '</a>';
778 }
779 $bt['dates'] = implode('<span class="date_tire">, </span>', $bt['dates']);
780 echo $bt['dates'];//date_i18n('d.m.Y H:i',$bt['start_time'] ), ' - ', date_i18n('d.m.Y H:i',$bt['end_time']); ?>
781 </div>
782 </span>
783 </td>
784 <td style="border-right: 0;border-left: 1px solid #ccc;text-align: center;" <?php echo $alternative_color; ?> >
785 <!-- <span class="wpbc_mobile_legend"><?php _e('GID' ,'booking'); ?>:</span>-->
786 <?php echo $bt['sync_gid']; ?></td>
787 </tr>
788 <?php } ?>
789
790 <tr class="wpbc_table_footer">
791 <td colspan="6" style="text-align: center;">
792 <a href="javascript:void(0)" class="button button-primary" style="float:none;margin:10px;"
793 onclick="javascript:location.reload();" ><?php _e('Reload page' ,'booking'); ?></a>
794 <a href="javascript:void(0)" class="button" style="float:none;margin:10px;"
795 onclick="javascript:jQuery('#gcal_imported_events<?php echo $this->getResource(); ?>').remove();" ><?php _e('Hide' ,'booking'); ?></a>
796 <a href="javascript:void(0)" class="button" style="float:none;margin:10px;"
797 onclick="javascript: if ( wpbc_are_you_sure('<?php echo esc_js(__('Do you really want to delete selected booking(s) ?' ,'booking')); ?>') ) {
798 //delete_booking(
799 trash__restore_booking( 1, <?php //FixIn: 7.0.1 ?>
800 get_selected_bookings_id_in_this_list('#gcal_imported_events<?php echo $this->getResource(); ?> .events_items', 13)
801 , <?php echo $this->getUserID(); ?>
802 , '<?php echo wpbc_get_maybe_reloaded_booking_locale(); ?>'
803 , 1
804 );
805 } "
806 ><?php _e('Delete selected booking(s)' ,'booking'); ?></i></a>
807
808 </td>
809 </tr>
810
811 </table>
812 </div>
813 <script type="text/javascript">
814 jQuery("#gcal_imported_events<?php echo $this->getResource(); ?>").insertAfter("#toolbar_booking_listing");
815 </script>
816 <?php
817 }
818
819 }
820