1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Bundle\Controller; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
18
|
|
|
use Symfony\Component\Intl\Intl; |
19
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
20
|
|
|
use Translation\Bundle\Exception\MessageValidationException; |
21
|
|
|
use Translation\Bundle\Model\WebUiMessage; |
22
|
|
|
use Translation\Bundle\Service\StorageService; |
23
|
|
|
use Translation\Common\Exception\StorageException; |
24
|
|
|
use Translation\Bundle\Model\CatalogueMessage; |
25
|
|
|
use Translation\Common\Model\Message; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Tobias Nyholm <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class WebUIController extends Controller |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Show a dashboard for the configuration. |
34
|
|
|
* |
35
|
|
|
* @param string|null $configName |
36
|
|
|
* |
37
|
|
|
* @return Response |
38
|
|
|
*/ |
39
|
|
|
public function indexAction($configName = null) |
40
|
|
|
{ |
41
|
|
|
if (!$this->getParameter('php_translation.webui.enabled')) { |
42
|
|
|
return new Response('You are not allowed here. Check you config. ', 400); |
43
|
|
|
} |
44
|
|
|
$config = $this->getConfiguration($configName); |
45
|
|
|
$localeMap = $this->getLocale2LanguageMap(); |
46
|
|
|
$catalogues = $this->get('php_translation.catalogue_fetcher')->getCatalogues(array_keys($localeMap), [$config['output_dir']]); |
47
|
|
|
|
48
|
|
|
$catalogueSize = []; |
49
|
|
|
$maxDomainSize = []; |
50
|
|
|
$maxCatalogueSize = 1; |
51
|
|
|
|
52
|
|
|
// For each catalogue (or locale) |
53
|
|
|
/** @var MessageCatalogue $catalogue */ |
54
|
|
|
foreach ($catalogues as $catalogue) { |
55
|
|
|
$locale = $catalogue->getLocale(); |
56
|
|
|
$domains = $catalogue->all(); |
57
|
|
|
ksort($domains); |
58
|
|
|
$catalogueSize[$locale] = 0; |
59
|
|
|
foreach ($domains as $domain => $messages) { |
60
|
|
|
$count = count($messages); |
61
|
|
|
$catalogueSize[$locale] += $count; |
62
|
|
|
if (!isset($maxDomainSize[$domain]) || $count > $maxDomainSize[$domain]) { |
63
|
|
|
$maxDomainSize[$domain] = $count; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($catalogueSize[$locale] > $maxCatalogueSize) { |
68
|
|
|
$maxCatalogueSize = $catalogueSize[$locale]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->render('TranslationBundle:WebUI:index.html.twig', [ |
73
|
|
|
'catalogues' => $catalogues, |
74
|
|
|
'catalogueSize' => $catalogueSize, |
75
|
|
|
'maxDomainSize' => $maxDomainSize, |
76
|
|
|
'maxCatalogueSize' => $maxCatalogueSize, |
77
|
|
|
'localeMap' => $localeMap, |
78
|
|
|
'configName' => $configName, |
79
|
|
|
'configNames' => $this->get('php_translation.configuration_manager')->getNames(), |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Show a catalogue. |
85
|
|
|
* |
86
|
|
|
* @param string $configName |
87
|
|
|
* @param string $locale |
88
|
|
|
* @param string $domain |
89
|
|
|
* |
90
|
|
|
* @return Response |
91
|
|
|
*/ |
92
|
|
|
public function showAction($configName, $locale, $domain) |
93
|
|
|
{ |
94
|
|
|
if (!$this->getParameter('php_translation.webui.enabled')) { |
95
|
|
|
return new Response('You are not allowed here. Check you config. ', 400); |
96
|
|
|
} |
97
|
|
|
$config = $this->getConfiguration($configName); |
98
|
|
|
$locales = $this->getParameter('php_translation.locales'); |
99
|
|
|
|
100
|
|
|
// Get a catalogue manager and load it with all the catalogues |
101
|
|
|
$catalogueManager = $this->get('php_translation.catalogue_manager'); |
102
|
|
|
$catalogueManager->load($this->get('php_translation.catalogue_fetcher')->getCatalogues($locales, [$config['output_dir']])); |
103
|
|
|
|
104
|
|
|
/** @var CatalogueMessage[] $messages */ |
105
|
|
|
$messages = $catalogueManager->getMessages($locale, $domain); |
106
|
|
|
usort($messages, function (CatalogueMessage $a, CatalogueMessage $b) { |
107
|
|
|
return strcmp($a->getKey(), $b->getKey()); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
return $this->render('TranslationBundle:WebUI:show.html.twig', [ |
111
|
|
|
'messages' => $messages, |
112
|
|
|
'domains' => $catalogueManager->getDomains(), |
113
|
|
|
'currentDomain' => $domain, |
114
|
|
|
'locales' => $locales, |
115
|
|
|
'currentLocale' => $locale, |
116
|
|
|
'configName' => $configName, |
117
|
|
|
'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'), |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Request $request |
125
|
|
|
* @param string $configName |
126
|
|
|
* @param string $locale |
127
|
|
|
* @param string $domain |
128
|
|
|
* |
129
|
|
|
* @return Response |
130
|
|
|
*/ |
131
|
|
|
public function createAction(Request $request, $configName, $locale, $domain) |
132
|
|
|
{ |
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
|
|
|
|
137
|
|
|
/** @var StorageService $storage */ |
138
|
|
|
$storage = $this->get('php_translation.storage.'.$configName); |
139
|
|
|
try { |
140
|
|
|
$message = $this->getMessage($request, ['Create']); |
141
|
|
|
} catch (MessageValidationException $e) { |
142
|
|
|
return new Response($e->getMessage(), 400); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
try { |
146
|
|
|
$storage->create(new Message($message->getKey(), $domain, $locale, $message->getMessage())); |
147
|
|
|
} catch (StorageException $e) { |
148
|
|
|
throw new BadRequestHttpException(sprintf( |
149
|
|
|
'Key "%s" does already exist for "%s" on domain "%s".', |
150
|
|
|
$message->getKey(), |
151
|
|
|
$locale, |
152
|
|
|
$domain |
153
|
|
|
), $e); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->render('TranslationBundle:WebUI:create.html.twig', [ |
157
|
|
|
'message' => $message, |
158
|
|
|
]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param Request $request |
163
|
|
|
* @param string $configName |
164
|
|
|
* @param string $locale |
165
|
|
|
* @param string $domain |
166
|
|
|
* |
167
|
|
|
* @return Response |
168
|
|
|
*/ |
169
|
|
View Code Duplication |
public function editAction(Request $request, $configName, $locale, $domain) |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
if (!$this->getParameter('php_translation.webui.enabled')) { |
172
|
|
|
return new Response('You are not allowed here. Check you config. ', 400); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
try { |
176
|
|
|
$message = $this->getMessage($request, ['Edit']); |
177
|
|
|
} catch (MessageValidationException $e) { |
178
|
|
|
return new Response($e->getMessage(), 400); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** @var StorageService $storage */ |
182
|
|
|
$storage = $this->get('php_translation.storage.'.$configName); |
183
|
|
|
$storage->update(new Message($message->getKey(), $domain, $locale, $message->getMessage())); |
184
|
|
|
|
185
|
|
|
return new Response('Translation updated'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param Request $request |
190
|
|
|
* @param string $configName |
191
|
|
|
* @param string $locale |
192
|
|
|
* @param string $domain |
193
|
|
|
* |
194
|
|
|
* @return Response |
195
|
|
|
*/ |
196
|
|
View Code Duplication |
public function deleteAction(Request $request, $configName, $locale, $domain) |
|
|
|
|
197
|
|
|
{ |
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
|
|
|
|
202
|
|
|
try { |
203
|
|
|
$message = $this->getMessage($request, ['Delete']); |
204
|
|
|
} catch (MessageValidationException $e) { |
205
|
|
|
return new Response($e->getMessage(), 400); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** @var StorageService $storage */ |
209
|
|
|
$storage = $this->get('php_translation.storage.'.$configName); |
210
|
|
|
$storage->delete($locale, $domain, $message->getKey()); |
211
|
|
|
|
212
|
|
|
return new Response('Message was deleted'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param $configName |
217
|
|
|
* |
218
|
|
|
* @return array |
219
|
|
|
*/ |
220
|
|
|
private function getConfiguration(&$configName) |
221
|
|
|
{ |
222
|
|
|
$configurationManager = $this->get('php_translation.configuration_manager'); |
223
|
|
|
$configName = $configName !== null ? $configName : $configurationManager->getFirstName(); |
224
|
|
|
if ($configName === null) { |
225
|
|
|
throw new \LogicException('You must configure at least one key under translation.configs'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$config = $configurationManager->getConfiguration($configName); |
229
|
|
|
|
230
|
|
|
if (empty($config)) { |
231
|
|
|
throw $this->createNotFoundException('No translation configuration named "'.$configName.'" was found.'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $config; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param Request $request |
239
|
|
|
* @param array $validationGroups |
240
|
|
|
* |
241
|
|
|
* @return WebUiMessage |
242
|
|
|
*/ |
243
|
|
|
private function getMessage(Request $request, array $validationGroups = []) |
244
|
|
|
{ |
245
|
|
|
$json = $request->getContent(); |
246
|
|
|
$data = json_decode($json, true); |
247
|
|
|
$message = new WebUiMessage(); |
248
|
|
|
if (isset($data['key'])) { |
249
|
|
|
$message->setKey($data['key']); |
250
|
|
|
} |
251
|
|
|
if (isset($data['message'])) { |
252
|
|
|
$message->setMessage($data['message']); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$errors = $this->get('validator')->validate($message, null, $validationGroups); |
256
|
|
|
if (count($errors) > 0) { |
257
|
|
|
throw MessageValidationException::create(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $message; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* This will return a map of our configured locales and their language name. |
265
|
|
|
* |
266
|
|
|
* @return array locale => language |
267
|
|
|
*/ |
268
|
|
|
private function getLocale2LanguageMap() |
269
|
|
|
{ |
270
|
|
|
$configuedLocales = $this->getParameter('php_translation.locales'); |
271
|
|
|
$names = Intl::getLocaleBundle()->getLocaleNames('en'); |
272
|
|
|
$map = []; |
273
|
|
|
foreach ($configuedLocales as $l) { |
274
|
|
|
$map[$l] = $names[$l]; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $map; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.