From e947e788f96e353ca8df1d64ec1044691b69f28c Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Fri, 1 May 2020 16:29:28 +0200 Subject: [PATCH] Rebuild logs view --- config/routes.php | 6 ++ includes/includes.php | 1 - includes/pages/admin_free.php | 8 ++- includes/pages/admin_log.php | 45 -------------- includes/sys_menu.php | 4 +- resources/assets/themes/base.less | 4 ++ resources/lang/de_DE/default.po | 21 +++++-- resources/lang/en_US/default.po | 15 +++++ resources/views/admin/log.twig | 58 +++++++++++++++++++ resources/views/macros/form.twig | 6 +- resources/views/pages/design.twig | 7 ++- src/Controllers/Admin/LogsController.php | 47 +++++++++++++++ src/Middleware/LegacyMiddleware.php | 4 -- .../Controllers/Admin/LogsControllerTest.php | 57 ++++++++++++++++++ 14 files changed, 218 insertions(+), 65 deletions(-) delete mode 100644 includes/pages/admin_log.php create mode 100644 resources/views/admin/log.twig create mode 100644 src/Controllers/Admin/LogsController.php create mode 100644 tests/Unit/Controllers/Admin/LogsControllerTest.php diff --git a/config/routes.php b/config/routes.php index 7cc2823d..c46b1b95 100644 --- a/config/routes.php +++ b/config/routes.php @@ -39,6 +39,10 @@ $route->get('/design', 'DesignController@index'); $route->addGroup( '/admin', function (RouteCollector $route) { + // Log + $route->get('/logs', 'Admin\\LogsController@index'); + $route->post('/logs', 'Admin\\LogsController@index'); + // Schedule $route->addGroup( '/schedule', @@ -48,6 +52,8 @@ $route->addGroup( $route->post('/import', 'Admin\\Schedule\\ImportSchedule@importSchedule'); } ); + + // News $route->addGroup( '/news', function (RouteCollector $route) { diff --git a/includes/includes.php b/includes/includes.php index 017e5c3e..ffbc84cc 100644 --- a/includes/includes.php +++ b/includes/includes.php @@ -68,7 +68,6 @@ $includeFiles = [ __DIR__ . '/../includes/pages/admin_arrive.php', __DIR__ . '/../includes/pages/admin_free.php', __DIR__ . '/../includes/pages/admin_groups.php', - __DIR__ . '/../includes/pages/admin_log.php', __DIR__ . '/../includes/pages/admin_questions.php', __DIR__ . '/../includes/pages/admin_rooms.php', __DIR__ . '/../includes/pages/admin_shifts.php', diff --git a/includes/pages/admin_free.php b/includes/pages/admin_free.php index 709954a0..4aac524b 100644 --- a/includes/pages/admin_free.php +++ b/includes/pages/admin_free.php @@ -102,9 +102,11 @@ function admin_free() form([ div('row', [ div('col-md-12 form-inline', [ - form_text('search', __('Search'), $search), - form_select('angeltype', __('Angeltype'), $angel_types, $angelType), - form_submit('submit', __('Search')) + div('inline-form-spacing', [ + form_text('search', __('Search'), $search), + form_select('angeltype', __('Angeltype'), $angel_types, $angelType), + form_submit('submit', __('Search')) + ]), ]), ]), ]), diff --git a/includes/pages/admin_log.php b/includes/pages/admin_log.php deleted file mode 100644 index 24903f6d..00000000 --- a/includes/pages/admin_log.php +++ /dev/null @@ -1,45 +0,0 @@ -has('keyword')) { - $filter = strip_request_item('keyword'); - } - - $log_entries = LogEntry::filter($filter); - - $entries = []; - foreach ($log_entries as $entry) { - $data = $entry->toArray(); - $data['message'] = nl2br(htmlspecialchars($data['message'])); - $data['created_at'] = date_format($entry->created_at, 'd.m.Y H:i'); - $entries[] = $data; - } - - return page_with_title(admin_log_title(), [ - msg(), - form([ - form_text('keyword', __('Search'), $filter), - form_submit(__('Search'), 'Go') - ]), - table([ - 'created_at' => 'Time', - 'level' => 'Type', - 'message' => 'Log Entry' - ], $entries) - ]); -} diff --git a/includes/sys_menu.php b/includes/sys_menu.php index 75b8df4b..03406c4e 100644 --- a/includes/sys_menu.php +++ b/includes/sys_menu.php @@ -108,6 +108,8 @@ function make_navigation() $admin_menu = []; $admin_pages = [ + // path => name + // path => [name, permission] 'admin_arrive' => 'Arrived angels', 'admin_active' => 'Active angels', 'admin_user' => 'All Angels', @@ -118,7 +120,7 @@ function make_navigation() 'admin_rooms' => 'Rooms', 'admin_groups' => 'Grouprights', 'admin/schedule' => ['schedule.import', 'schedule.import'], - 'admin_log' => 'Log', + 'admin/logs' => ['log.log', 'admin_log'], 'admin_event_config' => 'Event config', ]; diff --git a/resources/assets/themes/base.less b/resources/assets/themes/base.less index d0751180..aac32b8f 100644 --- a/resources/assets/themes/base.less +++ b/resources/assets/themes/base.less @@ -248,6 +248,10 @@ h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 { overflow-x: inherit; } +.inline-form-spacing { + margin-bottom: @form-group-margin-bottom; +} + @keyframes pulse { 0% { transform: rotate(0deg); diff --git a/resources/lang/de_DE/default.po b/resources/lang/de_DE/default.po index 75dc4a80..d8fbe6b2 100644 --- a/resources/lang/de_DE/default.po +++ b/resources/lang/de_DE/default.po @@ -986,8 +986,6 @@ msgstr "Alle Schichten anzeigen" #: includes/pages/admin_active.php:307 includes/pages/admin_arrive.php:178 #: includes/pages/admin_arrive.php:179 includes/pages/admin_free.php:105 -#: includes/pages/admin_free.php:107 includes/pages/admin_log.php:36 -#: includes/pages/admin_log.php:37 msgid "Search" msgstr "Suche" @@ -1294,10 +1292,6 @@ msgstr "Zu löschende Schichten" msgid "It's done!" msgstr "Erledigt!" -#: includes/pages/admin_log.php:10 includes/sys_menu.php:121 -msgid "Log" -msgstr "Log" - #: includes/pages/user_messages.php:121 msgid "Message" msgstr "Nachricht" @@ -2866,3 +2860,18 @@ msgstr "Treffen" msgid "news.edit.message" msgstr "Nachricht" + +msgid "form.search" +msgstr "Suchen" + +msgid "log.log" +msgstr "Logs" + +msgid "log.time" +msgstr "Zeit" + +msgid "log.level" +msgstr "Level" + +msgid "log.message" +msgstr "Nachricht" diff --git a/resources/lang/en_US/default.po b/resources/lang/en_US/default.po index bafb2621..af7a3d0b 100644 --- a/resources/lang/en_US/default.po +++ b/resources/lang/en_US/default.po @@ -144,3 +144,18 @@ msgstr "Meeting" msgid "news.edit.message" msgstr "Message" + +msgid "form.search" +msgstr "Search" + +msgid "log.log" +msgstr "Logs" + +msgid "log.time" +msgstr "Time" + +msgid "log.level" +msgstr "Level" + +msgid "log.message" +msgstr "Message" diff --git a/resources/views/admin/log.twig b/resources/views/admin/log.twig new file mode 100644 index 00000000..f5b10754 --- /dev/null +++ b/resources/views/admin/log.twig @@ -0,0 +1,58 @@ +{% extends 'layouts/app.twig' %} +{% import 'macros/base.twig' as m %} +{% import 'macros/form.twig' as f %} + +{% set title %}{% block title %}{{ __('log.log') }}{% endblock %}{% endset %} + +{% block content %} +
+

{{ block('title') }}

+ +
+
+
+
+ {{ csrf() }} + + {{ f.input('search', __('form.search'), 'text', {'value': search, 'hide_label': true}) }} + + {{ f.submit(__('form.search')) }} +
+
+ + + + + + + + {% for entry in entries %} + {%- set type = 'default' %} + {%- if entry.level in ['notice', 'info'] %} + {%- set type = 'info' %} + {%- endif %} + {%- if entry.level in ['error', 'warning'] %} + {%- set type = 'warning' %} + {%- endif %} + {%- if entry.level in ['emergency', 'alert', 'critical'] %} + {%- set type = 'danger' %} + {%- endif %} + + {%- set td_type = '' %} + {%- if type in ['warning', 'danger'] %} + {%- set td_type = type %} + {%- endif %} + + + + + + + {% endfor %} +
{{ __('log.time') }}{{ __('log.level') }}{{ __('log.message') }}
{{ entry.created_at.format(__('Y-m-d H:i')) }} + {{ entry.level|capitalize }} + {{ entry.message|nl2br }}
+
+
+
+{% endblock %} diff --git a/resources/views/macros/form.twig b/resources/views/macros/form.twig index 66780db8..a7a9a7a4 100644 --- a/resources/views/macros/form.twig +++ b/resources/views/macros/form.twig @@ -1,11 +1,11 @@ {% macro input(name, label, type, opt) %}
{% if label -%} - + {%- endif %} + {%- endmacro %} {% macro button(label, opt) %} diff --git a/resources/views/pages/design.twig b/resources/views/pages/design.twig index 105b78bb..a79b3978 100644 --- a/resources/views/pages/design.twig +++ b/resources/views/pages/design.twig @@ -101,6 +101,9 @@
{{ f.input('form-input-text', 'Text', 'text', {'value': 'Value'}) }}
+
+ {{ f.input('form-input-text-hidden-label', 'Hidden label', 'text', {'value': 'Hidden label', 'hide_label': true}) }} +
{{ f.input('form-input-text-disabled', 'Disabled', 'text', {'disabled': true, 'value': 'Value'}) }}
@@ -113,11 +116,11 @@
{{ f.input('form-input-number', 'Number', 'number', {'value': 42}) }}
+
+
{{ f.textarea('form-input-textarea', 'Textarea', {'rows': 2, 'value': lipsum}) }}
-
-
{{ f.select('form-input-select', {'lorem': 'Ipsum', 'dolor': 'Sit'}, 'Select', 'dolor') }}
diff --git a/src/Controllers/Admin/LogsController.php b/src/Controllers/Admin/LogsController.php new file mode 100644 index 00000000..b95e4e93 --- /dev/null +++ b/src/Controllers/Admin/LogsController.php @@ -0,0 +1,47 @@ +log = $log; + $this->response = $response; + } + + /** + * @param Request $request + * @return Response + */ + public function index(Request $request): Response + { + $search = $request->input('search'); + $entries = $this->log->filter($search); + + return $this->response->withView( + 'admin/log.twig', + ['entries' => $entries, 'search' => $search] + ); + } +} diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php index bb2e73f4..c3bceb53 100644 --- a/src/Middleware/LegacyMiddleware.php +++ b/src/Middleware/LegacyMiddleware.php @@ -188,10 +188,6 @@ class LegacyMiddleware implements MiddlewareInterface $title = admin_shifts_title(); $content = admin_shifts(); return [$title, $content]; - case 'admin_log': - $title = admin_log_title(); - $content = admin_log(); - return [$title, $content]; } throw_redirect(page_link_to('login')); diff --git a/tests/Unit/Controllers/Admin/LogsControllerTest.php b/tests/Unit/Controllers/Admin/LogsControllerTest.php new file mode 100644 index 00000000..2f6676b9 --- /dev/null +++ b/tests/Unit/Controllers/Admin/LogsControllerTest.php @@ -0,0 +1,57 @@ +create(['level' => LogLevel::ALERT, 'message' => 'Alert test']); + $alert = $log->find($alert)->first(); + $error = $log->create(['level' => LogLevel::ERROR, 'message' => 'Error test']); + $error = $log->find($error)->first(); + + $response = $this->createMock(Response::class); + $response->expects($this->exactly(2)) + ->method('withView') + ->withConsecutive( + ['admin/log.twig', ['entries' => new Collection([$error, $alert]), 'search' => null]], + ['admin/log.twig', ['entries' => new Collection([$error]), 'search' => 'error']] + ) + ->willReturn($response); + + $request = Request::create('/'); + + $controller = new LogsController($log, $response); + $controller->index($request); + + $request->request->set('search', 'error'); + $controller->index($request); + } + + /** + * Setup the DB + */ + public function setUp(): void + { + parent::setUp(); + + $this->initDatabase(); + } +}