Skip to content

Commit

Permalink
added synchronice children between draft and live
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Apr 13, 2017
1 parent 2df74dd commit 307e46a
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 44 deletions.
5 changes: 0 additions & 5 deletions Controller/ArticlePageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,6 @@ public function deleteAction($articleUuid, $uuid, Request $request)
$documentManager->remove($document);
$documentManager->flush();

// FIXME this is a current hack which publishes article automatically when removing a single page
$document = $documentManager->find($articleUuid, $locale);
$documentManager->publish($document, $locale);
$documentManager->flush();

return $this->handleView($this->view(null));
}

Expand Down
3 changes: 2 additions & 1 deletion Document/Behavior/PageBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
namespace Sulu\Bundle\ArticleBundle\Document\Behavior;

use Sulu\Component\DocumentManager\Behavior\Mapping\ParentBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\PathBehavior;
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
interface PageBehavior extends ParentBehavior, UuidBehavior, PathBehavior
{
/**
* Returns page-number.
Expand Down
29 changes: 26 additions & 3 deletions Document/Subscriber/ArticlePageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\Content\Metadata\PropertyMetadata;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent;
use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -71,6 +73,8 @@ public static function getSubscribedEvents()
['setStructureTypeToParent', -2048],
['setWorkflowStageOnArticle', -2048],
],
Events::REMOVE => ['setWorkflowStageOnArticle'],
Events::METADATA_LOAD => ['handleMetadataLoad'],
];
}

Expand Down Expand Up @@ -107,9 +111,9 @@ public function setTitleOnPersist(PersistEvent $event)
/**
* Set workflow-stage to test for article.
*
* @param PersistEvent $event
* @param PersistEvent|RemoveEvent $event
*/
public function setWorkflowStageOnArticle(PersistEvent $event)
public function setWorkflowStageOnArticle($event)
{
$document = $event->getDocument();
if (!$document instanceof ArticlePageDocument
Expand All @@ -119,7 +123,11 @@ public function setWorkflowStageOnArticle(PersistEvent $event)
}

$document->getParent()->setWorkflowStage(WorkflowStage::TEST);
$this->documentManager->persist($document->getParent(), $event->getLocale(), $event->getOptions());
$this->documentManager->persist(
$document->getParent(),
$this->documentInspector->getLocale($document),
$event instanceof PersistEvent ? $event->getOptions() : []
);
}

/**
Expand Down Expand Up @@ -165,4 +173,19 @@ public function setStructureTypeToParent(PersistEvent $event)
$document->getParent()->setStructureType($document->getStructureType());
$this->documentManager->persist($document->getParent(), $event->getLocale());
}

/**
* Extend metadata for article-page.
*
* @param MetadataLoadEvent $event
*/
public function handleMetadata(MetadataLoadEvent $event)
{
$metadata = $event->getMetadata();
if ($metadata->getClass() !== ArticlePageDocument::class) {
return;
}

$metadata->setSyncRemoveLive(false);
}
}
12 changes: 12 additions & 0 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ public function publishChildren(PublishEvent $event)
return;
}

$liveNode = $event->getNode();
$draftNode = $this->documentInspector->getNode($document);

$removedChildrenNames = array_diff(
iterator_to_array($liveNode->getNodeNames()),
iterator_to_array($draftNode->getNodeNames())
);

foreach ($removedChildrenNames as $removedChildrenName) {
$liveNode->getNode($removedChildrenName)->remove();
}

foreach ($document->getChildren() as $child) {
if ($this->documentInspector->getLocalizationState($child) !== LocalizationState::GHOST) {
$this->documentManager->publish($child, $event->getLocale());
Expand Down
59 changes: 54 additions & 5 deletions Document/Subscriber/PageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

namespace Sulu\Bundle\ArticleBundle\Document\Subscriber;

use PHPCR\NodeInterface;
use PHPCR\SessionInterface;
use Sulu\Bundle\ArticleBundle\Document\Behavior\PageBehavior;
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\Events;
use Sulu\Component\DocumentManager\PropertyEncoder;
Expand All @@ -37,14 +40,24 @@ class PageSubscriber implements EventSubscriberInterface
*/
private $propertyEncoder;

/**
* @var SessionInterface
*/
private $liveSession;

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

/**
Expand All @@ -56,6 +69,7 @@ public static function getSubscribedEvents()
Events::HYDRATE => ['handleHydrate'],
Events::PERSIST => [['handlePersist', -1024]],
Events::REMOVE => [['handleRemove', 5]],
Events::PUBLISH => ['handlePublish'],
];
}

Expand Down Expand Up @@ -119,10 +133,8 @@ public function handleRemove(RemoveEvent $event)
return;
}

$parentDocument = $document->getParent();

$page = 1;
foreach ($parentDocument->getChildren() as $child) {
foreach ($document->getParent()->getChildren() as $child) {
if (!$child instanceof PageBehavior || $child->getUuid() === $document->getUuid()) {
continue;
}
Expand All @@ -131,4 +143,41 @@ public function handleRemove(RemoveEvent $event)
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), ++$page);
}
}

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

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

$child->setPageNumber(++$page);

$childNode = $this->getLiveNode($child);
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), $child->getPageNumber());
}
}

/**
* Returns the live node for given document.
*
* @param PageBehavior $document
*
* @return NodeInterface
*/
private function getLiveNode(PageBehavior $document)
{
return $this->liveSession->getNode($document->getPath());
}
}
40 changes: 30 additions & 10 deletions Document/Subscriber/RoutableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Sulu\Bundle\ArticleBundle\Document\Behavior\RoutablePageBehavior;
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface;
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorPoolInterface;
use Sulu\Bundle\RouteBundle\Generator\ChainRouteGeneratorInterface;
use Sulu\Bundle\RouteBundle\Manager\RouteManagerInterface;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\DocumentManager\Behavior\Mapping\ChildrenBehavior;
Expand All @@ -39,9 +39,9 @@ class RoutableSubscriber implements EventSubscriberInterface
const ROUTES_FIELD = 'routes';

/**
* @var RouteGeneratorPoolInterface
* @var ChainRouteGeneratorInterface
*/
private $routeGeneratorPool;
private $chainRouteGenerator;

/**
* @var RouteManagerInterface
Expand Down Expand Up @@ -69,22 +69,22 @@ class RoutableSubscriber implements EventSubscriberInterface
private $documentInspector;

/**
* @param RouteGeneratorPoolInterface $routeGeneratorPool
* @param ChainRouteGeneratorInterface $chainRouteGenerator
* @param RouteManagerInterface $routeManager
* @param RouteRepositoryInterface $routeRepository
* @param EntityManagerInterface $entityManager
* @param PropertyEncoder $propertyEncoder
* @param DocumentInspector $documentInspector
*/
public function __construct(
RouteGeneratorPoolInterface $routeGeneratorPool,
ChainRouteGeneratorInterface $chainRouteGenerator,
RouteManagerInterface $routeManager,
RouteRepositoryInterface $routeRepository,
EntityManagerInterface $entityManager,
PropertyEncoder $propertyEncoder,
DocumentInspector $documentInspector
) {
$this->routeGeneratorPool = $routeGeneratorPool;
$this->chainRouteGenerator = $chainRouteGenerator;
$this->routeManager = $routeManager;
$this->routeRepository = $routeRepository;
$this->entityManager = $entityManager;
Expand Down Expand Up @@ -147,7 +147,7 @@ public function handlePersist(AbstractMappingEvent $event)

$document->setUuid($event->getNode()->getIdentifier());

$generatedRoute = $this->routeGeneratorPool->generate(
$generatedRoute = $this->chainRouteGenerator->generate(
$document,
$event->getOption('route_path') ?: $document->getRoutePath()
);
Expand Down Expand Up @@ -197,7 +197,27 @@ public function handlePublish(PublishEvent $event)
*
* @return RouteInterface
*/
private function createOrUpdateRoute(RoutablePageBehavior $document, $locale)
private function createOrUpdatePageRoute(RoutablePageBehavior $document, $locale)
{
$route = $this->routeRepository->findByEntity($document->getClass(), $document->getUuid(), $locale);
if ($route) {
$document->setRoute($route);

return $this->routeManager->update($document);
}

return $this->routeManager->create($document);
}

/**
* Create or update for given document.
*
* @param RoutableBehavior $document
* @param string $locale
*
* @return RouteInterface
*/
private function createOrUpdateRoute(RoutableBehavior $document, $locale)
{
$route = $this->routeRepository->findByEntity($document->getClass(), $document->getUuid(), $locale);
if ($route) {
Expand All @@ -220,7 +240,7 @@ private function removeOldChildRoutes(SessionInterface $session, array $oldRoute
{
foreach ($oldRoutes as $oldRoute) {
$oldRouteEntity = $this->routeRepository->findByPath($oldRoute, $locale);
if (!$this->nodeExists($session, $oldRouteEntity->getEntityId())) {
if ($oldRouteEntity && !$this->nodeExists($session, $oldRouteEntity->getEntityId())) {
$this->entityManager->remove($oldRouteEntity);
}
}
Expand All @@ -244,7 +264,7 @@ private function generateChildRoutes(ChildrenBehavior $document, $locale)
continue;
}

$childRoute = $this->createOrUpdateRoute($child, $locale);
$childRoute = $this->createOrUpdatePageRoute($child, $locale);
$this->entityManager->persist($childRoute);

$child->setRoutePath($childRoute->getPath());
Expand Down
3 changes: 2 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
</service>
<service id="sulu_article.subscriber.routable"
class="Sulu\Bundle\ArticleBundle\Document\Subscriber\RoutableSubscriber">
<argument type="service" id="sulu_route.generator_pool"/>
<argument type="service" id="sulu_route.chain_generator"/>
<argument type="service" id="sulu_route.manager.route_manager"/>
<argument type="service" id="sulu.repository.route"/>
<argument type="service" id="doctrine.orm.entity_manager"/>
Expand All @@ -121,6 +121,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"/>

<tag name="sulu_document_manager.event_subscriber"/>
</service>
Expand Down
Loading

0 comments on commit 307e46a

Please sign in to comment.