2019-11-27 23:43:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Engelsystem\Controllers\Admin\Schedule;
|
|
|
|
|
2023-02-02 22:53:51 +01:00
|
|
|
use Engelsystem\Controllers\NotificationType;
|
2022-07-31 19:17:44 +02:00
|
|
|
use Engelsystem\Helpers\Carbon;
|
2022-06-16 23:00:56 +02:00
|
|
|
use DateTimeInterface;
|
2019-11-27 23:43:21 +01:00
|
|
|
use Engelsystem\Controllers\BaseController;
|
2020-11-21 20:54:04 +01:00
|
|
|
use Engelsystem\Controllers\HasUserNotifications;
|
2019-11-27 23:43:21 +01:00
|
|
|
use Engelsystem\Helpers\Schedule\Event;
|
|
|
|
use Engelsystem\Helpers\Schedule\Room;
|
|
|
|
use Engelsystem\Helpers\Schedule\Schedule;
|
|
|
|
use Engelsystem\Helpers\Schedule\XmlParser;
|
2022-12-31 16:25:16 +01:00
|
|
|
use Engelsystem\Helpers\Uuid;
|
2019-11-27 23:43:21 +01:00
|
|
|
use Engelsystem\Http\Request;
|
|
|
|
use Engelsystem\Http\Response;
|
2020-09-06 23:50:36 +02:00
|
|
|
use Engelsystem\Models\Room as RoomModel;
|
2019-11-27 23:43:21 +01:00
|
|
|
use Engelsystem\Models\Shifts\Schedule as ScheduleUrl;
|
|
|
|
use Engelsystem\Models\Shifts\ScheduleShift;
|
2023-01-03 22:19:03 +01:00
|
|
|
use Engelsystem\Models\Shifts\Shift;
|
2022-11-08 00:33:42 +01:00
|
|
|
use Engelsystem\Models\Shifts\ShiftType;
|
2022-07-31 19:17:44 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
2019-11-27 23:43:21 +01:00
|
|
|
use ErrorException;
|
|
|
|
use GuzzleHttp\Client as GuzzleClient;
|
2022-12-21 12:06:58 +01:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
2019-11-27 23:43:21 +01:00
|
|
|
use Illuminate\Database\Connection as DatabaseConnection;
|
|
|
|
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
|
|
|
|
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
|
|
|
|
|
|
|
class ImportSchedule extends BaseController
|
|
|
|
{
|
2020-11-21 20:54:04 +01:00
|
|
|
use HasUserNotifications;
|
|
|
|
|
2019-11-27 23:43:21 +01:00
|
|
|
/** @var DatabaseConnection */
|
|
|
|
protected $db;
|
|
|
|
|
|
|
|
/** @var LoggerInterface */
|
|
|
|
protected $log;
|
|
|
|
|
2022-12-15 19:50:56 +01:00
|
|
|
protected array $permissions = [
|
2019-11-27 23:43:21 +01:00
|
|
|
'schedule.import',
|
|
|
|
];
|
|
|
|
|
|
|
|
/** @var XmlParser */
|
|
|
|
protected $parser;
|
|
|
|
|
|
|
|
/** @var Response */
|
|
|
|
protected $response;
|
|
|
|
|
|
|
|
/** @var SessionInterface */
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
/** @var string */
|
2020-04-07 15:08:49 +02:00
|
|
|
protected $url = '/admin/schedule';
|
2019-11-27 23:43:21 +01:00
|
|
|
|
|
|
|
/** @var GuzzleClient */
|
|
|
|
protected $guzzle;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
Response $response,
|
|
|
|
SessionInterface $session,
|
|
|
|
GuzzleClient $guzzle,
|
|
|
|
XmlParser $parser,
|
|
|
|
DatabaseConnection $db,
|
|
|
|
LoggerInterface $log
|
|
|
|
) {
|
|
|
|
$this->guzzle = $guzzle;
|
|
|
|
$this->parser = $parser;
|
|
|
|
$this->response = $response;
|
|
|
|
$this->session = $session;
|
|
|
|
$this->db = $db;
|
|
|
|
$this->log = $log;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index(): Response
|
|
|
|
{
|
|
|
|
return $this->response->withView(
|
|
|
|
'admin/schedule/index.twig',
|
|
|
|
[
|
2020-11-21 20:54:04 +01:00
|
|
|
'is_index' => true,
|
|
|
|
'schedules' => ScheduleUrl::all(),
|
2023-02-02 22:53:51 +01:00
|
|
|
]
|
2020-11-21 20:54:04 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit(Request $request): Response
|
|
|
|
{
|
2022-12-11 18:01:34 +01:00
|
|
|
$scheduleId = $request->getAttribute('schedule_id'); // optional
|
|
|
|
|
|
|
|
$schedule = ScheduleUrl::find($scheduleId);
|
2020-11-21 20:54:04 +01:00
|
|
|
|
|
|
|
return $this->response->withView(
|
|
|
|
'admin/schedule/edit.twig',
|
|
|
|
[
|
|
|
|
'schedule' => $schedule,
|
2022-11-08 00:33:42 +01:00
|
|
|
'shift_types' => ShiftType::all()->pluck('name', 'id'),
|
2023-02-02 22:53:51 +01:00
|
|
|
]
|
2020-11-21 20:54:04 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save(Request $request): Response
|
|
|
|
{
|
2022-12-11 18:01:34 +01:00
|
|
|
$scheduleId = $request->getAttribute('schedule_id'); // optional
|
|
|
|
|
2020-11-21 20:54:04 +01:00
|
|
|
/** @var ScheduleUrl $schedule */
|
2022-12-11 18:01:34 +01:00
|
|
|
$schedule = ScheduleUrl::findOrNew($scheduleId);
|
2020-11-21 20:54:04 +01:00
|
|
|
|
|
|
|
$data = $this->validate($request, [
|
|
|
|
'name' => 'required',
|
|
|
|
'url' => 'required',
|
|
|
|
'shift_type' => 'required|int',
|
2020-12-26 02:35:49 +01:00
|
|
|
'minutes_before' => 'int',
|
|
|
|
'minutes_after' => 'int',
|
2020-11-21 20:54:04 +01:00
|
|
|
]);
|
|
|
|
|
2022-11-08 00:33:42 +01:00
|
|
|
if (!ShiftType::find($data['shift_type'])) {
|
2020-11-21 20:54:04 +01:00
|
|
|
throw new ErrorException('schedule.import.invalid-shift-type');
|
|
|
|
}
|
|
|
|
|
|
|
|
$schedule->name = $data['name'];
|
|
|
|
$schedule->url = $data['url'];
|
|
|
|
$schedule->shift_type = $data['shift_type'];
|
|
|
|
$schedule->minutes_before = $data['minutes_before'];
|
|
|
|
$schedule->minutes_after = $data['minutes_after'];
|
|
|
|
|
|
|
|
$schedule->save();
|
|
|
|
|
|
|
|
$this->log->info(
|
|
|
|
'Schedule {name}: Url {url}, Shift Type {shift_type}, minutes before/after {before}/{after}',
|
|
|
|
[
|
|
|
|
'name' => $schedule->name,
|
|
|
|
'url' => $schedule->name,
|
|
|
|
'shift_type' => $schedule->shift_type,
|
|
|
|
'before' => $schedule->minutes_before,
|
|
|
|
'after' => $schedule->minutes_after,
|
2019-11-27 23:43:21 +01:00
|
|
|
]
|
|
|
|
);
|
2020-11-21 20:54:04 +01:00
|
|
|
|
|
|
|
$this->addNotification('schedule.edit.success');
|
|
|
|
|
|
|
|
return redirect('/admin/schedule/load/' . $schedule->id);
|
2019-11-27 23:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function loadSchedule(Request $request): Response
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
/**
|
|
|
|
* @var Event[] $newEvents
|
|
|
|
* @var Event[] $changeEvents
|
|
|
|
* @var Event[] $deleteEvents
|
|
|
|
* @var Room[] $newRooms
|
|
|
|
* @var int $shiftType
|
|
|
|
* @var ScheduleUrl $scheduleUrl
|
|
|
|
* @var Schedule $schedule
|
|
|
|
* @var int $minutesBefore
|
|
|
|
* @var int $minutesAfter
|
|
|
|
*/
|
|
|
|
list(
|
|
|
|
$newEvents,
|
|
|
|
$changeEvents,
|
|
|
|
$deleteEvents,
|
|
|
|
$newRooms,
|
2020-11-21 20:54:04 +01:00
|
|
|
,
|
2019-11-27 23:43:21 +01:00
|
|
|
$scheduleUrl,
|
2020-11-21 20:54:04 +01:00
|
|
|
$schedule
|
2019-11-27 23:43:21 +01:00
|
|
|
) = $this->getScheduleData($request);
|
|
|
|
} catch (ErrorException $e) {
|
2023-02-02 22:53:51 +01:00
|
|
|
$this->addNotification($e->getMessage(), NotificationType::ERROR);
|
2020-11-21 20:54:04 +01:00
|
|
|
return back();
|
2019-11-27 23:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response->withView(
|
|
|
|
'admin/schedule/load.twig',
|
|
|
|
[
|
2022-11-08 00:33:42 +01:00
|
|
|
'schedule_id' => $scheduleUrl->id,
|
|
|
|
'schedule' => $schedule,
|
|
|
|
'rooms' => [
|
2019-11-27 23:43:21 +01:00
|
|
|
'add' => $newRooms,
|
|
|
|
],
|
2022-11-08 00:33:42 +01:00
|
|
|
'shifts' => [
|
2019-11-27 23:43:21 +01:00
|
|
|
'add' => $newEvents,
|
|
|
|
'update' => $changeEvents,
|
|
|
|
'delete' => $deleteEvents,
|
|
|
|
],
|
2023-02-02 22:53:51 +01:00
|
|
|
]
|
2019-11-27 23:43:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function importSchedule(Request $request): Response
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
/**
|
|
|
|
* @var Event[] $newEvents
|
|
|
|
* @var Event[] $changeEvents
|
|
|
|
* @var Event[] $deleteEvents
|
|
|
|
* @var Room[] $newRooms
|
|
|
|
* @var int $shiftType
|
|
|
|
* @var ScheduleUrl $scheduleUrl
|
|
|
|
*/
|
|
|
|
list(
|
|
|
|
$newEvents,
|
|
|
|
$changeEvents,
|
|
|
|
$deleteEvents,
|
|
|
|
$newRooms,
|
|
|
|
$shiftType,
|
|
|
|
$scheduleUrl
|
|
|
|
) = $this->getScheduleData($request);
|
|
|
|
} catch (ErrorException $e) {
|
2023-02-02 22:53:51 +01:00
|
|
|
$this->addNotification($e->getMessage(), NotificationType::ERROR);
|
2020-11-21 20:54:04 +01:00
|
|
|
return back();
|
2019-11-27 23:43:21 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 20:54:04 +01:00
|
|
|
$this->log('Started schedule "{name}" import', ['name' => $scheduleUrl->name]);
|
2019-11-27 23:43:21 +01:00
|
|
|
|
|
|
|
foreach ($newRooms as $room) {
|
|
|
|
$this->createRoom($room);
|
|
|
|
}
|
|
|
|
|
|
|
|
$rooms = $this->getAllRooms();
|
|
|
|
foreach ($newEvents as $event) {
|
|
|
|
$this->createEvent(
|
|
|
|
$event,
|
2022-06-16 23:00:56 +02:00
|
|
|
$shiftType,
|
2019-11-27 23:43:21 +01:00
|
|
|
$rooms
|
|
|
|
->where('name', $event->getRoom()->getName())
|
|
|
|
->first(),
|
|
|
|
$scheduleUrl
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($changeEvents as $event) {
|
|
|
|
$this->updateEvent(
|
|
|
|
$event,
|
2022-06-16 23:00:56 +02:00
|
|
|
$shiftType,
|
2019-11-27 23:43:21 +01:00
|
|
|
$rooms
|
|
|
|
->where('name', $event->getRoom()->getName())
|
|
|
|
->first()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($deleteEvents as $event) {
|
2022-07-31 19:17:44 +02:00
|
|
|
$this->fireDeleteShiftEntryEvents($event);
|
2019-11-27 23:43:21 +01:00
|
|
|
$this->deleteEvent($event);
|
|
|
|
}
|
|
|
|
|
2020-11-21 20:54:04 +01:00
|
|
|
$scheduleUrl->touch();
|
|
|
|
$this->log('Ended schedule "{name}" import', ['name' => $scheduleUrl->name]);
|
2019-11-27 23:43:21 +01:00
|
|
|
|
2023-02-02 22:53:51 +01:00
|
|
|
$this->addNotification('schedule.import.success');
|
|
|
|
return redirect($this->url, 303);
|
2019-11-27 23:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function createRoom(Room $room): void
|
|
|
|
{
|
2020-09-06 23:50:36 +02:00
|
|
|
$roomModel = new RoomModel();
|
|
|
|
$roomModel->name = $room->getName();
|
|
|
|
$roomModel->save();
|
2019-11-27 23:43:21 +01:00
|
|
|
|
2023-10-13 11:53:13 +02:00
|
|
|
$this->log('Created schedule location "{location}"', ['location' => $room->getName()]);
|
2019-11-27 23:43:21 +01:00
|
|
|
}
|
|
|
|
|
2022-07-31 19:17:44 +02:00
|
|
|
protected function fireDeleteShiftEntryEvents(Event $event): void
|
|
|
|
{
|
|
|
|
$shiftEntries = $this->db
|
2023-01-18 13:02:11 +01:00
|
|
|
->table('shift_entries')
|
2022-07-31 19:17:44 +02:00
|
|
|
->select([
|
2023-01-03 22:19:03 +01:00
|
|
|
'shift_types.name', 'shifts.title', 'angel_types.name AS type', 'rooms.id AS room_id',
|
2023-02-05 18:03:00 +01:00
|
|
|
'shifts.start', 'shifts.end', 'shift_entries.user_id', 'shift_entries.freeloaded',
|
2022-07-31 19:17:44 +02:00
|
|
|
])
|
2023-01-18 13:02:11 +01:00
|
|
|
->join('shifts', 'shifts.id', 'shift_entries.shift_id')
|
2023-01-03 22:19:03 +01:00
|
|
|
->join('schedule_shift', 'shifts.id', 'schedule_shift.shift_id')
|
|
|
|
->join('rooms', 'rooms.id', 'shifts.room_id')
|
2023-01-18 13:02:11 +01:00
|
|
|
->join('angel_types', 'angel_types.id', 'shift_entries.angel_type_id')
|
2023-01-03 22:19:03 +01:00
|
|
|
->join('shift_types', 'shift_types.id', 'shifts.shift_type_id')
|
2022-07-31 19:17:44 +02:00
|
|
|
->where('schedule_shift.guid', $event->getGuid())
|
|
|
|
->get();
|
|
|
|
|
|
|
|
foreach ($shiftEntries as $shiftEntry) {
|
|
|
|
event('shift.entry.deleting', [
|
|
|
|
'user' => User::find($shiftEntry->user_id),
|
2023-01-03 22:19:03 +01:00
|
|
|
'start' => Carbon::make($shiftEntry->start),
|
|
|
|
'end' => Carbon::make($shiftEntry->end),
|
2022-07-31 19:17:44 +02:00
|
|
|
'name' => $shiftEntry->name,
|
|
|
|
'title' => $shiftEntry->title,
|
|
|
|
'type' => $shiftEntry->type,
|
|
|
|
'room' => RoomModel::find($shiftEntry->room_id),
|
|
|
|
'freeloaded' => $shiftEntry->freeloaded,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 22:19:03 +01:00
|
|
|
protected function createEvent(Event $event, int $shiftTypeId, RoomModel $room, ScheduleUrl $scheduleUrl): void
|
2019-11-27 23:43:21 +01:00
|
|
|
{
|
|
|
|
$user = auth()->user();
|
2023-01-03 22:19:03 +01:00
|
|
|
$eventTimeZone = Carbon::now()->timezone;
|
|
|
|
|
|
|
|
$shift = new Shift();
|
|
|
|
$shift->title = $event->getTitle();
|
|
|
|
$shift->shift_type_id = $shiftTypeId;
|
|
|
|
$shift->start = $event->getDate()->copy()->timezone($eventTimeZone);
|
|
|
|
$shift->end = $event->getEndDate()->copy()->timezone($eventTimeZone);
|
|
|
|
$shift->room()->associate($room);
|
|
|
|
$shift->url = $event->getUrl() ?? '';
|
2022-12-31 16:25:16 +01:00
|
|
|
$shift->transaction_id = Uuid::uuidBy($scheduleUrl->id, '5c4ed01e');
|
2023-01-03 22:19:03 +01:00
|
|
|
$shift->createdBy()->associate($user);
|
|
|
|
$shift->save();
|
|
|
|
|
|
|
|
$scheduleShift = new ScheduleShift(['guid' => $event->getGuid()]);
|
2019-11-27 23:43:21 +01:00
|
|
|
$scheduleShift->schedule()->associate($scheduleUrl);
|
2023-01-03 22:19:03 +01:00
|
|
|
$scheduleShift->shift()->associate($shift);
|
2019-11-27 23:43:21 +01:00
|
|
|
$scheduleShift->save();
|
|
|
|
|
|
|
|
$this->log(
|
2023-10-13 11:53:13 +02:00
|
|
|
'Created schedule shift "{shift}" in "{location}" ({from} {to}, {guid})',
|
2019-11-27 23:43:21 +01:00
|
|
|
[
|
2023-10-13 11:53:13 +02:00
|
|
|
'shift' => $shift->title,
|
|
|
|
'location' => $shift->room->name,
|
|
|
|
'from' => $shift->start->format(DateTimeInterface::RFC3339),
|
|
|
|
'to' => $shift->end->format(DateTimeInterface::RFC3339),
|
|
|
|
'guid' => $scheduleShift->guid,
|
2019-11-27 23:43:21 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-03 22:19:03 +01:00
|
|
|
protected function updateEvent(Event $event, int $shiftTypeId, RoomModel $room): void
|
2019-11-27 23:43:21 +01:00
|
|
|
{
|
|
|
|
$user = auth()->user();
|
2023-01-03 22:19:03 +01:00
|
|
|
$eventTimeZone = Carbon::now()->timezone;
|
|
|
|
|
|
|
|
/** @var ScheduleShift $scheduleShift */
|
|
|
|
$scheduleShift = ScheduleShift::whereGuid($event->getGuid())->first();
|
|
|
|
$shift = $scheduleShift->shift;
|
|
|
|
$shift->title = $event->getTitle();
|
|
|
|
$shift->shift_type_id = $shiftTypeId;
|
|
|
|
$shift->start = $event->getDate()->copy()->timezone($eventTimeZone);
|
|
|
|
$shift->end = $event->getEndDate()->copy()->timezone($eventTimeZone);
|
|
|
|
$shift->room()->associate($room);
|
|
|
|
$shift->url = $event->getUrl() ?? '';
|
|
|
|
$shift->updatedBy()->associate($user);
|
|
|
|
$shift->save();
|
2019-11-27 23:43:21 +01:00
|
|
|
|
|
|
|
$this->log(
|
2023-10-13 11:53:13 +02:00
|
|
|
'Updated schedule shift "{shift}" in "{location}" ({from} {to}, {guid})',
|
2019-11-27 23:43:21 +01:00
|
|
|
[
|
2023-10-13 11:53:13 +02:00
|
|
|
'shift' => $shift->title,
|
|
|
|
'location' => $shift->room->name,
|
|
|
|
'from' => $shift->start->format(DateTimeInterface::RFC3339),
|
|
|
|
'to' => $shift->end->format(DateTimeInterface::RFC3339),
|
|
|
|
'guid' => $scheduleShift->guid,
|
2019-11-27 23:43:21 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-03 22:19:03 +01:00
|
|
|
protected function deleteEvent(Event $event): void
|
2019-11-27 23:43:21 +01:00
|
|
|
{
|
2023-01-03 22:19:03 +01:00
|
|
|
/** @var ScheduleShift $scheduleShift */
|
|
|
|
$scheduleShift = ScheduleShift::whereGuid($event->getGuid())->first();
|
|
|
|
$shift = $scheduleShift->shift;
|
|
|
|
$shift->delete();
|
2019-11-27 23:43:21 +01:00
|
|
|
|
|
|
|
$this->log(
|
2023-10-13 11:53:13 +02:00
|
|
|
'Deleted schedule shift "{shift}" in {location} ({from} {to}, {guid})',
|
2019-11-27 23:43:21 +01:00
|
|
|
[
|
2023-10-13 11:53:13 +02:00
|
|
|
'shift' => $shift->title,
|
|
|
|
'location' => $shift->room->name,
|
|
|
|
'from' => $shift->start->format(DateTimeInterface::RFC3339),
|
|
|
|
'to' => $shift->end->format(DateTimeInterface::RFC3339),
|
|
|
|
'guid' => $scheduleShift->guid,
|
2019-11-27 23:43:21 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2022-06-16 23:00:56 +02:00
|
|
|
* @return Event[]|Room[]|RoomModel[]
|
2019-11-27 23:43:21 +01:00
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
|
|
|
protected function getScheduleData(Request $request)
|
|
|
|
{
|
2022-12-25 11:59:45 +01:00
|
|
|
$scheduleId = (int) $request->getAttribute('schedule_id');
|
2022-12-11 18:01:34 +01:00
|
|
|
|
2020-11-21 20:54:04 +01:00
|
|
|
/** @var ScheduleUrl $scheduleUrl */
|
2022-12-11 18:01:34 +01:00
|
|
|
$scheduleUrl = ScheduleUrl::findOrFail($scheduleId);
|
2019-11-27 23:43:21 +01:00
|
|
|
|
2022-12-21 12:06:58 +01:00
|
|
|
try {
|
|
|
|
$scheduleResponse = $this->guzzle->get($scheduleUrl->url);
|
|
|
|
} catch (ConnectException $e) {
|
|
|
|
throw new ErrorException('schedule.import.request-error');
|
|
|
|
}
|
|
|
|
|
2019-11-27 23:43:21 +01:00
|
|
|
if ($scheduleResponse->getStatusCode() != 200) {
|
|
|
|
throw new ErrorException('schedule.import.request-error');
|
|
|
|
}
|
|
|
|
|
2022-12-25 11:59:45 +01:00
|
|
|
$scheduleData = (string) $scheduleResponse->getBody();
|
2019-11-27 23:43:21 +01:00
|
|
|
if (!$this->parser->load($scheduleData)) {
|
|
|
|
throw new ErrorException('schedule.import.read-error');
|
|
|
|
}
|
|
|
|
|
2020-11-21 20:54:04 +01:00
|
|
|
$shiftType = $scheduleUrl->shift_type;
|
2019-11-27 23:43:21 +01:00
|
|
|
$schedule = $this->parser->getSchedule();
|
2020-11-21 20:54:04 +01:00
|
|
|
$minutesBefore = $scheduleUrl->minutes_before;
|
|
|
|
$minutesAfter = $scheduleUrl->minutes_after;
|
2019-11-27 23:43:21 +01:00
|
|
|
$newRooms = $this->newRooms($schedule->getRooms());
|
|
|
|
return array_merge(
|
|
|
|
$this->shiftsDiff($schedule, $scheduleUrl, $shiftType, $minutesBefore, $minutesAfter),
|
|
|
|
[$newRooms, $shiftType, $scheduleUrl, $schedule, $minutesBefore, $minutesAfter]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Room[] $scheduleRooms
|
|
|
|
* @return Room[]
|
|
|
|
*/
|
|
|
|
protected function newRooms(array $scheduleRooms): array
|
|
|
|
{
|
|
|
|
$newRooms = [];
|
|
|
|
$allRooms = $this->getAllRooms();
|
|
|
|
|
|
|
|
foreach ($scheduleRooms as $room) {
|
|
|
|
if ($allRooms->where('name', $room->getName())->count()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newRooms[] = $room;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $newRooms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Schedule $schedule
|
|
|
|
* @param ScheduleUrl $scheduleUrl
|
|
|
|
* @param int $shiftType
|
|
|
|
* @param int $minutesBefore
|
|
|
|
* @param int $minutesAfter
|
|
|
|
* @return Event[]
|
|
|
|
*/
|
|
|
|
protected function shiftsDiff(
|
|
|
|
Schedule $schedule,
|
|
|
|
ScheduleUrl $scheduleUrl,
|
|
|
|
int $shiftType,
|
|
|
|
int $minutesBefore,
|
|
|
|
int $minutesAfter
|
|
|
|
): array {
|
|
|
|
/** @var Event[] $newEvents */
|
|
|
|
$newEvents = [];
|
|
|
|
/** @var Event[] $changeEvents */
|
|
|
|
$changeEvents = [];
|
|
|
|
/** @var Event[] $scheduleEvents */
|
|
|
|
$scheduleEvents = [];
|
|
|
|
/** @var Event[] $deleteEvents */
|
|
|
|
$deleteEvents = [];
|
|
|
|
$rooms = $this->getAllRooms();
|
2023-01-03 22:19:03 +01:00
|
|
|
$eventTimeZone = Carbon::now()->timezone;
|
2019-11-27 23:43:21 +01:00
|
|
|
|
|
|
|
foreach ($schedule->getDay() as $day) {
|
|
|
|
foreach ($day->getRoom() as $room) {
|
|
|
|
foreach ($room->getEvent() as $event) {
|
|
|
|
$scheduleEvents[$event->getGuid()] = $event;
|
|
|
|
|
2023-01-03 22:19:03 +01:00
|
|
|
$event->getDate()->timezone($eventTimeZone)->subMinutes($minutesBefore);
|
|
|
|
$event->getEndDate()->timezone($eventTimeZone)->addMinutes($minutesAfter);
|
|
|
|
$event->setTitle(
|
|
|
|
$event->getLanguage()
|
|
|
|
? sprintf('%s [%s]', $event->getTitle(), $event->getLanguage())
|
|
|
|
: $event->getTitle()
|
|
|
|
);
|
2019-11-27 23:43:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scheduleEventsGuidList = array_keys($scheduleEvents);
|
|
|
|
$existingShifts = $this->getScheduleShiftsByGuid($scheduleUrl, $scheduleEventsGuidList);
|
|
|
|
foreach ($existingShifts as $shift) {
|
|
|
|
$guid = $shift->guid;
|
2023-01-03 22:19:03 +01:00
|
|
|
/** @var Shift $shift */
|
|
|
|
$shift = Shift::with('room')->find($shift->shift_id);
|
2019-11-27 23:43:21 +01:00
|
|
|
$event = $scheduleEvents[$guid];
|
2019-12-01 22:10:10 +01:00
|
|
|
$room = $rooms->where('name', $event->getRoom()->getName())->first();
|
2019-11-27 23:43:21 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
$shift->title != $event->getTitle()
|
|
|
|
|| $shift->shift_type_id != $shiftType
|
2023-01-03 22:19:03 +01:00
|
|
|
|| $shift->start != $event->getDate()
|
|
|
|
|| $shift->end != $event->getEndDate()
|
2019-12-01 22:10:10 +01:00
|
|
|
|| $shift->room_id != ($room->id ?? '')
|
2023-01-03 22:19:03 +01:00
|
|
|
|| $shift->url != ($event->getUrl() ?? '')
|
2019-11-27 23:43:21 +01:00
|
|
|
) {
|
|
|
|
$changeEvents[$guid] = $event;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($scheduleEvents[$guid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($scheduleEvents as $scheduleEvent) {
|
|
|
|
$newEvents[$scheduleEvent->getGuid()] = $scheduleEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scheduleShifts = $this->getScheduleShiftsWhereNotGuid($scheduleUrl, $scheduleEventsGuidList);
|
|
|
|
foreach ($scheduleShifts as $shift) {
|
|
|
|
$event = $this->eventFromScheduleShift($shift);
|
|
|
|
$deleteEvents[$event->getGuid()] = $event;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$newEvents, $changeEvents, $deleteEvents];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function eventFromScheduleShift(ScheduleShift $scheduleShift): Event
|
|
|
|
{
|
2023-01-03 22:19:03 +01:00
|
|
|
/** @var Shift $shift */
|
|
|
|
$shift = Shift::with('room')->find($scheduleShift->shift_id);
|
|
|
|
$duration = $shift->start->diff($shift->end);
|
2019-11-27 23:43:21 +01:00
|
|
|
|
2022-06-16 23:00:56 +02:00
|
|
|
return new Event(
|
2019-11-27 23:43:21 +01:00
|
|
|
$scheduleShift->guid,
|
|
|
|
0,
|
2023-01-03 22:19:03 +01:00
|
|
|
new Room($shift->room->name),
|
2019-11-27 23:43:21 +01:00
|
|
|
$shift->title,
|
|
|
|
'',
|
|
|
|
'n/a',
|
2023-01-03 22:19:03 +01:00
|
|
|
$shift->start,
|
|
|
|
$shift->start->format('H:i'),
|
2019-11-27 23:43:21 +01:00
|
|
|
$duration->format('%H:%I'),
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 23:50:36 +02:00
|
|
|
* @return RoomModel[]|Collection
|
2019-11-27 23:43:21 +01:00
|
|
|
*/
|
|
|
|
protected function getAllRooms(): Collection
|
|
|
|
{
|
2020-09-06 23:50:36 +02:00
|
|
|
return RoomModel::all();
|
2019-11-27 23:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ScheduleUrl $scheduleUrl
|
|
|
|
* @param string[] $events
|
2020-04-07 15:08:49 +02:00
|
|
|
* @return QueryBuilder[]|DatabaseCollection|Collection|ScheduleShift[]
|
2019-11-27 23:43:21 +01:00
|
|
|
*/
|
|
|
|
protected function getScheduleShiftsByGuid(ScheduleUrl $scheduleUrl, array $events)
|
|
|
|
{
|
|
|
|
return ScheduleShift::query()
|
|
|
|
->whereIn('guid', $events)
|
|
|
|
->where('schedule_id', $scheduleUrl->id)
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ScheduleUrl $scheduleUrl
|
|
|
|
* @param string[] $events
|
2020-04-07 15:08:49 +02:00
|
|
|
* @return QueryBuilder[]|DatabaseCollection|Collection|ScheduleShift[]
|
2019-11-27 23:43:21 +01:00
|
|
|
*/
|
|
|
|
protected function getScheduleShiftsWhereNotGuid(ScheduleUrl $scheduleUrl, array $events)
|
|
|
|
{
|
|
|
|
return ScheduleShift::query()
|
|
|
|
->whereNotIn('guid', $events)
|
|
|
|
->where('schedule_id', $scheduleUrl->id)
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
|
|
|
*/
|
|
|
|
protected function log(string $message, array $context = []): void
|
|
|
|
{
|
|
|
|
$this->log->info($message, $context);
|
|
|
|
}
|
|
|
|
}
|