|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Lexik\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\Lexik\Event\RemoveLocaleCacheEvent; |
|
6
|
|
|
use Sonata\AdminBundle\Controller\CRUDController; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
11
|
|
|
|
|
12
|
|
|
class TranslationCRUDController extends CRUDController |
|
13
|
|
|
{ |
|
14
|
|
|
use NonActionTrait; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @return RedirectResponse|Response |
|
18
|
|
|
* @throws NotFoundHttpException |
|
19
|
|
|
*/ |
|
20
|
|
|
public function createTransUnitAction() |
|
21
|
|
|
{ |
|
22
|
|
|
$request = $this->getRequest(); |
|
23
|
|
|
$parameters = $this->getRequest()->request; |
|
24
|
|
|
if (!$request->isMethod('POST')) { |
|
25
|
|
|
return $this->renderJson([ |
|
26
|
|
|
'message' => 'method not allowed', |
|
27
|
|
|
], 403); |
|
28
|
|
|
} |
|
29
|
|
|
$admin = $this->admin; |
|
30
|
|
|
if (false === $admin->isGranted('EDIT')) { |
|
31
|
|
|
return $this->renderJson([ |
|
32
|
|
|
'message' => 'access denied', |
|
33
|
|
|
], 403); |
|
34
|
|
|
} |
|
35
|
|
|
$keyName = $parameters->get('key'); |
|
36
|
|
|
$domainName = $parameters->get('domain'); |
|
37
|
|
|
if (!$keyName || !$domainName) { |
|
38
|
|
|
return $this->renderJson([ |
|
39
|
|
|
'message' => 'missing key or domain', |
|
40
|
|
|
], 422); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/* @var $transUnitManager \Lexik\Bundle\TranslationBundle\Manager\TransUnitManager */ |
|
44
|
|
|
$transUnitManager = $this->get('lexik_translation.trans_unit.manager'); |
|
45
|
|
|
$transUnit = $transUnitManager->create($keyName, $domainName, true); |
|
46
|
|
|
|
|
47
|
|
|
return $this->editAction($transUnit->getId()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Edit action |
|
52
|
|
|
* |
|
53
|
|
|
* @param int|string|null $id |
|
54
|
|
|
* |
|
55
|
|
|
* @param Request|null $request |
|
56
|
|
|
* |
|
57
|
|
|
* @return RedirectResponse|Response |
|
58
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function editAction($id = null, Request $request = null) |
|
61
|
|
|
{ |
|
62
|
|
|
if (!$request) { |
|
63
|
|
|
$request = $this->getRequest(); |
|
64
|
|
|
} |
|
65
|
|
|
if (!$request->isMethod('POST')) { |
|
66
|
|
|
return $this->redirect($this->admin->generateUrl('list')); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/* @var $transUnit \Lexik\Bundle\TranslationBundle\Model\TransUnit */ |
|
70
|
|
|
$transUnit = $this->get('lexik_translation.translation_storage')->getTransUnitById($id); |
|
71
|
|
|
if (!$transUnit) { |
|
|
|
|
|
|
72
|
|
|
throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (false === $this->admin->isGranted('EDIT', $transUnit)) { |
|
76
|
|
|
return $this->renderJson([ |
|
77
|
|
|
'message' => 'access denied', |
|
78
|
|
|
], 403); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->admin->setSubject($transUnit); |
|
82
|
|
|
|
|
83
|
|
|
/* @var $transUnitManager \Lexik\Bundle\TranslationBundle\Manager\TransUnitManager */ |
|
84
|
|
|
$transUnitManager = $this->get('lexik_translation.trans_unit.manager'); |
|
85
|
|
|
|
|
86
|
|
|
$parameters = $this->getRequest()->request; |
|
87
|
|
|
|
|
88
|
|
|
$locale = $parameters->get('locale'); |
|
89
|
|
|
$content = $parameters->get('value'); |
|
90
|
|
|
|
|
91
|
|
|
if (!$locale) { |
|
92
|
|
|
return $this->renderJson([ |
|
93
|
|
|
'message' => 'locale missing', |
|
94
|
|
|
], 422); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/* @var $translation \Lexik\Bundle\TranslationBundle\Entity\Translation */ |
|
98
|
|
|
if ($parameters->get('pk')) { |
|
99
|
|
|
$translation = $transUnitManager->updateTranslation($transUnit, $locale, $content, true); |
|
100
|
|
|
} else { |
|
101
|
|
|
$translation = $transUnitManager->addTranslation($transUnit, $locale, $content, null, true); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ($request->query->get('clear_cache')) { |
|
105
|
|
|
$this->get('translator')->removeLocalesCacheFiles([$locale]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->renderJson([ |
|
109
|
|
|
'key' => $transUnit->getKey(), |
|
110
|
|
|
'domain' => $transUnit->getDomain(), |
|
111
|
|
|
'pk' => $translation->getId(), |
|
|
|
|
|
|
112
|
|
|
'locale' => $translation->getLocale(), |
|
113
|
|
|
'value' => $translation->getContent(), |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return RedirectResponse |
|
119
|
|
|
*/ |
|
120
|
|
|
public function clearCacheAction() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->get('event_dispatcher') |
|
123
|
|
|
->dispatch(RemoveLocaleCacheEvent::PRE_REMOVE_LOCAL_CACHE, new RemoveLocaleCacheEvent($this->getManagedLocales())); |
|
124
|
|
|
$this->get('translator')->removeLocalesCacheFiles($this->getManagedLocales()); |
|
125
|
|
|
$this->get('event_dispatcher') |
|
126
|
|
|
->dispatch(RemoveLocaleCacheEvent::POST_REMOVE_LOCAL_CACHE, new RemoveLocaleCacheEvent($this->getManagedLocales())); |
|
127
|
|
|
|
|
128
|
|
|
/** @var $session Session */ |
|
129
|
|
|
$session = $this->get('session'); |
|
130
|
|
|
$session->getFlashBag()->set('sonata_flash_success', 'translations.cache_removed'); |
|
131
|
|
|
|
|
132
|
|
|
return $this->redirect($this->admin->generateUrl('list')); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|