oc-adrema-plugin/components/EventManager.php

184 lines
5.5 KiB
PHP
Raw Normal View History

2024-02-03 22:36:56 +01:00
<?php
2024-02-03 22:51:26 +01:00
namespace Silva\Adrema\Components;
2024-02-03 22:36:56 +01:00
2024-02-04 01:12:22 +01:00
use Illuminate\Support\Collection;
2024-02-03 22:51:26 +01:00
use Cms\Classes\ComponentBase;
use Cms\Classes\Page;
use Silva\Adrema\Exceptions\ComponentException;
use Silva\Adrema\Models\Settings;
2024-02-04 01:12:22 +01:00
use Silva\Adrema\Support\FetchAllEvents;
2024-02-03 22:51:26 +01:00
abstract class EventManager extends ComponentBase
2024-02-03 22:36:56 +01:00
{
2024-02-03 22:51:26 +01:00
2024-02-04 01:12:22 +01:00
public array $settings = [];
public ?array $event = null;
2024-03-06 22:07:04 +01:00
public array $eventMeta = [];
2024-02-04 01:12:22 +01:00
/** @var Collection<int, mixed> */
public Collection $events;
2024-02-03 22:51:26 +01:00
public string $currentUrl;
2024-02-04 01:12:22 +01:00
abstract protected function isRegistering(): bool;
2024-02-03 22:51:26 +01:00
2024-02-03 22:58:25 +01:00
public function componentDetails()
{
$componentName = str(class_basename(static::class))->snake();
return [
'name' => __("{$componentName}_component_name"),
'description' => __("{$componentName}_component_description"),
];
}
public function onRun()
{
$this->addAssets();
2024-02-04 01:12:22 +01:00
$this->loadColors();
2024-02-03 22:58:25 +01:00
$this->loadSettings();
$this->loadSingleEvent();
2024-02-04 01:12:22 +01:00
$this->page['breadcrumbs'] = $this->getBreadcrumbs();
}
private function getBreadcrumbs(): Collection
{
$breadcrumbs = collect([]);
$breadcrumbs->push(['url' => $this->settings['indexUrl'], 'title' => 'Veranstaltungen', 'isActive' => $this->event === null]);
if ($this->event) {
$breadcrumbs->push([
'url' => (string) str($this->settings['singleUrl'])->replace(':slug', $this->event['slug']),
'title' => $this->event['name'],
'isActive' => !$this->isRegistering()
]);
}
if ($this->event && $this->isRegistering()) {
$breadcrumbs->push([
'url' => (string) str($this->settings['registerUrl'])->replace(':slug', $this->event['slug']),
'title' => 'Anmeldung',
'isActive' => true,
]);
}
return $breadcrumbs;
2024-02-03 22:58:25 +01:00
}
2024-02-03 22:51:26 +01:00
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,
];
}
2024-02-03 23:00:47 +01:00
2024-02-04 01:12:22 +01:00
protected function loadSingleEvent(): void
2024-02-03 23:00:47 +01:00
{
$eventSlug = $this->property('eventSlug');
if (!$eventSlug) {
return;
}
2024-03-06 22:07:04 +01:00
$this->eventMeta = data_get(app(FetchAllEvents::class)->run(), 'meta', []);
2024-02-04 01:12:22 +01:00
$this->events = $this->page['events'] = collect(app(FetchAllEvents::class)->run()['data']);
$this->event = $this->page['event'] = $this->events->first(fn ($event) => $event['slug'] == $eventSlug);
2024-02-03 23:00:47 +01:00
$this->currentUrl = url()->current();
}
private function addAssets(): void
{
2024-04-08 18:47:52 +02:00
$this->addJs('assets/vendor/adrema-form/dist/main.js');
// $this->addJs('http://localhost:5174/src/main.js', ['type' => 'module']);
2024-04-08 20:36:57 +02:00
$this->addCss('assets/vendor/adrema-form/dist/style.css');
2024-02-03 23:00:47 +01:00
}
private function loadSettings(): void
{
$this->settings = [
2024-02-04 01:12:22 +01:00
...$this->settings,
2024-02-03 23:00:47 +01:00
...$this->resolvePageUrls(),
];
}
2024-02-04 01:12:22 +01:00
protected function loadColors(): void
{
$this->settings = [
...$this->settings,
'primary_color' => Settings::get('primary_color'),
2024-03-17 03:14:30 +01:00
'secondary_color' => Settings::get('secondary_color'),
'circle_color' => Settings::get('circle_color'),
'font_color' => Settings::get('font_color'),
2024-02-04 01:12:22 +01:00
];
}
2024-02-03 22:36:56 +01:00
}