Skip to content

Commit

Permalink
Added page-number argument to website-article-controller (#142)
Browse files Browse the repository at this point in the history
* added serialization of pages

* added pages data to article-document

* fixed comments
  • Loading branch information
wachterjohannes committed Apr 14, 2017
1 parent 99d315a commit 3a3a15b
Show file tree
Hide file tree
Showing 17 changed files with 669 additions and 161 deletions.
26 changes: 23 additions & 3 deletions Controller/WebsiteArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,43 @@ class WebsiteArticleController extends Controller
* @param Request $request
* @param ArticleInterface $object
* @param string $view
* @param int $pageNumber
*
* @return Response
*/
public function indexAction(Request $request, ArticleInterface $object, $view)
public function indexAction(Request $request, ArticleInterface $object, $view, $pageNumber = 1)
{
return $this->renderArticle($request, $object, $view, $pageNumber);
}

/**
* Render article with given view.
*
* @param Request $request
* @param ArticleInterface $object
* @param string $view
* @param int $pageNumber
*
* @return Response
*/
private function renderArticle(Request $request, ArticleInterface $object, $view, $pageNumber, $attributes = [])
{
$requestFormat = $request->getRequestFormat();
$viewTemplate = $view . '.' . $requestFormat . '.twig';

$content = $this->get('jms_serializer')->serialize(
$object,
'array',
SerializationContext::create()
->setSerializeNull(true)
->setGroups(['website', 'content'])
->setAttribute('website', true)
->setAttribute('pageNumber', $pageNumber)
);

return $this->render(
$view . '.html.twig',
$this->get('sulu_website.resolver.template_attribute')->resolve($content),
$viewTemplate,
$this->get('sulu_website.resolver.template_attribute')->resolve(array_merge($content, $attributes)),
$this->createResponse($request)
);
}
Expand Down
64 changes: 64 additions & 0 deletions Document/ArticleDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ class ArticleDocument implements
*/
private $title;

/**
* @var string
*/
private $pageTitle;

/**
* @var array
*/
private $pages;

/**
* @var RouteInterface
*/
Expand Down Expand Up @@ -504,4 +514,58 @@ public function getPageUuid()
{
return $this->getUuid();
}

/**
* {@inheritdoc}
*/
public function getPageNumber()
{
return 1;
}

/**
* {@inheritdoc}
*/
public function getPageTitle()
{
return $this->pageTitle;
}

/**
* Set pageTitle.
*
* @param string $pageTitle
*
* @return $this
*/
public function setPageTitle($pageTitle)
{
$this->pageTitle = $pageTitle;

return $this;
}

/**
* Returns pages.
*
* @return array
*/
public function getPages()
{
return $this->pages;
}

/**
* Set pages.
*
* @param array $pages
*
* @return $this
*/
public function setPages($pages)
{
$this->pages = $pages;

return $this;
}
}
14 changes: 14 additions & 0 deletions Document/ArticleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ public function getArticleUuid();
*/
public function getPageUuid();

/**
* Returns page-number.
*
* @return int
*/
public function getPageNumber();

/**
* Returns page-title.
*
* @return string
*/
public function getPageTitle();

/**
* Returns structure-type.
*
Expand Down
124 changes: 2 additions & 122 deletions Document/Serializer/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@
use JMS\Serializer\EventDispatcher\Events;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\Proxy\LazyLoadingInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticleInterface;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
use Sulu\Component\Content\Compat\StructureManagerInterface;
use Sulu\Component\Content\ContentTypeManagerInterface;

/**
* Extends serialization for articles.
Expand All @@ -34,29 +30,12 @@ class ArticleSubscriber implements EventSubscriberInterface
*/
private $structureManager;

/**
* @var ContentTypeManagerInterface
*/
private $contentTypeManager;

/**
* @var LazyLoadingValueHolderFactory
*/
private $proxyFactory;

/**
* @param StructureManagerInterface $structureManager
* @param ContentTypeManagerInterface $contentTypeManager
* @param LazyLoadingValueHolderFactory $proxyFactory
*/
public function __construct(
StructureManagerInterface $structureManager,
ContentTypeManagerInterface $contentTypeManager,
LazyLoadingValueHolderFactory $proxyFactory
) {
public function __construct(StructureManagerInterface $structureManager)
{
$this->structureManager = $structureManager;
$this->contentTypeManager = $contentTypeManager;
$this->proxyFactory = $proxyFactory;
}

/**
Expand All @@ -70,11 +49,6 @@ public static function getSubscribedEvents()
'format' => 'json',
'method' => 'addTypeOnPostSerialize',
],
[
'event' => Events::POST_SERIALIZE,
'format' => 'array',
'method' => 'resolveContentOnPostSerialize',
],
];
}

Expand All @@ -96,98 +70,4 @@ public function addTypeOnPostSerialize(ObjectEvent $event)
$structure = $this->structureManager->getStructure($article->getStructureType(), 'article');
$visitor->addData('articleType', $context->accept($this->getType($structure->getStructure())));
}

/**
* Resolve content on serialization.
*
* @param ObjectEvent $event
*/
public function resolveContentOnPostSerialize(ObjectEvent $event)
{
$article = $event->getObject();
$visitor = $event->getVisitor();
$context = $event->getContext();

if (!$article instanceof ArticleInterface || !$context->attributes->containsKey('website')) {
return;
}

$visitor->addData('uuid', $context->accept($article->getArticleUuid()));
$visitor->addData('pageUuid', $context->accept($article->getPageUuid()));

$visitor->addData('extension', $context->accept($article->getExtensionsData()->toArray()));

$content = $this->resolve($article);
foreach ($content as $name => $value) {
$visitor->addData($name, $value);
}
}

/**
* Returns content and view of article.
*
* @param ArticleInterface $article
*
* @return array
*/
private function resolve(ArticleInterface $article)
{
$structure = $this->structureManager->getStructure($article->getStructureType(), 'article');
$structure->setDocument($article);

$data = $article->getStructure()->toArray();

$content = $this->proxyFactory->createProxy(
\ArrayObject::class,
function (
&$wrappedObject,
LazyLoadingInterface $proxy,
$method,
array $parameters,
&$initializer
) use ($structure, $data) {
$content = [];
foreach ($structure->getProperties(true) as $child) {
if (array_key_exists($child->getName(), $data)) {
$child->setValue($data[$child->getName()]);
}

$contentType = $this->contentTypeManager->get($child->getContentTypeName());
$content[$child->getName()] = $contentType->getContentData($child);
}

$initializer = null;
$wrappedObject = new \ArrayObject($content);

return true;
}
);
$view = $this->proxyFactory->createProxy(
\ArrayObject::class,
function (
&$wrappedObject,
LazyLoadingInterface $proxy,
$method,
array $parameters,
&$initializer
) use ($structure, $data) {
$view = [];
foreach ($structure->getProperties(true) as $child) {
if (array_key_exists($child->getName(), $data)) {
$child->setValue($data[$child->getName()]);
}

$contentType = $this->contentTypeManager->get($child->getContentTypeName());
$view[$child->getName()] = $contentType->getViewData($child);
}

$initializer = null;
$wrappedObject = new \ArrayObject($view);

return true;
}
);

return ['content' => $content, 'view' => $view];
}
}
Loading

0 comments on commit 3a3a15b

Please sign in to comment.