Add EventManager

This commit is contained in:
philipp lang 2024-02-03 22:51:26 +01:00
parent fa6027ed89
commit 3f05f5544f
3 changed files with 124 additions and 216 deletions

View File

@ -2,22 +2,8 @@
namespace Silva\Adrema\Components; namespace Silva\Adrema\Components;
use Cms\Classes\ComponentBase; class EventIndex extends EventManager
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
{ {
public $settings;
/** @var string The active event */
public ?array $event;
public string $currentUrl;
public function componentDetails() public function componentDetails()
{ {
return [ return [
@ -28,97 +14,8 @@ class EventIndex extends ComponentBase
public function onRun() public function onRun()
{ {
// $this->addJs('assets/vendor/adrema-form/dist/main.js'); $this->addAssets();
$this->addJs('http://localhost:5174/src/main.js', ['type' => 'module']); $this->loadSettings();
$this->loadSingleEvent();
$this->settings = [
'primary_color' => Settings::get('primary_color'),
...$this->resolvePageUrls(),
];
$eventSlug = $this->property('eventSlug');
if ($eventSlug) {
$this->event = app(FetchSingleEvent::class)->run($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

@ -1,7 +1,121 @@
<?php <?php
namespace Silva\Adrema\Support; namespace Silva\Adrema\Components;
abstract class EventManager use Cms\Classes\ComponentBase;
use Cms\Classes\Page;
use Silva\Adrema\Exceptions\ComponentException;
use Silva\Adrema\Models\Settings;
use Silva\Adrema\Support\FetchSingleEvent;
abstract class EventManager extends ComponentBase
{ {
public array $settings;
public ?array $event;
public string $currentUrl;
protected function addAssets(): void
{
// $this->addJs('assets/vendor/adrema-form/dist/main.js');
$this->addJs('http://localhost:5174/src/main.js', ['type' => 'module']);
}
protected function loadSettings(): void
{
$this->settings = [
'primary_color' => Settings::get('primary_color'),
...$this->resolvePageUrls(),
];
}
protected function loadSingleEvent(): void
{
$eventSlug = $this->property('eventSlug');
if (!$eventSlug) {
return;
}
$this->event = app(FetchSingleEvent::class)->run($eventSlug);
$this->page->title = $this->event['name'];
$this->currentUrl = url()->current();
}
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();
}
private 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;
}
/**
* @return array<string, string>
*/
private 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,
];
}
} }

View File

@ -2,23 +2,8 @@
namespace Silva\Adrema\Components; namespace Silva\Adrema\Components;
use Cms\Classes\ComponentBase; class EventRegister extends EventManager
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() public function componentDetails()
{ {
return [ return [
@ -29,96 +14,8 @@ class EventRegister extends ComponentBase
public function onRun() public function onRun()
{ {
// $this->addJs('assets/vendor/adrema-form/dist/main.js'); $this->addAssets();
$this->addJs('http://localhost:5174/src/main.js', ['type' => 'module']); $this->loadSettings();
$this->loadSingleEvent();
$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();
} }
} }