Skip to content

Commit

Permalink
added article-api action order
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Jun 1, 2017
1 parent e7b1677 commit db212fe
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 7 deletions.
24 changes: 24 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,23 @@ 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();

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

/**
* {@inheritdoc}
*/
Expand Down
39 changes: 37 additions & 2 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,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 +125,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 +239,24 @@ 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());
}

/**
* Persist page-data.
*
Expand All @@ -248,6 +269,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 +303,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
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
class="Sulu\Bundle\ArticleBundle\Document\Subscriber\PageSubscriber">
<argument type="service" id="sulu_document_manager.document_inspector"/>
<argument type="service" id="sulu_document_manager.property_encoder"/>
<argument type="service" id="sulu_document_manager.live_session"/>
<argument type="service" id="sulu_document_manager.document_manager"/>

<tag name="sulu_document_manager.event_subscriber"/>
</service>
Expand Down
45 changes: 45 additions & 0 deletions Tests/Functional/Controller/ArticleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ protected function post($title = 'Test-Article', $template = 'default')
return json_decode($client->getResponse()->getContent(), true);
}

protected function postPage($article, $pageTitle = 'Test-Page')
{
$client = $this->createAuthenticatedClient();
$client->request(
'POST',
'/api/articles/' . $article['id'] . '/pages?locale=de',
[
'pageTitle' => $pageTitle,
'template' => $article['template'],
'authored' => '2016-01-01',
]
);

$this->assertHttpStatusCode(200, $client->getResponse());

return json_decode($client->getResponse()->getContent(), true);
}

public function testPost($title = 'Test-Article', $template = 'default')
{
$response = $this->post($title, $template);
Expand Down Expand Up @@ -1048,6 +1066,33 @@ public function testPostPageTreeRouteGenerateRemovePage()
);
}

public function testOrderPages()
{
$article = $this->post();
$pages = [
$this->postPage($article, 'Page 1'),
$this->postPage($article, 'Page 2'),
$this->postPage($article, 'Page 3'),
];
$expectedPages = [$pages[1]['id'], $pages[2]['id'], $pages[0]['id']];

$client = $this->createAuthenticatedClient();
$client->request(
'POST',
'/api/articles/' . $article['id'] . '?action=order&locale=de',
['pages' => $expectedPages]
);

$this->assertHttpStatusCode(200, $client->getResponse());
$response = json_decode($client->getResponse()->getContent(), true);

$responsePages = $response['_embedded']['pages'];
for ($i = 0; $i < count($expectedPages); ++$i) {
$this->assertEquals($expectedPages[$i], $responsePages[$i]['id']);
$this->assertEquals($i + 2, $responsePages[$i]['pageNumber']);
}
}

private function postPageTreeRoute($routePathData, $title = 'Test Article')
{
$client = $this->createAuthenticatedClient();
Expand Down
53 changes: 53 additions & 0 deletions Tests/Unit/Document/Subscriber/ArticleSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Sulu\Component\DocumentManager\Event\PublishEvent;
use Sulu\Component\DocumentManager\Event\RemoveDraftEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Event\ReorderEvent;

class ArticleSubscriberTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -451,4 +452,56 @@ public function testPersistPageData()

$this->articleSubscriber->persistPageData($event->reveal());
}

public function testPersistPageDataOnReorder()
{
$node = $this->prophesize(NodeInterface::class);

$orderedDocument = $this->prophesize(ArticlePageDocument::class);
$orderedDocument->getParent()->willReturn($this->document->reveal());
$this->document->getLocale()->willReturn($this->locale);
$this->documentInspector->getNode($this->document->reveal())->willReturn($node->reveal());

$event = $this->prophesize(ReorderEvent::class);
$event->getDocument()->willReturn($orderedDocument->reveal());

$pages = [
[
'uuid' => '123-123-123',
'title' => 'Test article: page 1',
'routePath' => '/test-article',
'pageNumber' => 1,
],
[
'uuid' => '321-321-321',
'title' => 'Test article: page 2',
'routePath' => '/test-article/page-2',
'pageNumber' => 2,
],
];

$this->document->getUuid()->willReturn($pages[0]['uuid']);
$this->document->getPageTitle()->willReturn($pages[0]['title']);
$this->document->getRoutePath()->willReturn($pages[0]['routePath']);
$this->document->getPageNumber()->willReturn($pages[0]['pageNumber']);

$child = $this->prophesize(ArticlePageDocument::class);
$child->getUuid()->willReturn($pages[1]['uuid']);
$child->getPageTitle()->willReturn($pages[1]['title']);
$child->getRoutePath()->willReturn($pages[1]['routePath']);
$child->getPageNumber()->willReturn($pages[1]['pageNumber']);
$this->document->getChildren()->willReturn(new \ArrayIterator([$child->reveal()]));

$this->documentInspector->getLocalizationState($child->reveal())->willReturn(LocalizationState::LOCALIZED);

$propertyName = 'i18n:' . $this->locale . '-' . ArticleSubscriber::PAGES_PROPERTY;
$this->propertyEncoder->localizedSystemName(ArticleSubscriber::PAGES_PROPERTY, $this->locale)->willReturn(
$propertyName
);

$this->document->setPages($pages)->shouldBeCalled();
$node->setProperty($propertyName, json_encode($pages))->shouldBeCalled();

$this->articleSubscriber->persistPageDataOnReorder($event->reveal());
}
}
Loading

0 comments on commit db212fe

Please sign in to comment.