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\Service\StorageService; |
22
|
|
|
use Translation\Common\Exception\StorageException; |
23
|
|
|
use Translation\Bundle\Model\CatalogueMessage; |
24
|
|
|
use Translation\Common\Model\Message; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Tobias Nyholm <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class WebUIController extends Controller |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Show a dashboard for the configuration. |
33
|
|
|
* |
34
|
|
|
* @param string|null $configName |
35
|
|
|
* |
36
|
|
|
* @return Response |
37
|
|
|
*/ |
38
|
1 |
|
public function indexAction($configName = null) |
39
|
|
|
{ |
40
|
1 |
|
if (!$this->getParameter('php_translation.webui.enabled')) { |
41
|
|
|
return new Response('You are not allowed here. Check you config. ', 400); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
$configManager = $this->get('php_translation.configuration_manager'); |
45
|
1 |
|
$config = $configManager->getConfiguration($configName); |
46
|
1 |
|
$localeMap = $this->getLocale2LanguageMap(); |
47
|
1 |
|
$catalogues = $this->get('php_translation.catalogue_fetcher')->getCatalogues($config); |
48
|
|
|
|
49
|
1 |
|
$catalogueSize = []; |
50
|
1 |
|
$maxDomainSize = []; |
51
|
1 |
|
$maxCatalogueSize = 1; |
52
|
|
|
|
53
|
|
|
// For each catalogue (or locale) |
54
|
|
|
/** @var MessageCatalogue $catalogue */ |
55
|
1 |
|
foreach ($catalogues as $catalogue) { |
56
|
1 |
|
$locale = $catalogue->getLocale(); |
57
|
1 |
|
$domains = $catalogue->all(); |
58
|
1 |
|
ksort($domains); |
59
|
1 |
|
$catalogueSize[$locale] = 0; |
60
|
1 |
|
foreach ($domains as $domain => $messages) { |
61
|
1 |
|
$count = count(array_filter($messages, function ($message) { |
62
|
1 |
|
return '' !== $message; |
63
|
1 |
|
})); |
64
|
1 |
|
$catalogueSize[$locale] += $count; |
65
|
1 |
|
if (!isset($maxDomainSize[$domain]) || $count > $maxDomainSize[$domain]) { |
66
|
1 |
|
$maxDomainSize[$domain] = $count; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
if ($catalogueSize[$locale] > $maxCatalogueSize) { |
71
|
1 |
|
$maxCatalogueSize = $catalogueSize[$locale]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
return $this->render('@Translation/WebUI/index.html.twig', [ |
76
|
1 |
|
'catalogues' => $catalogues, |
77
|
1 |
|
'catalogueSize' => $catalogueSize, |
78
|
1 |
|
'maxDomainSize' => $maxDomainSize, |
79
|
1 |
|
'maxCatalogueSize' => $maxCatalogueSize, |
80
|
1 |
|
'localeMap' => $localeMap, |
81
|
1 |
|
'configName' => $config->getName(), |
82
|
1 |
|
'configNames' => $configManager->getNames(), |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Show a catalogue. |
88
|
|
|
* |
89
|
|
|
* @param string $configName |
90
|
|
|
* @param string $locale |
91
|
|
|
* @param string $domain |
92
|
|
|
* |
93
|
|
|
* @return Response |
94
|
|
|
*/ |
95
|
1 |
|
public function showAction($configName, $locale, $domain) |
96
|
|
|
{ |
97
|
1 |
|
if (!$this->getParameter('php_translation.webui.enabled')) { |
98
|
|
|
return new Response('You are not allowed here. Check you config. ', 400); |
99
|
|
|
} |
100
|
1 |
|
$configManager = $this->get('php_translation.configuration_manager'); |
101
|
1 |
|
$config = $configManager->getConfiguration($configName); |
102
|
|
|
|
103
|
|
|
// Get a catalogue manager and load it with all the catalogues |
104
|
1 |
|
$catalogueManager = $this->get('php_translation.catalogue_manager'); |
105
|
1 |
|
$catalogueManager->load($this->get('php_translation.catalogue_fetcher')->getCatalogues($config)); |
106
|
|
|
|
107
|
|
|
/** @var CatalogueMessage[] $messages */ |
108
|
1 |
|
$messages = $catalogueManager->getMessages($locale, $domain); |
109
|
1 |
|
usort($messages, function (CatalogueMessage $a, CatalogueMessage $b) { |
110
|
1 |
|
return strcmp($a->getKey(), $b->getKey()); |
111
|
1 |
|
}); |
112
|
|
|
|
113
|
1 |
|
return $this->render('@Translation/WebUI/show.html.twig', [ |
114
|
1 |
|
'messages' => $messages, |
115
|
1 |
|
'domains' => $catalogueManager->getDomains(), |
116
|
1 |
|
'currentDomain' => $domain, |
117
|
1 |
|
'locales' => $this->getParameter('php_translation.locales'), |
118
|
1 |
|
'currentLocale' => $locale, |
119
|
1 |
|
'configName' => $config->getName(), |
120
|
1 |
|
'configNames' => $configManager->getNames(), |
121
|
1 |
|
'allow_create' => $this->getParameter('php_translation.webui.allow_create'), |
122
|
1 |
|
'allow_delete' => $this->getParameter('php_translation.webui.allow_delete'), |
123
|
1 |
|
'file_base_path' => $this->getParameter('php_translation.webui.file_base_path'), |
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param Request $request |
129
|
|
|
* @param string $configName |
130
|
|
|
* @param string $locale |
131
|
|
|
* @param string $domain |
132
|
|
|
* |
133
|
|
|
* @return Response |
134
|
|
|
*/ |
135
|
1 |
|
public function createAction(Request $request, $configName, $locale, $domain) |
136
|
|
|
{ |
137
|
1 |
|
if (!$this->getParameter('php_translation.webui.enabled') || !$this->getParameter('php_translation.webui.allow_create')) { |
138
|
|
|
return new Response('You are not allowed to create. Check you config. ', 400); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** @var StorageService $storage */ |
142
|
1 |
|
$storage = $this->get('php_translation.storage.'.$configName); |
143
|
|
|
|
144
|
|
|
try { |
145
|
1 |
|
$message = $this->getMessageFromRequest($request); |
146
|
1 |
|
$message->setDomain($domain); |
147
|
1 |
|
$message->setLocale($locale); |
148
|
1 |
|
$this->validateMessage($message, ['Create']); |
149
|
1 |
|
} catch (MessageValidationException $e) { |
150
|
1 |
|
return new Response($e->getMessage(), 400); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
try { |
154
|
1 |
|
$storage->create($message); |
155
|
|
|
} catch (StorageException $e) { |
156
|
|
|
throw new BadRequestHttpException(sprintf( |
157
|
|
|
'Key "%s" does already exist for "%s" on domain "%s".', |
158
|
|
|
$message->getKey(), |
159
|
|
|
$locale, |
160
|
|
|
$domain |
161
|
|
|
), $e); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return $this->render('@Translation/WebUI/create.html.twig', [ |
165
|
1 |
|
'message' => $message, |
166
|
|
|
]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param Request $request |
171
|
|
|
* @param string $configName |
172
|
|
|
* @param string $locale |
173
|
|
|
* @param string $domain |
174
|
|
|
* |
175
|
|
|
* @return Response |
176
|
|
|
*/ |
177
|
1 |
|
public function editAction(Request $request, $configName, $locale, $domain) |
178
|
|
|
{ |
179
|
1 |
|
if (!$this->getParameter('php_translation.webui.enabled')) { |
180
|
|
|
return new Response('You are not allowed here. Check you config. ', 400); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
try { |
184
|
1 |
|
$message = $this->getMessageFromRequest($request); |
185
|
1 |
|
$message->setDomain($domain); |
186
|
1 |
|
$message->setLocale($locale); |
187
|
1 |
|
$this->validateMessage($message, ['Edit']); |
188
|
1 |
|
} catch (MessageValidationException $e) { |
189
|
1 |
|
return new Response($e->getMessage(), 400); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** @var StorageService $storage */ |
193
|
1 |
|
$storage = $this->get('php_translation.storage.'.$configName); |
194
|
1 |
|
$storage->update($message); |
195
|
|
|
|
196
|
1 |
|
return new Response('Translation updated'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param Request $request |
201
|
|
|
* @param string $configName |
202
|
|
|
* @param string $locale |
203
|
|
|
* @param string $domain |
204
|
|
|
* |
205
|
|
|
* @return Response |
206
|
|
|
*/ |
207
|
1 |
|
public function deleteAction(Request $request, $configName, $locale, $domain) |
208
|
|
|
{ |
209
|
1 |
|
if (!$this->getParameter('php_translation.webui.enabled') || !$this->getParameter('php_translation.webui.allow_delete')) { |
210
|
|
|
return new Response('You are not allowed to create. Check you config. ', 400); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
try { |
214
|
1 |
|
$message = $this->getMessageFromRequest($request); |
215
|
1 |
|
$message->setLocale($locale); |
216
|
1 |
|
$message->setDomain($domain); |
217
|
1 |
|
$this->validateMessage($message, ['Delete']); |
218
|
|
|
} catch (MessageValidationException $e) { |
219
|
|
|
return new Response($e->getMessage(), 400); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** @var StorageService $storage */ |
223
|
1 |
|
$storage = $this->get('php_translation.storage.'.$configName); |
224
|
1 |
|
$storage->delete($locale, $domain, $message->getKey()); |
225
|
|
|
|
226
|
1 |
|
return new Response('Message was deleted'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param Request $request |
231
|
|
|
* |
232
|
|
|
* @return Message |
233
|
|
|
*/ |
234
|
3 |
|
private function getMessageFromRequest(Request $request) |
235
|
|
|
{ |
236
|
3 |
|
$json = $request->getContent(); |
237
|
3 |
|
$data = json_decode($json, true); |
238
|
3 |
|
$message = new Message(); |
239
|
3 |
|
if (isset($data['key'])) { |
240
|
3 |
|
$message->setKey($data['key']); |
241
|
|
|
} |
242
|
3 |
|
if (isset($data['message'])) { |
243
|
2 |
|
$message->setTranslation($data['message']); |
244
|
|
|
} |
245
|
|
|
|
246
|
3 |
|
return $message; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* This will return a map of our configured locales and their language name. |
251
|
|
|
* |
252
|
|
|
* @return array locale => language |
253
|
|
|
*/ |
254
|
1 |
|
private function getLocale2LanguageMap() |
255
|
|
|
{ |
256
|
1 |
|
$configuedLocales = $this->getParameter('php_translation.locales'); |
257
|
1 |
|
$names = Intl::getLocaleBundle()->getLocaleNames('en'); |
258
|
1 |
|
$map = []; |
259
|
1 |
|
foreach ($configuedLocales as $l) { |
260
|
1 |
|
$map[$l] = $names[$l]; |
261
|
|
|
} |
262
|
|
|
|
263
|
1 |
|
return $map; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param Message $message |
268
|
|
|
* @param array $validationGroups |
269
|
|
|
* |
270
|
|
|
* @throws MessageValidationException |
271
|
|
|
*/ |
272
|
3 |
|
private function validateMessage(Message $message, array $validationGroups) |
273
|
|
|
{ |
274
|
3 |
|
$errors = $this->get('validator')->validate($message, null, $validationGroups); |
275
|
3 |
|
if (count($errors) > 0) { |
276
|
2 |
|
throw MessageValidationException::create(); |
277
|
|
|
} |
278
|
3 |
|
} |
279
|
|
|
} |
280
|
|
|
|