| 1 |
<?php |
| 2 |
namespace Timetics\Core\Staffs\Calendars; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
use Timetics\Core\Bookings\Booking; |
| 7 |
|
| 8 |
/** |
| 9 |
* Calendar sync with timetics booking |
| 10 |
*/ |
| 11 |
class CalendarSyncFacade { |
| 12 |
/** |
| 13 |
* Store staff id |
| 14 |
* |
| 15 |
* @var integer |
| 16 |
*/ |
| 17 |
private static $staff_id; |
| 18 |
|
| 19 |
/** |
| 20 |
* Sync booking |
| 21 |
* |
| 22 |
* @param string $teamMemberId [$teamMemberId description] |
| 23 |
* @param array $bookingData [$bookingData description] |
| 24 |
* |
| 25 |
* @return bool [return description] |
| 26 |
*/ |
| 27 |
public static function syncBooking(string $teamMemberId, array $bookingData): bool { |
| 28 |
$services = CalendarSyncFactory::getSyncServices( $teamMemberId ); |
| 29 |
$status = true; |
| 30 |
|
| 31 |
foreach ( $services as $service ) { |
| 32 |
$status = $status && $service->syncToCalendar( $bookingData ); |
| 33 |
} |
| 34 |
|
| 35 |
return $status; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Fetch booking with calendar services |
| 40 |
* |
| 41 |
* @param string $teamMemberId |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public static function fetchBookings( string $teamMemberId ): array { |
| 46 |
$services = CalendarSyncFactory::getSyncServices($teamMemberId); |
| 47 |
$allBookings = []; |
| 48 |
self::$staff_id = $teamMemberId; |
| 49 |
|
| 50 |
foreach ( $services as $service ) { |
| 51 |
$allBookings = array_merge( $allBookings, $service->fetchBookings( $teamMemberId ) ); |
| 52 |
|
| 53 |
self::create_bookings( $allBookings ); |
| 54 |
} |
| 55 |
|
| 56 |
return $allBookings; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Create bookings with calendar events |
| 61 |
* |
| 62 |
* @param array $bookings Booking list from calendar |
| 63 |
* |
| 64 |
* @return bool |
| 65 |
*/ |
| 66 |
private static function create_bookings( $bookings ) { |
| 67 |
if ( $bookings ) { |
| 68 |
foreach ( $bookings as $booking_data ) { |
| 69 |
$booking = new Booking(); |
| 70 |
$booking->set_props([ |
| 71 |
'staff' => self::$staff_id, |
| 72 |
'start_date' => $booking_data['start_date'], |
| 73 |
'start_time' => $booking_data['start_time'], |
| 74 |
'end_date' => $booking_data['end_date'], |
| 75 |
'end_time' => $booking_data['end_time'], |
| 76 |
'description' => $booking_data['description'], |
| 77 |
'post_status' => 'pending' |
| 78 |
]); |
| 79 |
$booking->save(); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|