Add script for importing schedules

2022-03-24
Luca 3 years ago
parent a788bc9cde
commit b1cb122b86

@ -0,0 +1,32 @@
#!/usr/bin/env php
<?php
use Engelsystem\Controllers\Admin\Schedule\ImportSchedule;
use ErrorException;
use stdClass;
require_once __DIR__ . '/../includes/application.php';
$script = array_shift($argv);
if (count($argv) === 0) {
echo "usage: $script '*'|SCHEDULE_ID [SCHEDULE_ID...]" . PHP_EOL;
exit;
}
$app = app();
$scheduleIds = $argv;
if ($argv[0] === '*') {
$db = $app->get('database');
$scheduleIds = array_map(fn(stdClass $schedule) => $schedule->id, $db->select('SELECT id FROM schedules');
}
$importer = $app->get(ImportSchedule::class);
foreach ($scheduleIds as $id) {
try {
$importer->doImport($id);
} catch (ErrorException $e) {
echo "import '$id' failed:" . PHP_EOL . $e->getMessage() . PHP_EOL;
}
}

@ -169,6 +169,8 @@ class ImportSchedule extends BaseController
public function loadSchedule(Request $request): Response
{
try {
$id = $request->getAttribute('id');
/**
* @var Event[] $newEvents
* @var Event[] $changeEvents
@ -188,7 +190,7 @@ class ImportSchedule extends BaseController
,
$scheduleUrl,
$schedule
) = $this->getScheduleData($request);
) = $this->getScheduleData($id);
} catch (ErrorException $e) {
$this->addNotification($e->getMessage(), 'errors');
return back();
@ -219,27 +221,41 @@ class ImportSchedule extends BaseController
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);
$id = $request->getAttribute('id');
$this->doImport($id);
} catch (ErrorException $e) {
$this->addNotification($e->getMessage(), 'errors');
return back();
}
return redirect($this->url, 303)
->with('messages', ['schedule.import.success']);
}
/**
* @param string $id
*
* @throws ErrorException
*/
public function doImport(string $id): void
{
/**
* @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($id);
$this->log('Started schedule "{name}" import', ['name' => $scheduleUrl->name]);
foreach ($newRooms as $room) {
@ -274,9 +290,6 @@ class ImportSchedule extends BaseController
$scheduleUrl->touch();
$this->log('Ended schedule "{name}" import', ['name' => $scheduleUrl->name]);
return redirect($this->url, 303)
->with('messages', ['schedule.import.success']);
}
/**
@ -311,7 +324,7 @@ class ImportSchedule extends BaseController
'end' => $shift->getEndDate()->unix(),
'RID' => $room->id,
'URL' => $shift->getUrl(),
'created_by_user_id' => $user->id,
'created_by_user_id' => $user ? $user->id : null,
'created_at_timestamp' => time(),
'edited_by_user_id' => null,
'edited_at_timestamp' => 0,
@ -357,7 +370,7 @@ class ImportSchedule extends BaseController
'end' => $shift->getEndDate()->unix(),
'RID' => $room->id,
'URL' => $shift->getUrl(),
'edited_by_user_id' => $user->id,
'edited_by_user_id' => $user ? $user->id : null,
'edited_at_timestamp' => time(),
]
);
@ -397,13 +410,12 @@ class ImportSchedule extends BaseController
}
/**
* @param Request $request
* @param string $id
* @return Event[]|Room[]|RoomModel[]|ScheduleUrl|Schedule|string
* @throws ErrorException
*/
protected function getScheduleData(Request $request)
protected function getScheduleData(string $id)
{
$id = $request->getAttribute('id');
/** @var ScheduleUrl $scheduleUrl */
$scheduleUrl = ScheduleUrl::findOrFail($id);

Loading…
Cancel
Save