Skip to content

Commit

Permalink
added copy article functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Apr 19, 2017
1 parent 292cbe5 commit 5a7d060
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 10 deletions.
9 changes: 9 additions & 0 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ONGR\ElasticsearchDSL\Query\TermQuery;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
use Sulu\Bundle\ArticleBundle\Admin\ArticleAdmin;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\Form\ArticleDocumentType;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
use Sulu\Component\Content\Form\Exception\InvalidFormException;
Expand Down Expand Up @@ -342,6 +343,14 @@ public function postTriggerAction($uuid, Request $request)
$destLocales = $this->getRequestParameter($request, 'dest', true);
$data = $this->getMapper()->copyLanguage($uuid, $userId, null, $locale, explode(',', $destLocales));
break;
case 'copy':
/** @var ArticleDocument $document */
$document = $this->getDocumentManager()->find($uuid, $locale);
$copiedPath = $this->getDocumentManager()->copy($document, dirname($document->getPath()));
$this->getDocumentManager()->flush();

$data = $this->getDocumentManager()->find($copiedPath, $locale);
break;
default:
throw new RestException('Unrecognized action: ' . $action);
}
Expand Down
13 changes: 10 additions & 3 deletions Document/ArticleDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,23 @@ public function getRoute()
}

/**
* Set route.
*
* @param RouteInterface $route
* {@inheritdoc}
*/
public function setRoute(RouteInterface $route)
{
$this->route = $route;
$this->routePath = $route->getPath();
}

/**
* {@inheritdoc}
*/
public function removeRoute()
{
$this->route = null;
$this->routePath = null;
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions Document/Behavior/RoutableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ interface RoutableBehavior extends RoutableInterface, UuidBehavior, LocaleBehavi
*/
public function getRoutePath();

/**
* Remove route
*/
public function removeRoute();

/**
* Set route-path.
*
Expand Down
22 changes: 22 additions & 0 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Sulu\Bundle\ArticleBundle\Document\Index\IndexerInterface;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
use Sulu\Component\DocumentManager\Event\CopyEvent;
use Sulu\Component\DocumentManager\Event\FlushEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Event\UnpublishEvent;
Expand Down Expand Up @@ -78,6 +79,7 @@ public static function getSubscribedEvents()
Events::UNPUBLISH => 'handleUnpublish',
Events::REMOVE_DRAFT => ['handleScheduleIndex', -1024],
Events::FLUSH => [['handleFlush', -2048], ['handleFlushLive', -2048]],
Events::COPY => ['handleCopy'],
];
}

Expand Down Expand Up @@ -228,4 +230,24 @@ public function handleRemoveLive($event)
$this->liveIndexer->remove($document);
$this->liveIndexer->flush();
}


/**
* Schedule document to index.
*
* @param CopyEvent $event
*/
public function handleCopy(CopyEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticleDocument) {
return;
}

$uuid = $event->getCopiedNode()->getIdentifier();
$this->documents[$uuid] = [
'uuid' => $uuid,
'locale' => $document->getLocale(),
];
}
}
74 changes: 70 additions & 4 deletions Document/Subscriber/RoutableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
namespace Sulu\Bundle\ArticleBundle\Document\Subscriber;

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\Behavior\RoutableBehavior;
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface;
use Sulu\Bundle\RouteBundle\Manager\RouteManagerInterface;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
use Sulu\Component\DocumentManager\Event\ConfigureOptionsEvent;
use Sulu\Component\DocumentManager\Event\CopyEvent;
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Events;
Expand All @@ -27,6 +32,8 @@
*/
class RoutableSubscriber implements EventSubscriberInterface
{
const ROUTE_FIELD = 'routePath';

/**
* @var RouteManagerInterface
*/
Expand All @@ -42,16 +49,43 @@ class RoutableSubscriber implements EventSubscriberInterface
*/
private $entityManager;

/**
* @var DocumentManagerInterface
*/
private $documentManager;

/**
* @var DocumentInspector
*/
private $documentInspector;

/**
* @var PropertyEncoder
*/
private $propertyEncoder;

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

/**
Expand All @@ -63,6 +97,7 @@ public static function getSubscribedEvents()
Events::HYDRATE => [['handleHydrate', -500]],
Events::PERSIST => [['handleRouteUpdate', 1], ['handleRoute', 0]],
Events::REMOVE => [['handleRemove', -500]],
Events::COPY => ['handleCopy', -2000],
Events::METADATA_LOAD => 'handleMetadataLoad',
Events::CONFIGURE_OPTIONS => 'configureOptions',
];
Expand Down Expand Up @@ -149,6 +184,37 @@ public function handleRemove(RemoveEvent $event)
$this->entityManager->flush();
}

/**
* Update routes for copied article.
*
* @param CopyEvent $event
*/
public function handleCopy(CopyEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof RoutableBehavior) {
return;
}

$locales = $this->documentInspector->getLocales($document);
foreach ($locales as $locale) {
/** @var ArticleDocument $localizedDocument */
$localizedDocument = $this->documentManager->find($event->getCopiedPath(), $locale);

$localizedDocument->removeRoute();
$route = $this->routeManager->create($localizedDocument);
$document->setRoutePath($route->getPath());
$this->entityManager->persist($route);

$event->getCopiedNode()->setProperty(
$this->propertyEncoder->localizedSystemName(self::ROUTE_FIELD, $locale),
$route->getPath()
);
}

$this->entityManager->flush();
}

/**
* Add route to metadata.
*
Expand All @@ -162,10 +228,10 @@ public function handleMetadataLoad(MetadataLoadEvent $event)

$metadata = $event->getMetadata();
$metadata->addFieldMapping(
'routePath',
self::ROUTE_FIELD,
[
'encoding' => 'system_localized',
'property' => 'routePath',
'property' => self::ROUTE_FIELD,
]
);
}
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
<argument type="service" id="sulu_route.manager.route_manager"/>
<argument type="service" id="sulu.repository.route"/>
<argument type="service" id="doctrine.orm.entity_manager"/>
<argument type="service" id="sulu_document_manager.document_manager"/>
<argument type="service" id="sulu_document_manager.document_inspector"/>
<argument type="service" id="sulu_document_manager.property_encoder"/>

<tag name="sulu_document_manager.event_subscriber"/>
</service>
Expand Down
2 changes: 1 addition & 1 deletion Resources/public/dist/components/articles/edit/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Resources/public/dist/services/manager.js

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

17 changes: 17 additions & 0 deletions Resources/public/js/components/articles/edit/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ define([
unpublishConfirmTitle: 'sulu-content.unpublish-confirm-title',
deleteDraftConfirmTitle: 'sulu-content.delete-draft-confirm-title',
deleteDraftConfirmText: 'sulu-content.delete-draft-confirm-text',
copy: 'sulu_article.edit.copy',
openGhostOverlay: {
info: 'sulu_article.settings.open-ghost-overlay.info',
new: 'sulu_article.settings.open-ghost-overlay.new',
Expand Down Expand Up @@ -161,6 +162,16 @@ define([
}
};

if (SecurityChecker.hasPermission(this.data, 'edit')) {
editDropdown.copy = {
options: {
title: this.translations.copy,
callback: this.copy.bind(this)
}
};
}


if (!this.sandbox.util.isEmpty(editDropdown)) {
buttons.edit = {
options: {
Expand Down Expand Up @@ -534,6 +545,12 @@ define([
});
},

copy: function() {
ArticleManager.copy(this.data.id, this.options.locale).done(function(data) {
this.toEdit(this.options.locale, data.id);
}.bind(this));
},

saved: function(id, data, action) {
this.setData(data);

Expand Down
10 changes: 10 additions & 0 deletions Resources/public/js/services/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ define(['jquery', 'services/husky/util'], function($, Util) {
);
},

/**
* Copy given article.
*
* @param {String} id
* @param {String} locale
*/
copy: function(id, locale) {
return Util.save(templates.url({id: id, locale: locale, action: 'copy'}), 'POST');
},

/**
* Returns copy article from a given locale to a array of other locales url.
*
Expand Down
4 changes: 4 additions & 0 deletions Resources/translations/sulu/backend.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
<source>sulu_article.contact-selection-overlay.title</source>
<target>Artikel nach Kontakt filtern</target>
</trans-unit>
<trans-unit id="copy-01">
<source>sulu_article.edit.copy</source>
<target>Artikel kopieren</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 4 additions & 0 deletions Resources/translations/sulu/backend.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
<source>sulu_article.contact-selection-overlay.title</source>
<target>Filter article by contact</target>
</trans-unit>
<trans-unit id="copy-01">
<source>sulu_article.edit.copy</source>
<target>Copy Article</target>
</trans-unit>
</body>
</file>
</xliff>
Loading

0 comments on commit 5a7d060

Please sign in to comment.