|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Copyright (C) 2015 Michael Giesler |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of Dembelo. |
|
6
|
|
|
* |
|
7
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
|
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License 3 for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
|
18
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @package AdminBundle |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace AdminBundle\Controller; |
|
26
|
|
|
|
|
27
|
|
|
use DembeloMain\Document\User; |
|
28
|
|
|
use DembeloMain\Model\Repository\Doctrine\ODM\AbstractRepository; |
|
29
|
|
|
use DembeloMain\Document\Importfile; |
|
30
|
|
|
use DembeloMain\Model\Repository\ImportfileRepositoryInterface; |
|
31
|
|
|
use DembeloMain\Model\Repository\LicenseeRepositoryInterface; |
|
32
|
|
|
use DembeloMain\Model\Repository\TopicRepositoryInterface; |
|
33
|
|
|
use DembeloMain\Model\Repository\UserRepositoryInterface; |
|
34
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
35
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
36
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
37
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
38
|
|
|
use DembeloMain\Document\Topic; |
|
39
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder; |
|
40
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
|
41
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Class DefaultController |
|
45
|
|
|
* @Route(service="app.admin_controller_default") |
|
46
|
|
|
*/ |
|
47
|
|
|
class DefaultController |
|
48
|
|
|
{ |
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $configTwineDirectory; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var Templating |
|
56
|
|
|
*/ |
|
57
|
|
|
private $templating; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var UserRepositoryInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
private $userRepository; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var LicenseeRepositoryInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
private $licenseeRepository; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var TopicRepositoryInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
private $topicRepository; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var ImportfileRepositoryInterface |
|
76
|
|
|
*/ |
|
77
|
|
|
private $importfileRepository; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var UserPasswordEncoder |
|
81
|
|
|
*/ |
|
82
|
|
|
private $userPasswordEncoder; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var string |
|
86
|
|
|
*/ |
|
87
|
|
|
private $topicImageDirectory; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var Filesystem |
|
91
|
|
|
*/ |
|
92
|
|
|
private $filesystem; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* DefaultController constructor. |
|
96
|
|
|
* @param Templating $templating |
|
97
|
|
|
* @param UserRepositoryInterface $userRepository |
|
98
|
|
|
* @param LicenseeRepositoryInterface $licenseeRepository |
|
99
|
|
|
* @param TopicRepositoryInterface $topicRepository |
|
100
|
|
|
* @param ImportfileRepositoryInterface $importfileRepository |
|
101
|
|
|
* @param UserPasswordEncoder $userPasswordEncoder |
|
102
|
|
|
* @param string $configTwineDirectory |
|
103
|
|
|
* @param string $topicImageDirectory |
|
104
|
|
|
* @param Filesystem $filesystem |
|
105
|
|
|
*/ |
|
106
|
7 |
|
public function __construct(Templating $templating, UserRepositoryInterface $userRepository, LicenseeRepositoryInterface $licenseeRepository, TopicRepositoryInterface $topicRepository, ImportfileRepositoryInterface $importfileRepository, UserPasswordEncoder $userPasswordEncoder, string $configTwineDirectory, string $topicImageDirectory, Filesystem $filesystem) |
|
107
|
|
|
{ |
|
108
|
7 |
|
$this->configTwineDirectory = $configTwineDirectory; |
|
109
|
7 |
|
$this->userRepository = $userRepository; |
|
110
|
7 |
|
$this->templating = $templating; |
|
111
|
7 |
|
$this->licenseeRepository = $licenseeRepository; |
|
112
|
7 |
|
$this->topicRepository = $topicRepository; |
|
113
|
7 |
|
$this->importfileRepository = $importfileRepository; |
|
114
|
7 |
|
$this->userPasswordEncoder = $userPasswordEncoder; |
|
115
|
7 |
|
$this->topicImageDirectory = $topicImageDirectory; |
|
116
|
7 |
|
$this->filesystem = $filesystem; |
|
117
|
7 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @Route("/", name="admin_mainpage") |
|
121
|
|
|
* |
|
122
|
|
|
* @return Response |
|
123
|
|
|
* |
|
124
|
|
|
* @throws \RuntimeException |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function indexAction(): Response |
|
127
|
|
|
{ |
|
128
|
|
|
$mainMenuData = [ |
|
129
|
1 |
|
['id' => '1', 'type' => 'folder', 'value' => 'Benutzer', 'css' => 'folder_music'], |
|
130
|
|
|
['id' => '2', 'type' => 'folder', 'value' => 'Lizenznehmer', 'css' => 'folder_music'], |
|
131
|
|
|
['id' => '3', 'type' => 'folder', 'value' => 'Themenfelder', 'css' => 'folder_music'], |
|
132
|
|
|
['id' => '4', 'type' => 'folder', 'value' => 'Importe', 'css' => 'folder_music'], |
|
133
|
|
|
['id' => '5', 'type' => 'folder', 'value' => 'Textknoten', 'css' => 'folder_music'], |
|
134
|
|
|
]; |
|
135
|
|
|
|
|
136
|
1 |
|
$jsonEncoder = new JsonEncoder(); |
|
137
|
|
|
|
|
138
|
1 |
|
return $this->templating->renderResponse( |
|
139
|
1 |
|
'AdminBundle::index.html.twig', |
|
140
|
|
|
[ |
|
141
|
1 |
|
'mainMenuData' => $jsonEncoder->encode($mainMenuData, 'json'), |
|
142
|
|
|
] |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @Route("/save", name="admin_formsave") |
|
148
|
|
|
* |
|
149
|
|
|
* @param Request $request |
|
150
|
|
|
* |
|
151
|
|
|
* @return Response |
|
152
|
|
|
* |
|
153
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
|
154
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
|
155
|
|
|
* @throws \Exception |
|
156
|
|
|
* @throws \RuntimeException |
|
157
|
|
|
* @throws \InvalidArgumentException |
|
158
|
|
|
*/ |
|
159
|
6 |
|
public function formsaveAction(Request $request): Response |
|
160
|
|
|
{ |
|
161
|
6 |
|
$params = $request->request->all(); |
|
162
|
|
|
|
|
163
|
6 |
|
if (!isset($params['id'])) { |
|
164
|
1 |
|
return new Response(\json_encode(array('error' => true))); |
|
165
|
|
|
} |
|
166
|
5 |
|
$formtype = $params['formtype'] ?? ''; |
|
167
|
|
|
|
|
168
|
|
|
/* @var $repository AbstractRepository */ |
|
169
|
|
|
switch ($formtype) { |
|
170
|
5 |
|
case 'user': |
|
171
|
1 |
|
$repository = $this->userRepository; |
|
172
|
1 |
|
break; |
|
173
|
4 |
|
case 'topic': |
|
174
|
1 |
|
$repository = $this->topicRepository; |
|
175
|
1 |
|
break; |
|
176
|
3 |
|
case 'licensee': |
|
177
|
1 |
|
$repository = $this->licenseeRepository; |
|
178
|
1 |
|
break; |
|
179
|
2 |
|
case 'importfile': |
|
180
|
1 |
|
$repository = $this->importfileRepository; |
|
181
|
1 |
|
break; |
|
182
|
|
|
default: |
|
183
|
1 |
|
return new Response(\json_encode(array('error' => true))); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
4 |
|
if (isset($params['id']) && $params['id'] === 'new') { |
|
187
|
1 |
|
$className = $repository->getClassName(); |
|
188
|
1 |
|
$item = new $className(); |
|
189
|
|
|
} else { |
|
190
|
3 |
|
$item = $repository->find($params['id']); |
|
191
|
3 |
|
if (null === $item || $item->getId() !== $params['id']) { |
|
192
|
|
|
return new Response(\json_encode(array('error' => true))); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
4 |
|
foreach ($params as $param => $value) { |
|
197
|
4 |
|
if (in_array($param, array('id', 'formtype', 'filename', 'orgname'), true)) { |
|
198
|
4 |
|
continue; |
|
199
|
|
|
} |
|
200
|
2 |
|
if ('password' === $param && empty($value)) { |
|
201
|
|
|
continue; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
2 |
|
if ($item instanceof User && 'password' === $param) { |
|
205
|
1 |
|
$value = $this->userPasswordEncoder->encodePassword($item, $value); |
|
206
|
2 |
|
} elseif ('' === $value && in_array($param, ['licenseeId', 'imported'], true)) { |
|
207
|
|
|
$value = null; |
|
208
|
|
|
} |
|
209
|
2 |
|
$method = 'set'.ucfirst($param); |
|
210
|
2 |
|
if (method_exists($item, $method)) { |
|
211
|
2 |
|
$item->$method($value); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
4 |
|
if (method_exists($item, 'setMetadata')) { |
|
216
|
1 |
|
$item->setMetadata('updated', time()); |
|
217
|
|
|
} |
|
218
|
4 |
|
$repository->save($item); |
|
|
|
|
|
|
219
|
|
|
|
|
220
|
4 |
|
if ($item instanceof Topic |
|
221
|
4 |
|
&& array_key_exists('imageFileName', $params) |
|
222
|
4 |
|
&& array_key_exists('originalImageName', $params) |
|
223
|
4 |
|
&& !empty($params['imageFileName']) |
|
224
|
4 |
|
&& !empty($params['originalImageName']) |
|
225
|
|
|
) { |
|
226
|
1 |
|
$this->saveTopicImage($item, $params['imageFileName'], $params['originalImageName']); |
|
227
|
1 |
|
$repository->save($item); |
|
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
4 |
|
if ($item instanceof Importfile |
|
231
|
4 |
|
&& array_key_exists('filename', $params) |
|
232
|
4 |
|
&& array_key_exists('orgname', $params) |
|
233
|
4 |
|
&& !empty($params['filename']) |
|
234
|
4 |
|
&& !empty($params['orgname']) |
|
235
|
|
|
) { |
|
236
|
1 |
|
$this->saveFile($item, $params['filename'], $params['orgname']); |
|
237
|
1 |
|
$repository->save($item); |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
$output = array( |
|
241
|
4 |
|
'error' => false, |
|
242
|
4 |
|
'newId' => $item->getId(), |
|
243
|
|
|
); |
|
244
|
|
|
|
|
245
|
4 |
|
return new Response(\json_encode($output)); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* saves temporary file to final place |
|
250
|
|
|
* |
|
251
|
|
|
* @param Importfile $item importfile instance |
|
252
|
|
|
* @param string $filename filename hash |
|
253
|
|
|
* @param string $orgname original name |
|
254
|
|
|
* |
|
255
|
|
|
* @return void |
|
256
|
|
|
* |
|
257
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
|
258
|
|
|
* @throws \RuntimeException |
|
259
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
|
260
|
|
|
*/ |
|
261
|
1 |
|
private function saveFile(Importfile $item, $filename, $orgname): void |
|
262
|
|
|
{ |
|
263
|
1 |
|
$directory = $this->configTwineDirectory; |
|
264
|
1 |
|
$file = $directory.$filename; |
|
265
|
1 |
|
if (!$this->filesystem->exists($file)) { |
|
266
|
|
|
return; |
|
267
|
|
|
} |
|
268
|
1 |
|
$finalDirectory = $directory.$item->getLicenseeId().'/'; |
|
269
|
1 |
|
$this->filesystem->mkdir($finalDirectory); |
|
270
|
1 |
|
$finalName = $finalDirectory.$item->getId(); |
|
271
|
|
|
|
|
272
|
1 |
|
$this->filesystem->rename($file, $finalName); |
|
273
|
|
|
|
|
274
|
1 |
|
$item->setOriginalname($orgname); |
|
275
|
1 |
|
$item->setFilename($finalName); |
|
276
|
1 |
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* saves temporary file to final place |
|
280
|
|
|
* |
|
281
|
|
|
* @param Topic $item topic instance |
|
282
|
|
|
* @param string $filename filename hash |
|
283
|
|
|
* @param string $orgname original name |
|
284
|
|
|
* |
|
285
|
|
|
* @return void |
|
286
|
|
|
* |
|
287
|
|
|
* @throws \RuntimeException |
|
288
|
|
|
*/ |
|
289
|
1 |
|
private function saveTopicImage(Topic $item, string $filename, string $orgname): void |
|
290
|
|
|
{ |
|
291
|
1 |
|
$directory = $this->topicImageDirectory; |
|
292
|
1 |
|
$finalDirectory = $directory.$item->getId().'/'; |
|
293
|
1 |
|
$this->filesystem->mkdir($finalDirectory); |
|
294
|
|
|
|
|
295
|
1 |
|
$finalName = $finalDirectory.$orgname; |
|
296
|
1 |
|
$file = $directory.$filename; |
|
297
|
1 |
|
$this->filesystem->rename($file, $finalName); |
|
298
|
1 |
|
$item->setOriginalImageName($orgname); |
|
299
|
1 |
|
$item->setImageFilename($finalName); |
|
300
|
1 |
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|