Add Event register

This commit is contained in:
philipp lang 2024-02-03 22:36:56 +01:00
parent 9610bee8ca
commit fa6027ed89
11 changed files with 215 additions and 33 deletions

View File

@ -4,6 +4,7 @@ namespace Silva\Adrema;
use Backend; use Backend;
use Silva\Adrema\Components\EventIndex; use Silva\Adrema\Components\EventIndex;
use Silva\Adrema\Components\EventRegister;
use Silva\Adrema\Models\Settings; use Silva\Adrema\Models\Settings;
use System\Classes\PluginBase; use System\Classes\PluginBase;
@ -50,6 +51,7 @@ class Plugin extends PluginBase
{ {
return [ return [
EventIndex::class => 'adrema_event_index', EventIndex::class => 'adrema_event_index',
EventRegister::class => 'adrema_event_register',
]; ];
} }

@ -1 +1 @@
Subproject commit 57e0c30952ee42ce2d8a4c9fb9af4e4c709d657e Subproject commit c55bb174c2caa5ed1cda99a04c877455e0387f13

View File

@ -7,6 +7,7 @@ use Cms\Classes\Page;
use Silva\Adrema\Exceptions\ComponentException; use Silva\Adrema\Exceptions\ComponentException;
use Silva\Adrema\Models\Settings; use Silva\Adrema\Models\Settings;
use Silva\Adrema\Support\FetchAllEvents; use Silva\Adrema\Support\FetchAllEvents;
use Silva\Adrema\Support\FetchSingleEvent;
class EventIndex extends ComponentBase class EventIndex extends ComponentBase
{ {
@ -14,7 +15,6 @@ class EventIndex extends ComponentBase
public $settings; public $settings;
/** @var string The active event */ /** @var string The active event */
public ?string $eventSlug;
public ?array $event; public ?array $event;
public string $currentUrl; public string $currentUrl;
@ -28,22 +28,20 @@ class EventIndex extends ComponentBase
public function onRun() public function onRun()
{ {
$this->addJs('assets/vendor/adrema-form/dist/main.js'); // $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('http://localhost:5174/src/main.js', ['type' => 'module']);
$this->settings = [ $this->settings = [
'primary_color' => Settings::get('primary_color'), 'primary_color' => Settings::get('primary_color'),
...$this->resolvePageUrls(), ...$this->resolvePageUrls(),
]; ];
$this->eventSlug = $this->property('eventSlug');
if ($this->eventSlug) { $eventSlug = $this->property('eventSlug');
$events = data_get(app(FetchAllEvents::class)->run(), 'data');
throw_if(is_null($events), ComponentException::class, 'event_fetching_failed');
$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->page->title = $this->event['name'];
$this->currentUrl = url()->current();
} }
} }

View File

@ -0,0 +1,7 @@
<?php
namespace Silva\Adrema\Support;
abstract class EventManager
{
}

View File

@ -0,0 +1,124 @@
<?php
namespace Silva\Adrema\Components;
use Cms\Classes\ComponentBase;
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 EventRegister extends ComponentBase
{
public $settings;
/** @var string The active event */
public ?string $eventSlug;
public ?array $event;
public string $currentUrl;
public function componentDetails()
{
return [
'name' => '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<string, string>
*/
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();
}
}

View File

@ -8,11 +8,15 @@
{% endput %} {% endput %}
{% endif %} {% endif %}
{% put metatags %}
<meta name="adrema_base_url" content="/adrema-api">
{% endput %}
<event-index <event-index
style="--primary: {{__SELF__.settings.primary_color}}; --primaryfg: #d1f8ff; --secondary: #800a19; --font: hsl(181, 84%, 78%); --circle: hsl(181, 86%, 16%)" style="--primary: {{__SELF__.settings.primary_color}}; --primaryfg: #d1f8ff; --secondary: #800a19; --font: hsl(181, 84%, 78%); --circle: hsl(181, 86%, 16%)"
index-url="{{__SELF__.settings.indexUrl}}" index-url="{{__SELF__.settings.indexUrl}}"
single-url="{{__SELF__.settings.singleUrl}}" single-url="{{__SELF__.settings.singleUrl}}"
register-url="{{__SELF__.settings.registerUrl}}" register-url="{{__SELF__.settings.registerUrl}}"
url="/api/silva_adrema_event_overview" {% if __SELF__.event %} visible-event-slug="{{__SELF__.event.slug }}" {% endif %}
{% if __SELF__.event %} event="{{__SELF__.event.slug }}" {% endif %}
></event-index> ></event-index>

View File

@ -0,0 +1,24 @@
{% if __SELF__.event %}
{% put metatags %}
<meta property="og:title" content="{{__SELF__.event.name}}">
<meta property="og:url" content="{{__SELF__.currentUrl}}">
<meta property="og:description" content="{{__SELF__.event.excerpt}}">
<meta property="og:image" content="{{__SELF__.event.image}}">
<meta property="og:type" content="article">
{% endput %}
{% endif %}
{% put metatags %}
<meta name="adrema_base_url" content="/adrema-api">
{% endput %}
<script type="text/javascript">
var adrema_event = {{__SELF__.event.config | json_encode | raw}};
</script>
<event-form
style="--primary: {{__SELF__.settings.primary_color}}; --primaryfg: #d1f8ff; --secondary: #800a19; --font: hsl(181, 84%, 78%); --circle: hsl(181, 86%, 16%)"
index-url="{{__SELF__.settings.indexUrl}}"
single-url="{{__SELF__.settings.singleUrl}}"
register-url="{{__SELF__.settings.registerUrl}}"
></event-form>

View File

@ -1,13 +1,9 @@
<?php <?php
use Silva\Adrema\Support\FetchAllEvents; use Silva\Adrema\Support\Proxy;
Route::get('/api/silva_adrema_event_overview', function () { Route::get(
$events = app(FetchAllEvents::class)->run(); '/adrema-api/{route}',
fn (string $route) => Response::json(app(Proxy::class)->run($route))
if (is_null($events)) { )
return Response::json(['message' => 'Fehler beim Laden der Veranstaltungen'], 422); ->where('route', '[a-zA-Z0-9\-/]+')->middleware('api');
}
return Response::json($events);
})->middleware('api');

View File

@ -2,20 +2,10 @@
namespace Silva\Adrema\Support; namespace Silva\Adrema\Support;
use Silva\Adrema\Models\Settings;
use Http;
class FetchAllEvents class FetchAllEvents
{ {
public function run(): ?array public function run(): ?array
{ {
$baseUrl = Settings::get('base_url'); return app(Proxy::class)->run('/api/form');
$response = Http::get($baseUrl . '/api/form');
if (!$response->ok()) {
return null;
}
return $response->json();
} }
} }

View File

@ -0,0 +1,14 @@
<?php
namespace Silva\Adrema\Support;
class FetchSingleEvent
{
public function run(string $slug): ?array
{
$events = data_get(app(FetchAllEvents::class)->run(), 'data');
throw_if(is_null($events), ComponentException::class, 'event_fetching_failed');
return collect($events)->first(fn ($event) => $event['slug'] === $slug);
}
}

23
support/Proxy.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace Silva\Adrema\Support;
use Silva\Adrema\Models\Settings;
use Http;
class Proxy
{
public function run(string $url): ?array
{
$url = str($url)->start('/');
$baseUrl = Settings::get('base_url');
$response = Http::get($baseUrl . $url);
if (!$response->ok()) {
return null;
}
return $response->json();
}
}