/** * Find array of time slots available for booking * for given date. * * @access private * @param string $date * @return array */ private function _findAvailableTime($date) { $result = array(); $dayofweek = date('w', strtotime($date)) + 1; // 1-7 $is_date_today = $date == date('Y-m-d', current_time('timestamp')); $current_time = date('H:i:s', ceil(current_time('timestamp') / AB_BookingConfiguration::getTimeSlotLength()) * AB_BookingConfiguration::getTimeSlotLength()); foreach ($this->staff_working_hours as $staff_id => $hours) { if (isset($hours[$dayofweek]) && $this->isWorkingDay($date, $staff_id)) { // Find intersection between working and requested hours //(excluding time slots in the past). $working_start_time = $is_date_today && $current_time > $hours[$dayofweek]['start_time'] ? $current_time : $hours[$dayofweek]['start_time']; $intersection = $this->_findIntersection($this->_strToTime($working_start_time), $this->_strToTime($hours[$dayofweek]['end_time']), $this->_strToTime($this->_userData->getRequestedTimeFrom()), $this->_strToTime($this->_userData->getRequestedTimeTo())); if (is_array($intersection) && !array_key_exists('start', $intersection)) { $intersections = $intersection; foreach ($intersections as $intersection) { if ($intersection && $this->service_duration <= $intersection['end'] - $intersection['start']) { // Initialize time frames. $timeframes = array(array('start' => $intersection['start'], 'end' => $intersection['end'], 'staff_id' => $staff_id)); // Remove breaks from the time frames. foreach ($hours[$dayofweek]['breaks'] as $break) { $timeframes = $this->_removeTimePeriod($timeframes, $this->_strToTime($break['start']), $this->_strToTime($break['end'])); } // Remove bookings from the time frames. $bookings = isset($this->bookings[$staff_id]) ? $this->bookings[$staff_id] : array(); foreach ($bookings as $booking) { $bookingStart = new DateTime($booking->start_date); if ($date == $bookingStart->format('Y-m-d')) { $bookingEnd = new DateTime($booking->end_date); $booking_start = $bookingStart->format('U') % (24 * 60 * 60); $booking_end = $bookingEnd->format('U') % (24 * 60 * 60); $timeframes = $this->_removeTimePeriod($timeframes, $booking_start, $booking_end); } } $result = array_merge($result, $timeframes); } } } else { if ($intersection && $this->service_duration <= $intersection['end'] - $intersection['start']) { // Initialize time frames. $timeframes = array(array('start' => $intersection['start'], 'end' => $intersection['end'], 'staff_id' => $staff_id)); // Remove breaks from the time frames. foreach ($hours[$dayofweek]['breaks'] as $break) { $timeframes = $this->_removeTimePeriod($timeframes, $this->_strToTime($break['start']), $this->_strToTime($break['end'])); } // Remove bookings from the time frames. $bookings = isset($this->bookings[$staff_id]) ? $this->bookings[$staff_id] : array(); foreach ($bookings as $booking) { $bookingStart = new DateTime($booking->start_date); if ($date == $bookingStart->format('Y-m-d')) { $bookingEnd = new DateTime($booking->end_date); $booking_start = $bookingStart->format('U') % (24 * 60 * 60); $booking_end = $bookingEnd->format('U') % (24 * 60 * 60); $timeframes = $this->_removeTimePeriod($timeframes, $booking_start, $booking_end); } } $result = array_merge($result, $timeframes); } } } } usort($result, create_function('$a, $b', 'return $a[\'start\'] - $b[\'start\'];')); return $result; }