PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.28
Booktics – Appointment Booking Calendar for Service Businesses v1.0.28
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / core / team-member / services / team-member-availability.php

team-member-availability.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.28, at core/team-member/services/team-member-availability.php

145 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics\Team_Member\Services;
4
5 use Booktics\Abstracts\Booktics_Database;
6 use Booktics\Services\Schedule_Record;
7 use DateTime;
8 use Exception;
9
10 class Team_Member_Availability {
11 /**
12 * Store team member id
13 *
14 * @var string|int
15 */
16 private $team_member;
17
18 /**
19 * Booktics database object
20 *
21 * @var Booktics_Database
22 */
23 private $db;
24
25 /**
26 * Booktics schedule table name
27 *
28 * @var string
29 */
30 private $table_name;
31
32 /**
33 * Init team member and db
34 *
35 * @param $team_member
36 */
37 public function __construct( $team_member ) {
38 $this->team_member = $team_member->to_array();
39 $this->db = new Booktics_Database();
40 $this->table_name = booktics_get_table_name( 'booktics_schedules' );
41 }
42
43 /**
44 * @param $start_date
45 * @param $days
46 *
47 * @return array
48 * @throws Exception
49 */
50 public function get_weekly_schedule( $start_date, $days = 7 ): array {
51 // If team member status is pending, return empty array
52 if ( $this->team_member['status'] === 'pending' ) {
53 return array();
54 }
55
56 $results = array();
57 $date = new DateTime( $start_date );
58
59 for ( $i = 0; $i < $days; $i++ ) {
60 $date_str = $date->format( 'Y-m-d' );
61 $status = $this->get_status( $date_str );
62 $results[] = array(
63 'date' => $date_str,
64 'day' => $date->format( 'D' ),
65 'status' => $status,
66 );
67 $date->modify( '+1 day' );
68 }
69
70 return $results;
71 }
72
73 /**
74 * Get status for a given date
75 *
76 * @param string $date Date in Y-m-d format
77 *
78 * @return string
79 */
80 public function get_status( $date ): string {
81 // If team member status is pending, return 'pending'
82 if ( $this->team_member['status'] === 'pending' ) {
83 return 'pending';
84 }
85
86 if ( ! $this->team_member['enable_custom_availability'] ) {
87 return $this->get_default_workday_status( $date );
88 }
89
90 // Get all schedules for the team member
91 $schedules = (new Schedule_Record())->get_schedules( $this->team_member['id'] );
92
93 // If no schedules exist, it's a holiday
94 if ( empty( $schedules ) || ! isset( $schedules['schedule'] ) ) {
95 return 'holiday';
96 }
97
98 $date_obj = new DateTime( $date );
99 $day_name = strtolower( $date_obj->format( 'D' ) );
100 $date_str = $date_obj->format( 'Y-m-d' );
101
102 // Check regular schedule
103 if ( ! empty( $schedules['schedule'][ $day_name ] ) ) {
104 $day_slots = $schedules['schedule'][ $day_name ];
105 // If there are time slots, it's a working day
106 if ( ! empty( $day_slots ) ) {
107 return 'working';
108 }
109 }
110
111 // Check holidays
112 if ( ! empty( $schedules['holiday'] ) ) {
113 foreach ( $schedules['holiday'] as $holiday_date => $holiday_data ) {
114 if ( $holiday_date === $date_str ) {
115 return 'holiday';
116 }
117 }
118 }
119
120 // If no schedule is found for this day, it's a holiday
121 return 'holiday';
122 }
123
124 private function get_default_workday_status( $date ) {
125 $date_obj = new DateTime( $date );
126 $day_name = strtolower( $date_obj->format( 'D' ) );
127 $availability = booktics_get_option( 'schedule' ) ?? array();
128
129 // Ensure availability is always an array
130 if ( ! is_array( $availability ) ) {
131 $availability = array();
132 }
133
134 // schedule keys in settings are default working days from settings
135 $default_working_days = array_keys( $availability );
136
137 if ( ! in_array( $day_name, $default_working_days ) ) {
138 return 'holiday';
139 }
140
141 return 'working';
142 }
143
144 }
145