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

View File

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

View File

@ -1,5 +1,6 @@
<?php
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User;
use Engelsystem\ShiftSignupState;
use Illuminate\Database\Eloquent\Collection;
@ -122,7 +123,7 @@ function shift_entry_create_controller_admin($shift, $angeltype)
$angeltypes_select[$a['id']] = $a['name'];
}
$room = Room($shift['RID']);
$room = Room::find($shift['RID']);
return [
ShiftEntry_create_title(),
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;
}
$room = Room($shift['RID']);
$room = Room::find($shift['RID']);
return [
ShiftEntry_create_title(),
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));
}
$room = Room($shift['RID']);
$room = Room::find($shift['RID']);
return [
ShiftEntry_create_title(),
ShiftEntry_create_view_user($shift, $room, $angeltype, $comment)
@ -345,7 +346,9 @@ function shift_entry_delete_controller()
$angeltype = AngelType($shiftEntry['TID']);
$signout_user = User::find($shiftEntry['UID']);
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));
}

View File

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

View File

@ -1,5 +1,6 @@
<?php
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User;
/**
@ -9,8 +10,8 @@ use Engelsystem\Models\User\User;
function mail_shift_change($old_shift, $new_shift)
{
$users = ShiftEntries_by_shift($old_shift['SID']);
$old_room = Room($old_shift['RID']);
$new_room = Room($new_shift['RID']);
$old_room = Room::find($old_shift['RID']);
$new_room = Room::find($new_shift['RID']);
$noticeable_changes = false;
@ -46,7 +47,7 @@ function mail_shift_change($old_shift, $new_shift)
}
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;
}
@ -61,7 +62,7 @@ function mail_shift_change($old_shift, $new_shift)
$message .= $new_shift['name'] . "\n";
$message .= $new_shift['title'] . "\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) {
$user = (new User())->forceFill($user);
@ -82,14 +83,14 @@ function mail_shift_change($old_shift, $new_shift)
function mail_shift_delete($shift)
{
$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 .= $shift['name'] . "\n";
$message .= $shift['title'] . "\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) {
$user = (new User())->forceFill($user);
@ -114,13 +115,13 @@ function mail_shift_assign($user, $shift)
return;
}
$room = Room($shift['RID']);
$room = Room::find($shift['RID']);
$message = __('You have been assigned to a Shift:') . "\n";
$message .= $shift['name'] . "\n";
$message .= $shift['title'] . "\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);
}
@ -135,13 +136,13 @@ function mail_shift_removed($user, $shift)
return;
}
$room = Room($shift['RID']);
$room = Room::find($shift['RID']);
$message = __('You have been removed from a Shift:') . "\n";
$message .= $shift['name'] . "\n";
$message .= $shift['title'] . "\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);
}

View File

@ -9,10 +9,11 @@ use Engelsystem\Database\DB;
/**
* Insert a new needed angel type.
*
* @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 $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 $shift_id The shift. Can be null, but then a room_id must be given.
* @param int $angeltype_id The angeltype
* @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?
*
* @return int|false
*/
function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count)

View File

@ -1,7 +1,8 @@
<?php
use Engelsystem\Database\DB;
use Engelsystem\Models\Room;
use Engelsystem\ValidationResult;
use Illuminate\Support\Collection;
/**
* Validate a name for a room.
@ -10,76 +11,74 @@ use Engelsystem\ValidationResult;
* @param int $room_id The room id
* @return ValidationResult
*/
function Room_validate_name($name, $room_id)
function Room_validate_name(string $name, int $room_id)
{
$valid = true;
if (empty($name)) {
$valid = false;
}
if (count(DB::select('SELECT RID FROM `Room` WHERE `Name`=? AND NOT `RID`=?', [
$name,
$room_id
])) > 0) {
$roomCount = Room::query()
->where('name', $name)
->where('id', '!=', $room_id)
->count();
if ($roomCount) {
$valid = false;
}
return new ValidationResult($valid, $name);
}
/**
* returns a list of rooms.
*
* @return array
* @return Room[]|Collection
*/
function Rooms()
{
return DB::select('SELECT * FROM `Room` ORDER BY `Name`');
return Room::all()->sortBy('name');
}
/**
* Returns Room id array
*
* @return array
* @return int[]
*/
function Room_ids()
{
$result = DB::select('SELECT `RID` FROM `Room`');
return select_array($result, 'RID', 'RID');
return Room::query()
->select('id')
->pluck('id')
->toArray();
}
/**
* Delete a room
*
* @param int $room_id
* @param Room $room
*/
function Room_delete($room_id)
function Room_delete(Room $room)
{
$room = Room($room_id);
DB::delete('DELETE FROM `Room` WHERE `RID` = ?', [
$room_id
]);
engelsystem_log('Room deleted: ' . $room['Name']);
$room->delete();
engelsystem_log('Room deleted: ' . $room->name);
}
/**
* Create a new 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 description Markdown description
* @return false|int
* @param string $name Name of the room
* @param string|null $map_url URL to a map tha can be displayed in an iframe
* @param string|null $description
*
* @return null|int
*/
function Room_create($name, $map_url, $description)
function Room_create(string $name, string $map_url = null, string $description = null)
{
DB::insert('
INSERT INTO `Room` (`Name`, `map_url`, `description`)
VALUES (?, ?, ?)
', [
$name,
$map_url,
$description
]);
$result = DB::getPdo()->lastInsertId();
$room = new Room();
$room->name = $name;
$room->description = $description;
$room->map_url = $map_url;
$room->save();
engelsystem_log(
'Room created: ' . $name
@ -87,57 +86,28 @@ function Room_create($name, $map_url, $description)
. ', description: ' . $description
);
return $result;
return $room->id;
}
/**
* Update a room
*
* @param int $room_id The rooms id
* @param string $name Name of the room
* @param string $map_url URL to a map tha can be displayed in an iframe
* @param string $description Markdown description
* @return int
* @param int $room_id The rooms id
* @param string $name Name of the room
* @param string|null $map_url URL to a map tha can be displayed in an iframe
* @param string|null $description Markdown description
*/
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('
UPDATE `Room`
SET
`Name`=?,
`map_url`=?,
`description`=?
WHERE `RID`=?
LIMIT 1', [
$name,
$map_url,
$description,
$room_id
]);
$room = Room::find($room_id);
$room->name = $name;
$room->description = $description ?: null;
$room->map_url = $map_url ?: null;
$room->save();
engelsystem_log(
'Room updated: ' . $name .
', map_url: ' . $map_url .
', 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 Engelsystem\Database\DB;
use Engelsystem\Models\Room;
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.
*
@ -76,7 +58,7 @@ function ShiftEntry_create($shift_entry)
$user = User::find($shift_entry['UID']);
$shift = Shift($shift_entry['SID']);
$shifttype = ShiftType($shift['shifttype_id']);
$room = Room($shift['RID']);
$room = Room::find($shift['RID']);
$angeltype = AngelType($shift_entry['TID']);
$result = DB::insert('
INSERT INTO `ShiftEntry` (
@ -102,7 +84,7 @@ function ShiftEntry_create($shift_entry)
'User ' . User_Nick_render($user, true)
. ' signed up for shift ' . $shift['name']
. ' (' . $shifttype['name'] . ')'
. ' at ' . $room['Name']
. ' at ' . $room->name
. ' from ' . date('Y-m-d H:i', $shift['start'])
. ' to ' . date('Y-m-d H:i', $shift['end'])
. ' as ' . $angeltype['name']
@ -161,14 +143,14 @@ function ShiftEntry_delete($shiftEntry)
$signout_user = User::find($shiftEntry['UID']);
$shift = Shift($shiftEntry['SID']);
$shifttype = ShiftType($shift['shifttype_id']);
$room = Room($shift['RID']);
$room = Room::find($shift['RID']);
$angeltype = AngelType($shiftEntry['TID']);
engelsystem_log(
'Shift signout: ' . User_Nick_render($signout_user, true)
. ' from shift ' . $shift['name']
. ' (' . $shifttype['name'] . ')'
. ' at ' . $room['Name']
. ' at ' . $room->name
. ' from ' . date('Y-m-d H:i', $shift['start'])
. ' to ' . date('Y-m-d H:i', $shift['end'])
. ' as ' . $angeltype['name']

View File

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

View File

@ -2,6 +2,7 @@
use Carbon\Carbon;
use Engelsystem\Database\Db;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User;
/**
@ -143,7 +144,7 @@ function UserWorkLog_from_shift($shift)
return;
}
$room = Room($shift['RID']);
$room = Room::find($shift['RID']);
foreach ($shift['ShiftEntry'] as $entry) {
if ($entry['freeloaded']) {
continue;
@ -173,7 +174,7 @@ function UserWorkLog_from_shift($shift)
$shift['name'],
$shift['title'],
$type['name'],
$room['Name'],
$room->name,
Carbon::createFromTimestamp($shift['start'])->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]);
$privileges = $request->postData('privileges');
$privileges = $request->request->all('privileges');
if (!is_array($privileges)) {
$privileges = [];
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -176,7 +176,7 @@ function make_room_navigation($menu)
$room_menu[] = toolbar_item_divider();
}
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) {
$menu[] = toolbar_dropdown('map-marker', __('Rooms'), $room_menu);

View File

@ -2,7 +2,9 @@
use Carbon\Carbon;
use Engelsystem\Http\Exceptions\HttpTemporaryRedirect;
use Engelsystem\Models\BaseModel;
use Engelsystem\ValidationResult;
use Illuminate\Support\Collection;
/**
* 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.
*
* @param array $data The data array
* @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 array|Collection $data The data array
* @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
*
* @return array
* @return array|Collection
*/
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 = [];
foreach ($data as $value) {
$return[$value[$key_name]] = $value[$value_name];

View File

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

View File

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

View File

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

View File

@ -1,5 +1,6 @@
<?php
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User;
use Engelsystem\ShiftSignupState;
use Illuminate\Support\Collection;
@ -8,10 +9,10 @@ use Illuminate\Support\Collection;
* Renders the basic shift view header.
*
* @param array $shift
* @param array $room
* @param Room $room
* @return string HTML
*/
function Shift_view_header($shift, $room)
function Shift_view_header($shift, Room $room)
{
return div('row', [
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 $shifttype
* @param array $room
* @param Room $room
* @param array[] $angeltypes_source
* @param ShiftSignupState $shift_signup_state
* @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');
$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_delete_link($shift), glyph('trash') . __('delete')) : '',
$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'));

View File

@ -1,6 +1,7 @@
<?php
use Carbon\Carbon;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\User;
/**
@ -377,7 +378,7 @@ function User_view_myshift($shift, $user_source, $its_me)
. ' - '
. date('H:i', $shift['end']),
'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,
'comment' => ''
];

View File

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

View File

@ -12,6 +12,7 @@ use Engelsystem\Models\Message;
use Engelsystem\Models\News;
use Engelsystem\Models\NewsComment;
use Engelsystem\Models\Question;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\PasswordReset;
use Engelsystem\Models\User\PersonalData;
use Engelsystem\Models\User\Settings;
@ -364,6 +365,15 @@ class Stats
);
}
/**
* @return int
*/
public function rooms(): int
{
return Room::query()
->count();
}
/**
* @return int
* @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\NewsComment;
use Engelsystem\Models\Question;
use Engelsystem\Models\Room;
use Engelsystem\Models\User\PasswordReset;
use Engelsystem\Models\User\PersonalData;
use Engelsystem\Models\User\Settings;
@ -119,6 +120,20 @@ class StatsTest extends TestCase
], $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
*/