Skip to content

Commit

Permalink
Merge pull request #701 from mikadamczyk/EZP-29748
Browse files Browse the repository at this point in the history
EZP-29748: Replacing the outdated verbiage of eZ Publish from eZ Platform when adding a Policy
  • Loading branch information
Łukasz Serwatka committed Nov 22, 2018
2 parents deb399f + f3f8c55 commit 9f2a5ad
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 53 deletions.
6 changes: 3 additions & 3 deletions features/Roles.feature
Expand Up @@ -126,7 +126,7 @@ Feature: Roles management
Given there's "Test Role edited" on "Roles" list
And I go to "Test Role edited" "Role" page
When I start creating new "Policy" in "Test Role edited"
And I select policy "Class / All functions"
And I select policy "Content Type / All functions"
And I click on the edit action bar button "Discard changes"
Then I should be on "Role" "Test Role edited" page
And "Policies" list in "Role" "Test Role edited" is empty
Expand All @@ -139,7 +139,7 @@ Feature: Roles management
When I start creating new "Policy" in "Test Role edited"
And I select policy "Content / Read"
And I click on the edit action bar button "Create"
And I select options from "Class"
And I select options from "Content Type"
| option |
| File |
And I click on the edit action bar button "Update"
Expand All @@ -163,7 +163,7 @@ Feature: Roles management
Given there's "Test Role edited" on "Roles" list
And I go to "Test Role edited" "Role" page
When I start editing "Policy" "Content" from "Test Role edited"
And I select options from "Class"
And I select options from "Content Type"
| option |
| Article |
| Folder |
Expand Down
6 changes: 6 additions & 0 deletions src/bundle/Resources/config/services/translation.yml
Expand Up @@ -7,3 +7,9 @@ services:
EzSystems\EzPlatformAdminUi\Translation\Extractor\JavaScriptFileVisitor:
tags:
- { name: jms_translation.file_visitor }

EzSystems\EzPlatformAdminUi\Translation\Extractor\PolicyTranslationExtractor:
arguments:
- '%ezpublish.api.role.policy_map%'
tags:
- { name: jms_translation.extractor, alias: ez_policy }
15 changes: 0 additions & 15 deletions src/bundle/Resources/translations/ezrepoforms_role.en.xliff
Expand Up @@ -21,21 +21,6 @@
<target state="new">Delete</target>
<note>key: policy_delete.delete</note>
</trans-unit>
<trans-unit id="deab5c91a76488732d0405e72c36702ed2aea3d6" resname="role.policy.all_functions">
<source>All functions</source>
<target state="new">All functions</target>
<note>key: role.policy.all_functions</note>
</trans-unit>
<trans-unit id="d29c07272fff793138cbb6f7dbb41b08a16ff815" resname="role.policy.all_modules">
<source>All modules</source>
<target state="new">All modules</target>
<note>key: role.policy.all_modules</note>
</trans-unit>
<trans-unit id="d4690e1d0c7d2cf0468ab5aa04caa891e1b5efbd" resname="role.policy.all_modules_all_functions">
<source>All modules / All functions</source>
<target state="new">All modules / All functions</target>
<note>key: role.policy.all_modules_all_functions</note>
</trans-unit>
<trans-unit id="0eda0d605bd42eb8989a82e546d09ace8bc1b4d9" resname="role.policy.limitation.location.udw_button">
<source>Select locations</source>
<target state="new">Select locations</target>
Expand Down
305 changes: 305 additions & 0 deletions src/bundle/Resources/translations/forms.en.xliff

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/bundle/Resources/views/admin/policy/list.html.twig
Expand Up @@ -60,7 +60,7 @@
{% do form_policies_delete.policies.setRendered %}
{% endif %}
</td>
<td class="ez-table__cell">{{ policy.module|capitalize }}</td>
<td class="ez-table__cell">{{ ('role.policy.' ~ policy.module)|trans({}, 'forms') }}</td>
<td class="ez-table__cell">{{ policy.function|capitalize }}</td>
<td class="ez-table__cell">
{%- if policy.limitations is not empty -%}
Expand Down
Expand Up @@ -26,7 +26,7 @@
{%- endif -%}
</td>
<td>
{{ policy.module|capitalize }}
{{ ('role.policy.' ~ policy.module)|trans({}, 'forms') }}
</td>
<td>
{{ policy.function|capitalize }}
Expand Down
45 changes: 12 additions & 33 deletions src/lib/Form/Type/Policy/PolicyChoiceType.php
Expand Up @@ -8,6 +8,7 @@

namespace EzSystems\EzPlatformAdminUi\Form\Type\Policy;

use EzSystems\EzPlatformAdminUi\Translation\Extractor\PolicyTranslationExtractor;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
Expand All @@ -28,7 +29,7 @@ class PolicyChoiceType extends AbstractType
*/
public function __construct(TranslatorInterface $translator, array $policyMap)
{
$this->policyChoices = $this->buildPolicyChoicesFromMap($translator, $policyMap);
$this->policyChoices = $this->buildPolicyChoicesFromMap($policyMap);
}

/**
Expand Down Expand Up @@ -83,56 +84,34 @@ public function getParent(): ?string

/**
* Returns a usable hash for the policy choice widget.
* Key is the humanized "module" name.
* Value is a hash with "<module>|<function"> as key and humanized "function" name as value.
* Key is the translation key based on "module" name.
* Value is a hash with translation key based on "module" and "function as a key and "<module>|<function"> as a value.
*
* @param TranslatorInterface $translator
* @param array $policyMap
*
* @return array
*/
private function buildPolicyChoicesFromMap(TranslatorInterface $translator, array $policyMap): array
private function buildPolicyChoicesFromMap(array $policyMap): array
{
$allModules = $translator->trans(/** @Desc("All modules") */'role.policy.all_modules', [], 'ezrepoforms_role');
$allFunctions = $translator->trans(/** @Desc("All functions") */'role.policy.all_functions', [], 'ezrepoforms_role');
$allModulesAndFunctions = $translator->trans(/** @Desc("All modules / All functions") */'role.policy.all_modules_all_functions', [], 'ezrepoforms_role');

$policyChoices = [
$allModules => [
$allModulesAndFunctions => '*|*',
PolicyTranslationExtractor::MESSAGE_ID_PREFIX . PolicyTranslationExtractor::ALL_MODULES => [
PolicyTranslationExtractor::MESSAGE_ID_PREFIX . PolicyTranslationExtractor::ALL_MODULES_ALL_FUNCTIONS => '*|*',
],
];

foreach ($policyMap as $module => $functionList) {
$humanizedModule = $this->humanize($module);
$moduleKey = PolicyTranslationExtractor::MESSAGE_ID_PREFIX . $module;
// For each module, add possibility to grant access to all functions.
$policyChoices[$humanizedModule] = [
"$humanizedModule / " . $allFunctions => "$module|*",
$policyChoices[$moduleKey] = [
$moduleKey . '.' . PolicyTranslationExtractor::ALL_FUNCTIONS => "$module|*",
];

foreach ($functionList as $function => $limitationList) {
$policyChoices[$humanizedModule][$humanizedModule . ' / ' . $this->humanize($function)] = "$module|$function";
$moduleFunctionKey = PolicyTranslationExtractor::MESSAGE_ID_PREFIX . "{$module}.{$function}";
$policyChoices[$moduleKey][$moduleFunctionKey] = "$module|$function";
}
}

return $policyChoices;
}

/**
* Makes a technical name human readable.
*
* Sequences of underscores are replaced by single spaces. The first letter
* of the resulting string is capitalized, while all other letters are
* turned to lowercase.
*
* @see \Symfony\Component\Form\FormRenderer::humanize()
*
* @param string $text the text to humanize
*
* @return string the humanized text
*/
private function humanize(string $text): string
{
return ucfirst(trim(strtolower(preg_replace(['/([A-Z])/', '/[_\s]+/'], ['_$1', ' '], $text))));
}
}
99 changes: 99 additions & 0 deletions src/lib/Translation/Extractor/PolicyTranslationExtractor.php
@@ -0,0 +1,99 @@
<?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\Translation\Extractor;

use JMS\TranslationBundle\Model\MessageCatalogue;
use JMS\TranslationBundle\Model\Message\XliffMessage;
use JMS\TranslationBundle\Translation\ExtractorInterface;

/**
* Generates translation strings for limitation types.
*/
class PolicyTranslationExtractor implements ExtractorInterface
{
const MESSAGE_DOMAIN = 'forms';
const MESSAGE_ID_PREFIX = 'role.policy.';
const ALL_MODULES = 'all_modules';
const ALL_FUNCTIONS = 'all_functions';
const ALL_MODULES_ALL_FUNCTIONS = 'all_modules_all_functions';

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

/**
* @param array $policyMap
*/
public function __construct(array $policyMap)
{
$this->policyMap = $policyMap;
}

/**
* @return \JMS\TranslationBundle\Model\MessageCatalogue
*/
public function extract(): MessageCatalogue
{
$catalogue = new MessageCatalogue();

$catalogue->add($this->createMessage(self::ALL_MODULES, 'All modules'));
$catalogue->add($this->createMessage(self::ALL_MODULES_ALL_FUNCTIONS, 'All modules / All functions'));

foreach ($this->policyMap as $module => $functionList) {
$catalogue->add($this->createMessage($module, $this->humanize($module)));
$catalogue->add($this->createMessage($module . '.' . self::ALL_FUNCTIONS, $this->humanize($module) . ' / All functions'));

foreach ($functionList as $function => $limitationList) {
$catalogue->add($this->createMessage($module . '.' . $function, $this->humanize($module) . ' / ' . $this->humanize($function)));
}
}

return $catalogue;
}

/**
* @param string $id
* @param string $desc
*
* @return \JMS\TranslationBundle\Model\Message\XliffMessage|null
*/
private function createMessage(string $id, string $desc): ?XliffMessage
{
$id = self::MESSAGE_ID_PREFIX . $id;

$message = new XliffMessage($id, self::MESSAGE_DOMAIN);
$message->setNew(false);
$message->setMeaning($desc);
$message->setDesc($desc);
$message->setLocaleString($desc);
$message->addNote('key: ' . $id);

return $message;
}

/**
* Makes a technical name human readable.
*
* Sequences of underscores are replaced by single spaces. The first letter
* of the resulting string is capitalized, while all other letters are
* turned to lowercase.
*
* @see \Symfony\Component\Form\FormRenderer::humanize()
*
* @param string $text the text to humanize
*
* @return string the humanized text
*/
private function humanize(string $text): string
{
$replace = ['Content Type'];
$search = ['class'];

return ucfirst(trim(str_replace($search, $replace, strtolower(preg_replace(['/([A-Z])/', '/[_\s]+/'], ['_$1', ' '], $text)))));
}
}

0 comments on commit 9f2a5ad

Please sign in to comment.