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