wi-adrema-plugin/components/EventIndex.php

127 lines
3.7 KiB
PHP
Raw Normal View History

2024-02-01 00:23:58 +01:00
<?php
namespace Silva\Adrema\Components;
use Cms\Classes\ComponentBase;
use Cms\Classes\Page;
use Silva\Adrema\Exceptions\ComponentException;
use Silva\Adrema\Models\Settings;
2024-02-01 02:04:27 +01:00
use Silva\Adrema\Support\FetchAllEvents;
2024-02-01 00:23:58 +01:00
class EventIndex extends ComponentBase
{
public $settings;
/** @var string The active event */
2024-02-01 01:58:40 +01:00
public ?string $eventSlug;
public ?array $event;
public string $currentUrl;
2024-02-01 00:23:58 +01:00
public function componentDetails()
{
return [
'name' => 'EventOverview Component',
'description' => 'No description provided yet...'
];
}
public function onRun()
{
$this->addJs('assets/vendor/adrema-form/dist/main.js');
2024-02-01 01:58:40 +01:00
$this->currentUrl = url()->current();
// $this->addJs('http://localhost:5174/src/main.js', ['type' => 'module']);
2024-02-01 00:23:58 +01:00
$this->settings = [
'primary_color' => Settings::get('primary_color'),
...$this->resolvePageUrls(),
];
2024-02-01 01:58:40 +01:00
$this->eventSlug = $this->property('eventSlug');
if ($this->eventSlug) {
2024-02-01 02:04:27 +01:00
$events = data_get(app(FetchAllEvents::class)->run(), 'data');
2024-02-01 01:58:40 +01:00
throw_if(is_null($events), ComponentException::class, 'event_fetching_failed');
$this->event = collect($events)->first(fn ($event) => $event['slug'] === $this->eventSlug);
$this->page->title = $this->event['name'];
}
2024-02-01 00:23:58 +01:00
}
/**
* @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,
],
2024-02-01 01:58:40 +01:00
'eventSlug' => [
'title' => __('properties.event_slug_title'),
2024-02-01 00:23:58 +01:00
'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();
}
}