2019-11-27 23:43:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Engelsystem\Controllers\Admin\Schedule;
|
|
|
|
|
|
|
|
use Carbon\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;
|
|
|
|
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;
|
|
|
|
use ErrorException;
|
|
|
|
use GuzzleHttp\Client as GuzzleClient;
|
|
|
|
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 stdClass;
|
|
|
|
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;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $permissions = [
|
|
|
|
'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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Response $response
|
|
|
|
* @param SessionInterface $session
|
|
|
|
* @param GuzzleClient $guzzle
|
|
|
|
* @param XmlParser $parser
|
|
|
|
* @param DatabaseConnection $db
|
|
|
|
* @param LoggerInterface $log
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
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(),
|
|
|
|
] + $this->getNotifications()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit(Request $request): Response
|
|
|
|
{
|
|
|
|
$schedule = ScheduleUrl::find($request->getAttribute('id'));
|
|
|
|
|
|
|
|
return $this->response->withView(
|
|
|
|
'admin/schedule/edit.twig',
|
|
|
|
[
|
|
|
|
'schedule' => $schedule,
|
2019-11-27 23:43:21 +01:00
|
|
|
'shift_types' => $this->getShiftTypes(),
|
2020-11-21 20:54:04 +01:00
|
|
|
] + $this->getNotifications()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function save(Request $request): Response
|
|
|
|
{
|
|
|
|
$id = $request->getAttribute('id');
|
|
|
|
/** @var ScheduleUrl $schedule */
|
|
|
|
$schedule = ScheduleUrl::findOrNew($id);
|
|
|
|
|
|
|
|
$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
|
|
|
]);
|
|
|
|
|
|
|
|
if (!isset($this->getShiftTypes()[$data['shift_type']])) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
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) {
|
2020-11-21 20:54:04 +01:00
|
|
|
$this->addNotification($e->getMessage(), 'errors');
|
|
|
|
return back();
|
2019-11-27 23:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response->withView(
|
|
|
|
'admin/schedule/load.twig',
|
|
|
|
[
|
2020-11-21 20:54:04 +01:00
|
|
|
'schedule_id' => $scheduleUrl->id,
|
2019-11-27 23:43:21 +01:00
|
|
|
'schedule' => $schedule,
|
|
|
|
'rooms' => [
|
|
|
|
'add' => $newRooms,
|
|
|
|
],
|
|
|
|
'shifts' => [
|
|
|
|
'add' => $newEvents,
|
|
|
|
'update' => $changeEvents,
|
|
|
|
'delete' => $deleteEvents,
|
|
|
|
],
|
2020-11-21 20:54:04 +01:00
|
|
|
] + $this->getNotifications()
|
2019-11-27 23:43:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
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) {
|
2020-11-21 20:54:04 +01:00
|
|
|
$this->addNotification($e->getMessage(), 'errors');
|
|
|
|
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) {
|
|
|
|
$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
|
|
|
|
|
|
|
return redirect($this->url, 303)
|
2020-11-21 20:54:04 +01:00
|
|
|
->with('messages', ['schedule.import.success']);
|
2019-11-27 23:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Room $room
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
$this->log('Created schedule room "{room}"', ['room' => $room->getName()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Event $shift
|
|
|
|
* @param int $shiftTypeId
|
2020-09-06 23:50:36 +02:00
|
|
|
* @param RoomModel $room
|
2019-11-27 23:43:21 +01:00
|
|
|
* @param ScheduleUrl $scheduleUrl
|
|
|
|
*/
|
2020-09-06 23:50:36 +02:00
|
|
|
protected function createEvent(Event $shift, int $shiftTypeId, RoomModel $room, ScheduleUrl $scheduleUrl): void
|
2019-11-27 23:43:21 +01:00
|
|
|
{
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
$this->db
|
|
|
|
->table('Shifts')
|
|
|
|
->insert(
|
|
|
|
[
|
|
|
|
'title' => $shift->getTitle(),
|
|
|
|
'shifttype_id' => $shiftTypeId,
|
|
|
|
'start' => $shift->getDate()->unix(),
|
|
|
|
'end' => $shift->getEndDate()->unix(),
|
|
|
|
'RID' => $room->id,
|
|
|
|
'URL' => $shift->getUrl(),
|
|
|
|
'created_by_user_id' => $user->id,
|
|
|
|
'created_at_timestamp' => time(),
|
|
|
|
'edited_by_user_id' => null,
|
|
|
|
'edited_at_timestamp' => 0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$shiftId = $this->db->getDoctrineConnection()->lastInsertId();
|
|
|
|
|
|
|
|
$scheduleShift = new ScheduleShift(['shift_id' => $shiftId, 'guid' => $shift->getGuid()]);
|
|
|
|
$scheduleShift->schedule()->associate($scheduleUrl);
|
|
|
|
$scheduleShift->save();
|
|
|
|
|
|
|
|
$this->log(
|
|
|
|
'Created schedule shift "{shift}" in "{room}" ({from} {to}, {guid})',
|
|
|
|
[
|
|
|
|
'shift' => $shift->getTitle(),
|
|
|
|
'room' => $room->name,
|
2022-06-16 23:00:56 +02:00
|
|
|
'from' => $shift->getDate()->format(DateTimeInterface::RFC3339),
|
|
|
|
'to' => $shift->getEndDate()->format(DateTimeInterface::RFC3339),
|
2019-11-27 23:43:21 +01:00
|
|
|
'guid' => $shift->getGuid(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 23:50:36 +02:00
|
|
|
* @param Event $shift
|
|
|
|
* @param int $shiftTypeId
|
|
|
|
* @param RoomModel $room
|
2019-11-27 23:43:21 +01:00
|
|
|
*/
|
2020-09-06 23:50:36 +02:00
|
|
|
protected function updateEvent(Event $shift, int $shiftTypeId, RoomModel $room): void
|
2019-11-27 23:43:21 +01:00
|
|
|
{
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
$this->db
|
|
|
|
->table('Shifts')
|
|
|
|
->join('schedule_shift', 'Shifts.SID', 'schedule_shift.shift_id')
|
|
|
|
->where('schedule_shift.guid', $shift->getGuid())
|
|
|
|
->update(
|
|
|
|
[
|
|
|
|
'title' => $shift->getTitle(),
|
|
|
|
'shifttype_id' => $shiftTypeId,
|
|
|
|
'start' => $shift->getDate()->unix(),
|
|
|
|
'end' => $shift->getEndDate()->unix(),
|
|
|
|
'RID' => $room->id,
|
|
|
|
'URL' => $shift->getUrl(),
|
|
|
|
'edited_by_user_id' => $user->id,
|
|
|
|
'edited_at_timestamp' => time(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->log(
|
|
|
|
'Updated schedule shift "{shift}" in "{room}" ({from} {to}, {guid})',
|
|
|
|
[
|
|
|
|
'shift' => $shift->getTitle(),
|
|
|
|
'room' => $room->name,
|
2022-06-16 23:00:56 +02:00
|
|
|
'from' => $shift->getDate()->format(DateTimeInterface::RFC3339),
|
|
|
|
'to' => $shift->getEndDate()->format(DateTimeInterface::RFC3339),
|
2019-11-27 23:43:21 +01:00
|
|
|
'guid' => $shift->getGuid(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Event $shift
|
|
|
|
*/
|
|
|
|
protected function deleteEvent(Event $shift): void
|
|
|
|
{
|
|
|
|
$this->db
|
|
|
|
->table('Shifts')
|
|
|
|
->join('schedule_shift', 'Shifts.SID', 'schedule_shift.shift_id')
|
|
|
|
->where('schedule_shift.guid', $shift->getGuid())
|
|
|
|
->delete();
|
|
|
|
|
|
|
|
$this->log(
|
|
|
|
'Deleted schedule shift "{shift}" ({from} {to}, {guid})',
|
|
|
|
[
|
|
|
|
'shift' => $shift->getTitle(),
|
2022-06-16 23:00:56 +02:00
|
|
|
'from' => $shift->getDate()->format(DateTimeInterface::RFC3339),
|
|
|
|
'to' => $shift->getEndDate()->format(DateTimeInterface::RFC3339),
|
2019-11-27 23:43:21 +01:00
|
|
|
'guid' => $shift->getGuid(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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)
|
|
|
|
{
|
2020-11-21 20:54:04 +01:00
|
|
|
$id = $request->getAttribute('id');
|
|
|
|
/** @var ScheduleUrl $scheduleUrl */
|
|
|
|
$scheduleUrl = ScheduleUrl::findOrFail($id);
|
2019-11-27 23:43:21 +01:00
|
|
|
|
2020-11-21 20:54:04 +01:00
|
|
|
$scheduleResponse = $this->guzzle->get($scheduleUrl->url);
|
2019-11-27 23:43:21 +01:00
|
|
|
if ($scheduleResponse->getStatusCode() != 200) {
|
|
|
|
throw new ErrorException('schedule.import.request-error');
|
|
|
|
}
|
|
|
|
|
|
|
|
$scheduleData = (string)$scheduleResponse->getBody();
|
|
|
|
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();
|
|
|
|
|
|
|
|
foreach ($schedule->getDay() as $day) {
|
|
|
|
foreach ($day->getRoom() as $room) {
|
|
|
|
foreach ($room->getEvent() as $event) {
|
|
|
|
$scheduleEvents[$event->getGuid()] = $event;
|
|
|
|
|
|
|
|
$event->getDate()->subMinutes($minutesBefore);
|
|
|
|
$event->getEndDate()->addMinutes($minutesAfter);
|
2019-11-30 01:15:39 +01:00
|
|
|
$event->setTitle(sprintf('%s [%s]', $event->getTitle(), $event->getLanguage()));
|
2019-11-27 23:43:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scheduleEventsGuidList = array_keys($scheduleEvents);
|
|
|
|
$existingShifts = $this->getScheduleShiftsByGuid($scheduleUrl, $scheduleEventsGuidList);
|
|
|
|
foreach ($existingShifts as $shift) {
|
|
|
|
$guid = $shift->guid;
|
|
|
|
$shift = $this->loadShift($shift->shift_id);
|
|
|
|
$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
|
|
|
|
|| Carbon::createFromTimestamp($shift->start) != $event->getDate()
|
|
|
|
|| Carbon::createFromTimestamp($shift->end) != $event->getEndDate()
|
2019-12-01 22:10:10 +01:00
|
|
|
|| $shift->room_id != ($room->id ?? '')
|
2019-11-27 23:43:21 +01:00
|
|
|
|| $shift->url != $event->getUrl()
|
|
|
|
) {
|
|
|
|
$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];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ScheduleShift $scheduleShift
|
|
|
|
* @return Event
|
|
|
|
*/
|
|
|
|
protected function eventFromScheduleShift(ScheduleShift $scheduleShift): Event
|
|
|
|
{
|
|
|
|
$shift = $this->loadShift($scheduleShift->shift_id);
|
|
|
|
$start = Carbon::createFromTimestamp($shift->start);
|
|
|
|
$end = Carbon::createFromTimestamp($shift->end);
|
|
|
|
$duration = $start->diff($end);
|
|
|
|
|
2022-06-16 23:00:56 +02:00
|
|
|
return new Event(
|
2019-11-27 23:43:21 +01:00
|
|
|
$scheduleShift->guid,
|
|
|
|
0,
|
|
|
|
new Room($shift->room_name),
|
|
|
|
$shift->title,
|
|
|
|
'',
|
|
|
|
'n/a',
|
|
|
|
Carbon::createFromTimestamp($shift->start),
|
|
|
|
$start->format('H:i'),
|
|
|
|
$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 $id
|
|
|
|
* @return stdClass|null
|
|
|
|
*/
|
|
|
|
protected function loadShift($id): ?stdClass
|
|
|
|
{
|
|
|
|
return $this->db->selectOne(
|
|
|
|
'
|
2020-05-13 18:26:32 +02:00
|
|
|
SELECT
|
|
|
|
s.SID AS id,
|
|
|
|
s.title,
|
|
|
|
s.start,
|
|
|
|
s.end,
|
|
|
|
s.shifttype_id AS shift_type_id,
|
|
|
|
s.RID AS room_id,
|
|
|
|
r.Name AS room_name,
|
|
|
|
s.URL as url
|
|
|
|
FROM Shifts AS s
|
2020-09-06 23:50:36 +02:00
|
|
|
LEFT JOIN rooms r on s.RID = r.id
|
2020-05-13 18:26:32 +02:00
|
|
|
WHERE SID = ?
|
|
|
|
',
|
2019-11-27 23:43:21 +01:00
|
|
|
[$id]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
protected function getShiftTypes()
|
|
|
|
{
|
|
|
|
$return = [];
|
|
|
|
/** @var stdClass[] $shiftTypes */
|
|
|
|
$shiftTypes = $this->db->select('SELECT t.id, t.name FROM ShiftTypes AS t');
|
|
|
|
|
|
|
|
foreach ($shiftTypes as $shiftType) {
|
|
|
|
$return[$shiftType->id] = $shiftType->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
|
|
|
*/
|
|
|
|
protected function log(string $message, array $context = []): void
|
|
|
|
{
|
|
|
|
$this->log->info($message, $context);
|
|
|
|
}
|
|
|
|
}
|