Skip to content

Commit

Permalink
Preview working
Browse files Browse the repository at this point in the history
  • Loading branch information
manuxi committed Mar 31, 2021
1 parent 67b3983 commit be55020
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 55 deletions.
31 changes: 16 additions & 15 deletions src/Admin/EventAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -236,4 +232,9 @@ public function getSecurityContexts(): array
],
];
}

public function getConfigKey(): ?string
{
return 'sulu_event';
}
}
4 changes: 2 additions & 2 deletions src/Controller/Website/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
99 changes: 64 additions & 35 deletions src/Controller/Website/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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' => [
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -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;
}

}
2 changes: 1 addition & 1 deletion src/Entity/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Event implements AuditableTranslationInterface
/**
* @var Collection<string, EventTranslation>
*
* @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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Models/EventModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
52 changes: 52 additions & 0 deletions src/Preview/EventObjectProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Manuxi\SuluEventBundle\Preview;

use Manuxi\SuluEventBundle\Repository\EventRepository;
use Sulu\Bundle\PreviewBundle\Preview\Object\PreviewObjectProviderInterface;

class EventObjectProvider implements PreviewObjectProviderInterface
{
private $eventRepository;

public function __construct(EventRepository $eventRepository)
{
$this->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);
}
}
9 changes: 9 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,14 @@
<argument type="service" id="sulu_event.repository.location"/>
</service>

<service id="sulu_event.object_provider" public="true" class="Manuxi\SuluEventBundle\Preview\EventObjectProvider">
<argument type="service" id="sulu_event.repository.event"/>
<tag name="sulu_preview.object_provider" provider-key="events"/>
</service>

<service id="sulu_event.routing_provider" public="true" class="Manuxi\SuluEventBundle\Routing\EventRouteDefaultsProvider">
<argument type="service" id="sulu_event.repository.event"/>
<tag name="sulu_route.defaults_provider"/>
</service>
</services>
</container>
3 changes: 2 additions & 1 deletion src/Routing/EventRouteDefaultsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
}

Expand Down

0 comments on commit be55020

Please sign in to comment.