Skip to content

Commit

Permalink
EZP-30344: Implemented Translation Limitation - language limitation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikadamczyk committed Apr 4, 2019
1 parent 9a56766 commit 79483d9
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 67 deletions.
64 changes: 62 additions & 2 deletions src/bundle/Controller/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
use eZ\Publish\API\Repository\Exceptions as ApiException;
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\UserService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\User\Limitation;
use eZ\Publish\Core\Base\Exceptions\BadStateException;
use eZ\Publish\SPI\Limitation\Target\Version;
use EzSystems\EzPlatformAdminUi\Exception\InvalidArgumentException as AdminInvalidArgumentException;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\ContentVisibilityUpdateData;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\Draft\ContentCreateData;
Expand All @@ -27,9 +30,11 @@
use EzSystems\EzPlatformAdminUi\Form\Type\Content\ContentVisibilityUpdateType;
use EzSystems\EzPlatformAdminUi\Form\Type\Content\Translation\MainTranslationUpdateType;
use EzSystems\EzPlatformAdminUi\Notification\NotificationHandlerInterface;
use EzSystems\EzPlatformAdminUi\Permission\LookupLimitationsTransformer;
use EzSystems\EzPlatformAdminUi\Siteaccess\SiteaccessResolverInterface;
use EzSystems\EzPlatformAdminUi\Specification\ContentIsUser;
use EzSystems\EzPlatformAdminUi\Specification\ContentType\ContentTypeIsUser;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -68,6 +73,12 @@ class ContentController extends Controller
/** @var \eZ\Publish\API\Repository\UserService */
private $userService;

/** @var \eZ\Publish\API\Repository\PermissionResolver */
private $permissionResolver;

/** @var \EzSystems\EzPlatformAdminUi\Permission\LookupLimitationsTransformer */
private $lookupLimitationsTransformer;

/** @var array */
private $userContentTypeIdentifier;

Expand All @@ -81,6 +92,8 @@ class ContentController extends Controller
* @param \EzSystems\EzPlatformAdminUi\Siteaccess\SiteaccessResolverInterface $siteaccessResolver
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \eZ\Publish\API\Repository\UserService $userService
* @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
* @param \EzSystems\EzPlatformAdminUi\Permission\LookupLimitationsTransformer $lookupLimitationsTransformer
* @param array $userContentTypeIdentifier
*/
public function __construct(
Expand All @@ -93,6 +106,8 @@ public function __construct(
SiteaccessResolverInterface $siteaccessResolver,
LocationService $locationService,
UserService $userService,
PermissionResolver $permissionResolver,
LookupLimitationsTransformer $lookupLimitationsTransformer,
array $userContentTypeIdentifier
) {
$this->notificationHandler = $notificationHandler;
Expand All @@ -105,6 +120,8 @@ public function __construct(
$this->locationService = $locationService;
$this->userService = $userService;
$this->userContentTypeIdentifier = $userContentTypeIdentifier;
$this->permissionResolver = $permissionResolver;
$this->lookupLimitationsTransformer = $lookupLimitationsTransformer;
}

/**
Expand Down Expand Up @@ -338,7 +355,7 @@ public function previewAction(
}

/**
* @param Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
Expand Down Expand Up @@ -386,7 +403,7 @@ public function updateMainTranslationAction(Request $request): Response
}

/**
* @param Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
Expand Down Expand Up @@ -456,4 +473,47 @@ public function updateVisibilityAction(Request $request): Response

return $result instanceof Response ? $result : $this->redirectToRoute('ezplatform.dashboard');
}

/**
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
* @param string|null $languageCode
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function checkEditPermissionAction(Content $content, ?string $languageCode): JsonResponse
{
$targets = [];

if (null !== $languageCode) {
$targets[] = (new Version())->translateToAnyLanguageOf([$languageCode]);
}

$canEdit = $this->permissionResolver->canUser(
'content',
'edit',
$content,
$targets
);

$lookupLimitations = $this->permissionResolver->lookupLimitations(
'content',
'edit',
$content,
$targets,
[Limitation::LANGUAGE]
);

$editLanguagesLimitationValues = $this->lookupLimitationsTransformer->getFlattenedLimitationsValues($lookupLimitations);

$response = new JsonResponse();
$response->setData([
'canEdit' => $canEdit,
'editLanguagesLimitationValues' => $canEdit ? $editLanguagesLimitationValues : [],
]);

return $response;
}
}
1 change: 1 addition & 0 deletions src/bundle/Resources/config/bazinga_js_translation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ active_domains:
- 'fieldtypes_edit'
- 'notifications'
- 'search'
- 'content'
8 changes: 8 additions & 0 deletions src/bundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,14 @@ ezplatform.content.translate:
_controller: 'EzPlatformAdminUiBundle:ContentEdit:translate'
fromLanguageCode: ~

ezplatform.content.check_edit_permission:
path: /content/{contentId}/check-edit-permission/{languageCode}
options:
expose: true
defaults:
_controller: 'EzPlatformAdminUiBundle:Content:checkEditPermission'
languageCode: ~

#
# Search
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
(function (global, doc, $) {
(function (global, doc, $, eZ) {
const editVersion = (event) => {
const showErrorNotification = eZ.helpers.notification.showErrorNotification;
const contentDraftEditUrl = event.currentTarget.dataset.contentDraftEditUrl;
const versionHasConflictUrl = event.currentTarget.dataset.versionHasConflictUrl;
const contentId = event.currentTarget.dataset.contentId;
const languageCode = event.currentTarget.dataset.languageCode;
const checkEditPermissionLink = global.Routing.generate('ezplatform.content.check_edit_permission', { contentId, languageCode });
const errorMessage = Translator.trans(
/*@Desc("You cannot edit Content with ID: %id%")*/ 'content.edit.permission.error',
{ id: contentId },
'content'
);
const handleCanEditCheck = (response) => {
if (response.canEdit) {
return fetch(versionHasConflictUrl, { mode: 'same-origin', credentials: 'same-origin' });
}

event.preventDefault();

fetch(versionHasConflictUrl, {
credentials: 'same-origin'
}).then(function (response) {
throw new Error(errorMessage);
};
const handleVersionDraftConflict = (response) => {
// Status 409 means that a draft conflict has occurred and the modal must be displayed.
// Otherwise we can go to Content Item edit page.
if (response.status === 409) {
doc.querySelector('#edit-conflicted-draft').href = contentDraftEditUrl;
$('#version-conflict-modal').modal('show');
}
if (response.status === 403) {
response.text().then(showErrorNotification);
}
if (response.status === 200) {
global.location.href = contentDraftEditUrl;
}
})
};
};

event.preventDefault();

[...doc.querySelectorAll('.ez-btn--content-draft-edit')].forEach(link => link.addEventListener('click', editVersion, false));
})(window, document, window.jQuery);
fetch(checkEditPermissionLink, { mode: 'same-origin', credentials: 'same-origin' })
.then(eZ.helpers.request.getJsonFromResponse)
.then(handleCanEditCheck)
.then(handleVersionDraftConflict)
.catch(showErrorNotification);
};
doc.querySelectorAll('.ez-btn--content-draft-edit').forEach((button) => button.addEventListener('click', editVersion, false));
})(window, document, window.jQuery, window.eZ);
38 changes: 26 additions & 12 deletions src/bundle/Resources/public/js/scripts/button.content.edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
const contentId = event.currentTarget.dataset.contentId;
const versionNo = event.currentTarget.dataset.versionNo;
const languageCode = event.currentTarget.dataset.languageCode;
const contentInfoInput = versionEditForm.querySelector('input[name="' + versionEditFormName + '[content_info]"]');
const contentInfoInput = versionEditForm.querySelector(`input[name="${versionEditFormName}[content_info]"]`);
const versionInfoContentInfoInput = versionEditForm.querySelector(
'input[name="' + versionEditFormName + '[version_info][content_info]"]'
`input[name="${versionEditFormName}[version_info][content_info]"]`
);
const versionInfoVersionNoInput = versionEditForm.querySelector(
'input[name="' + versionEditFormName + '[version_info][version_no]"]'
);
const languageInput = versionEditForm.querySelector('#' + versionEditFormName + '_language_' + languageCode);
const versionInfoVersionNoInput = versionEditForm.querySelector(`input[name="${versionEditFormName}[version_info][version_no]"]`);
const languageInput = versionEditForm.querySelector(`#${versionEditFormName}_language_${languageCode}`);
const checkVersionDraftLink = global.Routing.generate('ezplatform.version_draft.has_no_conflict', { contentId });
const checkEditPermissionLink = global.Routing.generate('ezplatform.content.check_edit_permission', { contentId, languageCode });
const errorMessage = Translator.trans(
/*@Desc("You cannot edit Content with ID: %id%")*/ 'content.edit.permission.error',
{ id: contentId },
'content'
);
const submitVersionEditForm = () => {
contentInfoInput.value = contentId;
versionInfoContentInfoInput.value = contentId;
Expand All @@ -40,12 +44,14 @@
);
$('#version-draft-conflict-modal').modal('show');
};
const handleCanEditCheck = (response) => {
if (response.canEdit) {
return fetch(checkVersionDraftLink, { mode: 'same-origin', credentials: 'same-origin' });
}

event.preventDefault();

fetch(checkVersionDraftLink, {
credentials: 'same-origin',
}).then(function(response) {
throw new Error(errorMessage);
};
const handleDraftConflict = (response) => {
// Status 409 means that a draft conflict has occurred and the modal must be displayed.
// Otherwise we can go to Content Item edit page.
if (response.status === 409) {
Expand All @@ -55,7 +61,15 @@
} else if (response.status === 200) {
submitVersionEditForm();
}
});
};

event.preventDefault();

fetch(checkEditPermissionLink, { mode: 'same-origin', credentials: 'same-origin' })
.then(eZ.helpers.request.getJsonFromResponse)
.then(handleCanEditCheck)
.then(handleDraftConflict)
.catch(showErrorNotification);
};

[...doc.querySelectorAll('.ez-btn--content-edit')].forEach((button) => button.addEventListener('click', editVersion, false));
Expand Down
5 changes: 5 additions & 0 deletions src/bundle/Resources/translations/content.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<target state="new">Cannot check if the draft has no conflict with other drafts. %error%. </target>
<note>key: content.draft.conflict.error</note>
</trans-unit>
<trans-unit id="7b7f768e446eae09c01c831807e246e293f65349" resname="content.edit.permission.error">
<source>You cannot edit Content with ID: %id%</source>
<target state="new">You cannot edit Content with ID: %id%</target>
<note>key: content.edit.permission.error</note>
</trans-unit>
<trans-unit id="fbc24757e92f52997d730762e004765425845995" resname="content.hide.already_hidden">
<source>Content '%name%' was already hidden.</source>
<target state="new">Content '%name%' has been hidden.</target>
Expand Down
2 changes: 2 additions & 0 deletions src/bundle/Resources/views/admin/content_draft/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
<td class="ez-table__cell ez-table__cell--has-action-btns text-right">
<button class="btn btn-icon mx-2 ez-btn--content-draft-edit"
title="{{ 'drafts.list.action.edit'|trans|desc('Edit Draft') }}"
data-content-id="{{ row.contentId }}"
data-language-code="{{ row.language }}"
data-content-draft-edit-url="{{ path(content_draft_edit_url, {
'contentId': row.contentId,
'versionNo': row.version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@
<td class="ez-table__cell ez-table__cell--has-action-btns text-right">
<button
data-content-draft-edit-url="{{ edit_url }}"
data-content-id="{{ version.contentInfo.id }}"
data-version-has-conflict-url="{{ path('ezplatform.version.has_no_conflict', {
'contentId': version.contentInfo.id,
'versionNo': version.versionNo,
'languageCode': version.translations[0].languageCode
}) }}"
data-language-code="{{ version.initialLanguageCode }}"
class="btn btn-icon mx-2 ez-btn--content-draft-edit"
title="{{ 'tab.versions.table.action.draft.edit'|trans|desc('Edit Draft') }}">
<svg class="ez-icon ez-icon-edit">
Expand Down
11 changes: 11 additions & 0 deletions src/bundle/Resources/views/dashboard/macros.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% macro edit_content_button(content, title) %}
<button class="btn btn-icon mx-2 ez-btn--content-edit"
title="{{ title }}"
data-content-id="{{ content.contentId }}"
data-version-no="{{ content.version }}"
data-language-code="{{ content.language }}">
<svg class="ez-icon ez-icon-edit">
<use xlink:href="{{ asset('bundles/ezplatformadminui/img/ez-icons.svg') }}#edit"></use>
</svg>
</button>
{% endmacro %}
12 changes: 3 additions & 9 deletions src/bundle/Resources/views/dashboard/tab/all_content.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% trans_default_domain 'dashboard' %}

{% import "@ezdesign/dashboard/macros.html.twig" as macros %}

{% if data|length %}
<table class="ez-table table">
<thead>
Expand Down Expand Up @@ -31,15 +33,7 @@
</td>
<td class="ez-table__cell">{{ row.modified|ez_full_datetime }}</td>
<td class="ez-table__cell ez-table__cell--has-action-btns text-right">
<button class="btn btn-icon mx-2 ez-btn--content-edit"
title="{{ 'dashboard.table.all.content.edit'|trans|desc('Edit Content') }}"
data-content-id="{{ row.contentId }}"
data-version-no="{{ row.version }}"
data-language-code="{{ row.language }}">
<svg class="ez-icon ez-icon-edit">
<use xlink:href="{{ asset('bundles/ezplatformadminui/img/ez-icons.svg') }}#edit"></use>
</svg>
</button>
{{ macros.edit_content_button(row, 'dashboard.table.all.content.edit'|trans|desc('Edit Content')) }}
</td>
</tr>
{% endfor %}
Expand Down
12 changes: 3 additions & 9 deletions src/bundle/Resources/views/dashboard/tab/all_media.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% trans_default_domain 'dashboard' %}

{% import "@ezdesign/dashboard/macros.html.twig" as macros %}

{% if data|length %}
<table class="ez-table table">
<thead>
Expand Down Expand Up @@ -31,15 +33,7 @@
</td>
<td class="ez-table__cell">{{ row.modified|ez_full_datetime }}</td>
<td class="ez-table__cell ez-table__cell--has-action-btns text-right">
<button class="btn btn-icon mx-2 ez-btn--content-edit"
title="{{ 'dashboard.table.all.media.edit'|trans|desc('Edit Media') }}"
data-content-id="{{ row.contentId }}"
data-version-no="{{ row.version }}"
data-language-code="{{ row.language }}">
<svg class="ez-icon ez-icon-edit">
<use xlink:href="{{ asset('bundles/ezplatformadminui/img/ez-icons.svg') }}#edit"></use>
</svg>
</button>
{{ macros.edit_content_button(row, 'dashboard.table.all.media.edit'|trans|desc('Edit Media')) }}
</td>
</tr>
{% endfor %}
Expand Down
12 changes: 3 additions & 9 deletions src/bundle/Resources/views/dashboard/tab/my_content.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% trans_default_domain 'dashboard' %}

{% import "@ezdesign/dashboard/macros.html.twig" as macros %}

{% if data|length %}
<table class="ez-table table">
<thead>
Expand All @@ -23,15 +25,7 @@
<td class="ez-table__cell">{{ row.type }}</td>
<td class="ez-table__cell">{{ row.modified|ez_full_datetime }}</td>
<td class="ez-table__cell ez-table__cell--has-action-btns text-right">
<button class="btn btn-icon mx-2 ez-btn--content-edit"
title="{{ 'dashboard.table.content.edit'|trans|desc('Edit Content') }}"
data-content-id="{{ row.contentId }}"
data-version-no="{{ row.version }}"
data-language-code="{{ row.language }}">
<svg class="ez-icon ez-icon-edit">
<use xlink:href="{{ asset('bundles/ezplatformadminui/img/ez-icons.svg') }}#edit"></use>
</svg>
</button>
{{ macros.edit_content_button(row, 'dashboard.table.content.edit'|trans|desc('Edit Content')) }}
</td>
</tr>
{% endfor %}
Expand Down
Loading

0 comments on commit 79483d9

Please sign in to comment.