Skip to content

Commit

Permalink
Implemented copy locale feature (#73)
Browse files Browse the repository at this point in the history
* Implemented copy locale feature in list and edit form

* Implemented test

* Code clean up

* Code clean up

* Fixed delete button
  • Loading branch information
trickreich authored and wachterjohannes committed Feb 15, 2017
1 parent bec7610 commit 1114755
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 13 deletions.
14 changes: 14 additions & 0 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Sulu\Bundle\ArticleBundle\Document\Form\ArticleDocumentType;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
use Sulu\Component\Content\Form\Exception\InvalidFormException;
use Sulu\Component\Content\Mapper\ContentMapperInterface;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\Rest\Exception\MissingParameterException;
use Sulu\Component\Rest\Exception\RestException;
Expand Down Expand Up @@ -321,6 +322,7 @@ public function postTriggerAction($uuid, Request $request)
// prepare vars
$view = null;
$data = null;
$userId = $this->getUser()->getId();

try {
switch ($action) {
Expand All @@ -336,6 +338,10 @@ public function postTriggerAction($uuid, Request $request)
$this->getDocumentManager()->removeDraft($data, $locale);
$this->getDocumentManager()->flush();
break;
case 'copy-locale':
$destLocales = $this->getRequestParameter($request, 'dest', true);
$data = $this->getMapper()->copyLanguage($uuid, $userId, null, $locale, explode(',', $destLocales));
break;
default:
throw new RestException('Unrecognized action: ' . $action);
}
Expand Down Expand Up @@ -413,6 +419,14 @@ protected function getDocumentManager()
return $this->get('sulu_document_manager.document_manager');
}

/**
* @return ContentMapperInterface
*/
protected function getMapper()
{
return $this->get('sulu.content.mapper');
}

/**
* Delegates actions by given actionParameter, which can be retrieved from the request.
*
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/components/articles/list/main.js

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

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.

95 changes: 89 additions & 6 deletions Resources/public/js/components/articles/edit/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ define([
'services/suluarticle/article-manager',
'sulusecurity/services/user-manager',
'services/sulupreview/preview',
'sulusecurity/services/security-checker'
], function($, _, config, ArticleManager, UserManager, Preview, SecurityChecker) {
'sulusecurity/services/security-checker',
'sulucontent/components/copy-locale-overlay/main',
'sulucontent/components/open-ghost-overlay/main'
], function($, _, config, ArticleManager, UserManager, Preview, SecurityChecker, CopyLocale, OpenGhost) {

'use strict';

Expand Down Expand Up @@ -127,6 +129,26 @@ define([
};
}

editDropdown.copyLocale = {
options: {
title: this.sandbox.translate('toolbar.copy-locale'),
callback: function() {
CopyLocale.startCopyLocalesOverlay.call(this).then(function(newLocales) {
// reload form when the current locale is in newLocales
if (_.contains(newLocales, this.options.locale)) {
this.toEdit(this.options.locale);

return;
}

// save new created locales to data and show success label
this.data.concreteLanguages = _.uniq(this.data.concreteLanguages.concat(newLocales));
this.sandbox.emit('sulu.labels.success.show', 'labels.success.copy-locale-desc', 'labels.success');
}.bind(this));
}.bind(this)
}
};

if (!this.sandbox.util.isEmpty(editDropdown)) {
buttons.edit = {
options: {
Expand Down Expand Up @@ -170,6 +192,10 @@ define([
this.bindCustomEvents();
this.showDraftLabel();
this.setHeaderBar(true);
this.loadLocalizations();

// the open ghost overlay component needs the current locale in `this.options.language`
this.options.language = this.options.locale;
},

bindCustomEvents: function() {
Expand All @@ -179,11 +205,44 @@ define([
this.sandbox.on('sulu.tab.data-changed', this.setData.bind(this));
this.sandbox.on('sulu.article.error', this.handleError.bind(this));
this.sandbox.on('husky.tabs.header.item.select', this.tabChanged.bind(this));
this.sandbox.on('sulu.header.language-changed', this.languageChanged.bind(this));
},

this.sandbox.on('sulu.header.language-changed', function(item) {
this.sandbox.sulu.saveUserSetting(this.options.config.settingsKey, item.id);
/**
* Language changed event.
*
* @param {Object} item
*/
languageChanged: function(item) {
if (item.id === this.options.locale) {
return;
}

this.sandbox.sulu.saveUserSetting(this.options.config.settingsKey, item.id);

if (-1 === _(this.data.concreteLanguages).indexOf(item.id)) {
OpenGhost.openGhost.call(this, this.data).then(function(copy, src) {
if (!!copy) {
CopyLocale.copyLocale.call(
this,
this.data.id,
src,
[item.id],
function() {
this.toEdit(item.id);
}.bind(this)
);
} else {
// new article will be created
this.toEdit(item.id);
}
}.bind(this)).fail(function() {
// the open-ghost page got canceled, so reset the language changer
this.sandbox.emit('sulu.header.change-language', this.options.language);
}.bind(this));
} else {
this.toEdit(item.id);
}.bind(this));
}
},

/**
Expand Down Expand Up @@ -214,7 +273,7 @@ define([
deleteArticle: function() {
this.sandbox.sulu.showDeleteDialog(function(wasConfirmed) {
if (wasConfirmed) {
ArticleManager.delete(this.options.id).then(function() {
ArticleManager.remove(this.options.id, this.options.locale).then(function() {
this.toList();
}.bind(this));
}
Expand Down Expand Up @@ -477,6 +536,30 @@ define([
}

this.afterSaveAction(action, !this.options.id);
},

loadLocalizations: function() {
this.sandbox.util.load('/admin/api/localizations').then(function(data) {
this.localizations = data._embedded.localizations.map(function(localization) {
return {
id: localization.localization,
title: localization.localization
};
});
}.bind(this));
},

/**
* Returns copy article from a given locale to a array of other locales url.
*
* @param {string} id
* @param {string} src
* @param {string[]} dest
*
* @returns {string}
*/
getCopyLocaleUrl: function(id, src, dest) {
return ArticleManager.getCopyLocaleUrl(id, src, dest);
}
}
});
Loading

0 comments on commit 1114755

Please sign in to comment.