From fe35ecde0cf8ca39f2148af6e784ecaaab5cd4bc Mon Sep 17 00:00:00 2001 From: fritzmg Date: Thu, 16 Sep 2021 13:39:17 +0100 Subject: [PATCH] fix various languageMain and DCA issues --- src/ContaoManager/Plugin.php | 2 +- .../ConfigOnLoadCallbackListener.php | 16 +++++++++++++++- ...egistrationFormOptionsCallbackListener.php | 2 +- .../RegistrationsButtonCallbackListener.php | 15 +++++++++++++-- .../ChildRecordCallbackListener.php | 3 +-- .../FormDataLoadCallbackListener.php | 2 +- src/EventRegistration.php | 19 +++++++++++++------ .../contao/dca/tl_event_registration.php | 1 + 8 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/ContaoManager/Plugin.php b/src/ContaoManager/Plugin.php index 289cffa..66489b6 100644 --- a/src/ContaoManager/Plugin.php +++ b/src/ContaoManager/Plugin.php @@ -30,7 +30,7 @@ public function getBundles(ParserInterface $parser) BundleConfig::create(ContaoEventRegistrationBundle::class) ->setLoadAfter([ ContaoCalendarBundle::class, - 'notification_center' + 'notification_center', ]), ]; } diff --git a/src/EventListener/DataContainer/CalendarEvents/ConfigOnLoadCallbackListener.php b/src/EventListener/DataContainer/CalendarEvents/ConfigOnLoadCallbackListener.php index 9f60c73..cfcaafa 100644 --- a/src/EventListener/DataContainer/CalendarEvents/ConfigOnLoadCallbackListener.php +++ b/src/EventListener/DataContainer/CalendarEvents/ConfigOnLoadCallbackListener.php @@ -16,6 +16,7 @@ use Contao\CoreBundle\DataContainer\PaletteManipulator; use Contao\CoreBundle\ServiceAnnotation\Callback; use Contao\DataContainer; +use InspiredMinds\ContaoEventRegistration\EventRegistration; /** * Removes some fields if the event is not the main record. @@ -24,11 +25,24 @@ */ class ConfigOnLoadCallbackListener { + private $eventRegistration; + + public function __construct(EventRegistration $eventRegistration) + { + $this->eventRegistration = $eventRegistration; + } + public function __invoke(DataContainer $dc): void { $event = CalendarEventsModel::findById($dc->id); - if (null === $event || empty($event->languageMain)) { + if (null === $event) { + return; + } + + $mainEvent = $this->eventRegistration->getMainEvent($event); + dump($GLOBALS['TL_DCA']['tl_calendar_events']['fields']['languageMain']); + if ((int) $mainEvent->id === (int) $event->id) { return; } diff --git a/src/EventListener/DataContainer/CalendarEvents/RegistrationFormOptionsCallbackListener.php b/src/EventListener/DataContainer/CalendarEvents/RegistrationFormOptionsCallbackListener.php index a8976a1..cea9eb3 100644 --- a/src/EventListener/DataContainer/CalendarEvents/RegistrationFormOptionsCallbackListener.php +++ b/src/EventListener/DataContainer/CalendarEvents/RegistrationFormOptionsCallbackListener.php @@ -39,7 +39,7 @@ public function __invoke(): array return []; } - $forms = $this->db->executeQuery("SELECT id, title FROM tl_form ORDER BY title")->fetchAll(); + $forms = $this->db->executeQuery('SELECT id, title FROM tl_form ORDER BY title')->fetchAll(); if (false === $forms) { return []; diff --git a/src/EventListener/DataContainer/CalendarEvents/RegistrationsButtonCallbackListener.php b/src/EventListener/DataContainer/CalendarEvents/RegistrationsButtonCallbackListener.php index f400bb3..f9488fc 100644 --- a/src/EventListener/DataContainer/CalendarEvents/RegistrationsButtonCallbackListener.php +++ b/src/EventListener/DataContainer/CalendarEvents/RegistrationsButtonCallbackListener.php @@ -13,10 +13,12 @@ namespace InspiredMinds\ContaoEventRegistration\EventListener\DataContainer\CalendarEvents; use Contao\Backend; +use Contao\CalendarEventsModel; use Contao\CoreBundle\ServiceAnnotation\Callback; use Contao\Image; use Contao\Input; use Contao\StringUtil; +use InspiredMinds\ContaoEventRegistration\EventRegistration; use InspiredMinds\ContaoEventRegistration\Model\EventRegistrationModel; /** @@ -26,11 +28,20 @@ */ class RegistrationsButtonCallbackListener { + private $eventRegistration; + + public function __construct(EventRegistration $eventRegistration) + { + $this->eventRegistration = $eventRegistration; + } + public function __invoke(array $row, ?string $href, string $label, string $title, ?string $icon, string $attributes): string { - $href = Backend::addToUrl($href.'&id='.($row['languageMain'] ?: $row['id']).(Input::get('nb') ? '&nc=1' : '')); + $mainEvent = $this->eventRegistration->getMainEvent(CalendarEventsModel::findById($row['id'])); + + $href = Backend::addToUrl($href.'&id='.$mainEvent->id.(Input::get('nb') ? '&nc=1' : '')); - if (0 === EventRegistrationModel::countByPid((int) ($row['languageMain'] ?: $row['id']))) { + if (0 === EventRegistrationModel::countByPid((int) $mainEvent->id)) { $icon = 'mgroup_.svg'; } diff --git a/src/EventListener/DataContainer/EventRegistration/ChildRecordCallbackListener.php b/src/EventListener/DataContainer/EventRegistration/ChildRecordCallbackListener.php index 7a1b5d9..27b6bbb 100644 --- a/src/EventListener/DataContainer/EventRegistration/ChildRecordCallbackListener.php +++ b/src/EventListener/DataContainer/EventRegistration/ChildRecordCallbackListener.php @@ -59,8 +59,7 @@ public function __invoke(array $row): string if ($row['cancelled']) { $icon = 'unpublished.svg'; - } - elseif ($row['confirmed']) { + } elseif ($row['confirmed']) { $icon = 'visible.svg'; } diff --git a/src/EventListener/DataContainer/EventRegistration/FormDataLoadCallbackListener.php b/src/EventListener/DataContainer/EventRegistration/FormDataLoadCallbackListener.php index 5e99d44..4a1907f 100644 --- a/src/EventListener/DataContainer/EventRegistration/FormDataLoadCallbackListener.php +++ b/src/EventListener/DataContainer/EventRegistration/FormDataLoadCallbackListener.php @@ -21,6 +21,6 @@ class FormDataLoadCallbackListener { public function __invoke($value): string { - return json_encode(json_decode($value ?? '') ?: [], JSON_PRETTY_PRINT); + return json_encode(json_decode($value ?? '') ?: [], \JSON_PRETTY_PRINT); } } diff --git a/src/EventRegistration.php b/src/EventRegistration.php index dd7d37a..68adbca 100644 --- a/src/EventRegistration.php +++ b/src/EventRegistration.php @@ -64,12 +64,13 @@ public function addTemplateData(Template $template, CalendarEventsModel $event): return $this->getRegistrationCount($event); }; - if (!empty($event->languageMain)) { - $event = $this->getMainEvent($event); - $template->reg_min = $event->reg_min; - $template->reg_max = $event->reg_max; - $template->reg_regEnd = $event->reg_regEnd; - $template->reg_cancelEnd = $event->reg_cancelEnd; + $mainEvent = $this->getMainEvent($event); + + if ((int) $mainEvent->id !== (int) $event->id) { + $template->reg_min = $mainEvent->reg_min; + $template->reg_max = $mainEvent->reg_max; + $template->reg_regEnd = $mainEvent->reg_regEnd; + $template->reg_cancelEnd = $mainEvent->reg_cancelEnd; } } @@ -220,6 +221,12 @@ public function getMainEvent(CalendarEventsModel $event): CalendarEventsModel return $event; } + $calendar = CalendarModel::findByPk((int) $event->pid); + + if (null === $calendar || empty($calendar->master)) { + return $event; + } + $mainEvent = CalendarEventsModel::findById((int) $event->languageMain); if (null !== $mainEvent) { diff --git a/src/Resources/contao/dca/tl_event_registration.php b/src/Resources/contao/dca/tl_event_registration.php index e7dc4aa..66201b9 100644 --- a/src/Resources/contao/dca/tl_event_registration.php +++ b/src/Resources/contao/dca/tl_event_registration.php @@ -17,6 +17,7 @@ 'dataContainer' => 'Table', 'ptable' => 'tl_calendar_events', 'closed' => true, + 'doNotCopyRecords' => true, 'sql' => [ 'keys' => [ 'id' => 'primary',