Skip to content

Commit

Permalink
Fixed versioning and remove-draft of article-pages (#145)
Browse files Browse the repository at this point in the history
* fixed versioning of article-pages

* added upgrade note

* added constraint for jackalope/jackalope
  • Loading branch information
wachterjohannes authored and alexander-schranz committed Apr 20, 2017
1 parent 3e016b1 commit fd51fa9
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 12 deletions.
1 change: 1 addition & 0 deletions Controller/WebsiteArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function indexAction(Request $request, ArticleInterface $object, $view, $
* @param ArticleInterface $object
* @param string $view
* @param int $pageNumber
* @param array $attributes
*
* @return Response
*/
Expand Down
2 changes: 2 additions & 0 deletions Document/ArticlePageDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sulu\Bundle\ArticleBundle\Document\Behavior\PageBehavior;
use Sulu\Bundle\ArticleBundle\Document\Behavior\RoutablePageBehavior;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\Content\Document\Behavior\LocalizedStructureBehavior;
use Sulu\Component\Content\Document\Behavior\StructureBehavior;
use Sulu\Component\Content\Document\Structure\Structure;
use Sulu\Component\Content\Document\Structure\StructureInterface;
Expand All @@ -33,6 +34,7 @@ class ArticlePageDocument implements
AutoNameBehavior,
PathBehavior,
StructureBehavior,
LocalizedStructureBehavior,
RoutablePageBehavior,
PageBehavior,
ArticleInterface
Expand Down
2 changes: 1 addition & 1 deletion Document/Initializer/ArticleNodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ public function getDeclaredPropertyDefinitions()
*/
public function getDeclaredChildNodeDefinitions()
{
return [];
return [new ArticlePageNodeDefinition()];
}
}
109 changes: 109 additions & 0 deletions Document/Initializer/ArticlePageNodeDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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\Initializer;

use PHPCR\NodeType\NodeDefinitionInterface;
use PHPCR\Version\OnParentVersionAction;

/**
* Represents definition for article-pages as children for article-document.
*/
class ArticlePageNodeDefinition implements NodeDefinitionInterface
{
/**
* {@inheritdoc}
*/
public function getDeclaringNodeType()
{
return null;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return '*';
}

/**
* {@inheritdoc}
*/
public function isAutoCreated()
{
return false;
}

/**
* {@inheritdoc}
*/
public function isMandatory()
{
return false;
}

/**
* {@inheritdoc}
*/
public function getOnParentVersion()
{
return OnParentVersionAction::COPY;
}

/**
* {@inheritdoc}
*/
public function isProtected()
{
return false;
}

/**
* {@inheritdoc}
*/
public function getRequiredPrimaryTypes()
{
return null;
}

/**
* {@inheritdoc}
*/
public function getRequiredPrimaryTypeNames()
{
return null;
}

/**
* {@inheritdoc}
*/
public function getDefaultPrimaryType()
{
return null;
}

/**
* {@inheritdoc}
*/
public function getDefaultPrimaryTypeName()
{
return null;
}

/**
* {@inheritdoc}
*/
public function allowsSameNameSiblings()
{
return false;
}
}
11 changes: 10 additions & 1 deletion Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sulu\Bundle\ArticleBundle\Document\Subscriber;

use PHPCR\NodeInterface;
use PHPCR\PathNotFoundException;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
use Sulu\Bundle\ArticleBundle\Document\Index\IndexerInterface;
Expand Down Expand Up @@ -306,8 +307,16 @@ public function removeDraftChildren(RemoveDraftEvent $event)
}

foreach ($document->getChildren() as $child) {
if ($this->documentInspector->getLocalizationState($child) !== LocalizationState::GHOST) {
if ($this->documentInspector->getLocalizationState($child) === LocalizationState::GHOST) {
continue;
}

try {
$this->documentManager->removeDraft($child, $event->getLocale());
} catch (PathNotFoundException $exception) {
// child is not available in live workspace
$node = $this->documentInspector->getNode($child);
$node->remove();
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions Document/Subscriber/PageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
namespace Sulu\Bundle\ArticleBundle\Document\Subscriber;

use Sulu\Bundle\ArticleBundle\Document\Behavior\PageBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\ChildrenBehavior;
use Sulu\Component\DocumentManager\DocumentInspector;
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\RestoreEvent;
use Sulu\Component\DocumentManager\Events;
use Sulu\Component\DocumentManager\PropertyEncoder;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -58,6 +60,7 @@ public static function getSubscribedEvents()
Events::PERSIST => [['handlePersist', -1024]],
Events::REMOVE => [['handleRemove', 5]],
Events::PUBLISH => [['handlePublishPageNumber', -1024]],
Events::RESTORE => [['handleRestore', -1024]],
];
}

Expand Down Expand Up @@ -151,4 +154,27 @@ public function handleRemove(RemoveEvent $event)
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), ++$page);
}
}

/**
* Adjust the page-numbers of siblings when restoring a page.
*
* @param RestoreEvent $event
*/
public function handleRestore(RestoreEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ChildrenBehavior) {
return;
}

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

$childNode = $this->documentInspector->getNode($child);
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), ++$page);
}
}
}
2 changes: 1 addition & 1 deletion Resources/public/dist/components/articles/edit/main.js

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions Resources/public/js/components/articles/edit/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,7 @@ define([
ArticleManager.removeDraft(this.data.id, this.options.locale).always(function() {
this.sandbox.emit('sulu.header.toolbar.item.enable', 'edit');
}.bind(this)).then(function(response) {
// reload page
this.sandbox.emit(
'sulu.router.navigate',
this.sandbox.mvc.history.fragment,
true,
true
);
ArticleRouter.toEdit(this.options.id, this.options.locale);
this.saved(response.id, response);
}.bind(this)).fail(function() {
this.sandbox.emit('husky.label.header.reset');
Expand Down
18 changes: 18 additions & 0 deletions Tests/Unit/Document/Subscriber/ArticleSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sulu\Bundle\ArticleBundle\Tests\Unit\Document\Subscriber;

use PHPCR\NodeInterface;
use PHPCR\PathNotFoundException;
use Prophecy\Argument;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
Expand Down Expand Up @@ -335,6 +336,23 @@ public function testRemoveDraftChildren()
$this->articleSubscriber->removeDraftChildren($this->prophesizeEvent(RemoveDraftEvent::class, $this->locale));
}

public function testRemoveDraftChildrenNotExists()
{
$child = $this->prophesize(ArticlePageDocument::class)->reveal();

$this->document->getChildren()->willReturn([$child]);

$this->documentInspector->getLocalizationState($child)->willReturn(LocalizationState::LOCALIZED);
$this->documentManager->removeDraft($child, $this->locale)->shouldBeCalled()
->willThrow(new PathNotFoundException());

$node = $this->prophesize(NodeInterface::class);
$node->remove()->shouldBeCalled();
$this->documentInspector->getNode($child)->willReturn($node->reveal());

$this->articleSubscriber->removeDraftChildren($this->prophesizeEvent(RemoveDraftEvent::class, $this->locale));
}

public function testSetChildrenStructureType()
{
$children = [
Expand Down
19 changes: 19 additions & 0 deletions Tests/Unit/Document/Subscriber/PageSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Event\PublishEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Event\RestoreEvent;
use Sulu\Component\DocumentManager\PropertyEncoder;

class PageSubscriberTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -148,4 +149,22 @@ public function testHandlePublishPageNumber()

$this->pageSubscriber->handlePublishPageNumber($event->reveal());
}

public function testHandleRestore()
{
$event = $this->prophesize(RestoreEvent::class);

$parentDocument = $this->prophesize(ChildrenBehavior::class);
$parentDocument->getChildren()->willReturn([$this->document->reveal()]);

$event->getDocument()->willReturn($parentDocument->reveal());
$this->documentInspector->getNode($this->document->reveal())->willReturn($this->node->reveal());

$this->propertyEncoder->systemName(PageSubscriber::FIELD)->willReturn('sulu:' . PageSubscriber::FIELD);

$this->document->getPageNumber()->willReturn(2);
$this->node->setProperty('sulu:' . PageSubscriber::FIELD, 2)->shouldBeCalled();

$this->pageSubscriber->handleRestore($event->reveal());
}
}
42 changes: 42 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,48 @@

## dev-develop

### WebsiteArticleController

The multi-page feature needs a refactoring of the ``WebsiteArticleController``.
If you have overwritten it you have to adapt it.

__Before:__

```php
class CustomArticleController extends Controller
{
public function indexAction(Request $request, ArticleDocument $object, $view)
{
$content = $this->get('jms_serializer')->serialize(
$object,
'array',
SerializationContext::create()
->setSerializeNull(true)
->setGroups(['website', 'content'])
->setAttribute('website', true)
);

return $this->render(
$view . '.html.twig',
$this->get('sulu_website.resolver.template_attribute')->resolve($content),
$this->createResponse($request)
);
}
}
```

__After:__

```php
class CustomArticleController extends WebsiteArticleController
{
public function indexAction(Request $request, ArticleInterface $object, $view, $pageNumber = 1)
{
return $this->renderArticle($request, $object, $view, $pageNumber, []);
}
}
```

### Cachelifetime request attribute changed

The `_cacheLifetime` attribute available in the request parameter of a article
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
"require": {
"php": "^5.5 || ^7.0",
"sulu/sulu": "dev-develop",
"ongr/elasticsearch-bundle": "~1.0"
"ongr/elasticsearch-bundle": "~1.0",
"jackalope/jackalope": "^1.2.8 || >=1.3.3"
},
"require-dev": {
"zendframework/zend-stdlib": "2.3.1 as 2.0.0rc5",
"zendframework/zendsearch": "2.*@dev",
"symfony/monolog-bundle": "2.4.*",
"doctrine/doctrine-bundle": "1.4.*",
"jackalope/jackalope-doctrine-dbal": "^1.2.5",
"jackalope/jackalope-jackrabbit": "1.2.*",
"jackalope/jackalope-jackrabbit": "^1.2",
"phpunit/phpunit": "^4.8 || ^5.0",

"sulu/document-manager": "@dev",
Expand Down

0 comments on commit fd51fa9

Please sign in to comment.