1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use PlaygroundGame\Service\Game as AdminGameService; |
6
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
7
|
|
|
use Zend\View\Model\ViewModel; |
8
|
|
|
use PlaygroundGame\Options\ModuleOptions; |
9
|
|
|
use Zend\Paginator\Paginator; |
10
|
|
|
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter; |
11
|
|
|
use PlaygroundCore\ORM\Pagination\LargeTablePaginator; |
12
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator; |
13
|
|
|
use Zend\Stdlib\ErrorHandler; |
14
|
|
|
|
15
|
|
|
class GameController extends AbstractActionController |
16
|
|
|
{ |
17
|
|
|
protected $options; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \PlaygroundGame\Service\Game |
21
|
|
|
*/ |
22
|
|
|
protected $adminGameService; |
23
|
|
|
|
24
|
|
|
protected $game; |
25
|
|
|
|
26
|
|
|
public function checkGame() |
27
|
|
|
{ |
28
|
|
|
$gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
29
|
|
|
if (!$gameId) { |
30
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
31
|
|
|
} |
32
|
|
|
$service = $this->getAdminGameService(); |
33
|
|
|
$game = $service->getGameMapper()->findById($gameId); |
34
|
|
|
if (!$game) { |
35
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
36
|
|
|
} |
37
|
|
|
$this->game = $game; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function createForm($form) |
41
|
|
|
{ |
42
|
|
|
// I use the wonderful Form Generator to create the Post & Vote form |
43
|
|
|
$this->forward()->dispatch( |
44
|
|
|
'PlaygroundCore\Controller\Formgen', |
45
|
|
|
array( |
46
|
|
|
'controller' => 'PlaygroundCore\Controller\Formgen', |
47
|
|
|
'action' => 'create' |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
52
|
|
|
$data = $this->getRequest()->getPost()->toArray(); |
|
|
|
|
53
|
|
|
$form = $service->createForm($data, $game, $form); |
|
|
|
|
54
|
|
|
if ($form) { |
55
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The form was created'); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
$formTemplate=''; |
59
|
|
|
if ($form) { |
60
|
|
|
$formTemplate = $form->getFormTemplate(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return array( |
64
|
|
|
'form' => $form, |
65
|
|
|
'formTemplate' => $formTemplate, |
66
|
|
|
'gameId' => $this->game->getId(), |
67
|
|
|
'game' => $this->game, |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function editGame($templatePath, $formId) |
72
|
|
|
{ |
73
|
|
|
|
74
|
|
|
$viewModel = new ViewModel(); |
75
|
|
|
$viewModel->setTemplate($templatePath); |
76
|
|
|
|
77
|
|
|
$gameForm = new ViewModel(); |
78
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
79
|
|
|
|
80
|
|
|
$form = $this->getServiceLocator()->get($formId); |
81
|
|
|
$form->setAttribute( |
82
|
|
|
'action', |
83
|
|
|
$this->url()->fromRoute( |
84
|
|
|
'admin/playgroundgame/edit-' . $this->game->getClassType(), |
85
|
|
|
array('gameId' => $this->game->getId()) |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
$form->setAttribute('method', 'post'); |
89
|
|
|
|
90
|
|
|
if ($this->game->getFbAppId()) { |
91
|
|
|
$appIds = $form->get('fbAppId')->getOption('value_options'); |
92
|
|
|
$appIds[$this->game->getFbAppId()] = $this->game->getFbAppId(); |
93
|
|
|
$form->get('fbAppId')->setAttribute('options', $appIds); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$gameOptions = $this->getAdminGameService()->getOptions(); |
97
|
|
|
$gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $this->game->getId(). '.css'; |
98
|
|
|
if (is_file($gameStylesheet)) { |
99
|
|
|
$values = $form->get('stylesheet')->getValueOptions(); |
100
|
|
|
$values[$gameStylesheet] = 'Style personnalisé de ce jeu'; |
101
|
|
|
|
102
|
|
|
$form->get('stylesheet')->setAttribute('options', $values); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$form->bind($this->game); |
106
|
|
|
|
107
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
108
|
|
|
$data = array_replace_recursive( |
109
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
110
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
111
|
|
|
); |
112
|
|
|
if (empty($data['prizes'])) { |
113
|
|
|
$data['prizes'] = array(); |
114
|
|
|
} |
115
|
|
|
if (isset($data['drawDate']) && $data['drawDate']) { |
116
|
|
|
$data['drawDate'] = \DateTime::createFromFormat('d/m/Y', $data['drawDate']); |
117
|
|
|
} |
118
|
|
|
$result = $this->getAdminGameService()->edit($data, $this->game, $formId); |
119
|
|
|
|
120
|
|
|
if ($result) { |
121
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $this->game)); |
126
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
127
|
|
|
|
128
|
|
|
return $viewModel->setVariables( |
129
|
|
|
array( |
130
|
|
|
'form' => $form, |
131
|
|
|
'title' => 'Edit this game', |
132
|
|
|
) |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function listAction() |
138
|
|
|
{ |
139
|
|
|
$filter = $this->getEvent()->getRouteMatch()->getParam('filter'); |
140
|
|
|
$type = $this->getEvent()->getRouteMatch()->getParam('type'); |
141
|
|
|
|
142
|
|
|
$service = $this->getAdminGameService(); |
143
|
|
|
$adapter = new DoctrineAdapter(new ORMPaginator($service->getQueryGamesOrderBy($type, $filter))); |
144
|
|
|
$paginator = new Paginator($adapter); |
145
|
|
|
$paginator->setItemCountPerPage(25); |
146
|
|
|
$paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
147
|
|
|
|
148
|
|
|
foreach ($paginator as $game) { |
149
|
|
|
$game->entry = $service->getEntryMapper()->countByGame($game); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return array( |
153
|
|
|
'games' => $paginator, |
154
|
|
|
'type' => $type, |
155
|
|
|
'filter' => $filter, |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function entryAction() |
160
|
|
|
{ |
161
|
|
|
$this->checkGame(); |
162
|
|
|
|
163
|
|
|
$adapter = new DoctrineAdapter( |
164
|
|
|
new LargeTablePaginator( |
165
|
|
|
$this->getAdminGameService()->getEntriesQuery($this->game) |
166
|
|
|
) |
167
|
|
|
); |
168
|
|
|
$paginator = new Paginator($adapter); |
169
|
|
|
$paginator->setItemCountPerPage(10); |
170
|
|
|
$paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
171
|
|
|
|
172
|
|
|
$header = $this->getAdminGameService()->getEntriesHeader($this->game); |
173
|
|
|
$entries = $this->getAdminGameService()->getGameEntries($header, $paginator, $this->game); |
174
|
|
|
|
175
|
|
|
return array( |
176
|
|
|
'paginator' => $paginator, |
177
|
|
|
'entries' => $entries, |
178
|
|
|
'header' => $header, |
179
|
|
|
'game' => $this->game, |
180
|
|
|
'gameId' => $this->game->getId() |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function downloadAction() |
185
|
|
|
{ |
186
|
|
|
$this->checkGame(); |
187
|
|
|
$header = $this->getAdminGameService()->getEntriesHeader($this->game); |
188
|
|
|
$query = $this->getAdminGameService()->getEntriesQuery($this->game); |
189
|
|
|
|
190
|
|
|
$content = "\xEF\xBB\xBF"; // UTF-8 BOM |
191
|
|
|
$content .= $this->getAdminGameService()->getCSV( |
192
|
|
|
$this->getAdminGameService()->getGameEntries( |
193
|
|
|
$header, |
194
|
|
|
$query->getResult(), |
195
|
|
|
$this->game |
196
|
|
|
) |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
$response = $this->getResponse(); |
200
|
|
|
$headers = $response->getHeaders(); |
201
|
|
|
$headers->addHeaderLine('Content-Encoding: UTF-8'); |
202
|
|
|
$headers->addHeaderLine('Content-Type', 'text/csv; charset=UTF-8'); |
203
|
|
|
$headers->addHeaderLine('Content-Disposition', "attachment; filename=\"entry.csv\""); |
204
|
|
|
$headers->addHeaderLine('Accept-Ranges', 'bytes'); |
205
|
|
|
$headers->addHeaderLine('Content-Length', strlen($content)); |
206
|
|
|
|
207
|
|
|
$response->setContent($content); |
208
|
|
|
|
209
|
|
|
return $response; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// Only used for Quiz and Lottery |
213
|
|
|
public function drawAction() |
214
|
|
|
{ |
215
|
|
|
$this->checkGame(); |
216
|
|
|
|
217
|
|
|
$winningEntries = $this->getAdminGameService()->draw($this->game); |
218
|
|
|
|
219
|
|
|
$content = "\xEF\xBB\xBF"; // UTF-8 BOM |
220
|
|
|
$content .= "ID;Pseudo;Nom;Prenom;E-mail;Etat\n"; |
221
|
|
|
|
222
|
|
|
foreach ($winningEntries as $e) { |
223
|
|
|
$etat = 'gagnant'; |
224
|
|
|
|
225
|
|
|
$content .= $e->getUser()->getId() |
226
|
|
|
. ";" . $e->getUser()->getUsername() |
227
|
|
|
. ";" . $e->getUser()->getLastname() |
228
|
|
|
. ";" . $e->getUser()->getFirstname() |
229
|
|
|
. ";" . $e->getUser()->getEmail() |
230
|
|
|
. ";" . $etat |
231
|
|
|
."\n"; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$response = $this->getResponse(); |
235
|
|
|
$headers = $response->getHeaders(); |
236
|
|
|
$headers->addHeaderLine('Content-Encoding: UTF-8'); |
237
|
|
|
$headers->addHeaderLine('Content-Type', 'text/csv; charset=UTF-8'); |
238
|
|
|
$headers->addHeaderLine('Content-Disposition', "attachment; filename=\"gagnants.csv\""); |
239
|
|
|
$headers->addHeaderLine('Accept-Ranges', 'bytes'); |
240
|
|
|
$headers->addHeaderLine('Content-Length', strlen($content)); |
241
|
|
|
|
242
|
|
|
$response->setContent($content); |
243
|
|
|
|
244
|
|
|
return $response; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* This method serialize a game an export it as a txt file |
249
|
|
|
* @return \Zend\Stdlib\ResponseInterface |
250
|
|
|
*/ |
251
|
|
|
public function exportAction() |
252
|
|
|
{ |
253
|
|
|
$this->checkGame(); |
254
|
|
|
$content = serialize($this->game); |
255
|
|
|
|
256
|
|
|
$response = $this->getResponse(); |
257
|
|
|
$headers = $response->getHeaders(); |
258
|
|
|
$headers->addHeaderLine('Content-Encoding: UTF-8'); |
259
|
|
|
$headers->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8'); |
260
|
|
|
$headers->addHeaderLine( |
261
|
|
|
'Content-Disposition', |
262
|
|
|
"attachment; filename=\"". $this->game->getIdentifier() .".txt\"" |
263
|
|
|
); |
264
|
|
|
$headers->addHeaderLine('Accept-Ranges', 'bytes'); |
265
|
|
|
$headers->addHeaderLine('Content-Length', strlen($content)); |
266
|
|
|
|
267
|
|
|
$response->setContent($content); |
268
|
|
|
|
269
|
|
|
return $response; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* This method take an uploaded txt file containing a serialized game |
274
|
|
|
* and persist it in the database |
275
|
|
|
* @return unknown |
276
|
|
|
*/ |
277
|
|
|
public function importAction() |
278
|
|
|
{ |
279
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_import_form'); |
280
|
|
|
$form->setAttribute('action', $this->url()->fromRoute('admin/playgroundgame/import')); |
281
|
|
|
$form->setAttribute('method', 'post'); |
282
|
|
|
|
283
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
284
|
|
|
$data = array_replace_recursive( |
285
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
286
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
287
|
|
|
); |
288
|
|
|
|
289
|
|
|
if (! empty($data['import_file']['tmp_name'])) { |
290
|
|
|
ErrorHandler::start(); |
291
|
|
|
$game = unserialize(file_get_contents($data['import_file']['tmp_name'])); |
292
|
|
|
$game->setId(null); |
293
|
|
|
if ($data['slug']) { |
294
|
|
|
$game->setIdentifier($data['slug']); |
295
|
|
|
} |
296
|
|
|
$duplicate = $this->getAdminGameService()->getGameMapper()->findByIdentifier($game->getIdentifier()); |
297
|
|
|
if (!$duplicate) { |
298
|
|
|
$this->getAdminGameService()->getGameMapper()->insert($game); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
ErrorHandler::stop(true); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return array( |
308
|
|
|
'form' => $form, |
309
|
|
|
); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function removeAction() |
313
|
|
|
{ |
314
|
|
|
$this->checkGame(); |
315
|
|
|
|
316
|
|
|
try { |
317
|
|
|
$this->getAdminGameService()->getGameMapper()->remove($this->game); |
318
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game has been edited'); |
319
|
|
|
} catch (\Doctrine\DBAL\DBALException $e) { |
320
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
321
|
|
|
'Il y a déjà eu des participants à ce jeu. Vous ne pouvez plus le supprimer' |
322
|
|
|
); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function setActiveAction() |
329
|
|
|
{ |
330
|
|
|
$this->checkGame(); |
331
|
|
|
|
332
|
|
|
$this->game->setActive(!$this->game->getActive()); |
333
|
|
|
$this->getAdminGameService()->getGameMapper()->update($game); |
|
|
|
|
334
|
|
|
|
335
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function formAction() |
339
|
|
|
{ |
340
|
|
|
$this->checkGame(); |
341
|
|
|
|
342
|
|
|
$form = $this->game->getPlayerForm(); |
343
|
|
|
|
344
|
|
|
return $this->createForm($form); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
public function setOptions(ModuleOptions $options) |
348
|
|
|
{ |
349
|
|
|
$this->options = $options; |
350
|
|
|
|
351
|
|
|
return $this; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
public function getOptions() |
355
|
|
|
{ |
356
|
|
|
if (!$this->options instanceof ModuleOptions) { |
357
|
|
|
$this->setOptions($this->getServiceLocator()->get('playgroundgame_module_options')); |
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $this->options; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function getAdminGameService() |
364
|
|
|
{ |
365
|
|
|
if (!$this->adminGameService) { |
366
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_game_service'); |
|
|
|
|
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
return $this->adminGameService; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
public function setAdminGameService(AdminGameService $adminGameService) |
373
|
|
|
{ |
374
|
|
|
$this->adminGameService = $adminGameService; |
375
|
|
|
|
376
|
|
|
return $this; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: