/** * Get data for WeekCalendar in `week` mode. * * @return json */ public function executeWeekStaffAppointments() { $result = array('events' => array(), 'freebusys' => array()); $staff_id = $this->getParameter('staff_id'); if ($staff_id) { $staff = new AB_Staff(); $staff->load($staff_id); $start_date = $this->getParameter('start_date'); $end_date = $this->getParameter('end_date'); $staff_appointments = $staff->getAppointments($start_date, $end_date); foreach ($staff_appointments as $appointment) { $result['events'][] = $this->getAppointment($appointment); } $wpdb = $this->getWpdb(); $schedule = $wpdb->get_results($wpdb->prepare('SELECT ssi.* FROM `ab_staff_schedule_item` ssi WHERE ssi.staff_id = %d', $staff_id)); $holidays = $wpdb->get_results($wpdb->prepare('SELECT * FROM ab_holiday WHERE staff_id = %d OR staff_id IS NULL', $staff_id)); if (!empty($schedule)) { $wp_week_start_day = get_option('start_of_week', 1); $schedule_start_day = $schedule[0]->id - 1; // if wp week start day is higher than our // cut the list into 2 parts (before and after wp wp week start day) // move the second part of the list above the first one if ($wp_week_start_day > $schedule_start_day) { $schedule_start = array_slice($schedule, 0, $wp_week_start_day); $schedule_end = array_slice($schedule, $wp_week_start_day); $schedule = $schedule_end; foreach ($schedule_start as $schedule_item) { $schedule[] = $schedule_item; } } $active_schedule_items_ids = array(); foreach ($schedule as $item) { // if start time is NULL we consider that the day is "OFF" if (null !== $item->start_time) { $day_name = AB_DateUtils::getWeekDayByNumber($item->day_index - 1); if ($day_name == 'Sunday' && $wp_week_start_day == 0) { $date = date('Y-m-d', strtotime($day_name . ' last week', strtotime($start_date))); } else { $date = date('Y-m-d', strtotime($day_name . ' this week', strtotime($start_date))); } $startDate = new DateTime($date . ' ' . $item->start_time); $endDate = new DateTime($date . ' ' . $item->end_time); // Skip holidays foreach ($holidays as $holiday) { $holidayDate = new DateTime($holiday->holiday); if ($holiday->repeat_event) { if ($holidayDate->format('m-d') == $startDate->format('m-d')) { continue 2; } } else { if ($holidayDate->format('Y-m-d') == $startDate->format('Y-m-d')) { continue 2; } } } // get available day parts $result['freebusys'][] = $this->getFreeBusy($startDate, $endDate, true); $active_schedule_items_ids[] = $item->id; } } if (empty($active_schedule_items_ids)) { $active_schedule_items_ids = array(0); } $schedule_breaks = $wpdb->get_results('SELECT sib.*, ssi.day_index AS "day_index" FROM `ab_schedule_item_break` sib LEFT JOIN `ab_staff_schedule_item` ssi ON sib.staff_schedule_item_id = ssi.id WHERE sib.staff_schedule_item_id IN (' . implode(', ', $active_schedule_items_ids) . ')'); foreach ($schedule_breaks as $break_item) { $day_name = AB_DateUtils::getWeekDayByNumber($break_item->day_index - 1); $date = date('Y-m-d', strtotime($day_name . ' this week', strtotime($start_date))); $startDate = new DateTime($date . ' ' . $break_item->start_time); $endDate = new DateTime($date . ' ' . $break_item->end_time); // get breaks $result['freebusys'][] = $this->getFreeBusy($startDate, $endDate, false); } } } echo json_encode($result); exit; }