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 synchronize children between draft and live #139

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
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
18 changes: 13 additions & 5 deletions Document/Subscriber/ArticlePageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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 Sulu\Component\DocumentManager\NameResolver;
use Symfony\Cmf\Api\Slugifier\SlugifierInterface;
Expand Down Expand Up @@ -93,6 +94,7 @@ public static function getSubscribedEvents()
['setStructureTypeToParent', -2000],
['setWorkflowStageOnArticle', -2000],
],
Events::REMOVE => ['setWorkflowStageOnArticle'],
Events::METADATA_LOAD => ['handleMetadataLoad'],
];
}
Expand All @@ -115,9 +117,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 @@ -127,7 +129,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 @@ -238,7 +244,7 @@ public function setStructureTypeToParent(PersistEvent $event)
}

/**
* Add page-title to metadata.
* Extend metadata for article-page.
*
* @param MetadataLoadEvent $event
*/
Expand All @@ -248,7 +254,9 @@ public function handleMetadataLoad(MetadataLoadEvent $event)
return;
}

$event->getMetadata()->addFieldMapping(
$metadata = $event->getMetadata();
$metadata->setSyncRemoveLive(false);
$metadata->addFieldMapping(
'pageTitle',
[
'encoding' => 'system_localized',
Expand Down
43 changes: 43 additions & 0 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sulu\Bundle\ArticleBundle\Document\Subscriber;

use PHPCR\NodeInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
use Sulu\Bundle\ArticleBundle\Document\Index\IndexerInterface;
Expand Down Expand Up @@ -115,6 +116,7 @@ public static function getSubscribedEvents()
Events::PUBLISH => [
['handleScheduleIndexLive', 0],
['handleScheduleIndex', 0],
['synchronizeChildren', 0],
['publishChildren', 0],
['persistPageData', -2000],
],
Expand Down Expand Up @@ -168,6 +170,47 @@ public function handleScheduleIndexLive(AbstractMappingEvent $event)
];
}

/**
* Syncs children between live and draft.
*
* @param PublishEvent $event
*/
public function synchronizeChildren(PublishEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticleDocument) {
return;
}

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

$liveChildren = $this->getChildren($liveNode);
$draftChildren = $this->getChildren($draftNode);
$removedChildrenIds = array_diff(array_keys($liveChildren), array_keys($draftChildren));

foreach ($removedChildrenIds as $removedChildrenId) {
$liveChildren[$removedChildrenId]->remove();
}
}

/**
* Returns children of given node.
*
* @param NodeInterface $node
*
* @return NodeInterface[]
*/
private function getChildren(NodeInterface $node)
{
$result = [];
foreach ($node->getNodes() as $child) {
$result[$child->getIdentifier()] = $child;
}

return $result;
}

/**
* Publish pages when article will be published.
*
Expand Down
8 changes: 3 additions & 5 deletions Document/Subscriber/PageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public static function getSubscribedEvents()
return [
Events::HYDRATE => ['handleHydrate'],
Events::PERSIST => [['handlePersist', -1024]],
Events::PUBLISH => [['handlePublish', -1024]],
Events::REMOVE => [['handleRemove', 5]],
Events::PUBLISH => [['handlePublishPageNumber', -1024]],
];
}

Expand Down Expand Up @@ -117,7 +117,7 @@ public function handlePersist(PersistEvent $event)
*
* @param PublishEvent $event
*/
public function handlePublish(PublishEvent $event)
public function handlePublishPageNumber(PublishEvent $event)
{
$document = $event->getDocument();
$node = $event->getNode();
Expand All @@ -141,10 +141,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 Down
36 changes: 28 additions & 8 deletions Document/Subscriber/RoutableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class RoutableSubscriber implements EventSubscriberInterface
/**
* @var ChainRouteGeneratorInterface
*/
private $routeGeneratorPool;
private $chainRouteGenerator;

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

/**
* @param ChainRouteGeneratorInterface $routeGeneratorPool
* @param ChainRouteGeneratorInterface $chainRouteGenerator
* @param RouteManagerInterface $routeManager
* @param RouteRepositoryInterface $routeRepository
* @param EntityManagerInterface $entityManager
Expand All @@ -85,15 +85,15 @@ class RoutableSubscriber implements EventSubscriberInterface
* @param DocumentInspector $documentInspector
*/
public function __construct(
ChainRouteGeneratorInterface $routeGeneratorPool,
ChainRouteGeneratorInterface $chainRouteGenerator,
RouteManagerInterface $routeManager,
RouteRepositoryInterface $routeRepository,
EntityManagerInterface $entityManager,
PropertyEncoder $propertyEncoder,
StructureMetadataFactoryInterface $metadataFactory,
DocumentInspector $documentInspector
) {
$this->routeGeneratorPool = $routeGeneratorPool;
$this->chainRouteGenerator = $chainRouteGenerator;
$this->routeManager = $routeManager;
$this->routeRepository = $routeRepository;
$this->entityManager = $entityManager;
Expand Down Expand Up @@ -158,7 +158,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 @@ -208,7 +208,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 @@ -231,7 +251,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 @@ -255,7 +275,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
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ define([

startPreview: function(component, data) {
var pageData = getPage(component.options.page, data);
if (!pageData.id) {
return;
}

if (!!pageData.type && pageData.type.name === 'ghost') {
pageData = {id: pageData.id};
}
Expand Down
2 changes: 1 addition & 1 deletion Resources/translations/sulu/backend.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
</trans-unit>
<trans-unit id="multi-page-02">
<source>sulu_article.edit.new-page</source>
<target>Neue Seite erstellen</target>
<target>Seite hinzufügen</target>
</trans-unit>
</body>
</file>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function testSetWorkflowStageOnArticle()
$this->document->getParent()->willReturn($this->parentDocument->reveal());

$this->documentInspector->getLocalizationState($this->parentDocument->reveal())->willReturn(LocalizationState::LOCALIZED);
$this->documentInspector->getLocale($this->document->reveal())->willReturn($this->locale);

$this->parentDocument->setWorkflowStage(WorkflowStage::TEST)->shouldBeCalled();
$this->documentManager->persist($this->parentDocument->reveal(), $this->locale, Argument::any())->shouldBeCalled();
Expand Down
Loading