1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\CoreBundle\Controller; |
||
13 | |||
14 | use Mautic\CoreBundle\Form\Type\ThemeUploadType; |
||
15 | use Mautic\CoreBundle\Helper\InputHelper; |
||
16 | use Symfony\Component\Form\FormError; |
||
17 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
18 | use Symfony\Component\HttpFoundation\Response; |
||
19 | |||
20 | /** |
||
21 | * Class ThemeController. |
||
22 | */ |
||
23 | class ThemeController extends FormController |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
24 | { |
||
25 | /** |
||
26 | * @return JsonResponse|\Symfony\Component\HttpFoundation\Response |
||
27 | */ |
||
28 | public function indexAction() |
||
29 | { |
||
30 | //set some permissions |
||
31 | $permissions = $this->get('mautic.security')->isGranted([ |
||
32 | 'core:themes:view', |
||
33 | 'core:themes:create', |
||
34 | 'core:themes:edit', |
||
35 | 'core:themes:delete', |
||
36 | ], 'RETURN_ARRAY'); |
||
37 | |||
38 | if (!$permissions['core:themes:view']) { |
||
39 | return $this->accessDenied(); |
||
40 | } |
||
41 | |||
42 | $themeHelper = $this->container->get('mautic.helper.theme'); |
||
43 | $dir = $this->factory->getSystemPath('themes', true); |
||
44 | $action = $this->generateUrl('mautic_themes_index'); |
||
45 | $form = $this->get('form.factory')->create(ThemeUploadType::class, [], ['action' => $action]); |
||
46 | |||
47 | if ('POST' == $this->request->getMethod()) { |
||
48 | if (isset($form) && !$cancelled = $this->isFormCancelled($form)) { |
||
49 | if ($this->isFormValid($form)) { |
||
50 | $fileData = $form['file']->getData(); |
||
51 | |||
52 | if (!$fileData) { |
||
53 | $form->addError( |
||
54 | new FormError( |
||
55 | $this->translator->trans('mautic.core.theme.upload.empty', [], 'validators') |
||
56 | ) |
||
57 | ); |
||
58 | } else { |
||
59 | $fileName = InputHelper::filename($fileData->getClientOriginalName()); |
||
60 | $themeName = basename($fileName, '.zip'); |
||
61 | |||
62 | if (!empty($fileData)) { |
||
63 | $extension = pathinfo($fileName, PATHINFO_EXTENSION); |
||
64 | |||
65 | if ('zip' === $extension) { |
||
66 | try { |
||
67 | $fileData->move($dir, $fileName); |
||
68 | $themeHelper->install($dir.'/'.$fileName); |
||
69 | $this->addFlash('mautic.core.theme.installed', ['%name%' => $themeName]); |
||
70 | } catch (\Exception $e) { |
||
71 | $form->addError( |
||
72 | new FormError( |
||
73 | $this->translator->trans($e->getMessage(), [], 'validators') |
||
74 | ) |
||
75 | ); |
||
76 | } |
||
77 | } else { |
||
78 | $form->addError( |
||
79 | new FormError( |
||
80 | $this->translator->trans('mautic.core.not.allowed.file.extension', ['%extension%' => $extension], 'validators') |
||
81 | ) |
||
82 | ); |
||
83 | } |
||
84 | } else { |
||
85 | $form->addError( |
||
86 | new FormError( |
||
87 | $this->translator->trans('mautic.dashboard.upload.filenotfound', [], 'validators') |
||
88 | ) |
||
89 | ); |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | return $this->delegateView([ |
||
97 | 'viewParameters' => [ |
||
98 | 'items' => $themeHelper->getInstalledThemes('all', true, true), |
||
99 | 'defaultThemes' => $themeHelper->getDefaultThemes(), |
||
100 | 'form' => $form->createView(), |
||
101 | 'permissions' => $permissions, |
||
102 | 'security' => $this->get('mautic.security'), |
||
103 | ], |
||
104 | 'contentTemplate' => 'MauticCoreBundle:Theme:list.html.php', |
||
105 | 'passthroughVars' => [ |
||
106 | 'activeLink' => '#mautic_themes_index', |
||
107 | 'mauticContent' => 'theme', |
||
108 | 'route' => $this->generateUrl('mautic_themes_index'), |
||
109 | ], |
||
110 | ]); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Download a theme. |
||
115 | * |
||
116 | * @param string $themeName |
||
117 | * |
||
118 | * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response |
||
119 | */ |
||
120 | public function downloadAction($themeName) |
||
121 | { |
||
122 | $themeHelper = $this->container->get('mautic.helper.theme'); |
||
123 | $flashes = []; |
||
124 | $error = false; |
||
125 | |||
126 | if (!$this->get('mautic.security')->isGranted('core:themes:view')) { |
||
127 | return $this->accessDenied(); |
||
128 | } |
||
129 | |||
130 | if (!$themeHelper->exists($themeName)) { |
||
131 | $flashes[] = [ |
||
132 | 'type' => 'error', |
||
133 | 'msg' => 'mautic.core.theme.error.notfound', |
||
134 | 'msgVars' => ['%theme%' => $themeName], |
||
135 | ]; |
||
136 | $error = true; |
||
137 | } |
||
138 | |||
139 | try { |
||
140 | $zipPath = $themeHelper->zip($themeName); |
||
141 | } catch (\Exception $e) { |
||
142 | $flashes[] = [ |
||
143 | 'type' => 'error', |
||
144 | 'msg' => $e->getMessage(), |
||
145 | ]; |
||
146 | $error = true; |
||
147 | } |
||
148 | |||
149 | if (!$error && !$zipPath) { |
||
150 | $flashes[] = [ |
||
151 | 'type' => 'error', |
||
152 | 'msg' => 'mautic.core.permission.issue', |
||
153 | ]; |
||
154 | $error = true; |
||
155 | } |
||
156 | |||
157 | if ($error) { |
||
158 | return $this->postActionRedirect( |
||
159 | array_merge($this->getIndexPostActionVars(), [ |
||
160 | 'flashes' => $flashes, |
||
161 | ]) |
||
162 | ); |
||
163 | } |
||
164 | |||
165 | $response = new Response(); |
||
166 | $response->headers->set('Content-Type', 'application/octet-stream'); |
||
167 | $response->headers->set('Content-Length', filesize($zipPath)); |
||
168 | |||
169 | $stream = $this->request->get('stream', 0); |
||
170 | |||
171 | if (!$stream) { |
||
172 | $response->headers->set('Content-Disposition', 'attachment;filename="'.$themeName.'.zip"'); |
||
173 | } |
||
174 | |||
175 | $response->setContent(file_get_contents($zipPath)); |
||
176 | |||
177 | return $response; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Deletes the theme. |
||
182 | * |
||
183 | * @param string $themeName |
||
184 | * |
||
185 | * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse |
||
186 | */ |
||
187 | public function deleteAction($themeName) |
||
188 | { |
||
189 | $flashes = []; |
||
190 | |||
191 | if ('POST' == $this->request->getMethod()) { |
||
192 | $flashes = $this->deleteTheme($themeName); |
||
193 | } |
||
194 | |||
195 | return $this->postActionRedirect( |
||
196 | array_merge($this->getIndexPostActionVars(), [ |
||
197 | 'flashes' => $flashes, |
||
198 | ]) |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Deletes a group of themes. |
||
204 | * |
||
205 | * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse |
||
206 | */ |
||
207 | public function batchDeleteAction() |
||
208 | { |
||
209 | $flashes = []; |
||
210 | |||
211 | if ('POST' == $this->request->getMethod()) { |
||
212 | $themeNames = json_decode($this->request->query->get('ids', '{}')); |
||
213 | |||
214 | foreach ($themeNames as $themeName) { |
||
215 | $flashes = $this->deleteTheme($themeName); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | return $this->postActionRedirect( |
||
220 | array_merge($this->getIndexPostActionVars(), [ |
||
221 | 'flashes' => $flashes, |
||
222 | ]) |
||
223 | ); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Deletes a theme. |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | public function deleteTheme($themeName) |
||
232 | { |
||
233 | $flashes = []; |
||
234 | $themeHelper = $this->container->get('mautic.helper.theme'); |
||
235 | |||
236 | if (!$themeHelper->exists($themeName)) { |
||
237 | $flashes[] = [ |
||
238 | 'type' => 'error', |
||
239 | 'msg' => 'mautic.core.theme.error.notfound', |
||
240 | 'msgVars' => ['%theme%' => $themeName], |
||
241 | ]; |
||
242 | } elseif (!$this->get('mautic.security')->isGranted('core:themes:delete')) { |
||
243 | return $this->accessDenied(); |
||
244 | } elseif (in_array($themeName, $themeHelper->getDefaultThemes())) { |
||
245 | $flashes[] = [ |
||
246 | 'type' => 'error', |
||
247 | 'msg' => 'mautic.core.theme.cannot.be.removed', |
||
248 | 'msgVars' => ['%theme%' => $themeName], |
||
249 | ]; |
||
250 | } else { |
||
251 | try { |
||
252 | $theme = $themeHelper->getTheme($themeName); |
||
253 | $themeHelper->delete($themeName); |
||
254 | } catch (\Exception $e) { |
||
255 | $flashes[] = [ |
||
256 | 'type' => 'error', |
||
257 | 'msg' => 'mautic.core.error.delete.error', |
||
258 | 'msgVars' => ['%error%' => $e->getMessage()], |
||
259 | ]; |
||
260 | } |
||
261 | |||
262 | $flashes[] = [ |
||
263 | 'type' => 'notice', |
||
264 | 'msg' => 'mautic.core.notice.deleted', |
||
265 | 'msgVars' => [ |
||
266 | '%name%' => $theme->getName(), |
||
267 | '%id%' => $themeName, |
||
268 | ], |
||
269 | ]; |
||
270 | } |
||
271 | |||
272 | return $flashes; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * A helper method to keep the code DRY. |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | public function getIndexPostActionVars() |
||
281 | { |
||
282 | return [ |
||
283 | 'returnUrl' => $this->generateUrl('mautic_themes_index'), |
||
284 | 'contentTemplate' => 'MauticCoreBundle:theme:index', |
||
285 | 'passthroughVars' => [ |
||
286 | 'activeLink' => 'mautic_themes_index', |
||
287 | 'mauticContent' => 'theme', |
||
288 | ], |
||
289 | ]; |
||
290 | } |
||
291 | } |
||
292 |