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

Implemented sorting pages #179

Merged
merged 5 commits into from
Jun 7, 2017
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
23 changes: 23 additions & 0 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ public function postTriggerAction($uuid, Request $request)

$data = $this->getDocumentManager()->find($copiedPath, $locale);
break;
case 'order':
$this->orderPages($this->getRequestParameter($request, 'pages', true), $locale);
$this->getDocumentManager()->flush();
$this->getDocumentManager()->clear();

$data = $this->getDocumentManager()->find($uuid, $locale);
break;
default:
throw new RestException('Unrecognized action: ' . $action);
}
Expand All @@ -393,6 +400,22 @@ public function postTriggerAction($uuid, Request $request)
return $this->handleView($view);
}

/**
* Ordering given pages.
*
* @param array $pages
* @param string $locale
*/
private function orderPages(array $pages, $locale)
{
$documentManager = $this->getDocumentManager();

for ($i = 0; $i < count($pages); ++$i) {
$document = $documentManager->find($pages[$i], $locale);
$documentManager->reorder($document, null);
}
}

/**
* {@inheritdoc}
*/
Expand Down
70 changes: 68 additions & 2 deletions Document/Serializer/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,42 @@
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
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\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\Content\Metadata\PropertyMetadata;

/**
* Extends serialization for articles.
*/
class ArticleSubscriber implements EventSubscriberInterface
{
const PAGE_TITLE_TAG_NAME = 'sulu_article.page_title';
const PAGE_TITLE_PROPERTY_NAME = 'pageTitle';

use ArticleTypeTrait;

/**
* @var StructureManagerInterface
*/
private $structureManager;

/**
* @var StructureMetadataFactoryInterface
*/
private $structureMetadataFactory;

/**
* @param StructureManagerInterface $structureManager
* @param StructureMetadataFactoryInterface $structureMetadataFactory
*/
public function __construct(StructureManagerInterface $structureManager)
{
public function __construct(
StructureManagerInterface $structureManager,
StructureMetadataFactoryInterface $structureMetadataFactory
) {
$this->structureManager = $structureManager;
$this->structureMetadataFactory = $structureMetadataFactory;
}

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

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

/**
* Append page-title-property to result.
*
* @param ObjectEvent $event
*/
public function addPageTitlePropertyNameOnPostSerialize(ObjectEvent $event)
{
$article = $event->getObject();
$visitor = $event->getVisitor();
$context = $event->getContext();

if (!$article instanceof ArticleInterface) {
return;
}

$property = $this->getPageTitleProperty($article);
if ($property) {
$visitor->addData('_pageTitlePropertyName', $context->accept($property->getName()));
}
}

/**
* Find page-title property.
*
* @param ArticleInterface $document
*
* @return PropertyMetadata
*/
private function getPageTitleProperty(ArticleInterface $document)
{
$metadata = $this->structureMetadataFactory->getStructureMetadata(
'article',
$document->getStructureType()
);

if ($metadata->hasPropertyWithTagName(self::PAGE_TITLE_TAG_NAME)) {
return $metadata->getPropertyByTagName(self::PAGE_TITLE_TAG_NAME);
}

if ($metadata->hasProperty(self::PAGE_TITLE_PROPERTY_NAME)) {
return $metadata->getProperty(self::PAGE_TITLE_PROPERTY_NAME);
}

return null;
}
}
5 changes: 4 additions & 1 deletion Document/Serializer/ArticleWebsiteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
use Sulu\Component\Content\Compat\StructureInterface;
use Sulu\Component\Content\Compat\StructureManagerInterface;
use Sulu\Component\Content\ContentTypeManagerInterface;
use Sulu\Component\Util\SortUtils;

/**
* Extends serializer with addtional functionallity to prepare article(-page) data.
* Extends serializer with additional functionality to prepare article(-page) data.
*/
class ArticleWebsiteSubscriber implements EventSubscriberInterface
{
Expand Down Expand Up @@ -155,6 +156,8 @@ public function resolveContentForArticleOnPostSerialize(ObjectEvent $event)

if (null !== $children && $context->attributes->containsKey('pageNumber')) {
$pages = array_values(is_array($children) ? $children : iterator_to_array($children));
$pages = SortUtils::multisort($pages, 'pageNumber');

$pageNumber = $context->attributes->get('pageNumber')->get();
if ($pageNumber !== 1) {
$article = $pages[$pageNumber - 2];
Expand Down
48 changes: 46 additions & 2 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
use Sulu\Component\Content\Document\LocalizationState;
use Sulu\Component\Content\Document\WorkflowStage;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
use Sulu\Component\DocumentManager\Event\CopyEvent;
Expand All @@ -29,8 +30,10 @@
use Sulu\Component\DocumentManager\Event\PublishEvent;
use Sulu\Component\DocumentManager\Event\RemoveDraftEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Event\ReorderEvent;
use Sulu\Component\DocumentManager\Event\UnpublishEvent;
use Sulu\Component\DocumentManager\Events;
use Sulu\Component\Util\SortUtils;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand Down Expand Up @@ -123,6 +126,7 @@ public static function getSubscribedEvents()
['publishChildren', 0],
['persistPageData', -2000],
],
Events::REORDER => [['persistPageDataOnReorder', -2000]],
Events::UNPUBLISH => 'handleUnpublish',
Events::REMOVE_DRAFT => [['handleScheduleIndex', -1024], ['removeDraftChildren', 0]],
Events::FLUSH => [['handleFlush', -2048], ['handleFlushLive', -2048]],
Expand Down Expand Up @@ -236,6 +240,32 @@ public function publishChildren(PublishEvent $event)
}
}

/**
* Persist page-data for reordering children.
*
* @param ReorderEvent $event
*/
public function persistPageDataOnReorder(ReorderEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticlePageDocument) {
return;
}

$document = $document->getParent();
$node = $this->documentInspector->getNode($document);

$this->setPageData($document, $node, $document->getLocale());

$document->setWorkflowStage(WorkflowStage::TEST);
$this->documentManager->persist($document, $this->documentInspector->getLocale($document));

$this->documents[$document->getUuid()] = [
'uuid' => $document->getUuid(),
'locale' => $document->getLocale(),
];
}

/**
* Persist page-data.
*
Expand All @@ -248,6 +278,18 @@ public function persistPageData($event)
return;
}

$this->setPageData($document, $event->getNode(), $event->getLocale());
}

/**
* Set page-data for given document on given node.
*
* @param ArticleDocument $document
* @param NodeInterface $node
* @param string $locale
*/
private function setPageData(ArticleDocument $document, NodeInterface $node, $locale)
{
$pages = [
[
'uuid' => $document->getUuid(),
Expand All @@ -270,9 +312,11 @@ public function persistPageData($event)
}
}

$pages = SortUtils::multisort($pages, '[pageNumber]');

$document->setPages($pages);
$event->getNode()->setProperty(
$this->propertyEncoder->localizedSystemName(self::PAGES_PROPERTY, $event->getLocale()),
$node->setProperty(
$this->propertyEncoder->localizedSystemName(self::PAGES_PROPERTY, $locale),
json_encode($pages)
);
}
Expand Down
43 changes: 41 additions & 2 deletions Document/Subscriber/PageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
use Sulu\Bundle\ArticleBundle\Document\Behavior\PageBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\ChildrenBehavior;
use Sulu\Component\DocumentManager\DocumentInspector;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\HydrateEvent;
use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Event\PublishEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Event\ReorderEvent;
use Sulu\Component\DocumentManager\Event\RestoreEvent;
use Sulu\Component\DocumentManager\Events;
use Sulu\Component\DocumentManager\PropertyEncoder;
Expand All @@ -39,15 +41,24 @@ class PageSubscriber implements EventSubscriberInterface
* @var PropertyEncoder
*/
private $propertyEncoder;
/**
* @var DocumentManagerInterface
*/
private $documentManager;

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

/**
Expand All @@ -60,6 +71,7 @@ public static function getSubscribedEvents()
Events::PERSIST => [['handlePersist', -1024]],
Events::REMOVE => [['handleRemove', 5]],
Events::PUBLISH => [['handlePublishPageNumber', -1024]],
Events::REORDER => [['handleReorder', 0]],
Events::RESTORE => [['handleRestore', -1024]],
];
}
Expand Down Expand Up @@ -115,6 +127,33 @@ public function handlePersist(PersistEvent $event)
$document->setPageNumber($page);
}

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

$propertyName = $this->propertyEncoder->systemName(static::FIELD);
$parentNode = $this->documentInspector->getNode($document->getParent());

$page = 1;
foreach ($parentNode->getNodes() as $childNode) {
$child = $this->documentManager->find($childNode->getIdentifier(), $event->getLocale());
if (!$child instanceof PageBehavior) {
continue;
}

$childNode->setProperty($propertyName, ++$page);
$child->setPageNumber($page);
}
}

/**
* Copy page-number to live workspace.
*
Expand Down
Loading