diff --git a/db/migrations/2020_09_02_000000_create_rooms_table.php b/db/migrations/2020_09_02_000000_create_rooms_table.php new file mode 100644 index 00000000..8d963004 --- /dev/null +++ b/db/migrations/2020_09_02_000000_create_rooms_table.php @@ -0,0 +1,91 @@ +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'); + } +} diff --git a/includes/controller/public_dashboard_controller.php b/includes/controller/public_dashboard_controller.php index 37acbd63..531ad61b 100644 --- a/includes/controller/public_dashboard_controller.php +++ b/includes/controller/public_dashboard_controller.php @@ -1,5 +1,7 @@ $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' => [] ]; diff --git a/includes/controller/rooms_controller.php b/includes/controller/rooms_controller.php index f0449439..7d9187ad 100644 --- a/includes/controller/rooms_controller.php +++ b/includes/controller/rooms_controller.php @@ -1,5 +1,6 @@ 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()); } diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index 065f09cb..a63e95cc 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -1,5 +1,6 @@ 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)); } diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index 1cfc1107..65283a7f 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -1,6 +1,7 @@ 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)), '

' . __('Needed angels') . '

', @@ -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); diff --git a/includes/mailer/shifts_mailer.php b/includes/mailer/shifts_mailer.php index b496a007..07b7c256 100644 --- a/includes/mailer/shifts_mailer.php +++ b/includes/mailer/shifts_mailer.php @@ -1,5 +1,6 @@ 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); } diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php index c58e2811..ae74ee3f 100644 --- a/includes/model/NeededAngelTypes_model.php +++ b/includes/model/NeededAngelTypes_model.php @@ -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) diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php index dee8d2bb..5c232648 100644 --- a/includes/model/Room_model.php +++ b/includes/model/Room_model.php @@ -1,7 +1,8 @@ 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; } diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index d5d28b9a..9ba24294 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -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'] diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index ab79d50c..796f0024 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -1,6 +1,7 @@ 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` ', diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php index ec446119..4ac0af31 100644 --- a/includes/model/UserWorkLog_model.php +++ b/includes/model/UserWorkLog_model.php @@ -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')) ), diff --git a/includes/pages/admin_groups.php b/includes/pages/admin_groups.php index 1d4a9e03..0dea8145 100644 --- a/includes/pages/admin_groups.php +++ b/includes/pages/admin_groups.php @@ -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 = []; } diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index 348a5b6d..be11c5fc 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -1,4 +1,7 @@ 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')); diff --git a/includes/pages/admin_shifts.php b/includes/pages/admin_shifts.php index 0ac87cc4..b57f7f1a 100644 --- a/includes/pages/admin_shifts.php +++ b/includes/pages/admin_shifts.php @@ -1,6 +1,7 @@ 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']) . '
' - . Room_name_render(Room($shift['RID'])), + . Room_name_render(Room::find($shift['RID'])), 'title' => ShiftType_name_render(ShiftType($shifttype_id)) . ($shift['title'] ? '
' . $shift['title'] : ''), diff --git a/includes/pages/schedule/ImportSchedule.php b/includes/pages/schedule/ImportSchedule.php index f235a02e..0e50dba6 100644 --- a/includes/pages/schedule/ImportSchedule.php +++ b/includes/pages/schedule/ImportSchedule.php @@ -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] diff --git a/includes/pages/user_myshifts.php b/includes/pages/user_myshifts.php index ca6762f7..d8e8b7d4 100644 --- a/includes/pages/user_myshifts.php +++ b/includes/pages/user_myshifts.php @@ -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'], diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index 3f2b09b9..88533c20 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -1,7 +1,9 @@ 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()); } diff --git a/includes/sys_menu.php b/includes/sys_menu.php index 03406c4e..c608a30b 100644 --- a/includes/sys_menu.php +++ b/includes/sys_menu.php @@ -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); diff --git a/includes/sys_page.php b/includes/sys_page.php index d00a9b70..832b4871 100644 --- a/includes/sys_page.php +++ b/includes/sys_page.php @@ -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]; diff --git a/includes/view/Rooms_view.php b/includes/view/Rooms_view.php index 44327c79..77b6c3a6 100644 --- a/includes/view/Rooms_view.php +++ b/includes/view/Rooms_view.php @@ -1,16 +1,17 @@ user(); @@ -20,26 +21,26 @@ function Room_view($room, ShiftsFilterRenderer $shiftsFilterRenderer, ShiftCalen } $description = ''; - if (!empty($room['description'])) { + if ($room->description) { $description = '

' . __('Description') . '

'; $parsedown = new Parsedown(); - $description .= '
' . $parsedown->parse($room['description']) . '
'; + $description .= '
' . $parsedown->parse($room->description) . '
'; } $tabs = []; - if (!empty($room['map_url'])) { + if ($room->map_url) { $tabs[__('Map')] = sprintf( '
' . '' . '
', - $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 '' . glyph('map-marker') . $room['Name'] . ''; + return '' . glyph('map-marker') . $room->name . ''; } - return glyph('map-marker') . $room['Name']; + return glyph('map-marker') . $room->name; } diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index 4bcbd38d..452bffa0 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -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] = [ diff --git a/includes/view/ShiftCalendarShiftRenderer.php b/includes/view/ShiftCalendarShiftRenderer.php index 9e634b90..3447bfb0 100644 --- a/includes/view/ShiftCalendarShiftRenderer.php +++ b/includes/view/ShiftCalendarShiftRenderer.php @@ -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 ] diff --git a/includes/view/ShiftEntry_view.php b/includes/view/ShiftEntry_view.php index 689aa25a..eed89ded 100644 --- a/includes/view/ShiftEntry_view.php +++ b/includes/view/ShiftEntry_view.php @@ -1,5 +1,6 @@ %c', @@ -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'] . ' %c', diff --git a/includes/view/Shifts_view.php b/includes/view/Shifts_view.php index cbd0208d..2b365511 100644 --- a/includes/view/Shifts_view.php +++ b/includes/view/Shifts_view.php @@ -1,5 +1,6 @@ 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), ' ' . __('My shifts')); diff --git a/includes/view/User_view.php b/includes/view/User_view.php index 65cbced2..6ca1e332 100644 --- a/includes/view/User_view.php +++ b/includes/view/User_view.php @@ -1,6 +1,7 @@ sprintf('%.2f', ($shift['end'] - $shift['start']) / 3600) . ' h', - 'room' => Room_name_render($shift), + 'room' => Room_name_render(Room::find($shift['RID'])), 'shift_info' => $shift_info, 'comment' => '' ]; diff --git a/src/Controllers/Metrics/Controller.php b/src/Controllers/Metrics/Controller.php index 913b23ba..870e9a3b 100644 --- a/src/Controllers/Metrics/Controller.php +++ b/src/Controllers/Metrics/Controller.php @@ -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', diff --git a/src/Controllers/Metrics/Stats.php b/src/Controllers/Metrics/Stats.php index 6305670b..bcaa234d 100644 --- a/src/Controllers/Metrics/Stats.php +++ b/src/Controllers/Metrics/Stats.php @@ -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 diff --git a/src/Models/Room.php b/src/Models/Room.php new file mode 100644 index 00000000..93389ce3 --- /dev/null +++ b/src/Models/Room.php @@ -0,0 +1,36 @@ +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); - } - } -} diff --git a/tests/Unit/Controllers/Metrics/StatsTest.php b/tests/Unit/Controllers/Metrics/StatsTest.php index ce1786c3..f3f0058b 100644 --- a/tests/Unit/Controllers/Metrics/StatsTest.php +++ b/tests/Unit/Controllers/Metrics/StatsTest.php @@ -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 */