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\FileType; |
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
17
|
|
|
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension; |
18
|
|
|
use Symfony\Component\Form\Forms; |
19
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
22
|
|
|
|
23
|
|
|
class ManagementController extends BaseController |
24
|
|
|
{ |
25
|
|
|
public function indexAction() |
26
|
|
|
{ |
27
|
|
|
$this->denyAccessUnlessGranted('ROLE_ADMIN'); |
28
|
|
|
|
29
|
|
|
$tickets = $this->getDataStore()->fetchUndeletedTickets(); |
30
|
|
|
|
31
|
|
|
$performers = $this->getDataStore()->generatePerformerStats(); |
32
|
|
|
|
33
|
|
|
return $this->render( |
34
|
|
|
'default/manage.html.twig', |
35
|
|
|
[ |
36
|
|
|
'tickets' => $tickets, |
37
|
|
|
'performers' => $performers, |
38
|
|
|
'displayOptions' => $this->getDisplayOptions(), |
39
|
|
|
] |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
View Code Duplication |
public function helpAction($section = 'readme') |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$this->denyAccessUnlessGranted('ROLE_ADMIN'); |
47
|
|
|
|
48
|
|
|
$rootDir = realpath(__DIR__ . '/../../../../'); |
49
|
|
|
$map = [ |
50
|
|
|
'readme' => $rootDir . '/README.md', |
51
|
|
|
'CONTRIBUTING' => $rootDir . '/docs/CONTRIBUTING.md', |
52
|
|
|
'TODO' => $rootDir . '/docs/TODO.md', |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
if (!isset($map[$section])) { |
56
|
|
|
throw new NotFoundHttpException(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$markdown = file_get_contents($map[$section]); |
60
|
|
|
|
61
|
|
|
$markdown = preg_replace( |
62
|
|
|
'#\[docs/\w+.md\]\((./)?docs/(\w+).md\)#', |
63
|
|
|
'[docs/$2.md](/help/$2)', |
64
|
|
|
$markdown |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return $this->render( |
68
|
|
|
':default:help.html.twig', |
69
|
|
|
['helpText' => $markdown] |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function settingsAction(Request $request) |
74
|
|
|
{ |
75
|
|
|
$this->denyAccessUnlessGranted('ROLE_ADMIN'); |
76
|
|
|
|
77
|
|
|
$requiredResetText = 'THIS DELETES ALL TICKETS'; |
78
|
|
|
|
79
|
|
|
$settingKeys = [ |
80
|
|
|
'freeze' => false, |
81
|
|
|
'freezeMessage' => '', |
82
|
|
|
'remotesUrl' => '/' |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$formDefaults = $settingKeys; |
86
|
|
|
|
87
|
|
|
foreach ($settingKeys as $k => $v) { |
88
|
|
|
$value = $this->getDataStore()->getSetting($k); |
89
|
|
|
if (!is_null($value)) { |
90
|
|
|
$formDefaults[$k] = is_bool($v) ? (bool)$value : $value; // fixme handle type better |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$formFactory = Forms::createFormFactoryBuilder() |
95
|
|
|
->addExtension(new HttpFoundationExtension()) |
96
|
|
|
->getFormFactory(); |
97
|
|
|
|
98
|
|
|
$settingsSubmit = 'Save Settings'; |
99
|
|
|
$settingsForm = $formFactory->createNamedBuilder('settingsForm', FormType::class, $formDefaults) |
100
|
|
|
->add('freeze', CheckboxType::class, ['label' => 'Display "Queue Frozen" message']) |
101
|
|
|
->add('freezeMessage', TextType::class, ['label' => 'Customise "Queue Frozen" message']) |
102
|
|
|
->add('remotesUrl', TextType::class, ['label' => 'URL to display on remote screens']) |
103
|
|
|
->add($settingsSubmit, SubmitType::class) |
104
|
|
|
->getForm(); |
105
|
|
|
|
106
|
|
|
$settingsFormSaved = false; |
107
|
|
|
|
108
|
|
|
if ($request->request->has('settingsForm')) { |
109
|
|
|
$settingsForm->handleRequest($request); |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @noinspection PhpUndefinedMethodInspection |
113
|
|
|
*/ // isClicked on Submit |
114
|
|
|
if ($settingsForm->isSubmitted() |
115
|
|
|
&& $settingsForm->isValid() |
116
|
|
|
&& $settingsForm->get($settingsSubmit)->isClicked() |
117
|
|
|
) { |
118
|
|
|
$data = $settingsForm->getData(); |
119
|
|
|
foreach ($data as $k => $v) { |
120
|
|
|
$this->getDataStore()->updateSetting($k, $v); |
121
|
|
|
} |
122
|
|
|
$settingsFormSaved = true; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// ---------------- |
127
|
|
|
|
128
|
|
|
$resetSubmit = 'Reset all'; |
129
|
|
|
$resetForm = $formFactory->createNamedBuilder('resetForm', FormType::class, $formDefaults) |
130
|
|
|
->add('resetMessage', TextType::class) |
131
|
|
|
->add($resetSubmit, SubmitType::class) |
132
|
|
|
->getForm(); |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
$resetFormSaved = false; |
136
|
|
|
|
137
|
|
|
if ($request->request->has('resetForm')) { |
138
|
|
|
$resetForm->handleRequest($request); |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @noinspection PhpUndefinedMethodInspection |
142
|
|
|
*/ // isClicked on Submit |
143
|
|
|
if ($resetForm->isSubmitted() |
144
|
|
|
&& $resetForm->isValid() |
145
|
|
|
&& $resetForm->get($resetSubmit)->isClicked() |
146
|
|
|
) { |
147
|
|
|
$data = $resetForm->getData(); |
148
|
|
|
// var_dump($data); |
|
|
|
|
149
|
|
|
// die(); |
150
|
|
|
if (trim($data['resetMessage']) === $requiredResetText) { |
151
|
|
|
$this->getDataStore()->resetAllSessionData(); |
152
|
|
|
$resetFormSaved = true; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// ------------------- |
158
|
|
|
|
159
|
|
|
$songListSubmit = 'Upload song list'; |
160
|
|
|
$songListForm = $formFactory->createNamedBuilder('songListForm', FormType::class, $formDefaults) |
161
|
|
|
->add('songListFile', FileType::class) |
162
|
|
|
->add($songListSubmit, SubmitType::class) |
163
|
|
|
->getForm(); |
164
|
|
|
|
165
|
|
|
$songFormSaved = false; |
166
|
|
|
$songsLoaded = false; |
167
|
|
|
|
168
|
|
|
if ($request->request->has('songListForm')) { |
169
|
|
|
$songListForm->handleRequest($request); |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @noinspection PhpUndefinedMethodInspection |
173
|
|
|
*/ // isClicked on Submit |
174
|
|
|
if ($songListForm->isSubmitted() |
175
|
|
|
&& $songListForm->isValid() |
176
|
|
|
&& $songListForm->get($songListSubmit)->isClicked() |
177
|
|
|
) { |
178
|
|
|
$data = $songListForm->getData(); |
179
|
|
|
|
180
|
|
|
$file = $data['songListFile']; |
181
|
|
|
/** |
182
|
|
|
* @var UploadedFile $file |
183
|
|
|
*/ |
184
|
|
|
|
185
|
|
|
$loader = new SongLoader(); |
186
|
|
|
|
187
|
|
|
$songsLoaded = $loader->run($file->getPathname(), $this->get('database_connection')); |
188
|
|
|
|
189
|
|
|
$songFormSaved = true; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// ------------------- |
194
|
|
|
|
195
|
|
|
return $this->render( |
196
|
|
|
':admin:settings.html.twig', |
197
|
|
|
[ |
198
|
|
|
'settingsFormSaved' => $settingsFormSaved, |
199
|
|
|
'settingsForm' => $settingsForm->createView(), |
200
|
|
|
'resetForm' => $resetForm->createView(), |
201
|
|
|
'resetFormSaved' => $resetFormSaved, |
202
|
|
|
'resetRequiredText' => $requiredResetText, |
203
|
|
|
'songListForm' => $songListForm->createView(), |
204
|
|
|
'songFormSaved' => $songFormSaved, |
205
|
|
|
'songsLoaded' => $songsLoaded, |
206
|
|
|
] |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.