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