summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2019-11-27 23:43:21 +0100
committerIgor Scheller <igor.scheller@igorshp.de>2019-12-08 02:20:48 +0100
commit42721e95726559b4a601240bb5b0fe4e5d755b2a (patch)
tree6810e05f845ca787acc1d02fa82d3df15cd0ef9b /includes
parent377b390c97afb9106fd9a139819d00306f996f24 (diff)
Added Schedule parsing and replaced old Fahrplan importer
Resolves #553 (Change Frab Import from xCal to XML) Resolves #538 (Feature Request: Multi Frab Import)
Diffstat (limited to 'includes')
-rw-r--r--includes/includes.php3
-rw-r--r--includes/model/Room_model.php30
-rw-r--r--includes/model/Shifts_model.php75
-rw-r--r--includes/model/Stats.php18
-rw-r--r--includes/pages/admin_import.php478
-rw-r--r--includes/pages/admin_rooms.php11
-rw-r--r--includes/pages/admin_shifts.php1
-rw-r--r--includes/pages/schedule/ImportSchedule.php612
-rw-r--r--includes/sys_menu.php38
-rw-r--r--includes/sys_template.php36
10 files changed, 677 insertions, 625 deletions
diff --git a/includes/includes.php b/includes/includes.php
index 601a6ca2..50e98fe5 100644
--- a/includes/includes.php
+++ b/includes/includes.php
@@ -69,7 +69,6 @@ $includeFiles = [
__DIR__ . '/../includes/pages/admin_arrive.php',
__DIR__ . '/../includes/pages/admin_free.php',
__DIR__ . '/../includes/pages/admin_groups.php',
- __DIR__ . '/../includes/pages/admin_import.php',
__DIR__ . '/../includes/pages/admin_log.php',
__DIR__ . '/../includes/pages/admin_questions.php',
__DIR__ . '/../includes/pages/admin_rooms.php',
@@ -82,6 +81,8 @@ $includeFiles = [
__DIR__ . '/../includes/pages/user_questions.php',
__DIR__ . '/../includes/pages/user_settings.php',
__DIR__ . '/../includes/pages/user_shifts.php',
+
+ __DIR__ . '/../includes/pages/schedule/ImportSchedule.php',
];
foreach ($includeFiles as $file) {
diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php
index b29f68fe..f9eaf31d 100644
--- a/includes/model/Room_model.php
+++ b/includes/model/Room_model.php
@@ -62,35 +62,20 @@ function Room_delete($room_id)
}
/**
- * Delete a room by its name
- *
- * @param string $name
- */
-function Room_delete_by_name($name)
-{
- DB::delete('DELETE FROM `Room` WHERE `Name` = ?', [
- $name
- ]);
- engelsystem_log('Room deleted: ' . $name);
-}
-
-/**
* Create a new room
*
* @param string $name Name of the room
- * @param boolean $from_frab Is this a frab imported room?
* @param string $map_url URL to a map tha can be displayed in an iframe
* @param string description Markdown description
* @return false|int
*/
-function Room_create($name, $from_frab, $map_url, $description)
+function Room_create($name, $map_url, $description)
{
DB::insert('
- INSERT INTO `Room` (`Name`, `from_frab`, `map_url`, `description`)
- VALUES (?, ?, ?, ?)
+ INSERT INTO `Room` (`Name`, `map_url`, `description`)
+ VALUES (?, ?, ?)
', [
$name,
- (int)$from_frab,
$map_url,
$description
]);
@@ -98,7 +83,6 @@ function Room_create($name, $from_frab, $map_url, $description)
engelsystem_log(
'Room created: ' . $name
- . ', frab import: ' . ($from_frab ? 'Yes' : '')
. ', map_url: ' . $map_url
. ', description: ' . $description
);
@@ -107,28 +91,25 @@ function Room_create($name, $from_frab, $map_url, $description)
}
/**
- * update a room
+ * Update a room
*
* @param int $room_id The rooms id
* @param string $name Name of the room
- * @param boolean $from_frab Is this a frab imported room?
* @param string $map_url URL to a map tha can be displayed in an iframe
* @param string $description Markdown description
* @return int
*/
-function Room_update($room_id, $name, $from_frab, $map_url, $description)
+function Room_update($room_id, $name, $map_url, $description)
{
$result = DB::update('
UPDATE `Room`
SET
`Name`=?,
- `from_frab`=?,
`map_url`=?,
`description`=?
WHERE `RID`=?
LIMIT 1', [
$name,
- (int)$from_frab,
$map_url,
$description,
$room_id
@@ -136,7 +117,6 @@ function Room_update($room_id, $name, $from_frab, $map_url, $description)
engelsystem_log(
'Room updated: ' . $name .
- ', frab import: ' . ($from_frab ? 'Yes' : '') .
', map_url: ' . $map_url .
', description: ' . $description
);
diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php
index 01295fbc..fc88908d 100644
--- a/includes/model/Shifts_model.php
+++ b/includes/model/Shifts_model.php
@@ -14,17 +14,19 @@ function Shifts_by_angeltype($angeltype)
return DB::select('
SELECT DISTINCT `Shifts`.* FROM `Shifts`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id` = `Shifts`.`SID`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `NeededAngelTypes`.`angel_type_id` = ?
AND `NeededAngelTypes`.`count` > 0
- AND `Shifts`.`PSID` IS NULL
+ AND s.shift_id IS NULL
UNION
SELECT DISTINCT `Shifts`.* FROM `Shifts`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id` = `Shifts`.`RID`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `NeededAngelTypes`.`angel_type_id` = ?
AND `NeededAngelTypes`.`count` > 0
- AND NOT `Shifts`.`PSID` IS NULL
+ AND NOT s.shift_id IS NULL
', [$angeltype['id'], $angeltype['id']]);
}
@@ -41,19 +43,21 @@ function Shifts_free($start, $end)
SELECT * FROM (
SELECT *
FROM `Shifts`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE (`end` > ? AND `start` < ?)
AND (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
> (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
- AND `Shifts`.`PSID` IS NULL
+ AND s.shift_id IS NULL
UNION
SELECT *
FROM `Shifts`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE (`end` > ? AND `start` < ?)
AND (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`room_id`=`Shifts`.`RID`)
> (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
- AND NOT `Shifts`.`PSID` IS NULL
+ AND NOT s.shift_id IS NULL
) AS `tmp`
ORDER BY `tmp`.`start`
", [
@@ -70,16 +74,6 @@ function Shifts_free($start, $end)
}
/**
- * Returns all shifts with a PSID (from frab import)
- *
- * @return array[]
- */
-function Shifts_from_frab()
-{
- return DB::select('SELECT * FROM `Shifts` WHERE `PSID` IS NOT NULL ORDER BY `start`');
-}
-
-/**
* @param array|int $room
* @return array[]
*/
@@ -103,11 +97,12 @@ function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
JOIN `Room` USING (`RID`)
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id` = `Shifts`.`SID`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
AND `start` BETWEEN ? AND ?
AND `NeededAngelTypes`.`angel_type_id` IN (' . implode(',', $shiftsFilter->getTypes()) . ')
AND `NeededAngelTypes`.`count` > 0
- AND `Shifts`.`PSID` IS NULL
+ AND s.shift_id IS NULL
UNION
@@ -116,11 +111,12 @@ function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
JOIN `Room` USING (`RID`)
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
AND `start` BETWEEN ? AND ?
AND `NeededAngelTypes`.`angel_type_id` IN (' . implode(',', $shiftsFilter->getTypes()) . ')
AND `NeededAngelTypes`.`count` > 0
- AND NOT `Shifts`.`PSID` IS NULL) AS tmp_shifts
+ AND NOT s.shift_id IS NULL) AS tmp_shifts
ORDER BY `room_name`, `start`';
@@ -152,9 +148,10 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
FROM `Shifts`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
AND `start` BETWEEN ? AND ?
- AND `Shifts`.`PSID` IS NULL
+ AND s.shift_id IS NULL
UNION
@@ -168,9 +165,10 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
FROM `Shifts`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
AND `start` BETWEEN ? AND ?
- AND NOT `Shifts`.`PSID` IS NULL';
+ AND NOT s.shift_id IS NULL';
return DB::select(
$sql,
@@ -201,9 +199,10 @@ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype)
FROM `Shifts`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `Shifts`.`SID`=?
AND `AngelTypes`.`id`=?
- AND `Shifts`.`PSID` IS NULL
+ AND s.shift_id IS NULL
UNION
@@ -217,9 +216,10 @@ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype)
FROM `Shifts`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `Shifts`.`SID`=?
AND `AngelTypes`.`id`=?
- AND NOT `Shifts`.`PSID` IS NULL
+ AND NOT s.shift_id IS NULL
',
[
$shift['SID'],
@@ -495,16 +495,6 @@ function Shift_signup_allowed(
}
/**
- * Delete a shift by its external id.
- *
- * @param int $shift_psid
- */
-function Shift_delete_by_psid($shift_psid)
-{
- DB::delete('DELETE FROM `Shifts` WHERE `PSID`=?', [$shift_psid]);
-}
-
-/**
* Delete a shift.
*
* @param int $shift_id
@@ -535,7 +525,6 @@ function Shift_update($shift)
`RID` = ?,
`title` = ?,
`URL` = ?,
- `PSID` = ?,
`edited_by_user_id` = ?,
`edited_at_timestamp` = ?
WHERE `SID` = ?
@@ -547,7 +536,6 @@ function Shift_update($shift)
$shift['RID'],
$shift['title'],
$shift['URL'],
- $shift['PSID'],
$user->id,
time(),
$shift['SID']
@@ -556,25 +544,6 @@ function Shift_update($shift)
}
/**
- * Update a shift by its external id.
- *
- * @param array $shift
- * @return int
- * @throws Exception
- */
-function Shift_update_by_psid($shift)
-{
- $shift_source = DB::selectOne('SELECT `SID` FROM `Shifts` WHERE `PSID`=?', [$shift['PSID']]);
-
- if (empty($shift_source)) {
- throw new Exception('Shift not found.');
- }
-
- $shift['SID'] = $shift_source['SID'];
- return Shift_update($shift);
-}
-
-/**
* Create a new shift.
*
* @param array $shift
@@ -590,12 +559,11 @@ function Shift_create($shift)
`RID`,
`title`,
`URL`,
- `PSID`,
`created_by_user_id`,
`edited_at_timestamp`,
`created_at_timestamp`
)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
',
[
$shift['shifttype_id'],
@@ -604,7 +572,6 @@ function Shift_create($shift)
$shift['RID'],
$shift['title'],
$shift['URL'],
- $shift['PSID'],
auth()->user()->id,
time(),
time(),
diff --git a/includes/model/Stats.php b/includes/model/Stats.php
index c8342d82..e355b064 100644
--- a/includes/model/Stats.php
+++ b/includes/model/Stats.php
@@ -39,8 +39,9 @@ function stats_hours_to_work()
(SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
* (`Shifts`.`end` - `Shifts`.`start`)/3600 AS `count`
FROM `Shifts`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` >= ?
- AND `Shifts`.`PSID` IS NULL
+ AND s.shift_id IS NULL
UNION ALL
@@ -48,8 +49,9 @@ function stats_hours_to_work()
(SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`room_id`=`Shifts`.`RID`)
* (`Shifts`.`end` - `Shifts`.`start`)/3600 AS `count`
FROM `Shifts`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` >= ?
- AND NOT `Shifts`.`PSID` IS NULL
+ AND NOT s.shift_id IS NULL
) AS `tmp`
", [
time(),
@@ -90,8 +92,9 @@ function stats_angels_needed_three_hours()
)
AS `count`
FROM `Shifts`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` > ? AND `start` < ?
- AND `Shifts`.`PSID` IS NULL
+ AND s.shift_id IS NULL
UNION ALL
@@ -113,8 +116,9 @@ function stats_angels_needed_three_hours()
)
AS `count`
FROM `Shifts`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` > ? AND `start` < ?
- AND NOT `Shifts`.`PSID` IS NULL
+ AND NOT s.shift_id IS NULL
) AS `tmp`", [
$now,
$in3hours,
@@ -163,8 +167,9 @@ function stats_angels_needed_for_nightshifts()
)
AS `count`
FROM `Shifts`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` > ? AND `start` < ?
- AND `Shifts`.`PSID` IS NULL
+ AND s.shift_id IS NULL
UNION ALL
@@ -186,8 +191,9 @@ function stats_angels_needed_for_nightshifts()
)
AS `count`
FROM `Shifts`
+ LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` > ? AND `start` < ?
- AND NOT `Shifts`.`PSID` IS NULL
+ AND NOT s.shift_id IS NULL
) AS `tmp`", [
$night_start,
$night_end,
diff --git a/includes/pages/admin_import.php b/includes/pages/admin_import.php
deleted file mode 100644
index 1dbd742b..00000000
--- a/includes/pages/admin_import.php
+++ /dev/null
@@ -1,478 +0,0 @@
-<?php
-
-/**
- * @return string
- */
-function admin_import_title()
-{
- return __('Frab import');
-}
-
-/**
- * @return string
- */
-function admin_import()
-{
- global $rooms_import;
- $user = auth()->user();
- $html = '';
- $import_dir = __DIR__ . '/../../import';
- $request = request();
-
- $step = 'input';
- if (
- $request->has('step')
- && in_array($request->input('step'), [
- 'input',
- 'check',
- 'import'
- ])
- ) {
- $step = $request->input('step');
- }
-
- try {
- $test_handle = @fopen($import_dir . '/tmp', 'w');
- fclose($test_handle);
- @unlink($import_dir . '/tmp');
- } catch (Exception $e) {
- error(__('Webserver has no write-permission on import directory.'));
- }
-
- $import_file = $import_dir . '/import_' . $user->id . '.xml';
- $shifttype_id = null;
- $add_minutes_start = 15;
- $add_minutes_end = 15;
-
- $shifttypes_source = ShiftTypes();
- $shifttypes = [];
- foreach ($shifttypes_source as $shifttype) {
- $shifttypes[$shifttype['id']] = $shifttype['name'];
- }
-
- switch ($step) {
- case 'input':
- $valid = false;
-
- if ($request->hasPostData('submit')) {
- $valid = true;
-
- if ($request->has('shifttype_id') && isset($shifttypes[$request->input('shifttype_id')])) {
- $shifttype_id = $request->input('shifttype_id');
- } else {
- $valid = false;
- error(__('Please select a shift type.'));
- }
-
- $minutes_start = trim($request->input('add_minutes_start'));
- if ($request->has('add_minutes_start') && is_numeric($minutes_start)) {
- $add_minutes_start = $minutes_start;
- } else {
- $valid = false;
- error(__('Please enter an amount of minutes to add to a talk\'s begin.'));
- }
-
- if ($request->has('add_minutes_end') && is_numeric(trim($request->input('add_minutes_end')))) {
- $add_minutes_end = trim($request->input('add_minutes_end'));
- } else {
- $valid = false;
- error(__('Please enter an amount of minutes to add to a talk\'s end.'));
- }
-
- if (isset($_FILES['xcal_file']) && ($_FILES['xcal_file']['error'] == 0)) {
- if (move_uploaded_file($_FILES['xcal_file']['tmp_name'], $import_file)) {
- libxml_use_internal_errors(true);
- if (simplexml_load_file($import_file) === false) {
- $valid = false;
- error(__('No valid xml/xcal file provided.'));
- unlink($import_file);
- }
- } else {
- $valid = false;
- error(__('File upload went wrong.'));
- }
- } else {
- $valid = false;
- error(__('Please provide some data.'));
- }
- }
-
- if ($valid) {
- throw_redirect(
- page_link_to('admin_import', [
- 'step' => 'check',
- 'shifttype_id' => $shifttype_id,
- 'add_minutes_end' => $add_minutes_end,
- 'add_minutes_start' => $add_minutes_start,
- ])
- );
- } else {
- $html .= div('well well-sm text-center', [
- __('File Upload')
- . mute(glyph('arrow-right'))
- . mute(__('Validation'))
- . mute(glyph('arrow-right'))
- . mute(__('Import'))
- ]) . div('row', [
- div('col-md-offset-3 col-md-6', [
- form([
- form_info(
- '',
- __('This import will create/update/delete rooms and shifts by given FRAB-export file. The needed file format is xcal.')
- ),
- form_select('shifttype_id', __('Shifttype'), $shifttypes, $shifttype_id),
- form_spinner('add_minutes_start', __('Add minutes to start'), $add_minutes_start),
- form_spinner('add_minutes_end', __('Add minutes to end'), $add_minutes_end),
- form_file('xcal_file', __('xcal-File (.xcal)')),
- form_submit('submit', __('Import'))
- ])
- ])
- ]);
- }
- break;
-
- case 'check':
- if (!file_exists($import_file)) {
- error(__('Missing import file.'));
- throw_redirect(page_link_to('admin_import'));
- }
-
- if ($request->has('shifttype_id') && isset($shifttypes[$request->input('shifttype_id')])) {
- $shifttype_id = $request->input('shifttype_id');
- } else {
- error(__('Please select a shift type.'));
- throw_redirect(page_link_to('admin_import'));
- }
-
- if ($request->has('add_minutes_start') && is_numeric(trim($request->input('add_minutes_start')))) {
- $add_minutes_start = trim($request->input('add_minutes_start'));
- } else {
- error(__('Please enter an amount of minutes to add to a talk\'s begin.'));
- throw_redirect(page_link_to('admin_import'));
- }
-
- if ($request->has('add_minutes_end') && is_numeric(trim($request->input(('add_minutes_end'))))) {
- $add_minutes_end = trim($request->input('add_minutes_end'));
- } else {
- error(__('Please enter an amount of minutes to add to a talk\'s end.'));
- throw_redirect(page_link_to('admin_import'));
- }
-
- list($rooms_new, $rooms_deleted) = prepare_rooms($import_file);
- list($events_new, $events_updated, $events_deleted) = prepare_events(
- $import_file,
- $shifttype_id,
- $add_minutes_start,
- $add_minutes_end
- );
-
- $html .= div(
- 'well well-sm text-center',
- [
- '<span class="text-success">' . __('File Upload') . glyph('ok-circle') . '</span>'
- . mute(glyph('arrow-right'))
- . __('Validation')
- . mute(glyph('arrow-right'))
- . mute(__('Import'))
- ]
- )
- . form(
- [
- div('row', [
- div('col-sm-6', [
- '<h3>' . __('Rooms to create') . '</h3>',
- table(__('Name'), $rooms_new)
- ]),
- div('col-sm-6', [
- '<h3>' . __('Rooms to delete') . '</h3>',
- table(__('Name'), $rooms_deleted)
- ])
- ]),
- '<h3>' . __('Shifts to create') . '</h3>',
- table([
- 'day' => __('Day'),
- 'start' => __('Start'),
- 'end' => __('End'),
- 'shifttype' => __('Shift type'),
- 'title' => __('Title'),
- 'room' => __('Room')
- ], shifts_printable($events_new, $shifttypes)),
- '<h3>' . __('Shifts to update') . '</h3>',
- table([
- 'day' => __('Day'),
- 'start' => __('Start'),
- 'end' => __('End'),
- 'shifttype' => __('Shift type'),
- 'title' => __('Title'),
- 'room' => __('Room')
- ], shifts_printable($events_updated, $shifttypes)),
- '<h3>' . __('Shifts to delete') . '</h3>',
- table([
- 'day' => __('Day'),
- 'start' => __('Start'),
- 'end' => __('End'),
- 'shifttype' => __('Shift type'),
- 'title' => __('Title'),
- 'room' => __('Room')
- ], shifts_printable($events_deleted, $shifttypes)),
- form_submit('submit', __('Import'))
- ],
- page_link_to('admin_import', [
- 'step' => 'import',
- 'shifttype_id' => $shifttype_id,
- 'add_minutes_end' => $add_minutes_end,
- 'add_minutes_start' => $add_minutes_start,
- ])
- );
- break;
-
- case 'import':
- if (!file_exists($import_file)) {
- error(__('Missing import file.'));
- throw_redirect(page_link_to('admin_import'));
- }
-
- if (!file_exists($import_file)) {
- throw_redirect(page_link_to('admin_import'));
- }
-
- if ($request->has('shifttype_id') && isset($shifttypes[$request->input('shifttype_id')])) {
- $shifttype_id = $request->input('shifttype_id');
- } else {
- error(__('Please select a shift type.'));
- throw_redirect(page_link_to('admin_import'));
- }
-
- if ($request->has('add_minutes_start') && is_numeric(trim($request->input('add_minutes_start')))) {
- $add_minutes_start = trim($request->input('add_minutes_start'));
- } else {
- error(__('Please enter an amount of minutes to add to a talk\'s begin.'));
- throw_redirect(page_link_to('admin_import'));
- }
-
- if ($request->has('add_minutes_end') && is_numeric(trim($request->input('add_minutes_end')))) {
- $add_minutes_end = trim($request->input('add_minutes_end'));
- } else {
- error(__('Please enter an amount of minutes to add to a talk\'s end.'));
- throw_redirect(page_link_to('admin_import'));
- }
-
- list($rooms_new, $rooms_deleted) = prepare_rooms($import_file);
- foreach ($rooms_new as $room) {
- $result = Room_create($room, true, null, null);
- $rooms_import[trim($room)] = $result;
- }
- foreach ($rooms_deleted as $room) {
- Room_delete_by_name($room);
- }
-
- list($events_new, $events_updated, $events_deleted) = prepare_events(
- $import_file,
- $shifttype_id,
- $add_minutes_start,
- $add_minutes_end
- );
- foreach ($events_new as $event) {
- Shift_create($event);
- }
-
- foreach ($events_updated as $event) {
- Shift_update_by_psid($event);
- }
-
- foreach ($events_deleted as $event) {
- Shift_delete_by_psid($event['PSID']);
- }
-
- engelsystem_log('Frab import done');
-
- unlink($import_file);
-
- $html .= div('well well-sm text-center', [
- '<span class="text-success">' . __('File Upload') . glyph('ok-circle') . '</span>'
- . mute(glyph('arrow-right'))
- . '<span class="text-success">' . __('Validation') . glyph('ok-circle') . '</span>'
- . mute(glyph('arrow-right'))
- . '<span class="text-success">' . __('Import') . glyph('ok-circle') . '</span>'
- ]) . success(__('It\'s done!'), true);
- break;
- default:
- throw_redirect(page_link_to('admin_import'));
- }
-
- return page_with_title(admin_import_title(), [
- msg(),
- $html
- ]);
-}
-
-/**
- * @param string $file
- * @return array
- */
-function prepare_rooms($file)
-{
- global $rooms_import;
- $data = read_xml($file);
-
- // Load rooms from db for compare with input
- $rooms = Rooms();
- // Contains rooms from db with from_frab==true
- $rooms_db = [];
- // Contains all rooms from db
- $rooms_db_all = [];
- // Contains all rooms from db and frab
- $rooms_import = [];
- foreach ($rooms as $room) {
- if ($room['from_frab']) {
- $rooms_db[] = $room['Name'];
- }
- $rooms_db_all[] = $room['Name'];
- $rooms_import[$room['Name']] = $room['RID'];
- }
-
- $events = $data->vcalendar->vevent;
- $rooms_frab = [];
- foreach ($events as $event) {
- $rooms_frab[] = (string)$event->location;
- if (!isset($rooms_import[trim($event->location)])) {
- $rooms_import[trim($event->location)] = trim($event->location);
- }
- }
- $rooms_frab = array_unique($rooms_frab);
-
- $rooms_new = array_diff($rooms_frab, $rooms_db_all);
- $rooms_deleted = array_diff($rooms_db, $rooms_frab);
-
- return [
- $rooms_new,
- $rooms_deleted
- ];
-}
-
-/**
- * @param string $file
- * @param int $shifttype_id
- * @param int $add_minutes_start
- * @param int $add_minutes_end
- * @return array
- */
-function prepare_events($file, $shifttype_id, $add_minutes_start, $add_minutes_end)
-{
- global $rooms_import;
- $data = read_xml($file);
-
- $rooms = Rooms();
- $rooms_db = [];
- foreach ($rooms as $room) {
- $rooms_db[$room['Name']] = $room['RID'];
- }
-
- $events = $data->vcalendar->vevent;
- $shifts_pb = [];
- foreach ($events as $event) {
- $event_pb = $event->children('http://pentabarf.org');
- $event_id = trim($event_pb->{'event-id'});
- $shifts_pb[$event_id] = [
- 'shifttype_id' => $shifttype_id,
- 'start' => parse_date("Ymd\THis", $event->dtstart) - $add_minutes_start * 60,
- 'end' => parse_date("Ymd\THis", $event->dtend) + $add_minutes_end * 60,
- 'RID' => $rooms_import[trim($event->location)],
- 'title' => trim($event->summary),
- 'URL' => trim($event->url),
- 'PSID' => $event_id
- ];
- }
-
- $shifts = Shifts_from_frab();
- $shifts_db = [];
- foreach ($shifts as $shift) {
- $shifts_db[$shift['PSID']] = $shift;
- }
-
- $shifts_new = [];
- $shifts_updated = [];
- foreach ($shifts_pb as $shift) {
- if (!isset($shifts_db[$shift['PSID']])) {
- $shifts_new[] = $shift;
- } else {
- $tmp = $shifts_db[$shift['PSID']];
- if (
- $shift['shifttype_id'] != $tmp['shifttype_id']
- || $shift['title'] != $tmp['title']
- || $shift['start'] != $tmp['start']
- || $shift['end'] != $tmp['end']
- || $shift['RID'] != $tmp['RID']
- || $shift['URL'] != $tmp['URL']
- ) {
- $shifts_updated[] = $shift;
- }
- }
- }
-
- $shifts_deleted = [];
- foreach ($shifts_db as $shift) {
- if (!isset($shifts_pb[$shift['PSID']])) {
- $shifts_deleted[] = $shift;
- }
- }
-
- return [
- $shifts_new,
- $shifts_updated,
- $shifts_deleted
- ];
-}
-
-/**
- * @param string $file
- * @return SimpleXMLElement
- */
-function read_xml($file)
-{
- global $xml_import;
- if (!isset($xml_import)) {
- libxml_use_internal_errors(true);
- $xml_import = simplexml_load_file($file);
- }
- return $xml_import;
-}
-
-/**
- * @param array $shifts
- * @param array $shifttypes
- * @return array
- */
-function shifts_printable($shifts, $shifttypes)
-{
- global $rooms_import;
- $rooms = array_flip($rooms_import);
-
- uasort($shifts, 'shift_sort');
-
- $shifts_printable = [];
- foreach ($shifts as $shift) {
- $shifts_printable[] = [
- 'day' => date('l, Y-m-d', $shift['start']),
- 'start' => date('H:i', $shift['start']),
- 'shifttype' => ShiftType_name_render([
- 'id' => $shift['shifttype_id'],
- 'name' => $shifttypes[$shift['shifttype_id']]
- ]),
- 'title' => shorten($shift['title']),
- 'end' => date('H:i', $shift['end']),
- 'room' => $rooms[$shift['RID']]
- ];
- }
- return $shifts_printable;
-}
-
-/**
- * @param array $shift_a
- * @param array $shift_b
- * @return int
- */
-function shift_sort($shift_a, $shift_b)
-{
- return ($shift_a['start'] < $shift_b['start']) ? -1 : 1;
-}
diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php
index 74c0fbe3..733e56af 100644
--- a/includes/pages/admin_rooms.php
+++ b/includes/pages/admin_rooms.php
@@ -19,7 +19,6 @@ function admin_rooms()
foreach ($rooms_source as $room) {
$rooms[] = [
'name' => Room_name_render($room),
- 'from_frab' => glyph_bool($room['from_frab']),
'map_url' => glyph_bool(!empty($room['map_url'])),
'actions' => table_buttons([
button(
@@ -40,7 +39,6 @@ function admin_rooms()
if ($request->has('show')) {
$msg = '';
$name = '';
- $from_frab = false;
$map_url = null;
$description = null;
$room_id = 0;
@@ -61,7 +59,6 @@ function admin_rooms()
$room_id = $request->input('id');
$name = $room['Name'];
- $from_frab = $room['from_frab'];
$map_url = $room['map_url'];
$description = $room['description'];
@@ -88,8 +85,6 @@ function admin_rooms()
$msg .= error(__('Please enter a name.'), true);
}
- $from_frab = $request->has('from_frab');
-
if ($request->has('map_url')) {
$map_url = strip_request_item('map_url');
}
@@ -118,9 +113,9 @@ function admin_rooms()
if ($valid) {
if (empty($room_id)) {
- $room_id = Room_create($name, $from_frab, $map_url, $description);
+ $room_id = Room_create($name, $map_url, $description);
} else {
- Room_update($room_id, $name, $from_frab, $map_url, $description);
+ Room_update($room_id, $name, $map_url, $description);
}
NeededAngelTypes_delete_by_room($room_id);
@@ -159,7 +154,6 @@ function admin_rooms()
div('row', [
div('col-md-6', [
form_text('name', __('Name'), $name, false, 35),
- form_checkbox('from_frab', __('Frab import'), $from_frab),
form_text('map_url', __('Map URL'), $map_url),
form_info('', __('The map url is used to display an iframe on the room page.')),
form_textarea('description', __('Description'), $description),
@@ -212,7 +206,6 @@ function admin_rooms()
msg(),
table([
'name' => __('Name'),
- 'from_frab' => __('Frab import'),
'map_url' => __('Map'),
'actions' => ''
], $rooms)
diff --git a/includes/pages/admin_shifts.php b/includes/pages/admin_shifts.php
index dc61392f..dbcce180 100644
--- a/includes/pages/admin_shifts.php
+++ b/includes/pages/admin_shifts.php
@@ -350,7 +350,6 @@ function admin_shifts()
foreach ($session->get('admin_shifts_shifts', []) as $shift) {
$shift['URL'] = null;
- $shift['PSID'] = null;
$shift_id = Shift_create($shift);
engelsystem_log(
diff --git a/includes/pages/schedule/ImportSchedule.php b/includes/pages/schedule/ImportSchedule.php
new file mode 100644
index 00000000..1b03b57b
--- /dev/null
+++ b/includes/pages/schedule/ImportSchedule.php
@@ -0,0 +1,612 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Engelsystem\Controllers\Admin\Schedule;
+
+use Carbon\Carbon;
+use Engelsystem\Controllers\BaseController;
+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;
+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\Arr;
+use Illuminate\Support\Collection;
+use Psr\Log\LoggerInterface;
+use stdClass;
+use Symfony\Component\HttpFoundation\Session\SessionInterface;
+
+class ImportSchedule extends BaseController
+{
+ /** @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 */
+ protected $url = '/admin/schedule';
+
+ /** @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',
+ [
+ 'errors' => $this->getFromSession('errors'),
+ 'success' => $this->getFromSession('success'),
+ 'shift_types' => $this->getShiftTypes(),
+ ]
+ );
+ }
+
+ /**
+ * @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,
+ $shiftType,
+ $scheduleUrl,
+ $schedule,
+ $minutesBefore,
+ $minutesAfter
+ ) = $this->getScheduleData($request);
+ } catch (ErrorException $e) {
+ return back()->with('errors', [$e->getMessage()]);
+ }
+
+ return $this->response->withView(
+ 'admin/schedule/load.twig',
+ [
+ 'errors' => $this->getFromSession('errors'),
+ 'schedule_url' => $scheduleUrl->url,
+ 'shift_type' => $shiftType,
+ 'minutes_before' => $minutesBefore,
+ 'minutes_after' => $minutesAfter,
+ 'schedule' => $schedule,
+ 'rooms' => [
+ 'add' => $newRooms,
+ ],
+ 'shifts' => [
+ 'add' => $newEvents,
+ 'update' => $changeEvents,
+ 'delete' => $deleteEvents,
+ ],
+ ]
+ );
+ }
+
+ /**
+ * @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) {
+ return back()->with('errors', [$e->getMessage()]);
+ }
+
+ $this->log('Started schedule "{schedule}" import', ['schedule' => $scheduleUrl->url]);
+
+ foreach ($newRooms as $room) {
+ $this->createRoom($room);
+ }
+
+ $rooms = $this->getAllRooms();
+ foreach ($newEvents as $event) {
+ $this->createEvent(
+ $event,
+ (int)$shiftType,
+ $rooms
+ ->where('name', $event->getRoom()->getName())
+ ->first(),
+ $scheduleUrl
+ );
+ }
+
+ foreach ($changeEvents as $event) {
+ $this->updateEvent(
+ $event,
+ (int)$shiftType,
+ $rooms
+ ->where('name', $event->getRoom()->getName())
+ ->first()
+ );
+ }
+
+ foreach ($deleteEvents as $event) {
+ $this->deleteEvent($event);
+ }
+
+ $this->log('Ended schedule "{schedule}" import', ['schedule' => $scheduleUrl->url]);
+
+ return redirect($this->url, 303)
+ ->with('success', ['schedule.import.success']);
+ }
+
+ /**
+ * @param Room $room
+ */
+ protected function createRoom(Room $room): void
+ {
+ $this->db
+ ->table('Room')
+ ->insert(
+ [
+ 'Name' => $room->getName(),
+ ]
+ );
+
+ $this->log('Created schedule room "{room}"', ['room' => $room->getName()]);
+ }
+
+ /**
+ * @param Event $shift
+ * @param int $shiftTypeId
+ * @param stdClass $room
+ * @param ScheduleUrl $scheduleUrl
+ */
+ protected function createEvent(Event $shift, int $shiftTypeId, stdClass $room, ScheduleUrl $scheduleUrl): void
+ {
+ $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,
+ 'from' => $shift->getDate()->format(Carbon::RFC3339),
+ 'to' => $shift->getEndDate()->format(Carbon::RFC3339),
+ 'guid' => $shift->getGuid(),
+ ]
+ );
+ }
+
+ /**
+ * @param Event $shift
+ * @param int $shiftTypeId
+ * @param stdClass $room
+ */
+ protected function updateEvent(Event $shift, int $shiftTypeId, stdClass $room): void
+ {
+ $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,
+ 'from' => $shift->getDate()->format(Carbon::RFC3339),
+ 'to' => $shift->getEndDate()->format(Carbon::RFC3339),
+ '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(),
+ 'from' => $shift->getDate()->format(Carbon::RFC3339),
+ 'to' => $shift->getEndDate()->format(Carbon::RFC3339),
+ 'guid' => $shift->getGuid(),
+ ]
+ );
+ }
+
+ /**
+ * @param Request $request
+ * @return Event[]|Room[]|ScheduleUrl|Schedule|string
+ * @throws ErrorException
+ */
+ protected function getScheduleData(Request $request)
+ {
+ $data = $this->validate(
+ $request,
+ [
+ 'schedule-url' => 'required|url',
+ 'shift-type' => 'required|int',
+ 'minutes-before' => 'optional|int',
+ 'minutes-after' => 'optional|int',
+ ]
+ );
+
+ $scheduleResponse = $this->guzzle->get($data['schedule-url']);
+ 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');
+ }
+
+ $shiftType = (int)$data['shift-type'];
+ if (!isset($this->getShiftTypes()[$shiftType])) {
+ throw new ErrorException('schedule.import.invalid-shift-type');
+ }
+
+ $scheduleUrl = $this->getScheduleUrl($data['schedule-url']);
+ $schedule = $this->parser->getSchedule();
+ $minutesBefore = isset($data['minutes-before']) ? (int)$data['minutes-before'] : 15;
+ $minutesAfter = isset($data['minutes-after']) ? (int)$data['minutes-after'] : 15;
+ $newRooms = $this->newRooms($schedule->getRooms());
+ return array_merge(
+ $this->shiftsDiff($schedule, $scheduleUrl, $shiftType, $minutesBefore, $minutesAfter),
+ [$newRooms, $shiftType, $scheduleUrl, $schedule, $minutesBefore, $minutesAfter]
+ );
+ }
+
+ /**
+ * @param string $name
+ * @return Collection
+ */
+ protected function getFromSession(string $name): Collection
+ {
+ $data = Collection::make(Arr::flatten($this->session->get($name, [])));
+ $this->session->remove($name);
+
+ return $data;
+ }
+
+ /**
+ * @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);
+ }
+ }
+ }
+
+ $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];
+
+ if (
+ $shift->title != $event->getTitle()
+ || $shift->shift_type_id != $shiftType
+ || Carbon::createFromTimestamp($shift->start) != $event->getDate()
+ || Carbon::createFromTimestamp($shift->end) != $event->getEndDate()
+ || $shift->room_id != $rooms->where('name', $event->getRoom()->getName())->first()->id
+ || $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);
+
+ $event = new Event(
+ $scheduleShift->guid,
+ 0,
+ new Room($shift->room_name),
+ $shift->title,
+ '',
+ 'n/a',
+ Carbon::createFromTimestamp($shift->start),
+ $start->format('H:i'),
+ $duration->format('%H:%I'),
+ '',
+ '',
+ ''
+ );
+
+ return $event;
+ }
+
+ /**
+ * @return Collection
+ */
+ protected function getAllRooms(): Collection
+ {
+ return new Collection($this->db->select('SELECT RID as id, Name as name FROM Room'));
+ }
+
+ /**
+ * @param ScheduleUrl $scheduleUrl
+ * @param string[] $events
+ * @return QueryBuilder[]|DatabaseCollection|ScheduleShift[]
+ */
+ 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
+ * @return QueryBuilder[]|DatabaseCollection|ScheduleShift[]
+ */
+ 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(
+ '
+ 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
+ LEFT JOIN Room r on s.RID = r.RID
+ WHERE SID = ?
+ ',
+ [$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 $scheduleUrl
+ * @return ScheduleUrl
+ */
+ protected function getScheduleUrl(string $scheduleUrl): ScheduleUrl
+ {
+ if (!$schedule = ScheduleUrl::whereUrl($scheduleUrl)->first()) {
+ $schedule = new ScheduleUrl(['url' => $scheduleUrl]);
+ $schedule->save();
+
+ $this->log('Created schedule "{schedule}"', ['schedule' => $schedule->url]);
+ }
+
+ return $schedule;
+ }
+
+ /**
+ * @param string $message
+ * @param array $context
+ */
+ protected function log(string $message, array $context = []): void
+ {
+ $user = auth()->user();
+ $message = sprintf('%s (%u): %s', $user->name, $user->id, $message);
+
+ $this->log->info($message, $context);
+ }
+}
diff --git a/includes/sys_menu.php b/includes/sys_menu.php
index 3bb061ce..01a4b6d2 100644
--- a/includes/sys_menu.php
+++ b/includes/sys_menu.php
@@ -108,30 +108,38 @@ function make_navigation()
$admin_menu = [];
$admin_pages = [
- 'admin_arrive' => __('Arrived angels'),
- 'admin_active' => __('Active angels'),
- 'admin_user' => __('All Angels'),
- 'admin_free' => __('Free angels'),
- 'admin_questions' => __('Answer questions'),
- 'shifttypes' => __('Shifttypes'),
- 'admin_shifts' => __('Create shifts'),
- 'admin_rooms' => __('Rooms'),
- 'admin_groups' => __('Grouprights'),
- 'admin_import' => __('Frab import'),
- 'admin_log' => __('Log'),
- 'admin_event_config' => __('Event config'),
+ 'admin_arrive' => 'Arrived angels',
+ 'admin_active' => 'Active angels',
+ 'admin_user' => 'All Angels',
+ 'admin_free' => 'Free angels',
+ 'admin_questions' => 'Answer questions',
+ 'shifttypes' => 'Shifttypes',
+ 'admin_shifts' => 'Create shifts',
+ 'admin_rooms' => 'Rooms',
+ 'admin_groups' => 'Grouprights',
+ 'admin/schedule' => ['schedule.import', 'schedule.import'],
+ 'admin_log' => 'Log',
+ 'admin_event_config' => 'Event config',
];
if (config('autoarrive')) {
unset($admin_pages['admin_arrive']);
}
- foreach ($admin_pages as $menu_page => $title) {
- if (auth()->can($menu_page)) {
+ foreach ($admin_pages as $menu_page => $options) {
+ $options = (array)$options;
+ $permissions = $menu_page;
+ $title = $options[0];
+
+ if (isset($options[1])) {
+ $permissions = $options[1];
+ }
+
+ if (auth()->can($permissions)) {
$admin_menu[] = toolbar_item_link(
page_link_to($menu_page),
'',
- $title,
+ __($title),
$menu_page == $page
);
}
diff --git a/includes/sys_template.php b/includes/sys_template.php
index fad207a9..cf4c64aa 100644
--- a/includes/sys_template.php
+++ b/includes/sys_template.php
@@ -410,42 +410,6 @@ function table_buttons($buttons = [])
}
/**
- * @param string $str
- * @param int $length
- * @return string
- */
-function shorten($str, $length = 50)
-{
- if (strlen($str) < $length) {
- return $str;
- }
- return '<span title="' . htmlentities($str, ENT_COMPAT, 'UTF-8') . '">'
- . substr($str, 0, $length - 3)
- . '...</span>';
-}
-
-/**
- * @param array[] $array
- * @return string
- */
-function table_body($array)
-{
- $html = '';
- foreach ($array as $line) {
- $html .= '<tr>';
- if (is_array($line)) {
- foreach ($line as $td) {
- $html .= '<td>' . $td . '</td>';
- }
- } else {
- $html .= '<td>' . $line . '</td>';
- }
- $html .= '</tr>';
- }
- return $html;
-}
-
-/**
* @param string $msg
* @return mixed
*/