diff --git a/config/config.default.php b/config/config.default.php index 55982230..1896fbb0 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -350,6 +350,12 @@ return [ '4XL' => '4XLarge Straight-Cut', ], + // Whether to show the current day of the event (-2, -1, 0, 1, 2…) in footer and on the dashboard. + // The event start date has to be set for it to appear. + 'enable_show_day_of_event' => false, + // If true there will be a day 0 (-1, 0, 1…). If false there won't (-1, 1…) + 'event_has_day0' => true, + 'metrics' => [ // User work buckets in seconds 'work' => [1 * 60 * 60, 1.5 * 60 * 60, 2 * 60 * 60, 3 * 60 * 60, 5 * 60 * 60, 10 * 60 * 60, 20 * 60 * 60], diff --git a/includes/view/PublicDashboard_view.php b/includes/view/PublicDashboard_view.php index 29837176..a39946bf 100644 --- a/includes/view/PublicDashboard_view.php +++ b/includes/view/PublicDashboard_view.php @@ -1,5 +1,6 @@ get('filtered'); $filter = collect(session()->get('shifts-filter'))->only(['rooms', 'types'])->toArray(); return page([ div('wrapper', [ div('public-dashboard', [ - div('first row', [ - stats(__('Angels needed in the next 3 hrs'), $stats['needed-3-hours']), - stats(__('Angels needed for nightshifts'), $stats['needed-night']), - stats(__('Angels currently working'), $stats['angels-working'], 'default'), - stats(__('Hours to be worked'), $stats['hours-to-work'], 'default'), - ], 'statistics'), + div('first row', $stats, 'statistics'), $news, $needed_angels, ], 'public-dashboard'), diff --git a/resources/lang/de_DE/default.po b/resources/lang/de_DE/default.po index 4013dbcc..fb6cffa4 100644 --- a/resources/lang/de_DE/default.po +++ b/resources/lang/de_DE/default.po @@ -2426,3 +2426,9 @@ msgstr "Raum erstellen" msgid "room.edit.title" msgstr "Raum bearbeiten" + +msgid "event.day" +msgstr "Tag %1$d" + +msgid "dashboard.day" +msgstr "Tag" diff --git a/resources/lang/en_US/default.po b/resources/lang/en_US/default.po index 342e1110..4569cd7f 100644 --- a/resources/lang/en_US/default.po +++ b/resources/lang/en_US/default.po @@ -524,3 +524,9 @@ msgstr "Create room" msgid "room.edit.title" msgstr "Edit room" + +msgid "event.day" +msgstr "Day %1$d" + +msgid "dashboard.day" +msgstr "Day" diff --git a/resources/views/layouts/parts/footer.twig b/resources/views/layouts/parts/footer.twig index 9c2a95cc..571266e4 100644 --- a/resources/views/layouts/parts/footer.twig +++ b/resources/views/layouts/parts/footer.twig @@ -11,11 +11,17 @@ config('event_start').format(__('Y-m-d')), config('event_end').format(__('Y-m-d')) ]) }} + {%- if config('enable_show_day_of_event') and day_of_event is defined -%} + , {{ __('event.day', [day_of_event]) }} + {% endif %} {% elseif config('event_start') %} {{ __('%1$s, starting %2$s', [ config('name'), config('event_start').format(__('Y-m-d')) ]) }} + {%- if config('enable_show_day_of_event') and day_of_event is defined -%} + , {{ __('event.day', [day_of_event]) }} + {% endif %} {% else %} {{ config('name') }} {% endif %}
@@ -23,7 +29,10 @@ {{ __('Event from %1$s to %2$s', [ config('event_start').format(__('Y-m-d')), config('event_end').format(__('Y-m-d')) - ]) }}
+ ]) }} + {%- if config('enable_show_day_of_event') and day_of_event is defined -%} + , {{ __('event.day', [day_of_event]) }} + {% endif %}
{% endif %} {% endblock %} diff --git a/src/Helpers/DayOfEvent.php b/src/Helpers/DayOfEvent.php new file mode 100644 index 00000000..48776385 --- /dev/null +++ b/src/Helpers/DayOfEvent.php @@ -0,0 +1,52 @@ +copy()->startOfDay(); + + $now = Carbon::now()->startOfDay(); + $diff = $startOfEvent->diffInDays($now, false); + + if ($diff >= 0) { + // The first day of the event (diff 0) should be 1. + // The seconds day of the event (diff 1) should be 2. + // Add one day to the diff. + return $diff + 1; + } + + if (config('event_has_day0') && $diff < 0) { + // One day before the event (-1 diff) should day 0. + // Two days before the event (-2 diff) should be -1. + // Add one day to the diff. + return $diff + 1; + } + + + // This is the remaining case where the diff is negative (before event). + // One day before the event (-1 diff) should be day -1. + // Two days before the event (-2 diff) should be day -2. + // Return as it is. + return $diff; + } +} diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php index a6c58183..fb7c9a8a 100644 --- a/src/Renderer/Twig/Extensions/Globals.php +++ b/src/Renderer/Twig/Extensions/Globals.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Engelsystem\Renderer\Twig\Extensions; use Engelsystem\Helpers\Authenticator; +use Engelsystem\Helpers\DayOfEvent; use Engelsystem\Http\Request; use Twig\Extension\AbstractExtension as TwigExtension; use Twig\Extension\GlobalsInterface as GlobalsInterface; @@ -53,6 +54,7 @@ class Globals extends TwigExtension implements GlobalsInterface 'request' => $this->request, 'themeId' => $themeId, 'theme' => $theme, + 'day_of_event' => DayOfEvent::get(), ]; } } diff --git a/tests/Unit/Helpers/DayOfEventTest.php b/tests/Unit/Helpers/DayOfEventTest.php new file mode 100644 index 00000000..ad13bcd6 --- /dev/null +++ b/tests/Unit/Helpers/DayOfEventTest.php @@ -0,0 +1,68 @@ +createAndSetUpAppWithConfig([]); + $this->config = $app->get('config'); + } + + public function tearDown(): void + { + Carbon::setTestNow(); + } + + /** + * @return Array + */ + public function provideTestGetData(): array + { + return [ + 'day -2 (10:00, with day 0)' => [-2, true, '2023-07-31 15:23:42', '2023-07-28 10:00:00'], + 'day -2 (23:59, with day 0)' => [-2, true, '2023-07-31 15:23:42', '2023-07-28 23:59:59'], + 'day -1 (23:59, with day 0)' => [-1, true, '2023-07-31 15:23:42', '2023-07-29 23:59:59'], + 'day 0 (with day 0)' => [0, true, '2023-07-31 15:23:42', '2023-07-30 16:00:00'], + 'day 1 (with day 0)' => [1, true, '2023-07-31 15:23:42', '2023-07-31 10:00:00'], + 'day 2 (with day 0)' => [2, true, '2023-07-31 15:23:42', '2023-08-01 10:00:00'], + + 'day -2 (without day 0)' => [-2, false, '2023-07-31 15:23:42', '2023-07-29 10:00:00'], + 'day -1 (without day 0)' => [-1, false, '2023-07-31 15:23:42', '2023-07-30 23:59:59'], + 'day 1 (without day 0)' => [1, false, '2023-07-31 15:23:42', '2023-07-31 00:00:00'], + 'day 2 (without day 0)' => [2, false, '2023-07-31 15:23:42', '2023-08-01 16:00:00'], + + 'no start date' => [null, false, null, '2023-08-01 16:00:00'], + ]; + } + + /** + * @dataProvider provideTestGetData + * @covers \Engelsystem\Helpers\DayOfEvent + */ + public function testGet( + int | null $expected, + bool $eventHasDay0, + string | null $eventStart, + string $now + ): void { + $this->config->set( + 'event_start', + $eventStart ? Carbon::createFromFormat(self::FORMAT, $eventStart) : null + ); + $this->config->set('event_has_day0', $eventHasDay0); + Carbon::setTestNow(Carbon::createFromFormat(self::FORMAT, $now)); + $this->assertSame($expected, DayOfEvent::get()); + } +} diff --git a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php index 4076ac4c..c4fbee70 100644 --- a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php @@ -6,6 +6,7 @@ namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; use Engelsystem\Config\Config; use Engelsystem\Helpers\Authenticator; +use Engelsystem\Helpers\Carbon; use Engelsystem\Http\Request; use Engelsystem\Models\User\Settings; use Engelsystem\Models\User\User; @@ -17,6 +18,16 @@ class GlobalsTest extends ExtensionTest { use HasDatabase; + public static function setUpBeforeClass(): void + { + Carbon::setTestNow(Carbon::createFromFormat('Y-m-d', '2023-08-15')); + } + + public static function tearDownAfterClass(): void + { + Carbon::setTestNow(); + } + /** * @covers \Engelsystem\Renderer\Twig\Extensions\Globals::__construct * @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals @@ -35,7 +46,17 @@ class GlobalsTest extends ExtensionTest $user = User::factory() ->has(Settings::factory(['theme' => 42])) ->create(); - $config = new Config(['theme' => 23, 'themes' => [42 => $theme, 23 => $theme2, 1337 => $theme3]]); + $config = new Config( + [ + 'event_start' => Carbon::createFromFormat('Y-m-d', '2023-08-13'), + 'theme' => 23, + 'themes' => [ + 42 => $theme, + 23 => $theme2, + 1337 => $theme3, + ], + ] + ); $auth->expects($this->exactly(4)) ->method('user') @@ -49,9 +70,11 @@ class GlobalsTest extends ExtensionTest $this->app->instance('config', $config); $extension = new Globals($auth, $request); + $globals = $extension->getGlobals(); + + $this->assertGlobalsExists('day_of_event', -2, $globals); // No user - $globals = $extension->getGlobals(); $this->assertGlobalsExists('user', [], $globals); $this->assertGlobalsExists('user_messages', null, $globals); $this->assertGlobalsExists('request', $request, $globals);