| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Services; |
| 4 |
|
| 5 |
use Booktics\Abstracts\Booktics_Database; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Schedule_Record |
| 9 |
* Handles schedule record operations and data formatting. |
| 10 |
*/ |
| 11 |
class Schedule_Record { |
| 12 |
/** |
| 13 |
* Formats time string to 12-hour format with AM/PM. |
| 14 |
* |
| 15 |
* @param string $time |
| 16 |
* |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
private function format_time( $time ) { |
| 20 |
return date_i18n( 'h:i A', strtotime( get_date_from_gmt( $time ) ) ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Retrieves and formats schedules for a team member or service. |
| 25 |
* |
| 26 |
* @param int $id |
| 27 |
* @param string $type |
| 28 |
* |
| 29 |
* @return array |
| 30 |
*/ |
| 31 |
public function get_schedules( $id, $type = 'team_member' ) { |
| 32 |
$database = new Booktics_Database(); |
| 33 |
if ( $type === 'default' ) { |
| 34 |
$where = array( |
| 35 |
'team_member_id' => 0, |
| 36 |
'service_id' => 0, |
| 37 |
); |
| 38 |
} elseif ( $type === 'team_member' ) { |
| 39 |
$where = array( 'team_member_id' => $id ); |
| 40 |
} else { |
| 41 |
$where = array( 'service_id' => $id ); |
| 42 |
} |
| 43 |
|
| 44 |
$schedules = $database->select( 'booktics_schedules', $where ); |
| 45 |
|
| 46 |
$formatted = array( |
| 47 |
'schedule' => array(), |
| 48 |
'custom' => array(), |
| 49 |
'holiday' => array(), |
| 50 |
); |
| 51 |
|
| 52 |
if ( ! is_array( $schedules ) ) { |
| 53 |
return $formatted; |
| 54 |
} |
| 55 |
|
| 56 |
foreach ( $schedules as $schedule ) { |
| 57 |
if ( $schedule->custom_date == '0' ) { |
| 58 |
$day = strtolower( array_search( $schedule->week_day, Schedule_Manager::$day_map ) ); |
| 59 |
if ( $schedule->start_time == '0' && $schedule->end_time == '0' ) { |
| 60 |
continue; |
| 61 |
} |
| 62 |
$formatted['schedule'][ $day ][] = array( |
| 63 |
'start_time' => $this->format_time( $schedule->start_time ), |
| 64 |
'end_time' => $this->format_time( $schedule->end_time ), |
| 65 |
); |
| 66 |
} elseif ( $schedule->start_time == '0' && $schedule->end_time == '0' ) { |
| 67 |
$formatted['holiday'][] = $schedule->custom_date; |
| 68 |
} else { |
| 69 |
$formatted['custom'][] = array( |
| 70 |
$schedule->custom_date, |
| 71 |
$schedule->custom_date, |
| 72 |
array( |
| 73 |
'start_time' => $schedule->start_time, |
| 74 |
'end_time' => $schedule->end_time, |
| 75 |
), |
| 76 |
); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$formatted['holiday'] = $this->to_ranges( $formatted['holiday'] ); |
| 81 |
|
| 82 |
return $formatted; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Convert single dates into date ranges |
| 87 |
* |
| 88 |
* @param array $dates Array of individual dates |
| 89 |
* |
| 90 |
* @return array Array of date ranges [['start_date', 'end_date']] |
| 91 |
*/ |
| 92 |
public function to_ranges( $dates ) { |
| 93 |
if ( empty( $dates ) ) { |
| 94 |
return array(); |
| 95 |
} |
| 96 |
|
| 97 |
// Remove duplicates and sort dates |
| 98 |
$dates = array_unique( $dates ); |
| 99 |
sort( $dates ); |
| 100 |
|
| 101 |
$ranges = array(); |
| 102 |
$start = $dates[0]; |
| 103 |
$prev = $dates[0]; |
| 104 |
|
| 105 |
for ( $i = 1; $i <= count( $dates ); $i++ ) { |
| 106 |
$current = $dates[ $i ] ?? null; |
| 107 |
|
| 108 |
// If we've reached the end or found a gap in dates |
| 109 |
if ( $current === null || strtotime( $current ) > strtotime( '+1 day', strtotime( $prev ) ) ) { |
| 110 |
// Add the range only if start and end are different or it's the last single date |
| 111 |
$ranges[] = array( $start, $prev ); |
| 112 |
if ( $current !== null ) { |
| 113 |
$start = $current; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if ( $current !== null ) { |
| 118 |
$prev = $current; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $ranges; |
| 123 |
} |
| 124 |
} |
| 125 |
|