Added Room model

This commit is contained in:
Igor Scheller 2020-09-06 23:50:36 +02:00 committed by msquare
parent 8296ef0662
commit acf84f222d
30 changed files with 360 additions and 273 deletions

View File

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\Room;
use Illuminate\Database\Schema\Blueprint;
use stdClass;
class CreateRoomsTable extends Migration
{
use ChangesReferences;
use Reference;
/**
* Run the migration
*/
public function up(): void
{
$this->schema->create('rooms', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 35)->unique();
$table->string('map_url', 300)->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
if ($this->schema->hasTable('Room')) {
$connection = $this->schema->getConnection();
/** @var stdClass[] $previousRecords */
$previousRecords = $connection
->table('Room')
->get();
foreach ($previousRecords as $previousRecord) {
$room = new Room([
'name' => $previousRecord->Name,
'map_url' => $previousRecord->map_url ?: null,
'description' => $previousRecord->description ?: null,
]);
$room->setAttribute('id', $previousRecord->RID);
$room->save();
}
$this->changeReferences(
'Room',
'RID',
'rooms',
'id'
);
$this->schema->drop('Room');
}
}
/**
* Reverse the migration
*/
public function down(): void
{
$this->schema->create('Room', function (Blueprint $table) {
$table->increments('RID');
$table->string('Name', 35)->unique();
$table->string('map_url', 300)->nullable();
$table->mediumText('description')->nullable();
});
foreach (Room::all() as $room) {
$this->schema
->getConnection()
->table('Room')
->insert([
'RID' => $room->id,
'Name' => $room->name,
'map_url' => $room->map_url ?: null,
'description' => $room->description ?: null,
]);
}
$this->changeReferences(
'rooms',
'id',
'Room',
'RID'
);
$this->schema->drop('rooms');
}
}

View File

@ -1,5 +1,7 @@
<?php <?php
use Engelsystem\Models\Room;
/** /**
* Loads all data for the public dashboard * Loads all data for the public dashboard
* *
@ -38,7 +40,7 @@ function public_dashboard_controller()
function public_dashboard_controller_free_shift($shift) function public_dashboard_controller_free_shift($shift)
{ {
$shifttype = ShiftType($shift['shifttype_id']); $shifttype = ShiftType($shift['shifttype_id']);
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
$free_shift = [ $free_shift = [
'SID' => $shift['SID'], 'SID' => $shift['SID'],
@ -48,7 +50,7 @@ function public_dashboard_controller_free_shift($shift)
'duration' => round(($shift['end'] - $shift['start']) / 3600), 'duration' => round(($shift['end'] - $shift['start']) / 3600),
'shifttype_name' => $shifttype['name'], 'shifttype_name' => $shifttype['name'],
'title' => $shift['title'], 'title' => $shift['title'],
'room_name' => $room['Name'], 'room_name' => $room->name,
'needed_angels' => [] 'needed_angels' => []
]; ];

View File

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\Models\Room;
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
use Engelsystem\ShiftsFilterRenderer; use Engelsystem\ShiftsFilterRenderer;
@ -32,7 +33,7 @@ function room_controller()
$shiftsFilter = new ShiftsFilter( $shiftsFilter = new ShiftsFilter(
true, true,
[$room['RID']], [$room->id],
AngelType_ids() AngelType_ids()
); );
$selected_day = date('Y-m-d'); $selected_day = date('Y-m-d');
@ -51,7 +52,7 @@ function room_controller()
$shiftCalendarRenderer = shiftCalendarRendererByShiftFilter($shiftsFilter); $shiftCalendarRenderer = shiftCalendarRendererByShiftFilter($shiftsFilter);
return [ return [
$room['Name'], $room->name,
Room_view($room, $shiftsFilterRenderer, $shiftCalendarRenderer) Room_view($room, $shiftsFilterRenderer, $shiftCalendarRenderer)
]; ];
} }
@ -79,27 +80,18 @@ function rooms_controller()
} }
/** /**
* @param array $room * @param Room $room
* @return string * @return string
*/ */
function room_link($room) function room_link(Room $room)
{ {
return page_link_to('rooms', ['action' => 'view', 'room_id' => $room['RID']]); return page_link_to('rooms', ['action' => 'view', 'room_id' => $room->id]);
}
/**
* @param array $room
* @return string
*/
function room_edit_link($room)
{
return page_link_to('admin_rooms', ['show' => 'edit', 'id' => $room['RID']]);
} }
/** /**
* Loads room by request param room_id * Loads room by request param room_id
* *
* @return array * @return Room
*/ */
function load_room() function load_room()
{ {
@ -107,8 +99,8 @@ function load_room()
throw_redirect(page_link_to()); throw_redirect(page_link_to());
} }
$room = Room(request()->input('room_id')); $room = Room::find(request()->input('room_id'));
if (empty($room)) { if (!$room) {
throw_redirect(page_link_to()); throw_redirect(page_link_to());
} }

View File

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\ShiftSignupState; use Engelsystem\ShiftSignupState;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
@ -122,7 +123,7 @@ function shift_entry_create_controller_admin($shift, $angeltype)
$angeltypes_select[$a['id']] = $a['name']; $angeltypes_select[$a['id']] = $a['name'];
} }
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
return [ return [
ShiftEntry_create_title(), ShiftEntry_create_title(),
ShiftEntry_create_view_admin($shift, $room, $angeltype, $angeltypes_select, $signup_user, $users_select) ShiftEntry_create_view_admin($shift, $room, $angeltype, $angeltypes_select, $signup_user, $users_select)
@ -188,7 +189,7 @@ function shift_entry_create_controller_supporter($shift, $angeltype)
$users_select[$u->id] = $u->name; $users_select[$u->id] = $u->name;
} }
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
return [ return [
ShiftEntry_create_title(), ShiftEntry_create_title(),
ShiftEntry_create_view_supporter($shift, $room, $angeltype, $signup_user, $users_select) ShiftEntry_create_view_supporter($shift, $room, $angeltype, $signup_user, $users_select)
@ -268,7 +269,7 @@ function shift_entry_create_controller_user($shift, $angeltype)
throw_redirect(shift_link($shift)); throw_redirect(shift_link($shift));
} }
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
return [ return [
ShiftEntry_create_title(), ShiftEntry_create_title(),
ShiftEntry_create_view_user($shift, $room, $angeltype, $comment) ShiftEntry_create_view_user($shift, $room, $angeltype, $comment)
@ -345,7 +346,9 @@ function shift_entry_delete_controller()
$angeltype = AngelType($shiftEntry['TID']); $angeltype = AngelType($shiftEntry['TID']);
$signout_user = User::find($shiftEntry['UID']); $signout_user = User::find($shiftEntry['UID']);
if (!Shift_signout_allowed($shift, $angeltype, $signout_user->id)) { if (!Shift_signout_allowed($shift, $angeltype, $signout_user->id)) {
error(__('You are not allowed to remove this shift entry. If necessary, ask your supporter or heaven to do so.')); error(__(
'You are not allowed to remove this shift entry. If necessary, ask your supporter or heaven to do so.'
));
throw_redirect(user_link($signout_user->id)); throw_redirect(user_link($signout_user->id));
} }

View File

@ -1,6 +1,7 @@
<?php <?php
use Engelsystem\Http\Exceptions\HttpForbidden; use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Models\Room;
use Engelsystem\ShiftSignupState; use Engelsystem\ShiftSignupState;
/** /**
@ -59,7 +60,10 @@ function shift_edit_controller()
$shift = Shift($shift_id); $shift = Shift($shift_id);
$room = select_array(Rooms(), 'RID', 'Name'); $rooms = [];
foreach (Rooms() as $room) {
$rooms[$room->id] = $room->name;
}
$angeltypes = select_array(AngelTypes(), 'id', 'name'); $angeltypes = select_array(AngelTypes(), 'id', 'name');
$shifttypes = select_array(ShiftTypes(), 'id', 'name'); $shifttypes = select_array(ShiftTypes(), 'id', 'name');
@ -88,7 +92,7 @@ function shift_edit_controller()
if ( if (
$request->has('rid') $request->has('rid')
&& preg_match('/^\d+$/', $request->input('rid')) && preg_match('/^\d+$/', $request->input('rid'))
&& isset($room[$request->input('rid')]) && isset($rooms[$request->input('rid')])
) { ) {
$rid = $request->input('rid'); $rid = $request->input('rid');
} else { } else {
@ -186,7 +190,7 @@ function shift_edit_controller()
form([ form([
form_select('shifttype_id', __('Shifttype'), $shifttypes, $shifttype_id), form_select('shifttype_id', __('Shifttype'), $shifttypes, $shifttype_id),
form_text('title', __('Title'), $title), form_text('title', __('Title'), $title),
form_select('rid', __('Room:'), $room, $rid), form_select('rid', __('Room:'), $rooms, $rid),
form_text('start', __('Start:'), date('Y-m-d H:i', $start)), form_text('start', __('Start:'), date('Y-m-d H:i', $start)),
form_text('end', __('End:'), date('Y-m-d H:i', $end)), form_text('end', __('End:'), date('Y-m-d H:i', $end)),
'<h2>' . __('Needed angels') . '</h2>', '<h2>' . __('Needed angels') . '</h2>',
@ -270,7 +274,7 @@ function shift_controller()
} }
$shifttype = ShiftType($shift['shifttype_id']); $shifttype = ShiftType($shift['shifttype_id']);
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
$angeltypes = AngelTypes(); $angeltypes = AngelTypes();
$user_shifts = Shifts_by_user($user->id); $user_shifts = Shifts_by_user($user->id);

View File

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
@ -9,8 +10,8 @@ use Engelsystem\Models\User\User;
function mail_shift_change($old_shift, $new_shift) function mail_shift_change($old_shift, $new_shift)
{ {
$users = ShiftEntries_by_shift($old_shift['SID']); $users = ShiftEntries_by_shift($old_shift['SID']);
$old_room = Room($old_shift['RID']); $old_room = Room::find($old_shift['RID']);
$new_room = Room($new_shift['RID']); $new_room = Room::find($new_shift['RID']);
$noticeable_changes = false; $noticeable_changes = false;
@ -46,7 +47,7 @@ function mail_shift_change($old_shift, $new_shift)
} }
if ($old_shift['RID'] != $new_shift['RID']) { if ($old_shift['RID'] != $new_shift['RID']) {
$message .= sprintf(__('* Shift Location changed from %s to %s'), $old_room['Name'], $new_room['Name']) . "\n"; $message .= sprintf(__('* Shift Location changed from %s to %s'), $old_room->name, $new_room->name) . "\n";
$noticeable_changes = true; $noticeable_changes = true;
} }
@ -61,7 +62,7 @@ function mail_shift_change($old_shift, $new_shift)
$message .= $new_shift['name'] . "\n"; $message .= $new_shift['name'] . "\n";
$message .= $new_shift['title'] . "\n"; $message .= $new_shift['title'] . "\n";
$message .= date('Y-m-d H:i', $new_shift['start']) . ' - ' . date('H:i', $new_shift['end']) . "\n"; $message .= date('Y-m-d H:i', $new_shift['start']) . ' - ' . date('H:i', $new_shift['end']) . "\n";
$message .= $new_room['Name'] . "\n"; $message .= $new_room->name . "\n";
foreach ($users as $user) { foreach ($users as $user) {
$user = (new User())->forceFill($user); $user = (new User())->forceFill($user);
@ -82,14 +83,14 @@ function mail_shift_change($old_shift, $new_shift)
function mail_shift_delete($shift) function mail_shift_delete($shift)
{ {
$users = ShiftEntries_by_shift($shift['SID']); $users = ShiftEntries_by_shift($shift['SID']);
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
$message = __('A Shift you are registered on was deleted:') . "\n"; $message = __('A Shift you are registered on was deleted:') . "\n";
$message .= $shift['name'] . "\n"; $message .= $shift['name'] . "\n";
$message .= $shift['title'] . "\n"; $message .= $shift['title'] . "\n";
$message .= date('Y-m-d H:i', $shift['start']) . ' - ' . date('H:i', $shift['end']) . "\n"; $message .= date('Y-m-d H:i', $shift['start']) . ' - ' . date('H:i', $shift['end']) . "\n";
$message .= $room['Name'] . "\n"; $message .= $room->name . "\n";
foreach ($users as $user) { foreach ($users as $user) {
$user = (new User())->forceFill($user); $user = (new User())->forceFill($user);
@ -114,13 +115,13 @@ function mail_shift_assign($user, $shift)
return; return;
} }
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
$message = __('You have been assigned to a Shift:') . "\n"; $message = __('You have been assigned to a Shift:') . "\n";
$message .= $shift['name'] . "\n"; $message .= $shift['name'] . "\n";
$message .= $shift['title'] . "\n"; $message .= $shift['title'] . "\n";
$message .= date('Y-m-d H:i', $shift['start']) . ' - ' . date('H:i', $shift['end']) . "\n"; $message .= date('Y-m-d H:i', $shift['start']) . ' - ' . date('H:i', $shift['end']) . "\n";
$message .= $room['Name'] . "\n"; $message .= $room->name . "\n";
engelsystem_email_to_user($user, __('Assigned to Shift'), $message, true); engelsystem_email_to_user($user, __('Assigned to Shift'), $message, true);
} }
@ -135,13 +136,13 @@ function mail_shift_removed($user, $shift)
return; return;
} }
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
$message = __('You have been removed from a Shift:') . "\n"; $message = __('You have been removed from a Shift:') . "\n";
$message .= $shift['name'] . "\n"; $message .= $shift['name'] . "\n";
$message .= $shift['title'] . "\n"; $message .= $shift['title'] . "\n";
$message .= date('Y-m-d H:i', $shift['start']) . ' - ' . date('H:i', $shift['end']) . "\n"; $message .= date('Y-m-d H:i', $shift['start']) . ' - ' . date('H:i', $shift['end']) . "\n";
$message .= $room['Name'] . "\n"; $message .= $room->name . "\n";
engelsystem_email_to_user($user, __('Removed from Shift'), $message, true); engelsystem_email_to_user($user, __('Removed from Shift'), $message, true);
} }

View File

@ -11,8 +11,9 @@ use Engelsystem\Database\DB;
* *
* @param int $shift_id The shift. Can be null, but then a room_id must be given. * @param int $shift_id The shift. Can be null, but then a room_id must be given.
* @param int $angeltype_id The angeltype * @param int $angeltype_id The angeltype
* @param int $room_id The room. Can be null, but then a shift_id must be given. * @param int|null $room_id The room. Can be null, but then a shift_id must be given.
* @param int $count How many angels are needed? * @param int $count How many angels are needed?
*
* @return int|false * @return int|false
*/ */
function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count) function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count)

View File

@ -1,7 +1,8 @@
<?php <?php
use Engelsystem\Database\DB; use Engelsystem\Models\Room;
use Engelsystem\ValidationResult; use Engelsystem\ValidationResult;
use Illuminate\Support\Collection;
/** /**
* Validate a name for a room. * Validate a name for a room.
@ -10,76 +11,74 @@ use Engelsystem\ValidationResult;
* @param int $room_id The room id * @param int $room_id The room id
* @return ValidationResult * @return ValidationResult
*/ */
function Room_validate_name($name, $room_id) function Room_validate_name(string $name, int $room_id)
{ {
$valid = true; $valid = true;
if (empty($name)) { if (empty($name)) {
$valid = false; $valid = false;
} }
if (count(DB::select('SELECT RID FROM `Room` WHERE `Name`=? AND NOT `RID`=?', [ $roomCount = Room::query()
$name, ->where('name', $name)
$room_id ->where('id', '!=', $room_id)
])) > 0) { ->count();
if ($roomCount) {
$valid = false; $valid = false;
} }
return new ValidationResult($valid, $name); return new ValidationResult($valid, $name);
} }
/** /**
* returns a list of rooms. * returns a list of rooms.
* *
* @return array * @return Room[]|Collection
*/ */
function Rooms() function Rooms()
{ {
return DB::select('SELECT * FROM `Room` ORDER BY `Name`'); return Room::all()->sortBy('name');
} }
/** /**
* Returns Room id array * Returns Room id array
* *
* @return array * @return int[]
*/ */
function Room_ids() function Room_ids()
{ {
$result = DB::select('SELECT `RID` FROM `Room`'); return Room::query()
return select_array($result, 'RID', 'RID'); ->select('id')
->pluck('id')
->toArray();
} }
/** /**
* Delete a room * Delete a room
* *
* @param int $room_id * @param Room $room
*/ */
function Room_delete($room_id) function Room_delete(Room $room)
{ {
$room = Room($room_id); $room->delete();
DB::delete('DELETE FROM `Room` WHERE `RID` = ?', [ engelsystem_log('Room deleted: ' . $room->name);
$room_id
]);
engelsystem_log('Room deleted: ' . $room['Name']);
} }
/** /**
* Create a new room * Create a new room
* *
* @param string $name Name of the room * @param string $name Name of the room
* @param string $map_url URL to a map tha can be displayed in an iframe * @param string|null $map_url URL to a map tha can be displayed in an iframe
* @param string description Markdown description * @param string|null $description
* @return false|int *
* @return null|int
*/ */
function Room_create($name, $map_url, $description) function Room_create(string $name, string $map_url = null, string $description = null)
{ {
DB::insert(' $room = new Room();
INSERT INTO `Room` (`Name`, `map_url`, `description`) $room->name = $name;
VALUES (?, ?, ?) $room->description = $description;
', [ $room->map_url = $map_url;
$name, $room->save();
$map_url,
$description
]);
$result = DB::getPdo()->lastInsertId();
engelsystem_log( engelsystem_log(
'Room created: ' . $name 'Room created: ' . $name
@ -87,7 +86,7 @@ function Room_create($name, $map_url, $description)
. ', description: ' . $description . ', description: ' . $description
); );
return $result; return $room->id;
} }
/** /**
@ -95,49 +94,20 @@ function Room_create($name, $map_url, $description)
* *
* @param int $room_id The rooms id * @param int $room_id The rooms id
* @param string $name Name of the room * @param string $name Name of the room
* @param string $map_url URL to a map tha can be displayed in an iframe * @param string|null $map_url URL to a map tha can be displayed in an iframe
* @param string $description Markdown description * @param string|null $description Markdown description
* @return int
*/ */
function Room_update($room_id, $name, $map_url, $description) function Room_update(int $room_id, string $name, string $map_url = null, string $description = null)
{ {
$result = DB::update(' $room = Room::find($room_id);
UPDATE `Room` $room->name = $name;
SET $room->description = $description ?: null;
`Name`=?, $room->map_url = $map_url ?: null;
`map_url`=?, $room->save();
`description`=?
WHERE `RID`=?
LIMIT 1', [
$name,
$map_url,
$description,
$room_id
]);
engelsystem_log( engelsystem_log(
'Room updated: ' . $name . 'Room updated: ' . $name .
', map_url: ' . $map_url . ', map_url: ' . $map_url .
', description: ' . $description ', description: ' . $description
); );
return $result;
}
/**
* Returns room by id.
*
* @param int $room_id RID
* @return array|null
*/
function Room($room_id)
{
$room = DB::selectOne('
SELECT *
FROM `Room`
WHERE `RID` = ?', [
$room_id
]);
return empty($room) ? null : $room;
} }

View File

@ -2,27 +2,9 @@
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/**
* Returns an array with the attributes of shift entries.
* FIXME! Needs entity object.
*
* @return array
*/
function ShiftEntry_new()
{
return [
'id' => null,
'SID' => null,
'TID' => null,
'UID' => null,
'Comment' => null,
'freeloaded_comment' => null,
'freeloaded' => false
];
}
/** /**
* Counts all freeloaded shifts. * Counts all freeloaded shifts.
* *
@ -76,7 +58,7 @@ function ShiftEntry_create($shift_entry)
$user = User::find($shift_entry['UID']); $user = User::find($shift_entry['UID']);
$shift = Shift($shift_entry['SID']); $shift = Shift($shift_entry['SID']);
$shifttype = ShiftType($shift['shifttype_id']); $shifttype = ShiftType($shift['shifttype_id']);
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
$angeltype = AngelType($shift_entry['TID']); $angeltype = AngelType($shift_entry['TID']);
$result = DB::insert(' $result = DB::insert('
INSERT INTO `ShiftEntry` ( INSERT INTO `ShiftEntry` (
@ -102,7 +84,7 @@ function ShiftEntry_create($shift_entry)
'User ' . User_Nick_render($user, true) 'User ' . User_Nick_render($user, true)
. ' signed up for shift ' . $shift['name'] . ' signed up for shift ' . $shift['name']
. ' (' . $shifttype['name'] . ')' . ' (' . $shifttype['name'] . ')'
. ' at ' . $room['Name'] . ' at ' . $room->name
. ' from ' . date('Y-m-d H:i', $shift['start']) . ' from ' . date('Y-m-d H:i', $shift['start'])
. ' to ' . date('Y-m-d H:i', $shift['end']) . ' to ' . date('Y-m-d H:i', $shift['end'])
. ' as ' . $angeltype['name'] . ' as ' . $angeltype['name']
@ -161,14 +143,14 @@ function ShiftEntry_delete($shiftEntry)
$signout_user = User::find($shiftEntry['UID']); $signout_user = User::find($shiftEntry['UID']);
$shift = Shift($shiftEntry['SID']); $shift = Shift($shiftEntry['SID']);
$shifttype = ShiftType($shift['shifttype_id']); $shifttype = ShiftType($shift['shifttype_id']);
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
$angeltype = AngelType($shiftEntry['TID']); $angeltype = AngelType($shiftEntry['TID']);
engelsystem_log( engelsystem_log(
'Shift signout: ' . User_Nick_render($signout_user, true) 'Shift signout: ' . User_Nick_render($signout_user, true)
. ' from shift ' . $shift['name'] . ' from shift ' . $shift['name']
. ' (' . $shifttype['name'] . ')' . ' (' . $shifttype['name'] . ')'
. ' at ' . $room['Name'] . ' at ' . $room->name
. ' from ' . date('Y-m-d H:i', $shift['start']) . ' from ' . date('Y-m-d H:i', $shift['start'])
. ' to ' . date('Y-m-d H:i', $shift['end']) . ' to ' . date('Y-m-d H:i', $shift['end'])
. ' as ' . $angeltype['name'] . ' as ' . $angeltype['name']

View File

@ -1,6 +1,7 @@
<?php <?php
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
use Engelsystem\ShiftSignupState; use Engelsystem\ShiftSignupState;
@ -74,14 +75,14 @@ function Shifts_free($start, $end)
} }
/** /**
* @param array|int $room * @param Room $room
* @return array[] * @return array[]
*/ */
function Shifts_by_room($room) function Shifts_by_room(Room $room)
{ {
return DB::select( return DB::select(
'SELECT * FROM `Shifts` WHERE `RID`=? ORDER BY `start`', 'SELECT * FROM `Shifts` WHERE `RID`=? ORDER BY `start`',
[is_array($room) ? $room['RID'] : $room] [$room->id]
); );
} }
@ -93,9 +94,9 @@ function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
{ {
$sql = ' $sql = '
SELECT * FROM ( SELECT * FROM (
SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` AS `room_name` SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `rooms`.`name` AS `room_name`
FROM `Shifts` FROM `Shifts`
JOIN `Room` USING (`RID`) JOIN `rooms` ON `Shifts`.`RID` = `rooms`.`id`
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id` JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id` = `Shifts`.`SID` JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id` = `Shifts`.`SID`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
@ -107,9 +108,9 @@ function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
UNION UNION
SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` AS `room_name` SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `rooms`.`name` AS `room_name`
FROM `Shifts` FROM `Shifts`
JOIN `Room` USING (`RID`) JOIN `rooms` ON `Shifts`.`RID` = `rooms`.`id`
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id` JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID` JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
@ -596,6 +597,8 @@ function Shifts_by_user($userId, $include_freeload_comments = false)
{ {
return DB::select(' return DB::select('
SELECT SELECT
`rooms`.*,
`rooms`.name AS Name,
`ShiftTypes`.`id` AS `shifttype_id`, `ShiftTypes`.`id` AS `shifttype_id`,
`ShiftTypes`.`name`, `ShiftTypes`.`name`,
`ShiftEntry`.`id`, `ShiftEntry`.`id`,
@ -606,12 +609,11 @@ function Shifts_by_user($userId, $include_freeload_comments = false)
`ShiftEntry`.`Comment`, `ShiftEntry`.`Comment`,
' . ($include_freeload_comments ? '`ShiftEntry`.`freeload_comment`, ' : '') . ' ' . ($include_freeload_comments ? '`ShiftEntry`.`freeload_comment`, ' : '') . '
`Shifts`.*, `Shifts`.*,
@@session.time_zone AS timezone, @@session.time_zone AS timezone
`Room`.*
FROM `ShiftEntry` FROM `ShiftEntry`
JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`)
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`) JOIN `rooms` ON (`Shifts`.`RID` = `rooms`.`id`)
WHERE `UID` = ? WHERE `UID` = ?
ORDER BY `start` ORDER BY `start`
', ',

View File

@ -2,6 +2,7 @@
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
@ -143,7 +144,7 @@ function UserWorkLog_from_shift($shift)
return; return;
} }
$room = Room($shift['RID']); $room = Room::find($shift['RID']);
foreach ($shift['ShiftEntry'] as $entry) { foreach ($shift['ShiftEntry'] as $entry) {
if ($entry['freeloaded']) { if ($entry['freeloaded']) {
continue; continue;
@ -173,7 +174,7 @@ function UserWorkLog_from_shift($shift)
$shift['name'], $shift['name'],
$shift['title'], $shift['title'],
$type['name'], $type['name'],
$room['Name'], $room->name,
Carbon::createFromTimestamp($shift['start'])->format(__('m/d/Y h:i a')), Carbon::createFromTimestamp($shift['start'])->format(__('m/d/Y h:i a')),
Carbon::createFromTimestamp($shift['end'])->format(__('m/d/Y h:i a')) Carbon::createFromTimestamp($shift['end'])->format(__('m/d/Y h:i a'))
), ),

View File

@ -122,7 +122,7 @@ function admin_groups()
} }
$group = DB::selectOne('SELECT * FROM `Groups` WHERE `UID`=? LIMIT 1', [$group_id]); $group = DB::selectOne('SELECT * FROM `Groups` WHERE `UID`=? LIMIT 1', [$group_id]);
$privileges = $request->postData('privileges'); $privileges = $request->request->all('privileges');
if (!is_array($privileges)) { if (!is_array($privileges)) {
$privileges = []; $privileges = [];
} }

View File

@ -1,4 +1,7 @@
<?php <?php
use Engelsystem\Models\Room;
/** /**
* @return string * @return string
*/ */
@ -19,15 +22,15 @@ function admin_rooms()
foreach ($rooms_source as $room) { foreach ($rooms_source as $room) {
$rooms[] = [ $rooms[] = [
'name' => Room_name_render($room), 'name' => Room_name_render($room),
'map_url' => glyph_bool(!empty($room['map_url'])), 'map_url' => glyph_bool($room->map_url),
'actions' => table_buttons([ 'actions' => table_buttons([
button( button(
page_link_to('admin_rooms', ['show' => 'edit', 'id' => $room['RID']]), page_link_to('admin_rooms', ['show' => 'edit', 'id' => $room->id]),
__('edit'), __('edit'),
'btn-xs' 'btn-xs'
), ),
button( button(
page_link_to('admin_rooms', ['show' => 'delete', 'id' => $room['RID']]), page_link_to('admin_rooms', ['show' => 'delete', 'id' => $room->id]),
__('delete'), __('delete'),
'btn-xs' 'btn-xs'
) )
@ -52,15 +55,15 @@ function admin_rooms()
} }
if (test_request_int('id')) { if (test_request_int('id')) {
$room = Room($request->input('id')); $room = Room::find($request->input('id'));
if (empty($room)) { if (!$room) {
throw_redirect(page_link_to('admin_rooms')); throw_redirect(page_link_to('admin_rooms'));
} }
$room_id = $request->input('id'); $room_id = $room->id;
$name = $room['Name']; $name = $room->name;
$map_url = $room['map_url']; $map_url = $room->map_url;
$description = $room['description']; $description = $room->description;
$needed_angeltypes = NeededAngelTypes_by_room($room_id); $needed_angeltypes = NeededAngelTypes_by_room($room_id);
foreach ($needed_angeltypes as $needed_angeltype) { foreach ($needed_angeltypes as $needed_angeltype) {
@ -173,7 +176,8 @@ function admin_rooms()
], true); ], true);
} elseif ($request->input('show') == 'delete') { } elseif ($request->input('show') == 'delete') {
if ($request->hasPostData('ack')) { if ($request->hasPostData('ack')) {
$shifts = Shifts_by_room($room_id); $room = Room::find($room_id);
$shifts = Shifts_by_room($room);
foreach ($shifts as $shift) { foreach ($shifts as $shift) {
$shift = Shift($shift['SID']); $shift = Shift($shift['SID']);
@ -181,7 +185,7 @@ function admin_rooms()
mail_shift_delete($shift); mail_shift_delete($shift);
} }
Room_delete($room_id); Room_delete($room);
success(sprintf(__('Room %s deleted.'), $name)); success(sprintf(__('Room %s deleted.'), $name));
throw_redirect(page_link_to('admin_rooms')); throw_redirect(page_link_to('admin_rooms'));

View File

@ -1,6 +1,7 @@
<?php <?php
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
use Engelsystem\Models\Room;
/** /**
* @return string * @return string
@ -35,7 +36,7 @@ function admin_shifts()
$rooms = Rooms(); $rooms = Rooms();
$room_array = []; $room_array = [];
foreach ($rooms as $room) { foreach ($rooms as $room) {
$room_array[$room['RID']] = $room['Name']; $room_array[$room->id] = $room->name;
} }
// Engeltypen laden // Engeltypen laden
@ -78,7 +79,7 @@ function admin_shifts()
$rid = $request->input('rid'); $rid = $request->input('rid');
} else { } else {
$valid = false; $valid = false;
$rid = $rooms[0]['RID']; $rid = $rooms->first()->id;
error(__('Please select a location.')); error(__('Please select a location.'));
} }
@ -173,7 +174,8 @@ function admin_shifts()
if ($valid) { if ($valid) {
if ($angelmode == 'location') { if ($angelmode == 'location') {
$needed_angel_types = []; $needed_angel_types = [];
$needed_angel_types_location = DB::select(' $needed_angel_types_location = DB::select(
'
SELECT `angel_type_id`, `count` SELECT `angel_type_id`, `count`
FROM `NeededAngelTypes` FROM `NeededAngelTypes`
WHERE `room_id`=? WHERE `room_id`=?
@ -294,7 +296,7 @@ function admin_shifts()
. ' - ' . ' - '
. date('H:i', $shift['end']) . date('H:i', $shift['end'])
. '<br />' . '<br />'
. Room_name_render(Room($shift['RID'])), . Room_name_render(Room::find($shift['RID'])),
'title' => 'title' =>
ShiftType_name_render(ShiftType($shifttype_id)) ShiftType_name_render(ShiftType($shifttype_id))
. ($shift['title'] ? '<br />' . $shift['title'] : ''), . ($shift['title'] ? '<br />' . $shift['title'] : ''),

View File

@ -12,6 +12,7 @@ use Engelsystem\Helpers\Schedule\Schedule;
use Engelsystem\Helpers\Schedule\XmlParser; use Engelsystem\Helpers\Schedule\XmlParser;
use Engelsystem\Http\Request; use Engelsystem\Http\Request;
use Engelsystem\Http\Response; use Engelsystem\Http\Response;
use Engelsystem\Models\Room as RoomModel;
use Engelsystem\Models\Shifts\Schedule as ScheduleUrl; use Engelsystem\Models\Shifts\Schedule as ScheduleUrl;
use Engelsystem\Models\Shifts\ScheduleShift; use Engelsystem\Models\Shifts\ScheduleShift;
use ErrorException; use ErrorException;
@ -217,13 +218,9 @@ class ImportSchedule extends BaseController
*/ */
protected function createRoom(Room $room): void protected function createRoom(Room $room): void
{ {
$this->db $roomModel = new RoomModel();
->table('Room') $roomModel->name = $room->getName();
->insert( $roomModel->save();
[
'Name' => $room->getName(),
]
);
$this->log('Created schedule room "{room}"', ['room' => $room->getName()]); $this->log('Created schedule room "{room}"', ['room' => $room->getName()]);
} }
@ -231,10 +228,10 @@ class ImportSchedule extends BaseController
/** /**
* @param Event $shift * @param Event $shift
* @param int $shiftTypeId * @param int $shiftTypeId
* @param stdClass $room * @param RoomModel $room
* @param ScheduleUrl $scheduleUrl * @param ScheduleUrl $scheduleUrl
*/ */
protected function createEvent(Event $shift, int $shiftTypeId, stdClass $room, ScheduleUrl $scheduleUrl): void protected function createEvent(Event $shift, int $shiftTypeId, RoomModel $room, ScheduleUrl $scheduleUrl): void
{ {
$user = auth()->user(); $user = auth()->user();
@ -276,9 +273,9 @@ class ImportSchedule extends BaseController
/** /**
* @param Event $shift * @param Event $shift
* @param int $shiftTypeId * @param int $shiftTypeId
* @param stdClass $room * @param RoomModel $room
*/ */
protected function updateEvent(Event $shift, int $shiftTypeId, stdClass $room): void protected function updateEvent(Event $shift, int $shiftTypeId, RoomModel $room): void
{ {
$user = auth()->user(); $user = auth()->user();
@ -335,7 +332,7 @@ class ImportSchedule extends BaseController
/** /**
* @param Request $request * @param Request $request
* @return Event[]|Room[]|ScheduleUrl|Schedule|string * @return Event[]|Room[]|RoomModel[]|ScheduleUrl|Schedule|string
* @throws ErrorException * @throws ErrorException
*/ */
protected function getScheduleData(Request $request) protected function getScheduleData(Request $request)
@ -510,11 +507,11 @@ class ImportSchedule extends BaseController
} }
/** /**
* @return Collection * @return RoomModel[]|Collection
*/ */
protected function getAllRooms(): Collection protected function getAllRooms(): Collection
{ {
return new Collection($this->db->select('SELECT RID as id, Name as name FROM Room')); return RoomModel::all();
} }
/** /**
@ -561,7 +558,7 @@ class ImportSchedule extends BaseController
r.Name AS room_name, r.Name AS room_name,
s.URL as url s.URL as url
FROM Shifts AS s FROM Shifts AS s
LEFT JOIN Room r on s.RID = r.RID LEFT JOIN rooms r on s.RID = r.id
WHERE SID = ? WHERE SID = ?
', ',
[$id] [$id]

View File

@ -56,13 +56,13 @@ function user_myshifts()
`ShiftEntry`.`UID`, `ShiftEntry`.`UID`,
`ShiftTypes`.`name`, `ShiftTypes`.`name`,
`Shifts`.*, `Shifts`.*,
`Room`.`Name`, `rooms`.`name` as room_name,
`AngelTypes`.`name` AS `angel_type` `AngelTypes`.`name` AS `angel_type`
FROM `ShiftEntry` FROM `ShiftEntry`
JOIN `AngelTypes` ON (`ShiftEntry`.`TID` = `AngelTypes`.`id`) JOIN `AngelTypes` ON (`ShiftEntry`.`TID` = `AngelTypes`.`id`)
JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`)
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`) JOIN `rooms` ON (`Shifts`.`RID` = `rooms`.`id`)
WHERE `ShiftEntry`.`id`=? WHERE `ShiftEntry`.`id`=?
AND `UID`=? AND `UID`=?
LIMIT 1 LIMIT 1
@ -116,7 +116,7 @@ function user_myshifts()
return ShiftEntry_edit_view( return ShiftEntry_edit_view(
$shifts_user, $shifts_user,
date('Y-m-d H:i', $shift['start']) . ', ' . shift_length($shift), date('Y-m-d H:i', $shift['start']) . ', ' . shift_length($shift),
$shift['Name'], $shift['room_name'],
$shift['name'], $shift['name'],
$shift['angel_type'], $shift['angel_type'],
$shift['Comment'], $shift['Comment'],

View File

@ -1,7 +1,9 @@
<?php <?php
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
use Engelsystem\Models\Room;
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
use Illuminate\Support\Collection;
/** /**
* @return string * @return string
@ -95,17 +97,16 @@ function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $da
} }
/** /**
* @return array * @return Room[]|Collection
*/ */
function load_rooms() function load_rooms()
{ {
$rooms = DB::select( $rooms = Rooms();
'SELECT `RID` AS `id`, `Name` AS `name` FROM `Room` ORDER BY `Name`' if ($rooms->isEmpty()) {
);
if (empty($rooms)) {
error(__('The administration has not configured any rooms yet.')); error(__('The administration has not configured any rooms yet.'));
throw_redirect(page_link_to('/')); throw_redirect(page_link_to('/'));
} }
return $rooms; return $rooms;
} }
@ -188,7 +189,7 @@ function view_user_shifts()
} }
if (!$session->has('shifts-filter')) { if (!$session->has('shifts-filter')) {
$room_ids = collect($rooms)->pluck('id')->toArray(); $room_ids = $rooms->pluck('id')->toArray();
$shiftsFilter = new ShiftsFilter(auth()->can('user_shifts_admin'), $room_ids, $ownTypes); $shiftsFilter = new ShiftsFilter(auth()->can('user_shifts_admin'), $room_ids, $ownTypes);
$session->set('shifts-filter', $shiftsFilter->sessionExport()); $session->set('shifts-filter', $shiftsFilter->sessionExport());
} }

View File

@ -176,7 +176,7 @@ function make_room_navigation($menu)
$room_menu[] = toolbar_item_divider(); $room_menu[] = toolbar_item_divider();
} }
foreach ($rooms as $room) { foreach ($rooms as $room) {
$room_menu[] = toolbar_item_link(room_link($room), 'map-marker', $room['Name']); $room_menu[] = toolbar_item_link(room_link($room), 'map-marker', $room->name);
} }
if (count($room_menu) > 0) { if (count($room_menu) > 0) {
$menu[] = toolbar_dropdown('map-marker', __('Rooms'), $room_menu); $menu[] = toolbar_dropdown('map-marker', __('Rooms'), $room_menu);

View File

@ -2,7 +2,9 @@
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Http\Exceptions\HttpTemporaryRedirect; use Engelsystem\Http\Exceptions\HttpTemporaryRedirect;
use Engelsystem\Models\BaseModel;
use Engelsystem\ValidationResult; use Engelsystem\ValidationResult;
use Illuminate\Support\Collection;
/** /**
* Provide page/request helper functions * Provide page/request helper functions
@ -75,14 +77,20 @@ function raw_output($output = '')
/** /**
* Helper function for transforming list of entities into array for select boxes. * Helper function for transforming list of entities into array for select boxes.
* *
* @param array $data The data array * @param array|Collection $data The data array
* @param string $key_name name of the column to use as id/key * @param string $key_name name of the column to use as id/key
* @param string $value_name name of the column to use as displayed value * @param string $value_name name of the column to use as displayed value
* *
* @return array * @return array|Collection
*/ */
function select_array($data, $key_name, $value_name) function select_array($data, $key_name, $value_name)
{ {
if ($data instanceof Collection) {
return $data->mapToDictionary(function (BaseModel $model) use ($key_name, $value_name) {
return [$model->{$key_name} => $model->{$value_name}];
});
}
$return = []; $return = [];
foreach ($data as $value) { foreach ($data as $value) {
$return[$value[$key_name]] = $value[$value_name]; $return[$value[$key_name]] = $value[$value_name];

View File

@ -1,16 +1,17 @@
<?php <?php
use Engelsystem\Models\Room;
use Engelsystem\ShiftCalendarRenderer; use Engelsystem\ShiftCalendarRenderer;
use Engelsystem\ShiftsFilterRenderer; use Engelsystem\ShiftsFilterRenderer;
/** /**
* *
* @param array $room * @param Room $room
* @param ShiftsFilterRenderer $shiftsFilterRenderer * @param ShiftsFilterRenderer $shiftsFilterRenderer
* @param ShiftCalendarRenderer $shiftCalendarRenderer * @param ShiftCalendarRenderer $shiftCalendarRenderer
* @return string * @return string
*/ */
function Room_view($room, ShiftsFilterRenderer $shiftsFilterRenderer, ShiftCalendarRenderer $shiftCalendarRenderer) function Room_view(Room $room, ShiftsFilterRenderer $shiftsFilterRenderer, ShiftCalendarRenderer $shiftCalendarRenderer)
{ {
$user = auth()->user(); $user = auth()->user();
@ -20,26 +21,26 @@ function Room_view($room, ShiftsFilterRenderer $shiftsFilterRenderer, ShiftCalen
} }
$description = ''; $description = '';
if (!empty($room['description'])) { if ($room->description) {
$description = '<h3>' . __('Description') . '</h3>'; $description = '<h3>' . __('Description') . '</h3>';
$parsedown = new Parsedown(); $parsedown = new Parsedown();
$description .= '<div class="well">' . $parsedown->parse($room['description']) . '</div>'; $description .= '<div class="well">' . $parsedown->parse($room->description) . '</div>';
} }
$tabs = []; $tabs = [];
if (!empty($room['map_url'])) { if ($room->map_url) {
$tabs[__('Map')] = sprintf( $tabs[__('Map')] = sprintf(
'<div class="map">' '<div class="map">'
. '<iframe style="width: 100%%; min-height: 400px; border: 0 none;" src="%s"></iframe>' . '<iframe style="width: 100%%; min-height: 400px; border: 0 none;" src="%s"></iframe>'
. '</div>', . '</div>',
$room['map_url'] $room->map_url
); );
} }
$tabs[__('Shifts')] = div('first', [ $tabs[__('Shifts')] = div('first', [
$shiftsFilterRenderer->render(page_link_to('rooms', [ $shiftsFilterRenderer->render(page_link_to('rooms', [
'action' => 'view', 'action' => 'view',
'room_id' => $room['RID'] 'room_id' => $room->id
])), ])),
$shiftCalendarRenderer->render() $shiftCalendarRenderer->render()
]); ]);
@ -50,7 +51,7 @@ function Room_view($room, ShiftsFilterRenderer $shiftsFilterRenderer, ShiftCalen
$selected_tab = count($tabs) - 1; $selected_tab = count($tabs) - 1;
} }
return page_with_title(glyph('map-marker') . $room['Name'], [ return page_with_title(glyph('map-marker') . $room->name, [
$assignNotice, $assignNotice,
$description, $description,
auth()->can('admin_rooms') ? buttons([ auth()->can('admin_rooms') ? buttons([
@ -71,14 +72,14 @@ function Room_view($room, ShiftsFilterRenderer $shiftsFilterRenderer, ShiftCalen
/** /**
* *
* @param array $room * @param Room $room
* @return string * @return string
*/ */
function Room_name_render($room) function Room_name_render(Room $room)
{ {
if (auth()->can('view_rooms')) { if (auth()->can('view_rooms')) {
return '<a href="' . room_link($room) . '">' . glyph('map-marker') . $room['Name'] . '</a>'; return '<a href="' . room_link($room) . '">' . glyph('map-marker') . $room->name . '</a>';
} }
return glyph('map-marker') . $room['Name']; return glyph('map-marker') . $room->name;
} }

View File

@ -2,6 +2,8 @@
namespace Engelsystem; namespace Engelsystem;
use Engelsystem\Models\Room;
class ShiftCalendarRenderer class ShiftCalendarRenderer
{ {
/** /**
@ -77,10 +79,10 @@ class ShiftCalendarRenderer
foreach ($shifts as $shift) { foreach ($shifts as $shift) {
$room_id = $shift['RID']; $room_id = $shift['RID'];
$header = Room_name_render([ $room = new Room();
'RID' => $room_id, $room->name = $shift['room_name'];
'Name' => $shift['room_name'] $room->setAttribute('id', $room_id);
]); $header = Room_name_render($room);
if (!isset($lanes[$room_id])) { if (!isset($lanes[$room_id])) {
// initialize room with one lane // initialize room with one lane
$lanes[$room_id] = [ $lanes[$room_id] = [

View File

@ -2,6 +2,7 @@
namespace Engelsystem; namespace Engelsystem;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
@ -36,9 +37,14 @@ class ShiftCalendarShiftRenderer
$blocks = ceil(($shift['end'] - $shift['start']) / ShiftCalendarRenderer::SECONDS_PER_ROW); $blocks = ceil(($shift['end'] - $shift['start']) / ShiftCalendarRenderer::SECONDS_PER_ROW);
$blocks = max(1, $blocks); $blocks = max(1, $blocks);
$room = new Room();
$room->name = $shift['room_name'];
$room->setAttribute('id', $shift['RID']);
return [ return [
$blocks, $blocks,
div('shift-card" style="height: ' div(
'shift-card" style="height: '
. ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN)
. 'px;', . 'px;',
div( div(
@ -47,10 +53,7 @@ class ShiftCalendarShiftRenderer
$this->renderShiftHead($shift, $class, $shift_signup_state->getFreeEntries()), $this->renderShiftHead($shift, $class, $shift_signup_state->getFreeEntries()),
div('panel-body', [ div('panel-body', [
$info_text, $info_text,
Room_name_render([ Room_name_render($room)
'RID' => $shift['RID'],
'Name' => $shift['room_name']
])
]), ]),
$shifts_row $shifts_row
] ]

View File

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
@ -72,14 +73,14 @@ function ShiftEntry_delete_title()
* Admin puts user into shift. * Admin puts user into shift.
* *
* @param array $shift * @param array $shift
* @param array $room * @param Room $room
* @param array $angeltype * @param array $angeltype
* @param array $angeltypes_select * @param array $angeltypes_select
* @param User $signup_user * @param User $signup_user
* @param array $users_select * @param array $users_select
* @return string * @return string
*/ */
function ShiftEntry_create_view_admin($shift, $room, $angeltype, $angeltypes_select, $signup_user, $users_select) function ShiftEntry_create_view_admin($shift, Room $room, $angeltype, $angeltypes_select, $signup_user, $users_select)
{ {
return page_with_title( return page_with_title(
ShiftEntry_create_title() . ': ' . $shift['name'] ShiftEntry_create_title() . ': ' . $shift['name']
@ -99,13 +100,13 @@ function ShiftEntry_create_view_admin($shift, $room, $angeltype, $angeltypes_sel
* Supporter puts user into shift. * Supporter puts user into shift.
* *
* @param array $shift * @param array $shift
* @param array $room * @param Room $room
* @param array $angeltype * @param array $angeltype
* @param User $signup_user * @param User $signup_user
* @param array $users_select * @param array $users_select
* @return string * @return string
*/ */
function ShiftEntry_create_view_supporter($shift, $room, $angeltype, $signup_user, $users_select) function ShiftEntry_create_view_supporter($shift, Room $room, $angeltype, $signup_user, $users_select)
{ {
return page_with_title(ShiftEntry_create_title() . ': ' . $shift['name'] return page_with_title(ShiftEntry_create_title() . ': ' . $shift['name']
. ' <small class="moment-countdown" data-timestamp="' . $shift['start'] . '">%c</small>', . ' <small class="moment-countdown" data-timestamp="' . $shift['start'] . '">%c</small>',
@ -124,12 +125,12 @@ function ShiftEntry_create_view_supporter($shift, $room, $angeltype, $signup_use
* User joining a shift. * User joining a shift.
* *
* @param array $shift * @param array $shift
* @param array $room * @param Room $room
* @param array $angeltype * @param array $angeltype
* @param string $comment * @param string $comment
* @return string * @return string
*/ */
function ShiftEntry_create_view_user($shift, $room, $angeltype, $comment) function ShiftEntry_create_view_user($shift, Room $room, $angeltype, $comment)
{ {
return page_with_title(ShiftEntry_create_title() . ': ' . $shift['name'] return page_with_title(ShiftEntry_create_title() . ': ' . $shift['name']
. ' <small class="moment-countdown" data-timestamp="' . $shift['start'] . '">%c</small>', . ' <small class="moment-countdown" data-timestamp="' . $shift['start'] . '">%c</small>',

View File

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\ShiftSignupState; use Engelsystem\ShiftSignupState;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -8,10 +9,10 @@ use Illuminate\Support\Collection;
* Renders the basic shift view header. * Renders the basic shift view header.
* *
* @param array $shift * @param array $shift
* @param array $room * @param Room $room
* @return string HTML * @return string HTML
*/ */
function Shift_view_header($shift, $room) function Shift_view_header($shift, Room $room)
{ {
return div('row', [ return div('row', [
div('col-sm-3 col-xs-6', [ div('col-sm-3 col-xs-6', [
@ -96,12 +97,12 @@ function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null)
/** /**
* @param array $shift * @param array $shift
* @param array $shifttype * @param array $shifttype
* @param array $room * @param Room $room
* @param array[] $angeltypes_source * @param array[] $angeltypes_source
* @param ShiftSignupState $shift_signup_state * @param ShiftSignupState $shift_signup_state
* @return string * @return string
*/ */
function Shift_view($shift, $shifttype, $room, $angeltypes_source, ShiftSignupState $shift_signup_state) function Shift_view($shift, $shifttype, Room $room, $angeltypes_source, ShiftSignupState $shift_signup_state)
{ {
$shift_admin = auth()->can('admin_shifts'); $shift_admin = auth()->can('admin_shifts');
$user_shift_admin = auth()->can('user_shifts_admin'); $user_shift_admin = auth()->can('user_shifts_admin');
@ -155,7 +156,7 @@ function Shift_view($shift, $shifttype, $room, $angeltypes_source, ShiftSignupSt
$shift_admin ? button(shift_edit_link($shift), glyph('pencil') . __('edit')) : '', $shift_admin ? button(shift_edit_link($shift), glyph('pencil') . __('edit')) : '',
$shift_admin ? button(shift_delete_link($shift), glyph('trash') . __('delete')) : '', $shift_admin ? button(shift_delete_link($shift), glyph('trash') . __('delete')) : '',
$admin_shifttypes ? button(shifttype_link($shifttype), $shifttype['name']) : '', $admin_shifttypes ? button(shifttype_link($shifttype), $shifttype['name']) : '',
$admin_rooms ? button(room_link($room), glyph('map-marker') . $room['Name']) : '', $admin_rooms ? button(room_link($room), glyph('map-marker') . $room->name) : '',
]; ];
} }
$buttons[] = button(user_link(auth()->user()->id), '<span class="icon-icon_angel"></span> ' . __('My shifts')); $buttons[] = button(user_link(auth()->user()->id), '<span class="icon-icon_angel"></span> ' . __('My shifts'));

View File

@ -1,6 +1,7 @@
<?php <?php
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
@ -377,7 +378,7 @@ function User_view_myshift($shift, $user_source, $its_me)
. ' - ' . ' - '
. date('H:i', $shift['end']), . date('H:i', $shift['end']),
'duration' => sprintf('%.2f', ($shift['end'] - $shift['start']) / 3600) . '&nbsp;h', 'duration' => sprintf('%.2f', ($shift['end'] - $shift['start']) / 3600) . '&nbsp;h',
'room' => Room_name_render($shift), 'room' => Room_name_render(Room::find($shift['RID'])),
'shift_info' => $shift_info, 'shift_info' => $shift_info,
'comment' => '' 'comment' => ''
]; ];

View File

@ -151,6 +151,7 @@ class Controller extends BaseController
] + $userTshirtSizes, ] + $userTshirtSizes,
'locales' => ['type' => 'gauge', 'help' => 'The locales users have configured'] + $userLocales, 'locales' => ['type' => 'gauge', 'help' => 'The locales users have configured'] + $userLocales,
'themes' => ['type' => 'gauge', 'help' => 'The themes users have configured'] + $userThemes, 'themes' => ['type' => 'gauge', 'help' => 'The themes users have configured'] + $userThemes,
'rooms' => ['type' => 'gauge', $this->stats->rooms()],
'shifts' => ['type' => 'gauge', $this->stats->shifts()], 'shifts' => ['type' => 'gauge', $this->stats->shifts()],
'announcements' => [ 'announcements' => [
'type' => 'gauge', 'type' => 'gauge',

View File

@ -12,6 +12,7 @@ use Engelsystem\Models\Message;
use Engelsystem\Models\News; use Engelsystem\Models\News;
use Engelsystem\Models\NewsComment; use Engelsystem\Models\NewsComment;
use Engelsystem\Models\Question; use Engelsystem\Models\Question;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\PasswordReset; use Engelsystem\Models\User\PasswordReset;
use Engelsystem\Models\User\PersonalData; use Engelsystem\Models\User\PersonalData;
use Engelsystem\Models\User\Settings; use Engelsystem\Models\User\Settings;
@ -364,6 +365,15 @@ class Stats
); );
} }
/**
* @return int
*/
public function rooms(): int
{
return Room::query()
->count();
}
/** /**
* @return int * @return int
* @codeCoverageIgnore * @codeCoverageIgnore

36
src/Models/Room.php Normal file
View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Models;
use Carbon\Carbon;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
* @property int $id
* @property string $name
* @property string $map_url
* @property string $description
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @method static QueryBuilder|Room[] whereId($value)
* @method static QueryBuilder|Room[] whereName($value)
* @method static QueryBuilder|Room[] whereMapUrl($value)
* @method static QueryBuilder|Room[] whereDescription($value)
* @method static QueryBuilder|Room[] whereCreatedAt($value)
* @method static QueryBuilder|Room[] whereUpdatedAt($value)
*/
class Room extends BaseModel
{
/** @var bool Enable timestamps */
public $timestamps = true;
/** @var array */
protected $fillable = [
'name',
'map_url',
'description',
];
}

View File

@ -1,45 +0,0 @@
<?php
namespace Engelsystem\Test\Feature\Model;
use Engelsystem\Test\Feature\ApplicationFeatureTest;
class RoomModelTest extends ApplicationFeatureTest
{
/** @var int */
private $room_id = null;
/**
* @covers \Room_create
*/
public function createRoom()
{
$this->room_id = Room_create('test', null, null);
}
/**
* @covers \Room
*/
public function testRoom()
{
$this->createRoom();
$room = Room($this->room_id);
$this->assertNotEmpty($room);
$this->assertNotNull($room);
$this->assertEquals('test', $room['Name']);
$this->assertEmpty(Room(-1));
}
/**
* Cleanup
*/
protected function tearDown(): void
{
if ($this->room_id != null) {
Room_delete($this->room_id);
}
}
}

View File

@ -9,6 +9,7 @@ use Engelsystem\Models\Message;
use Engelsystem\Models\News; use Engelsystem\Models\News;
use Engelsystem\Models\NewsComment; use Engelsystem\Models\NewsComment;
use Engelsystem\Models\Question; use Engelsystem\Models\Question;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\PasswordReset; use Engelsystem\Models\User\PasswordReset;
use Engelsystem\Models\User\PersonalData; use Engelsystem\Models\User\PersonalData;
use Engelsystem\Models\User\Settings; use Engelsystem\Models\User\Settings;
@ -119,6 +120,20 @@ class StatsTest extends TestCase
], $themes->toArray()); ], $themes->toArray());
} }
/**
* @covers \Engelsystem\Controllers\Metrics\Stats::rooms
*/
public function testRooms()
{
(new Room(['name' => 'Room 1']))->save();
(new Room(['name' => 'Second room']))->save();
(new Room(['name' => 'Another room']))->save();
(new Room(['name' => 'Old room']))->save();
$stats = new Stats($this->database);
$this->assertEquals(4, $stats->rooms());
}
/** /**
* @covers \Engelsystem\Controllers\Metrics\Stats::announcements * @covers \Engelsystem\Controllers\Metrics\Stats::announcements
*/ */