Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added route-behavior to article-page #131

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions DependencyInjection/SuluArticleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ public function prepend(ContainerBuilder $container)
);
}

if ($container->hasExtension('sulu_route')) {
$container->prependExtensionConfig(
'sulu_route',
[
'mappings' => [
ArticlePageDocument::class => [
'generator' => 'article_page',
'options' => [
'route_schema' => '/{translator.trans("page")}-{object.getPageNumber()}',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would call the parameter just schema as we are in the route definition

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there it is also route_schema

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

'parent' => '{object.getParent().getRoutePath()}',
],
],
],
]
);
}

if ($container->hasExtension('fos_rest')) {
$container->prependExtensionConfig(
'fos_rest',
Expand Down
72 changes: 69 additions & 3 deletions Document/ArticlePageDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Sulu\Bundle\ArticleBundle\Document;

use Sulu\Bundle\ArticleBundle\Document\Behavior\PageBehavior;
use Sulu\Bundle\ArticleBundle\Document\Behavior\RoutableBehavior;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\Content\Document\Behavior\StructureBehavior;
use Sulu\Component\Content\Document\Structure\Structure;
use Sulu\Component\Content\Document\Structure\StructureInterface;
Expand All @@ -30,6 +33,8 @@ class ArticlePageDocument implements
AutoNameBehavior,
PathBehavior,
StructureBehavior,
RoutableBehavior,
PageBehavior,
ArticleInterface
{
/**
Expand Down Expand Up @@ -72,6 +77,16 @@ class ArticlePageDocument implements
*/
private $structure;

/**
* @var RouteInterface
*/
private $route;

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

/**
* @var int
*/
Expand Down Expand Up @@ -207,15 +222,66 @@ public function getStructure()
}

/**
* Returns page.
*
* @return int
* {@inheritdoc}
*/
public function getId()
{
return $this->uuid;
}

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

/**
* {@inheritdoc}
*/
public function setRoute(RouteInterface $route)
{
$this->route = $route;
$this->routePath = $route->getPath();

return $this;
}

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

/**
* {@inheritdoc}
*/
public function setRoutePath($routePath)
{
$this->routePath = $routePath;
}

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

/**
* {@inheritdoc}
*/
public function setPageNumber($pageNumber)
{
$this->pageNumber = $pageNumber;

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
37 changes: 37 additions & 0 deletions Document/Behavior/PageBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Document\Behavior;

use Sulu\Component\DocumentManager\Behavior\Mapping\ParentBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\UuidBehavior;

/**
* This behavior has to be attached to documents which should have a page-number property.
*/
interface PageBehavior extends ParentBehavior, UuidBehavior
{
/**
* Returns page-number.
*
* @return int
*/
public function getPageNumber();

/**
* Set page-number.
*
* @param int $pageNumber
*
* @return self
*/
public function setPageNumber($pageNumber);
}
33 changes: 0 additions & 33 deletions Document/Subscriber/ArticlePageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\Content\Metadata\PropertyMetadata;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\HydrateEvent;
use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -69,11 +68,9 @@ public static function getSubscribedEvents()
return [
Events::PERSIST => [
['setTitleOnPersist', 2048],
['setPageNumberOnPersist', 0],
['setStructureTypeToParent', -2048],
['setWorkflowStageOnArticle', -2048],
],
Events::HYDRATE => [['setPageNumberOnHydrate', 0]],
];
}

Expand Down Expand Up @@ -150,21 +147,6 @@ private function getPageTitleProperty(ArticlePageDocument $document)
return null;
}

/**
* Set page-number to document on persist.
*
* @param PersistEvent $event
*/
public function setPageNumberOnPersist(PersistEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticlePageDocument) {
return;
}

$event->getAccessor()->set('pageNumber', $event->getNode()->getIndex() + 1);
}

/**
* Set structure-type to parent document.
*
Expand All @@ -183,19 +165,4 @@ public function setStructureTypeToParent(PersistEvent $event)
$document->getParent()->setStructureType($document->getStructureType());
$this->documentManager->persist($document->getParent(), $event->getLocale());
}

/**
* Set page-number to document on persist.
*
* @param HydrateEvent $event
*/
public function setPageNumberOnHydrate(HydrateEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticlePageDocument) {
return;
}

$event->getAccessor()->set('pageNumber', $event->getNode()->getIndex() + 1);
}
}
135 changes: 135 additions & 0 deletions Document/Subscriber/PageSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Document\Subscriber;

use Sulu\Bundle\ArticleBundle\Document\Behavior\PageBehavior;
use Sulu\Component\DocumentManager\DocumentInspector;
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent;
use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Events;
use Sulu\Component\DocumentManager\PropertyEncoder;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Handles document-manager events to set and update page-numbers.
*/
class PageSubscriber implements EventSubscriberInterface
{
const FIELD = 'pageNumber';

/**
* @var DocumentInspector
*/
private $documentInspector;

/**
* @var PropertyEncoder
*/
private $propertyEncoder;

/**
* @param DocumentInspector $documentInspector
* @param PropertyEncoder $propertyEncoder
*/
public function __construct(DocumentInspector $documentInspector, PropertyEncoder $propertyEncoder)
{
$this->documentInspector = $documentInspector;
$this->propertyEncoder = $propertyEncoder;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
Events::PERSIST => [['handlePersist', -1024]],
Events::REMOVE => [['handleRemove', 5]],
Events::METADATA_LOAD => 'handleMetadataLoad',
];
}

/**
* Add page-number to metadata.
*
* @param MetadataLoadEvent $event
*/
public function handleMetadataLoad(MetadataLoadEvent $event)
{
$metadata = $event->getMetadata();

if (false === $metadata->getReflectionClass()->isSubclassOf(PageBehavior::class)) {
return;
}

$metadata->addFieldMapping(
'pageNumber',
[
'encoding' => 'system',
'property' => self::FIELD,
]
);
}

/**
* Set the page-number to new pages.
*
* @param PersistEvent $event
*/
public function handlePersist(PersistEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof PageBehavior || $document->getPageNumber()) {
return;
}

$parentDocument = $document->getParent();

$page = 1;
foreach ($parentDocument->getChildren() as $child) {
if (!$child instanceof PageBehavior) {
continue;
}

++$page;
}

$childNode = $this->documentInspector->getNode($document);
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), $page);
}

/**
* Adjust the page-numbers of siblings when removing a page.
*
* @param RemoveEvent $event
*/
public function handleRemove(RemoveEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof PageBehavior) {
return;
}

$parentDocument = $document->getParent();

$page = 1;
foreach ($parentDocument->getChildren() as $child) {
if (!$child instanceof PageBehavior || $child->getUuid() === $document->getUuid()) {
continue;
}

$childNode = $this->documentInspector->getNode($child);
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), $page++);
}
}
}
Loading