Skip to content

Commit 62bf5db

Browse files
authored
Cleanup (#32)
* Cleaned up configuration * Cleaning up bundle configuration * code syle * Add config to allow create/delete * cs * Make sure the web ui can be disabled * cs
1 parent fba80d2 commit 62bf5db

File tree

4 files changed

+115
-36
lines changed

4 files changed

+115
-36
lines changed

Controller/WebUIController.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class WebUIController extends Controller
3838
*/
3939
public function indexAction($configName = null)
4040
{
41+
if (!$this->getParameter('php_translation.webui.enabled')) {
42+
return new Response('You are not allowed here. Check you config. ', 400);
43+
}
4144
$config = $this->getConfiguration($configName);
4245
$localeMap = $this->getLocale2LanguageMap();
4346
$catalogues = $this->get('php_translation.catalogue_fetcher')->getCatalogues(array_keys($localeMap), [$config['output_dir']]);
@@ -88,6 +91,9 @@ public function indexAction($configName = null)
8891
*/
8992
public function showAction($configName, $locale, $domain)
9093
{
94+
if (!$this->getParameter('php_translation.webui.enabled')) {
95+
return new Response('You are not allowed here. Check you config. ', 400);
96+
}
9197
$config = $this->getConfiguration($configName);
9298
$locales = $this->getParameter('php_translation.locales');
9399

@@ -109,6 +115,8 @@ public function showAction($configName, $locale, $domain)
109115
'currentLocale' => $locale,
110116
'configName' => $configName,
111117
'configNames' => $this->get('php_translation.configuration_manager')->getNames(),
118+
'allow_create' => $this->getParameter('php_translation.webui.allow_create'),
119+
'allow_delete' => $this->getParameter('php_translation.webui.allow_delete'),
112120
]);
113121
}
114122

@@ -122,6 +130,10 @@ public function showAction($configName, $locale, $domain)
122130
*/
123131
public function createAction(Request $request, $configName, $locale, $domain)
124132
{
133+
if (!$this->getParameter('php_translation.webui.enabled') || !$this->getParameter('php_translation.webui.allow_create')) {
134+
return new Response('You are not allowed to create. Check you config. ', 400);
135+
}
136+
125137
/** @var StorageService $storage */
126138
$storage = $this->get('php_translation.storage.'.$configName);
127139
try {
@@ -156,6 +168,10 @@ public function createAction(Request $request, $configName, $locale, $domain)
156168
*/
157169
public function editAction(Request $request, $configName, $locale, $domain)
158170
{
171+
if (!$this->getParameter('php_translation.webui.enabled')) {
172+
return new Response('You are not allowed here. Check you config. ', 400);
173+
}
174+
159175
try {
160176
$message = $this->getMessage($request, ['Edit']);
161177
} catch (MessageValidationException $e) {
@@ -179,6 +195,10 @@ public function editAction(Request $request, $configName, $locale, $domain)
179195
*/
180196
public function deleteAction(Request $request, $configName, $locale, $domain)
181197
{
198+
if (!$this->getParameter('php_translation.webui.enabled') || !$this->getParameter('php_translation.webui.allow_delete')) {
199+
return new Response('You are not allowed to create. Check you config. ', 400);
200+
}
201+
182202
try {
183203
$message = $this->getMessage($request, ['Delete']);
184204
} catch (MessageValidationException $e) {

DependencyInjection/Configuration.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public function getConfigTreeBuilder()
3838

3939
$this->configsNode($root);
4040
$this->addAutoTranslateNode($root);
41-
$this->addTranslationServiceNode($root);
41+
$this->addEditInPlaceNode($root);
42+
$this->addWebUINode($root);
4243

4344
$root
4445
->children()
@@ -52,19 +53,6 @@ public function getConfigTreeBuilder()
5253
->booleanNode('allow_edit')->defaultTrue()->end()
5354
->end()
5455
->end()
55-
->arrayNode('webui')
56-
->canBeEnabled()
57-
->children()
58-
->booleanNode('allow_add')->defaultTrue()->end()
59-
->end()
60-
->end()
61-
->arrayNode('edit_in_place')
62-
->canBeEnabled()
63-
->children()
64-
->scalarNode('config_name')->defaultValue('default')->end()
65-
->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
66-
->end()
67-
->end()
6856
->arrayNode('auto_add_missing_translations')
6957
->canBeEnabled()
7058
->children()
@@ -173,15 +161,29 @@ private function addAutoTranslateNode(ArrayNodeDefinition $root)
173161
->end();
174162
}
175163

176-
private function addTranslationServiceNode(ArrayNodeDefinition $root)
164+
private function addEditInPlaceNode(ArrayNodeDefinition $root)
177165
{
178-
$root
179-
->children()
180-
->enumNode('storage')
181-
->info('Where translations are stored.')
182-
->values(['blackhole', 'filesystem', 'loco'])
183-
->defaultValue('filesystem')
166+
$root->children()
167+
->arrayNode('edit_in_place')
168+
->canBeEnabled()
169+
->children()
170+
->scalarNode('config_name')->defaultValue('default')->end()
171+
->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
184172
->end()
185-
->end();
173+
->end()
174+
->end();
175+
}
176+
177+
private function addWebUINode(ArrayNodeDefinition $root)
178+
{
179+
$root->children()
180+
->arrayNode('webui')
181+
->canBeEnabled()
182+
->children()
183+
->booleanNode('allow_create')->defaultTrue()->end()
184+
->booleanNode('allow_delete')->defaultTrue()->end()
185+
->end()
186+
->end()
187+
->end();
186188
}
187189
}

DependencyInjection/TranslationExtension.php

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ public function load(array $configs, ContainerBuilder $container)
4646

4747
$container->setParameter('php_translation.locales', $config['locales']);
4848
$container->setParameter('php_translation.default_locale', isset($config['default_locale']) ? $config['default_locale'] : $container->getParameter('kernel.default_locale'));
49+
$this->handleConfigNode($container, $config);
4950

5051
if ($config['webui']['enabled']) {
5152
$this->enableWebUi($container, $config);
53+
} else {
54+
$container->setParameter('php_translation.webui.enabled', false);
5255
}
5356

5457
if ($config['symfony_profiler']['enabled']) {
@@ -71,13 +74,24 @@ public function load(array $configs, ContainerBuilder $container)
7174
$loader->load('auto_translation.yml');
7275
$this->enableFallbackAutoTranslator($container, $config);
7376
}
77+
}
7478

79+
/**
80+
* Handle the config node to prepare the config manager.
81+
*
82+
* @param ContainerBuilder $container
83+
* @param array $config
84+
*/
85+
private function handleConfigNode(ContainerBuilder $container, array $config)
86+
{
87+
// $first will be the "default" configuration.
7588
$first = null;
7689
foreach ($config['configs'] as $name => &$c) {
7790
if ($first === null || $name === 'default') {
7891
$first = $name;
7992
}
8093
if (empty($c['project_root'])) {
94+
// Add a project root of none is set.
8195
$c['project_root'] = dirname($container->getParameter('kernel.root_dir'));
8296
}
8397

@@ -104,19 +118,40 @@ public function load(array $configs, ContainerBuilder $container)
104118

105119
if ($first !== null) {
106120
// Create some aliases for the default storage
107-
$container->setAlias('php_translation.storage', 'php_translation.storage.'.$first);
108-
$container->setAlias('php_translation.storage.default', 'php_translation.storage.'.$first);
121+
$container->setAlias(
122+
'php_translation.storage',
123+
'php_translation.storage.'.$first
124+
);
125+
$container->setAlias(
126+
'php_translation.storage.default',
127+
'php_translation.storage.'.$first
128+
);
109129
}
110130

111131
$container->getDefinition('php_translation.configuration_manager')
112132
->replaceArgument(0, $config['configs']);
113133
}
114134

115-
private function enableWebUi(ContainerBuilder $container, $config)
135+
/**
136+
* Handle config for WebUI.
137+
*
138+
* @param ContainerBuilder $container
139+
* @param array $config
140+
*/
141+
private function enableWebUi(ContainerBuilder $container, array $config)
116142
{
143+
$container->setParameter('php_translation.webui.enabled', true);
144+
$container->setParameter('php_translation.webui.allow_create', $config['webui']['allow_create']);
145+
$container->setParameter('php_translation.webui.allow_delete', $config['webui']['allow_delete']);
117146
}
118147

119-
private function enableEditInPlace(ContainerBuilder $container, $config)
148+
/**
149+
* Handle config for EditInPlace.
150+
*
151+
* @param ContainerBuilder $container
152+
* @param array $config
153+
*/
154+
private function enableEditInPlace(ContainerBuilder $container, array $config)
120155
{
121156
$name = $config['edit_in_place']['config_name'];
122157

@@ -134,12 +169,24 @@ private function enableEditInPlace(ContainerBuilder $container, $config)
134169
$def->replaceArgument(1, $activatorRef);
135170
}
136171

137-
private function enableSymfonyProfiler(ContainerBuilder $container, $config)
172+
/**
173+
* Handle config for Symfony Profiler.
174+
*
175+
* @param ContainerBuilder $container
176+
* @param array $config
177+
*/
178+
private function enableSymfonyProfiler(ContainerBuilder $container, array $config)
138179
{
139180
$container->setParameter('php_translation.toolbar.allow_edit', $config['symfony_profiler']['allow_edit']);
140181
}
141182

142-
private function enableFallbackAutoTranslator(ContainerBuilder $container, $config)
183+
/**
184+
* Handle config for fallback auto translate.
185+
*
186+
* @param ContainerBuilder $container
187+
* @param array $config
188+
*/
189+
private function enableFallbackAutoTranslator(ContainerBuilder $container, array $config)
143190
{
144191
$externalTranslatorId = 'php_translation.translator_service.'.$config['fallback_translation']['service'];
145192
$externalTranslatorDef = $container->getDefinition($externalTranslatorId);
@@ -150,6 +197,9 @@ private function enableFallbackAutoTranslator(ContainerBuilder $container, $conf
150197
$container->setParameter('php_translation.translator_service.api_key', $config['fallback_translation']['api_key']);
151198
}
152199

200+
/**
201+
* {@inheritdoc}
202+
*/
153203
public function getAlias()
154204
{
155205
return 'translation';

Resources/views/WebUI/show.html.twig

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@
3131

3232
<div class="container-fluid message-list">
3333

34-
<h1>Translations <a href="javascript:void(0);"
35-
class="btn btn-secondary"
36-
onclick='toggleElement("create-translation")'
37-
aria-controls="create-translation">Add new</a></h1>
34+
<h1>Translations
35+
{% if allow_create %}
36+
<a href="javascript:void(0);"
37+
class="btn btn-secondary"
38+
onclick='toggleElement("create-translation")'
39+
aria-controls="create-translation">Add new</a>
40+
{% endif %}
41+
</h1>
3842

43+
{% if allow_create %}
3944
<div class="collapse" id="create-translation">
4045
<form class="form" onsubmit="return createTranslation(this, '{{ path('translation_create', {configName: configName, locale:currentLocale,domain:currentDomain}) }}')">
4146
<div class="form-group">
@@ -50,15 +55,16 @@
5055
<div class="ajax-result"></div>
5156
</form>
5257
</div>
58+
{% endif %}
5359

5460
<div id="new-translations"></div>
5561
{% for idx, message in messages if message.new %}
56-
{{ macro.printMessage(idx + 1, message) }}
62+
{{ macro.printMessage(idx + 1, message, allow_delete) }}
5763
{% endfor %}
5864

5965
{% set idxStart = messages|length %}
6066
{% for idx, message in messages if not message.new %}
61-
{{ macro.printMessage(idx + idxStart, message) }}
67+
{{ macro.printMessage(idx + idxStart, message, allow_delete) }}
6268
{% endfor %}
6369
</div>
6470

@@ -67,7 +73,7 @@
6773
</script>
6874
{% endblock %}
6975

70-
{% macro printMessage(idx, message) %}
76+
{% macro printMessage(idx, message, allow_delete) %}
7177
<div class="message row" id="{{ message.key }}">
7278
<div class="col-md-6 col-xs-12">
7379
{% if message.new %}
@@ -77,8 +83,9 @@
7783
<span class="text-warning" title="Obsolete">&#x26A0;</span>
7884
{% endif %}
7985
<a class="message-key" href="#{{ message.key }}">{{ message.key }}</a>
86+
{% if allow_delete %}
8087
<a class="message-delete" href="javascript:void(0)" data-key="{{ message.key }}" title="Delete translation" onclick='confirm("Are you sure?")?deleteTranslation(this):false;'>&#x274C;</a>
81-
88+
{% endif %}
8289
<textarea
8390
class="message-textarea"
8491
data-key="{{ message.key }}"

0 commit comments

Comments
 (0)