Skip to content

Commit

Permalink
EZP-30344: Implemented Translation Limitation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikadamczyk committed Mar 28, 2019
1 parent 4f40acd commit c537959
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 36 deletions.
31 changes: 29 additions & 2 deletions src/bundle/Controller/ContentViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\LanguageService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
Expand All @@ -33,7 +34,9 @@
use EzSystems\EzPlatformAdminUi\Form\Data\User\UserDeleteData;
use EzSystems\EzPlatformAdminUi\Form\Data\User\UserEditData;
use EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory;
use EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Loader\ContentEditTranslationChoiceLoader;
use EzSystems\EzPlatformAdminUi\Form\Type\Content\ContentVisibilityUpdateType;
use EzSystems\EzPlatformAdminUi\Permission\LookupLimitationsTransformer;
use EzSystems\EzPlatformAdminUi\Specification\Content\ContentHaveAssetRelation;
use EzSystems\EzPlatformAdminUi\Specification\Content\ContentHaveUniqueRelation;
use EzSystems\EzPlatformAdminUi\Specification\ContentIsUser;
Expand Down Expand Up @@ -83,6 +86,12 @@ class ContentViewController extends Controller
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

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

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

/**
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
* @param \eZ\Publish\API\Repository\LanguageService $languageService
Expand All @@ -96,6 +105,8 @@ class ContentViewController extends Controller
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
* @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
* @param \EzSystems\EzPlatformAdminUi\Permission\LookupLimitationsTransformer $lookupLimitationsTransformer
*/
public function __construct(
ContentTypeService $contentTypeService,
Expand All @@ -109,7 +120,9 @@ public function __construct(
ContentService $contentService,
LocationService $locationService,
UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider,
ConfigResolverInterface $configResolver
ConfigResolverInterface $configResolver,
PermissionResolver $permissionResolver,
LookupLimitationsTransformer $lookupLimitationsTransformer
) {
$this->contentTypeService = $contentTypeService;
$this->languageService = $languageService;
Expand All @@ -123,6 +136,8 @@ public function __construct(
$this->locationService = $locationService;
$this->userLanguagePreferenceProvider = $userLanguagePreferenceProvider;
$this->configResolver = $configResolver;
$this->permissionResolver = $permissionResolver;
$this->lookupLimitationsTransformer = $lookupLimitationsTransformer;
}

/**
Expand Down Expand Up @@ -333,8 +348,20 @@ private function createContentEditForm(
?Language $language = null,
?Location $location = null
): FormInterface {
$languageCodes = $versionInfo->languageCodes ?? [];

return $this->formFactory->contentEdit(
new ContentEditData($contentInfo, $versionInfo, $language, $location)
new ContentEditData($contentInfo, $versionInfo, $language, $location),
null,
[
'choice_loader' => new ContentEditTranslationChoiceLoader(
$this->languageService,
$this->permissionResolver,
$contentInfo,
$this->lookupLimitationsTransformer,
$languageCodes
),
]
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services/permissions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ services:

EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface:
alias: EzSystems\EzPlatformAdminUi\Permission\PermissionChecker

EzSystems\EzPlatformAdminUi\Permission\LookupLimitationsTransformer: ~
11 changes: 7 additions & 4 deletions src/lib/Form/Factory/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,23 @@ public function __construct(
/**
* @param ContentEditData|null $data
* @param string|null $name
* @param array $options
*
* @return FormInterface
*
* @throws InvalidOptionsException
*/
public function contentEdit(
?ContentEditData $data = null,
?string $name = null
?string $name = null,
array $options = []
): FormInterface {
$name = $name ?: StringUtil::fqcnToBlockPrefix(ContentEditType::class);
$data = $data ?? new ContentEditData();
$options = null !== $data->getVersionInfo()
? ['language_codes' => $data->getVersionInfo()->languageCodes]
: [];

if (empty($options['language_codes']) && null !== $data->getVersionInfo()) {
$options['language_codes'] = $data->getVersionInfo()->languageCodes;
}

return $this->formFactory->createNamed(
$name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Loader;

use eZ\Publish\API\Repository\LanguageService;
use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\API\Repository\Values\User\Limitation;
use eZ\Publish\SPI\Limitation\Target\Version;
use EzSystems\EzPlatformAdminUi\Permission\LookupLimitationsTransformer;

class ContentEditTranslationChoiceLoader extends BaseChoiceLoader
{
/** @var \eZ\Publish\API\Repository\LanguageService */
protected $languageService;

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

/** @var string[] */
protected $languageCodes;

/** @var \eZ\Publish\API\Repository\Values\Content\ContentInfo */
private $contentInfo;

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

/**
* @param \eZ\Publish\API\Repository\LanguageService $languageService
* @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
* @param \EzSystems\EzPlatformAdminUi\Permission\LookupLimitationsTransformer $lookupLimitationsTransformer
* @param string[] $languageCodes
*/
public function __construct(
LanguageService $languageService,
PermissionResolver $permissionResolver,
?ContentInfo $contentInfo,
LookupLimitationsTransformer $lookupLimitationsTransformer,
$languageCodes
) {
$this->languageService = $languageService;
$this->permissionResolver = $permissionResolver;
$this->contentInfo = $contentInfo;
$this->languageCodes = $languageCodes;
$this->lookupLimitationsTransformer = $lookupLimitationsTransformer;
}

/**
* {@inheritdoc}
*/
public function getChoiceList(): array
{
$languages = $this->languageService->loadLanguages();
$limitationLanguageCodes = [];

if (!empty($this->languageCodes)) {
$languages = array_filter(
$languages,
function (Language $language) {
return \in_array($language->languageCode, $this->languageCodes, true);
}
);
}
$languagesCodes = array_column($languages, 'languageCode');
if (null !== $this->contentInfo) {
$lookupLimitations = $this->permissionResolver->lookupLimitations(
'content',
'edit',
$this->contentInfo,
[(new Version())->translateToAnyLanguageOf($languagesCodes)],
[Limitation\TranslationLimitation::IDENTIFIER]
);

$limitationLanguageCodes = $this->lookupLimitationsTransformer->getFlattenedLimitationsValues($lookupLimitations);
}

if (!empty($limitationLanguageCodes)) {
$languages = array_filter(
$languages,
function (Language $language) use ($limitationLanguageCodes) {
return \in_array($language->languageCode, $limitationLanguageCodes, true);
}
);
}

return $languages;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Loader;

use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation;
use eZ\Publish\API\Repository\Values\User\Limitation\TranslationLimitation;
use EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface;
use eZ\Publish\API\Repository\Values\Content\Language;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
Expand Down Expand Up @@ -61,7 +61,7 @@ public function loadChoiceList($value = null)
$restrictedLanguagesCodes = [];
$hasAccess = $this->permissionResolver->hasAccess($this->module, $this->function);
if (!is_bool($hasAccess)) {
$restrictedLanguagesCodes = $this->permissionChecker->getRestrictions($hasAccess, LanguageLimitation::class);
$restrictedLanguagesCodes = $this->permissionChecker->getRestrictions($hasAccess, TranslationLimitation::class);
}

$languages = $this->decorated->getChoiceList();
Expand Down
20 changes: 15 additions & 5 deletions src/lib/Form/Type/Content/Draft/ContentEditType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\API\Repository\LanguageService;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\Draft\ContentEditData;
use EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Loader\LanguageChoiceLoader;
use EzSystems\EzPlatformAdminUi\Form\Type\Content\ContentInfoType;
use EzSystems\EzPlatformAdminUi\Form\Type\Content\LocationType;
use EzSystems\EzPlatformAdminUi\Form\Type\Content\VersionInfoType;
Expand All @@ -22,15 +23,22 @@

class ContentEditType extends AbstractType
{
/** @var LanguageService */
/** @var \eZ\Publish\API\Repository\LanguageService */
protected $languageService;

/** @var \EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Loader\LanguageChoiceLoader */
private $languageChoiceLoader;

/**
* @param LanguageService $languageService
* @param \eZ\Publish\API\Repository\LanguageService $languageService
* @param \EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Loader\LanguageChoiceLoader $languageChoiceLoader
*/
public function __construct(LanguageService $languageService)
{
public function __construct(
LanguageService $languageService,
LanguageChoiceLoader $languageChoiceLoader
) {
$this->languageService = $languageService;
$this->languageChoiceLoader = $languageChoiceLoader;
}

public function buildForm(FormBuilderInterface $builder, array $options)
Expand Down Expand Up @@ -74,6 +82,7 @@ public function configureOptions(OptionsResolver $resolver)
'data_class' => ContentEditData::class,
'translation_domain' => 'forms',
'language_codes' => false,
'choice_loader' => $this->languageChoiceLoader,
])
->setAllowedTypes('language_codes', ['bool', 'array']);
}
Expand All @@ -89,9 +98,10 @@ private function getLanguageOptions(array $options): array
'label' => false,
'multiple' => false,
'expanded' => true,
'choice_loader' => $options['choice_loader'],
];

if (is_array($options['language_codes'])) {
if ($options['choice_loader'] instanceof LanguageChoiceLoader && is_array($options['language_codes'])) {
$languageOptions['choice_loader'] = new CallbackChoiceLoader(function () use ($options) {
return array_map([$this->languageService, 'loadLanguage'], $options['language_codes']);
});
Expand Down
Loading

0 comments on commit c537959

Please sign in to comment.