|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: wechsler |
|
5
|
|
|
* Date: 28/01/2017 |
|
6
|
|
|
* Time: 19:12 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Phase\TakeATicketBundle\Controller; |
|
10
|
|
|
|
|
11
|
|
|
use Phase\TakeATicket\SongLoader; |
|
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
|
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FileType; |
|
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType; |
|
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
18
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType; |
|
19
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
20
|
|
|
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension; |
|
21
|
|
|
use Symfony\Component\Form\Forms; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
24
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
25
|
|
|
|
|
26
|
|
|
class ManagementController extends BaseController |
|
27
|
|
|
{ |
|
28
|
|
|
public function indexAction() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->denyAccessUnlessGranted('ROLE_ADMIN'); |
|
31
|
|
|
|
|
32
|
|
|
$tickets = $this->getDataStore()->fetchUndeletedTickets(); |
|
33
|
|
|
|
|
34
|
|
|
$performers = $this->getDataStore()->generatePerformerStats(); |
|
35
|
|
|
|
|
36
|
|
|
$viewParams = $this->defaultViewParams(); |
|
37
|
|
|
|
|
38
|
|
|
$viewParams += [ |
|
39
|
|
|
'tickets' => $tickets, |
|
40
|
|
|
'performers' => $performers |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
return $this->render( |
|
44
|
|
|
'default/manage.html.twig', |
|
45
|
|
|
$viewParams |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function helpAction($section = 'readme') |
|
51
|
|
|
{ |
|
52
|
|
|
$this->denyAccessUnlessGranted('ROLE_ADMIN'); |
|
53
|
|
|
|
|
54
|
|
|
/** @noinspection RealpathInSteamContextInspection */ |
|
55
|
|
|
$rootDir = realpath(__DIR__ . '/../../../../'); |
|
56
|
|
|
$map = [ |
|
57
|
|
|
'readme' => $rootDir . '/README.md', |
|
58
|
|
|
'CONTRIBUTING' => $rootDir . '/docs/CONTRIBUTING.md', |
|
59
|
|
|
'TODO' => $rootDir . '/docs/TODO.md', |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
if (!isset($map[$section])) { |
|
63
|
|
|
throw new NotFoundHttpException(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$markdown = file_get_contents($map[$section]); |
|
67
|
|
|
|
|
68
|
|
|
$markdown = preg_replace( |
|
69
|
|
|
'#\[docs/\w+.md\]\((./)?docs/(\w+).md\)#', |
|
70
|
|
|
'[docs/$2.md](/help/$2)', |
|
71
|
|
|
$markdown |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
return $this->render( |
|
75
|
|
|
':default:help.html.twig', |
|
76
|
|
|
['helpText' => $markdown] |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function settingsAction(Request $request) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->denyAccessUnlessGranted('ROLE_ADMIN'); |
|
83
|
|
|
|
|
84
|
|
|
$requiredResetText = 'THIS DELETES ALL TICKETS'; |
|
85
|
|
|
|
|
86
|
|
|
$settingKeys = [ |
|
87
|
|
|
'freeze' => false, |
|
88
|
|
|
'freezeMessage' => '', |
|
89
|
|
|
'remotesUrl' => '/', |
|
90
|
|
|
'upcomingCount' => 3, |
|
91
|
|
|
'songInPreview' => false, |
|
92
|
|
|
'selfSubmission' => false, |
|
93
|
|
|
'selfSubmissionKey' => '' |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
|
|
$formDefaults = $settingKeys; |
|
97
|
|
|
|
|
98
|
|
|
$dataStore = $this->getDataStore(); |
|
99
|
|
|
|
|
100
|
|
|
foreach ($settingKeys as $key => $default) { |
|
101
|
|
|
$value = $dataStore->fetchSetting($key); |
|
102
|
|
|
if (is_null($value)) { |
|
103
|
|
|
$value = $default; |
|
104
|
|
|
} else { |
|
105
|
|
|
switch (gettype($default)) { |
|
106
|
|
|
case 'integer': |
|
107
|
|
|
$value = (int)$value; |
|
108
|
|
|
break; |
|
109
|
|
|
case 'boolean': |
|
110
|
|
|
$value = (bool)$value; |
|
111
|
|
|
break; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
$formDefaults[$key] = $value; // fixme handle type better |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$formFactory = Forms::createFormFactoryBuilder() |
|
118
|
|
|
->addExtension(new HttpFoundationExtension()) |
|
119
|
|
|
->getFormFactory(); |
|
120
|
|
|
|
|
121
|
|
|
$settingsSubmit = 'Save Settings'; |
|
122
|
|
|
$settingsForm = $formFactory->createNamedBuilder('settingsForm', FormType::class, $formDefaults) |
|
123
|
|
|
->add( |
|
124
|
|
|
'freeze', |
|
125
|
|
|
CheckboxType::class, |
|
126
|
|
|
['label' => 'Display "Queue Frozen" message', 'required' => false] |
|
127
|
|
|
) |
|
128
|
|
|
->add( |
|
129
|
|
|
'freezeMessage', |
|
130
|
|
|
TextType::class, |
|
131
|
|
|
['label' => 'Customise "Queue Frozen" message', 'required' => false] |
|
132
|
|
|
) |
|
133
|
|
|
->add( |
|
134
|
|
|
'remotesUrl', |
|
135
|
|
|
TextType::class, |
|
136
|
|
|
['label' => 'URL to display on remote screens', 'required' => false] |
|
137
|
|
|
) |
|
138
|
|
|
->add( |
|
139
|
|
|
'upcomingCount', |
|
140
|
|
|
NumberType::class, |
|
141
|
|
|
['label' => 'Upcoming songs to display', 'required' => false] |
|
142
|
|
|
) |
|
143
|
|
|
->add( |
|
144
|
|
|
'songInPreview', |
|
145
|
|
|
CheckboxType::class, |
|
146
|
|
|
['label' => 'Display song titles on public queue', 'required' => false] |
|
147
|
|
|
) |
|
148
|
|
|
->add( |
|
149
|
|
|
'selfSubmission', |
|
150
|
|
|
CheckboxType::class, |
|
151
|
|
|
['label' => 'Enable self-submission', 'required' => false] |
|
152
|
|
|
)->add( |
|
153
|
|
|
'selfSubmissionKey', |
|
154
|
|
|
TextType::class, |
|
155
|
|
|
['label' => 'Code required for self-submission (if any)', 'required' => false] |
|
156
|
|
|
) |
|
157
|
|
|
->add($settingsSubmit, SubmitType::class) |
|
158
|
|
|
->getForm(); |
|
159
|
|
|
|
|
160
|
|
|
$settingsFormSaved = false; |
|
161
|
|
|
|
|
162
|
|
|
if ($request->request->has('settingsForm')) { |
|
163
|
|
|
$settingsForm->handleRequest($request); |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @noinspection PhpUndefinedMethodInspection |
|
167
|
|
|
*/ // isClicked on Submit |
|
168
|
|
|
if ($settingsForm->isSubmitted() |
|
169
|
|
|
&& $settingsForm->isValid() |
|
170
|
|
|
&& $settingsForm->get($settingsSubmit)->isClicked() |
|
171
|
|
|
) { |
|
172
|
|
|
$data = $settingsForm->getData(); |
|
173
|
|
|
foreach ($data as $key => $default) { |
|
174
|
|
|
$dataStore->updateSetting($key, $default); |
|
175
|
|
|
} |
|
176
|
|
|
$settingsFormSaved = true; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
// ---------------- |
|
181
|
|
|
|
|
182
|
|
|
$resetSubmit = 'Reset all'; |
|
183
|
|
|
$resetForm = $formFactory->createNamedBuilder('resetForm', FormType::class) |
|
184
|
|
|
->add('resetMessage', TextType::class) |
|
185
|
|
|
->add($resetSubmit, SubmitType::class) |
|
186
|
|
|
->getForm(); |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
$resetFormSaved = false; |
|
190
|
|
|
|
|
191
|
|
|
if ($request->request->has('resetForm')) { |
|
192
|
|
|
$resetForm->handleRequest($request); |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @noinspection PhpUndefinedMethodInspection |
|
196
|
|
|
*/ // isClicked on Submit |
|
197
|
|
|
if ($resetForm->isSubmitted() |
|
198
|
|
|
&& $resetForm->isValid() |
|
199
|
|
|
&& $resetForm->get($resetSubmit)->isClicked() |
|
200
|
|
|
) { |
|
201
|
|
|
$data = $resetForm->getData(); |
|
202
|
|
|
if (trim($data['resetMessage']) === $requiredResetText) { |
|
203
|
|
|
$dataStore->resetAllSessionData(); |
|
204
|
|
|
$resetFormSaved = true; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// ------------------- |
|
210
|
|
|
|
|
211
|
|
|
$rowMapperManager = $this->container->get('songloader.rowmappermanager'); |
|
212
|
|
|
/** @var SongLoader\RowMapperManager $rowMapperManager */ |
|
213
|
|
|
$mappers = $rowMapperManager->getRowMappers(); |
|
214
|
|
|
$mapperInput = []; |
|
215
|
|
|
foreach ($mappers as $mapper) { |
|
216
|
|
|
$mapperInput[$mapper->getFormatterName()] = $mapper->getShortName(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$songListSubmit = 'Upload song list'; |
|
220
|
|
|
$songListForm = $formFactory->createNamedBuilder('songListForm', FormType::class) |
|
221
|
|
|
->add('rowMapper', ChoiceType::class, ['choices' => $mapperInput, 'label' => 'Input formatter']) |
|
222
|
|
|
->add('songListFile', FileType::class) |
|
223
|
|
|
->add($songListSubmit, SubmitType::class) |
|
224
|
|
|
->getForm(); |
|
225
|
|
|
|
|
226
|
|
|
$songFormSaved = false; |
|
227
|
|
|
$songsLoaded = false; |
|
228
|
|
|
|
|
229
|
|
|
if ($request->request->has('songListForm')) { |
|
230
|
|
|
$songListForm->handleRequest($request); |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @noinspection PhpUndefinedMethodInspection |
|
234
|
|
|
*/ // isClicked on Submit |
|
235
|
|
|
if ($songListForm->isSubmitted() |
|
236
|
|
|
&& $songListForm->isValid() |
|
237
|
|
|
&& $songListForm->get($songListSubmit)->isClicked() |
|
238
|
|
|
) { |
|
239
|
|
|
$data = $songListForm->getData(); |
|
240
|
|
|
|
|
241
|
|
|
$file = $data['songListFile']; |
|
242
|
|
|
/** |
|
243
|
|
|
* @var UploadedFile $file |
|
244
|
|
|
*/ |
|
245
|
|
|
|
|
246
|
|
|
$loader = new SongLoader(); |
|
247
|
|
|
$loader->setRowMapperClass($rowMapperManager->getRowMapperClassByShortName($data['rowMapper'])); |
|
248
|
|
|
$songsLoaded = $loader->run($file->getPathname(), $this->get('database_connection')); |
|
249
|
|
|
|
|
250
|
|
|
$songFormSaved = true; |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
// ------------------- |
|
255
|
|
|
|
|
256
|
|
|
$defaults = ['customCss' => $dataStore->fetchSetting('customCss')]; |
|
257
|
|
|
|
|
258
|
|
|
$stylingSubmit = 'Update styles'; |
|
259
|
|
|
$stylingForm = $formFactory->createNamedBuilder('stylingForm', FormType::class, $defaults) |
|
260
|
|
|
->add('backgroundImageFile', FileType::class, ['label' => 'New background image', 'required' => false]) |
|
261
|
|
|
->add('removeImageFile', CheckboxType::class, ['label' => 'Remove background image', 'required' => false]) |
|
262
|
|
|
->add( |
|
263
|
|
|
'customCss', |
|
264
|
|
|
TextareaType::class, |
|
265
|
|
|
['attr' => ['rows' => 8, 'cols' => 60, 'label' => 'Custom CSS'], 'required' => false] |
|
266
|
|
|
) |
|
267
|
|
|
->add($stylingSubmit, SubmitType::class) |
|
268
|
|
|
->getForm(); |
|
269
|
|
|
|
|
270
|
|
|
$styleFormSaved = false; |
|
271
|
|
|
$backgroundUpdated = false; |
|
272
|
|
|
|
|
273
|
|
|
if ($request->request->has('stylingForm')) { |
|
274
|
|
|
$stylingForm->handleRequest($request); |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @noinspection PhpUndefinedMethodInspection |
|
278
|
|
|
*/ // isClicked on Submit |
|
279
|
|
|
if ($stylingForm->isSubmitted() |
|
280
|
|
|
&& $stylingForm->isValid() |
|
281
|
|
|
&& $stylingForm->get($stylingSubmit)->isClicked() |
|
282
|
|
|
) { |
|
283
|
|
|
$data = $stylingForm->getData(); |
|
284
|
|
|
|
|
285
|
|
|
if ($data['removeImageFile']) { |
|
286
|
|
|
$this->deleteBackgroundImageFiles(); |
|
287
|
|
|
$dataStore->updateSetting('backgroundFilename', null); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
$file = $data['backgroundImageFile']; |
|
291
|
|
|
if ($file) { |
|
292
|
|
|
$mimeType = $file->getMimeType(); |
|
293
|
|
|
$pathName = $file->getPathName(); |
|
294
|
|
|
/** |
|
295
|
|
|
* @var UploadedFile $file |
|
296
|
|
|
*/ |
|
297
|
|
|
$suffixByMimeType = [ |
|
298
|
|
|
'image/jpeg' => 'jpg', |
|
299
|
|
|
'image/gif' => 'gif', |
|
300
|
|
|
'image/png' => 'png', |
|
301
|
|
|
]; |
|
302
|
|
|
|
|
303
|
|
|
if (array_key_exists($mimeType, $suffixByMimeType)) { |
|
304
|
|
|
$suffix = $suffixByMimeType[$mimeType]; |
|
305
|
|
|
$targetFile = 'background.' . $suffix; |
|
306
|
|
|
$destination = $this->getBackgroundImageDir() . $targetFile; |
|
307
|
|
|
move_uploaded_file($pathName, $destination); |
|
308
|
|
|
$dataStore->updateSetting('backgroundFilename', $targetFile); |
|
309
|
|
|
} else { |
|
310
|
|
|
throw new \UnexpectedValueException("Invalid mimetype '$mimeType'"); |
|
311
|
|
|
} |
|
312
|
|
|
$backgroundUpdated = true; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$dataStore->updateSetting('customCss', $data['customCss']); |
|
316
|
|
|
|
|
317
|
|
|
$styleFormSaved = true; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
// ------------------- |
|
322
|
|
|
|
|
323
|
|
|
return $this->render( |
|
324
|
|
|
':admin:settings.html.twig', |
|
325
|
|
|
[ |
|
326
|
|
|
'settingsFormSaved' => $settingsFormSaved, |
|
327
|
|
|
'settingsForm' => $settingsForm->createView(), |
|
328
|
|
|
'resetForm' => $resetForm->createView(), |
|
329
|
|
|
'resetFormSaved' => $resetFormSaved, |
|
330
|
|
|
'resetRequiredText' => $requiredResetText, |
|
331
|
|
|
'songListForm' => $songListForm->createView(), |
|
332
|
|
|
'songFormSaved' => $songFormSaved, |
|
333
|
|
|
'songsLoaded' => $songsLoaded, |
|
334
|
|
|
'stylingForm' => $stylingForm->createView(), |
|
335
|
|
|
'styleFormSaved' => $styleFormSaved, |
|
336
|
|
|
'backgroundUpdated' => $backgroundUpdated, |
|
337
|
|
|
] |
|
338
|
|
|
); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* @return string |
|
343
|
|
|
*/ |
|
344
|
|
|
protected function getBackgroundImageDir() |
|
345
|
|
|
{ |
|
346
|
|
|
return dirname($this->get('kernel')->getRootDir()) . '/web/uploads/'; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
protected function deleteBackgroundImageFiles() |
|
350
|
|
|
{ |
|
351
|
|
|
$files = glob($this->getBackgroundImageDir() . 'background.*'); |
|
352
|
|
|
foreach ($files as $file) { |
|
353
|
|
|
unlink($file); |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|