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

Implemented copy locale feature #73

Merged
merged 5 commits into from
Feb 15, 2017
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
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