From fa6027ed89549f16a94866acc0dfc7bfcdbb1b3a Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 3 Feb 2024 22:36:56 +0100 Subject: [PATCH] Add Event register --- Plugin.php | 2 + assets/vendor/adrema-form | 2 +- components/EventIndex.php | 16 ++-- components/EventManager.php | 7 ++ components/EventRegister.php | 124 +++++++++++++++++++++++++++ components/eventindex/default.htm | 8 +- components/eventregister/default.htm | 24 ++++++ routes.php | 16 ++-- support/FetchAllEvents.php | 12 +-- support/FetchSingleEvent.php | 14 +++ support/Proxy.php | 23 +++++ 11 files changed, 215 insertions(+), 33 deletions(-) create mode 100644 components/EventManager.php create mode 100644 components/EventRegister.php create mode 100644 components/eventregister/default.htm create mode 100644 support/FetchSingleEvent.php create mode 100644 support/Proxy.php diff --git a/Plugin.php b/Plugin.php index 1d10c4c..5d12c30 100644 --- a/Plugin.php +++ b/Plugin.php @@ -4,6 +4,7 @@ namespace Silva\Adrema; use Backend; use Silva\Adrema\Components\EventIndex; +use Silva\Adrema\Components\EventRegister; use Silva\Adrema\Models\Settings; use System\Classes\PluginBase; @@ -50,6 +51,7 @@ class Plugin extends PluginBase { return [ EventIndex::class => 'adrema_event_index', + EventRegister::class => 'adrema_event_register', ]; } diff --git a/assets/vendor/adrema-form b/assets/vendor/adrema-form index 57e0c30..c55bb17 160000 --- a/assets/vendor/adrema-form +++ b/assets/vendor/adrema-form @@ -1 +1 @@ -Subproject commit 57e0c30952ee42ce2d8a4c9fb9af4e4c709d657e +Subproject commit c55bb174c2caa5ed1cda99a04c877455e0387f13 diff --git a/components/EventIndex.php b/components/EventIndex.php index 39bf696..f37aaf5 100644 --- a/components/EventIndex.php +++ b/components/EventIndex.php @@ -7,6 +7,7 @@ use Cms\Classes\Page; use Silva\Adrema\Exceptions\ComponentException; use Silva\Adrema\Models\Settings; use Silva\Adrema\Support\FetchAllEvents; +use Silva\Adrema\Support\FetchSingleEvent; class EventIndex extends ComponentBase { @@ -14,7 +15,6 @@ class EventIndex extends ComponentBase public $settings; /** @var string The active event */ - public ?string $eventSlug; public ?array $event; public string $currentUrl; @@ -28,22 +28,20 @@ class EventIndex extends ComponentBase public function onRun() { - $this->addJs('assets/vendor/adrema-form/dist/main.js'); - $this->currentUrl = url()->current(); - // $this->addJs('http://localhost:5174/src/main.js', ['type' => 'module']); + // $this->addJs('assets/vendor/adrema-form/dist/main.js'); + $this->addJs('http://localhost:5174/src/main.js', ['type' => 'module']); $this->settings = [ 'primary_color' => Settings::get('primary_color'), ...$this->resolvePageUrls(), ]; - $this->eventSlug = $this->property('eventSlug'); - if ($this->eventSlug) { - $events = data_get(app(FetchAllEvents::class)->run(), 'data'); - throw_if(is_null($events), ComponentException::class, 'event_fetching_failed'); + $eventSlug = $this->property('eventSlug'); - $this->event = collect($events)->first(fn ($event) => $event['slug'] === $this->eventSlug); + if ($eventSlug) { + $this->event = app(FetchSingleEvent::class)->run($eventSlug); $this->page->title = $this->event['name']; + $this->currentUrl = url()->current(); } } diff --git a/components/EventManager.php b/components/EventManager.php new file mode 100644 index 0000000..17ece16 --- /dev/null +++ b/components/EventManager.php @@ -0,0 +1,7 @@ + 'EventRegister Component', + 'description' => 'No description provided yet...' + ]; + } + + public function onRun() + { + // $this->addJs('assets/vendor/adrema-form/dist/main.js'); + $this->addJs('http://localhost:5174/src/main.js', ['type' => 'module']); + + $this->settings = [ + 'primary_color' => Settings::get('primary_color'), + ...$this->resolvePageUrls(), + ]; + $this->eventSlug = $this->property('eventSlug'); + + if ($this->eventSlug) { + $this->event = app(FetchSingleEvent::class)->run($this->eventSlug); + $this->page->title = $this->event['name']; + $this->currentUrl = url()->current(); + } + } + + /** + * @return array + */ + protected function resolvePageUrls(): array + { + $indexPage = $this->getPageFromProperty('indexPage'); + $singlePage = $this->getPageFromProperty('singlePage'); + $registerPage = $this->getPageFromProperty('registerPage'); + + throw_if(!str($singlePage->url)->contains(':slug'), ComponentException::class, 'slug_not_found'); + throw_if(!str($registerPage->url)->contains(':slug'), ComponentException::class, 'slug_not_found'); + + return [ + 'indexUrl' => $indexPage->url, + 'singleUrl' => $singlePage->url, + 'registerUrl' => $registerPage->url, + ]; + } + + protected function getPageFromProperty(string $property): Page + { + throw_if(!$this->property('indexPage') || !$this->property('singlePage') || !$this->property('registerPage'), ComponentException::class, 'not_all_pages_set'); + + $page = Page::find($this->property($property)); + throw_if($page === null, ComponentException::class, 'page_not_found'); + + return $page; + } + + public function defineProperties() + { + return [ + 'indexPage' => [ + 'title' => __('properties.index_page_title'), + 'type' => 'dropdown', + 'default' => null, + ], + 'singlePage' => [ + 'title' => __('properties.single_page_title'), + 'type' => 'dropdown', + 'default' => null, + ], + 'registerPage' => [ + 'title' => __('properties.register_page_title'), + 'type' => 'dropdown', + 'default' => null, + ], + 'eventSlug' => [ + 'title' => __('properties.event_slug_title'), + 'type' => 'text', + 'default' => null, + ], + ]; + } + + public function getIndexPageOptions(): array + { + return $this->pageOptions(); + } + + public function getSinglePageOptions(): array + { + return $this->pageOptions(); + } + + public function getRegisterPageOptions(): array + { + return $this->pageOptions(); + } + + private function pageOptions(): array + { + return Page::get() + ->mapWithKeys(fn ($page) => [$page->fileName => $page->title]) + ->toArray(); + } +} diff --git a/components/eventindex/default.htm b/components/eventindex/default.htm index 1ddf708..36fa128 100644 --- a/components/eventindex/default.htm +++ b/components/eventindex/default.htm @@ -8,11 +8,15 @@ {% endput %} {% endif %} +{% put metatags %} + +{% endput %} + + diff --git a/components/eventregister/default.htm b/components/eventregister/default.htm new file mode 100644 index 0000000..d3492ef --- /dev/null +++ b/components/eventregister/default.htm @@ -0,0 +1,24 @@ +{% if __SELF__.event %} + {% put metatags %} + + + + + + {% endput %} +{% endif %} + +{% put metatags %} + +{% endput %} + + + + diff --git a/routes.php b/routes.php index dd91ac3..2f3d2a9 100644 --- a/routes.php +++ b/routes.php @@ -1,13 +1,9 @@ run(); - - if (is_null($events)) { - return Response::json(['message' => 'Fehler beim Laden der Veranstaltungen'], 422); - } - - return Response::json($events); -})->middleware('api'); +Route::get( + '/adrema-api/{route}', + fn (string $route) => Response::json(app(Proxy::class)->run($route)) +) + ->where('route', '[a-zA-Z0-9\-/]+')->middleware('api'); diff --git a/support/FetchAllEvents.php b/support/FetchAllEvents.php index e5ce7a7..68be16a 100644 --- a/support/FetchAllEvents.php +++ b/support/FetchAllEvents.php @@ -2,20 +2,10 @@ namespace Silva\Adrema\Support; -use Silva\Adrema\Models\Settings; -use Http; - class FetchAllEvents { public function run(): ?array { - $baseUrl = Settings::get('base_url'); - $response = Http::get($baseUrl . '/api/form'); - - if (!$response->ok()) { - return null; - } - - return $response->json(); + return app(Proxy::class)->run('/api/form'); } } diff --git a/support/FetchSingleEvent.php b/support/FetchSingleEvent.php new file mode 100644 index 0000000..99ed9af --- /dev/null +++ b/support/FetchSingleEvent.php @@ -0,0 +1,14 @@ +run(), 'data'); + throw_if(is_null($events), ComponentException::class, 'event_fetching_failed'); + + return collect($events)->first(fn ($event) => $event['slug'] === $slug); + } +} diff --git a/support/Proxy.php b/support/Proxy.php new file mode 100644 index 0000000..06f3c06 --- /dev/null +++ b/support/Proxy.php @@ -0,0 +1,23 @@ +start('/'); + + $baseUrl = Settings::get('base_url'); + $response = Http::get($baseUrl . $url); + + if (!$response->ok()) { + return null; + } + + return $response->json(); + } +}