Exemplo n.º 1
0
 /**
  * Constructor
  */
 public function __construct()
 {
     date_default_timezone_set(AB_Utils::getTimezoneString());
     wp_load_translations_early();
     $now = new DateTime();
     $this->mysql_now = $now->format('Y-m-d H:i:s');
     $this->sms = new AB_SMS();
     $this->sms_authorized = $this->sms->loadProfile();
     $query = AB_Notification::query()->where('active', 1)->whereIn('type', array('staff_agenda', 'client_follow_up', 'client_reminder'));
     foreach ($query->find() as $notification) {
         $this->processNotification($notification);
     }
 }
Exemplo n.º 2
0
 /**
  * Returns a collection of Google calendar events
  *
  * @param DateTime $startDate
  * @return array|false
  */
 public function getCalendarEvents(DateTime $startDate)
 {
     // get all events from calendar, without timeMin filter (the end of the event can be later then the start of searched time period)
     $result = array();
     try {
         $calendar_access = $this->getCalendarAccess();
         $time_slot_length = AB_BookingConfiguration::getTimeSlotLength();
         $limit_events = get_option('ab_settings_google_limit_events');
         $timeMin = clone $startDate;
         $timeMin = $timeMin->modify('-1 day')->format(DateTime::RFC3339);
         $events = $this->service->events->listEvents($this->getCalendarID(), array('singleEvents' => true, 'orderBy' => 'startTime', 'timeMin' => $timeMin, 'maxResults' => $limit_events ?: self::EVENTS_PER_REQUEST));
         while (true) {
             foreach ($events->getItems() as $event) {
                 /** @var Google_Service_Calendar_Event $event */
                 if ($event->getStatus() !== 'cancelled') {
                     // Skip events created by Bookly in non freeBusyReader calendar.
                     if ($calendar_access != 'freeBusyReader') {
                         $ext_properties = $event->getExtendedProperties();
                         if ($ext_properties !== null) {
                             $private = $ext_properties->private;
                             if ($private !== null && array_key_exists('service_id', $private)) {
                                 continue;
                             }
                         }
                     }
                     // Get start/end dates of event and transform them into WP timezone (Google doesn't transform whole day events into our timezone).
                     $event_start = $event->getStart();
                     $event_end = $event->getEnd();
                     if ($event_start->dateTime == null) {
                         // All day event.
                         $eventStartDate = new DateTime($event_start->date, new DateTimeZone($this->getCalendarTimezone()));
                         $eventEndDate = new DateTime($event_end->date, new DateTimeZone($this->getCalendarTimezone()));
                     } else {
                         // Regular event.
                         $eventStartDate = new DateTime($event_start->dateTime);
                         $eventEndDate = new DateTime($event_end->dateTime);
                     }
                     $eventStartDate->setTimezone(new DateTimeZone(AB_Utils::getTimezoneString()));
                     $eventEndDate->setTimezone(new DateTimeZone(AB_Utils::getTimezoneString()));
                     // Check if event intersects with out datetime interval.
                     if ($eventEndDate > $startDate) {
                         // If event lasts for more than 1 day, then divide it.
                         for ($loop_start = $eventStartDate; $loop_start < $eventEndDate; $loop_start->modify('+1 day')->setTime(0, 0, 0)) {
                             // Start date.
                             if ($loop_start->format('Ymd') > $eventStartDate->format('Ymd')) {
                                 $start_date = $loop_start->format('Y-m-d 00:00:00');
                             } else {
                                 $start_date = $loop_start->format('Y-m-d H:i:s');
                             }
                             // End date.
                             if ($loop_start->format('Ymd') < $eventEndDate->format('Ymd')) {
                                 $end_date = '10_symbols 24:00:00';
                             } else {
                                 $loop_end = clone $eventEndDate;
                                 $end_date = $loop_end->getTimestamp();
                                 // Round end time to a multiple of the default time slot length.
                                 $extra = $end_date % $time_slot_length;
                                 if ($extra) {
                                     $end_date += $time_slot_length - $extra;
                                 }
                                 $loop_end->setTimestamp($end_date);
                                 $end_date = $loop_end->format('Y-m-d H:i:s');
                                 if (substr($end_date, 11) == '00:00:00') {
                                     // Set time to 24:00:00 (date part does not matter, it just needs to be 10 characters length).
                                     $end_date = '10_symbols 24:00:00';
                                 }
                             }
                             $result[] = array('staff_id' => $this->staff->get('id'), 'start_date' => $start_date, 'end_date' => $end_date, 'number_of_bookings' => 1, 'from_google' => true);
                         }
                     }
                 }
             }
             if (!$limit_events && $events->getNextPageToken()) {
                 $events = $this->service->events->listEvents($this->getCalendarID(), array('singleEvents' => true, 'orderBy' => 'startTime', 'timeMin' => $timeMin, 'pageToken' => $events->getNextPageToken()));
             } else {
                 break;
             }
         }
         return $result;
     } catch (Exception $e) {
         $this->errors[] = $e->getMessage();
     }
     return false;
 }