diff --git a/src/Admin/EventAdmin.php b/src/Admin/EventAdmin.php index 25b1e70..e1b9e74 100644 --- a/src/Admin/EventAdmin.php +++ b/src/Admin/EventAdmin.php @@ -104,13 +104,15 @@ public function configureViews(ViewCollection $viewCollection): void $viewCollection->add($listView); // Configure Event Add View - $addFormView = $this->viewBuilderFactory->createResourceTabViewBuilder(static::ADD_FORM_VIEW, '/events/:locale/add') + $addFormView = $this->viewBuilderFactory + ->createResourceTabViewBuilder(static::ADD_FORM_VIEW, '/events/:locale/add') ->setResourceKey(Event::RESOURCE_KEY) ->setBackView(static::LIST_VIEW) ->addLocales($locales); $viewCollection->add($addFormView); - $addDetailsFormView = $this->viewBuilderFactory->createFormViewBuilder(static::ADD_FORM_DETAILS_VIEW, '/details') + $addDetailsFormView = $this->viewBuilderFactory + ->createFormViewBuilder(static::ADD_FORM_DETAILS_VIEW, '/details') ->setResourceKey(Event::RESOURCE_KEY) ->setFormKey(Event::FORM_KEY) ->setTabTitle('sulu_admin.details') @@ -120,7 +122,8 @@ public function configureViews(ViewCollection $viewCollection): void $viewCollection->add($addDetailsFormView); // Configure Event Edit View - $editFormView = $this->viewBuilderFactory->createResourceTabViewBuilder(static::EDIT_FORM_VIEW, '/events/:locale/:id') + $editFormView = $this->viewBuilderFactory + ->createResourceTabViewBuilder(static::EDIT_FORM_VIEW, '/events/:locale/:id') ->setResourceKey(Event::RESOURCE_KEY) ->setBackView(static::LIST_VIEW) ->setTitleProperty('title') @@ -139,7 +142,9 @@ public function configureViews(ViewCollection $viewCollection): void ), ]; - $editDetailsFormView = $this->viewBuilderFactory->createFormViewBuilder(static::EDIT_FORM_DETAILS_VIEW, '/details') + $editDetailsFormView = $this->viewBuilderFactory + ->createPreviewFormViewBuilder(static::EDIT_FORM_DETAILS_VIEW, '/details') + ->setPreviewCondition('id != null') ->setResourceKey(Event::RESOURCE_KEY) ->setFormKey(Event::FORM_KEY) ->setTabTitle('sulu_admin.details') @@ -162,19 +167,10 @@ public function configureViews(ViewCollection $viewCollection): void $formToolbarActionsWithoutType[] = new ToolbarAction('sulu_admin.save'); } -// $viewCollection->add( -// $this->viewBuilderFactory->createPreviewFormViewBuilder(static::EDIT_FORM_VIEW_EXCERPT, '/excerpt') -// ->setResourceKey(Event::RESOURCE_KEY) -// ->setFormKey('page_excerpt') -// ->setTabTitle('sulu_page.excerpt') -// ->addToolbarActions($formToolbarActionsWithoutType) -// ->setParent(static::EDIT_FORM_VIEW) -// ); - $viewCollection->add( $this->viewBuilderFactory ->createPreviewFormViewBuilder(static::EDIT_FORM_VIEW_SEO, '/seo') - ->disablePreviewWebspaceChooser() +// ->disablePreviewWebspaceChooser() ->setResourceKey(Event::RESOURCE_KEY) ->setFormKey('page_seo') ->setTabTitle('sulu_page.seo') @@ -189,7 +185,7 @@ public function configureViews(ViewCollection $viewCollection): void $viewCollection->add( $this->viewBuilderFactory ->createPreviewFormViewBuilder(static::EDIT_FORM_VIEW_EXCERPT, '/excerpt') - ->disablePreviewWebspaceChooser() +// ->disablePreviewWebspaceChooser() ->setResourceKey(Event::RESOURCE_KEY) ->setFormKey('page_excerpt') ->setTabTitle('sulu_page.excerpt') @@ -236,4 +232,9 @@ public function getSecurityContexts(): array ], ]; } + + public function getConfigKey(): ?string + { + return 'sulu_event'; + } } diff --git a/src/Controller/Website/AbstractController.php b/src/Controller/Website/AbstractController.php index 1fbd8ad..ce0cba6 100644 --- a/src/Controller/Website/AbstractController.php +++ b/src/Controller/Website/AbstractController.php @@ -4,11 +4,11 @@ use Sulu\Bundle\MediaBundle\Api\Media; use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as AbstractSymfonyController; +use Sulu\Bundle\WebsiteBundle\Controller\WebsiteController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; -abstract class AbstractController extends AbstractSymfonyController +abstract class AbstractController extends WebsiteController { protected $mediaManager; diff --git a/src/Controller/Website/EventController.php b/src/Controller/Website/EventController.php index 5857889..5d2dba7 100644 --- a/src/Controller/Website/EventController.php +++ b/src/Controller/Website/EventController.php @@ -7,13 +7,18 @@ use Manuxi\SuluEventBundle\Entity\Event; use Manuxi\SuluEventBundle\Repository\EventRepository; use JMS\Serializer\SerializerBuilder; +use Sulu\Bundle\HttpCacheBundle\Cache\SuluHttpCache; use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface; +use Sulu\Bundle\PreviewBundle\Preview\Preview; use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface; use Sulu\Bundle\WebsiteBundle\Resolver\TemplateAttributeResolverInterface; use Sulu\Component\Webspace\Manager\WebspaceManagerInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Contracts\Translation\TranslatorInterface; +use Twig\Environment; class EventController extends AbstractController { @@ -41,8 +46,11 @@ public function __construct( $this->routeRepository = $routeRepository; } - public function indexAction(Event $event): Response + public function indexAction(Event $event, string $view = 'pages/event', bool $preview = false, bool $partial = false): Response { + $requestFormat = $this->request->getRequestFormat(); + $viewTemplate = $view . '.' . $requestFormat . '.twig'; + $parameters = $this->templateAttributeResolver->resolve([ 'event' => $event, 'content' => [ @@ -55,7 +63,37 @@ public function indexAction(Event $event): Response 'created' => $event->getCreated(), ]); - return $this->render('pages/event.html.twig', $parameters); + $response = $this->createResponse($this->request); + + try { + if ($partial) { + $response->setContent( + $this->renderBlock( + $viewTemplate, + 'content', + $parameters + ) + ); + + return $response; + } elseif ($preview) { + $response->setContent( + $this->renderPreview($viewTemplate, $parameters) + ); + } else { + $response->setContent( + $this->renderView( + $viewTemplate, + $parameters + ) + ); + } + return $response; + + } catch (\InvalidArgumentException $exception) { + // template not found + throw new HttpException(406, 'Error encountered when rendering content', $exception); + } } /** @@ -102,40 +140,31 @@ public static function getSubscribedServices() return $subscribedServices; } - /** - * "seo" => array:7 [▼ - * "title" => "" - * "description" => "" - * "keywords" => "" - * "canonicalUrl" => "" - * "noIndex" => false - * "noFollow" => false - * "hideInSitemap" => false - * ]. - * - * @noinspection PhpUnusedPrivateMethodInspection - */ - private function getSeo(Event $event): array + private function createResponse(Request $request): Response { - return [$event]; - } + $response = new Response(); + $cacheLifetime = $request->attributes->get('_cacheLifetime'); + + if ($cacheLifetime) { + $response->setPublic(); + $response->headers->set( + SuluHttpCache::HEADER_REVERSE_PROXY_TTL, + $cacheLifetime + ); + $response->setMaxAge($this->getParameter('sulu_http_cache.cache.max_age')); + $response->setSharedMaxAge($this->getParameter('sulu_http_cache.cache.shared_max_age')); + } - /** - * "excerpt" => array:8 [▼ - * "title" => "" - * "more" => "" - * "description" => "" - * "categories" => [] - * "tags" => [] - * "segments" => [] - * "icon" => [] - * "images" => [] - * ]. - * - * @noinspection PhpUnusedPrivateMethodInspection - */ - private function getExcerpt(Event $event): array - { - return [$event]; + // we need to set the content type ourselves here + // else symfony will use the accept header of the client and the page could be cached with false content-type + // see following symfony issue: https://github.com/symfony/symfony/issues/35694 + $mimeType = $request->getMimeType($request->getRequestFormat()); + + if ($mimeType) { + $response->headers->set('Content-Type', $mimeType); + } + + return $response; } + } diff --git a/src/Entity/Event.php b/src/Entity/Event.php index cbd7af3..20eade3 100644 --- a/src/Entity/Event.php +++ b/src/Entity/Event.php @@ -71,7 +71,7 @@ class Event implements AuditableTranslationInterface /** * @var Collection * - * @ORM\OneToMany(targetEntity="Manuxi\SuluEventBundle\Entity\EventTranslation", mappedBy="event", cascade={"ALL"}, indexBy="locale") + * @ORM\OneToMany(targetEntity="Manuxi\SuluEventBundle\Entity\EventTranslation", mappedBy="event", cascade={"ALL"}, indexBy="locale", fetch="EXTRA_LAZY") * * @Serializer\Exclude */ diff --git a/src/Entity/Models/EventModel.php b/src/Entity/Models/EventModel.php index 54d91ca..15e9284 100644 --- a/src/Entity/Models/EventModel.php +++ b/src/Entity/Models/EventModel.php @@ -152,7 +152,7 @@ private function mapDataToEvent(Event $event, array $data): Event $description = $this->getProperty($data, 'description'); if ($description) { - $event->setTeaser($description); + $event->setDescription($description); } $startDate = $this->getProperty($data, 'startDate'); diff --git a/src/Preview/EventObjectProvider.php b/src/Preview/EventObjectProvider.php new file mode 100644 index 0000000..bad94a5 --- /dev/null +++ b/src/Preview/EventObjectProvider.php @@ -0,0 +1,52 @@ +eventRepository = $eventRepository; + } + + public function getObject($id, $locale) + { + return $this->eventRepository->findById((int)$id, $locale); + } + + public function getId($object) + { + return $object->getId(); + } + + public function setValues($object, $locale, array $data) + { + // TODO: Implement setValues() method. + } + + public function setContext($object, $locale, array $context) + { + if (\array_key_exists('template', $context)) { + $object->setStructureType($context['template']); + } + + return $object; + } + + public function serialize($object) + { + return serialize($object); + } + + public function deserialize($serializedObject, $objectClass) + { + return unserialize($serializedObject); + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 25ccb72..f60b642 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -120,5 +120,14 @@ + + + + + + + + + diff --git a/src/Routing/EventRouteDefaultsProvider.php b/src/Routing/EventRouteDefaultsProvider.php index 548f442..3274ccd 100644 --- a/src/Routing/EventRouteDefaultsProvider.php +++ b/src/Routing/EventRouteDefaultsProvider.php @@ -23,7 +23,8 @@ public function getByEntity($entityClass, $id, $locale, $object = null) { return [ '_controller' => EventController::class . '::indexAction', - 'event' => $object ?: $this->eventRepository->findById($id, $locale), +// 'event' => $object ?: $this->eventRepository->findById($id, $locale), + 'event' => $this->eventRepository->findById($id, $locale), ]; }