1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use PlaygroundGame\Entity\InstantWin; |
6
|
|
|
use PlaygroundGame\Entity\InstantWinOccurrence; |
7
|
|
|
use Zend\InputFilter; |
8
|
|
|
use Zend\Validator; |
9
|
|
|
use PlaygroundGame\Controller\Admin\GameController; |
10
|
|
|
use Zend\View\Model\ViewModel; |
11
|
|
|
use Zend\Paginator\Paginator; |
12
|
|
|
use PlaygroundCore\ORM\Pagination\LargeTablePaginator; |
13
|
|
|
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter; |
14
|
|
|
|
15
|
|
|
class InstantWinController extends GameController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var GameService |
19
|
|
|
*/ |
20
|
|
|
protected $adminGameService; |
21
|
|
|
|
22
|
|
|
public function removeAction() |
23
|
|
|
{ |
24
|
|
|
$service = $this->getAdminGameService(); |
25
|
|
|
$gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
26
|
|
|
if (!$gameId) { |
27
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$game = $service->getGameMapper()->findById($gameId); |
31
|
|
|
$service->getGameMapper()->remove($game); |
32
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game has been removed'); |
33
|
|
|
|
34
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function createInstantWinAction() |
38
|
|
|
{ |
39
|
|
|
$service = $this->getAdminGameService(); |
40
|
|
|
$viewModel = new ViewModel(); |
41
|
|
|
$viewModel->setTemplate('playground-game/instant-win/instantwin'); |
42
|
|
|
|
43
|
|
|
$gameForm = new ViewModel(); |
44
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
45
|
|
|
|
46
|
|
|
$instantwin = new InstantWin(); |
47
|
|
|
|
48
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwin_form'); |
49
|
|
|
$form->bind($instantwin); |
50
|
|
|
$form->setAttribute( |
51
|
|
|
'action', |
52
|
|
|
$this->url()->fromRoute('admin/playgroundgame/create-instantwin', array('gameId' => 0)) |
53
|
|
|
); |
54
|
|
|
$form->setAttribute('method', 'post'); |
55
|
|
|
|
56
|
|
|
$request = $this->getRequest(); |
57
|
|
|
if ($request->isPost()) { |
58
|
|
|
$data = array_replace_recursive( |
59
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
60
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
61
|
|
|
); |
62
|
|
|
if (empty($data['prizes'])) { |
63
|
|
|
$data['prizes'] = array(); |
64
|
|
|
} |
65
|
|
|
$game = $service->create($data, $instantwin, 'playgroundgame_instantwin_form'); |
66
|
|
|
if ($game) { |
67
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created'); |
68
|
|
|
|
69
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $instantwin)); |
73
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
74
|
|
|
|
75
|
|
|
return $viewModel->setVariables( |
76
|
|
|
array( |
77
|
|
|
'form' => $form, |
78
|
|
|
'title' => 'Create instant win', |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function editInstantWinAction() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$service = $this->getAdminGameService(); |
86
|
|
|
$gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
87
|
|
|
|
88
|
|
|
if (!$gameId) { |
89
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/create-instantwin'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$game = $service->getGameMapper()->findById($gameId); |
93
|
|
|
$viewModel = new ViewModel(); |
94
|
|
|
$viewModel->setTemplate('playground-game/instant-win/instantwin'); |
95
|
|
|
|
96
|
|
|
$gameForm = new ViewModel(); |
97
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
98
|
|
|
|
99
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwin_form'); |
100
|
|
|
$form->setAttribute( |
101
|
|
|
'action', |
102
|
|
|
$this->url()->fromRoute( |
103
|
|
|
'admin/playgroundgame/edit-instantwin', |
104
|
|
|
array('gameId' => $gameId) |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
$form->setAttribute('method', 'post'); |
108
|
|
|
|
109
|
|
|
if ($game->getFbAppId()) { |
110
|
|
|
$appIds = $form->get('fbAppId')->getOption('value_options'); |
111
|
|
|
$appIds[$game->getFbAppId()] = $game->getFbAppId(); |
112
|
|
|
$form->get('fbAppId')->setAttribute('options', $appIds); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$gameOptions = $this->getAdminGameService()->getOptions(); |
116
|
|
|
$gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $game->getId(). '.css'; |
117
|
|
|
if (is_file($gameStylesheet)) { |
118
|
|
|
$values = $form->get('stylesheet')->getValueOptions(); |
119
|
|
|
$values[$gameStylesheet] = 'Style personnalisé de ce jeu'; |
120
|
|
|
|
121
|
|
|
$form->get('stylesheet')->setAttribute('options', $values); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$form->bind($game); |
125
|
|
|
|
126
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
127
|
|
|
$data = array_replace_recursive( |
128
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
129
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
130
|
|
|
); |
131
|
|
|
if (empty($data['prizes'])) { |
132
|
|
|
$data['prizes'] = array(); |
133
|
|
|
} |
134
|
|
|
$result = $service->edit($data, $game, 'playgroundgame_instantwin_form'); |
135
|
|
|
|
136
|
|
|
if ($result) { |
137
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $game)); |
142
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
143
|
|
|
|
144
|
|
|
return $viewModel->setVariables( |
145
|
|
|
array( |
146
|
|
|
'form' => $form, |
147
|
|
|
'title' => 'Edit instant win', |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function listOccurrenceAction() |
153
|
|
|
{ |
154
|
|
|
$service = $this->getAdminGameService(); |
155
|
|
|
$gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
156
|
|
|
|
157
|
|
|
if (!$gameId) { |
158
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$game = $service->getGameMapper()->findById($gameId); |
162
|
|
|
|
163
|
|
|
$adapter = new DoctrineAdapter( |
164
|
|
|
new LargeTablePaginator( |
165
|
|
|
$service->getInstantWinOccurrenceMapper()->queryByGame($game) |
166
|
|
|
) |
167
|
|
|
); |
168
|
|
|
$paginator = new Paginator($adapter); |
169
|
|
|
$paginator->setItemCountPerPage(25); |
170
|
|
|
$paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
171
|
|
|
|
172
|
|
|
return new ViewModel( |
173
|
|
|
array( |
174
|
|
|
'occurrences' => $paginator, |
175
|
|
|
'gameId' => $gameId, |
176
|
|
|
'game' => $game, |
177
|
|
|
) |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function addOccurrenceAction() |
182
|
|
|
{ |
183
|
|
|
$viewModel = new ViewModel(); |
184
|
|
|
$viewModel->setTemplate('playground-game/instant-win/occurrence'); |
185
|
|
|
$service = $this->getAdminGameService(); |
186
|
|
|
$gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
187
|
|
|
if (!$gameId) { |
188
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
189
|
|
|
} |
190
|
|
|
$game = $service->getGameMapper()->findById($gameId); |
191
|
|
|
|
192
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form'); |
193
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
194
|
|
|
|
195
|
|
|
$form->setAttribute( |
196
|
|
|
'action', |
197
|
|
|
$this->url()->fromRoute( |
198
|
|
|
'admin/playgroundgame/instantwin-occurrence-add', |
199
|
|
|
array('gameId' => $gameId) |
200
|
|
|
) |
201
|
|
|
); |
202
|
|
|
$form->setAttribute('method', 'post'); |
203
|
|
|
$form->get('instant_win_id')->setAttribute('value', $gameId); |
204
|
|
|
$occurrence = new InstantWinOccurrence(); |
205
|
|
|
$form->bind($occurrence); |
206
|
|
|
|
207
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
208
|
|
|
$data = array_merge( |
209
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
210
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
// Change the format of the date |
214
|
|
|
$value = \DateTime::createFromFormat('d/m/Y H:i', $data['value']); |
215
|
|
|
$data['value'] = $value->format('Y-m-d H:i:s'); |
216
|
|
|
|
217
|
|
|
$occurrence = $service->updateOccurrence($data, $occurrence->getId()); |
218
|
|
View Code Duplication |
if ($occurrence) { |
|
|
|
|
219
|
|
|
// Redirect to list of games |
220
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was created'); |
221
|
|
|
return $this->redirect()->toRoute( |
222
|
|
|
'admin/playgroundgame/instantwin-occurrence-list', |
223
|
|
|
array('gameId'=>$gameId) |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
return $viewModel->setVariables( |
228
|
|
|
array( |
229
|
|
|
'form' => $form, |
230
|
|
|
'game' => $game, |
231
|
|
|
'occurrence_id' => 0, |
232
|
|
|
'title' => 'Add occurrence', |
233
|
|
|
) |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function importOccurrencesAction() |
238
|
|
|
{ |
239
|
|
|
$viewModel = new ViewModel(); |
240
|
|
|
$viewModel->setTemplate('playground-game/instant-win/import-occurrences'); |
241
|
|
|
$gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
242
|
|
|
if (!$gameId) { |
243
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
244
|
|
|
} |
245
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrenceimport_form'); |
246
|
|
|
|
247
|
|
|
$form->get('submit')->setAttribute('label', 'Import'); |
248
|
|
|
$form->setAttribute( |
249
|
|
|
'action', |
250
|
|
|
$this->url()->fromRoute( |
251
|
|
|
'admin/playgroundgame/instantwin-occurrences-import', |
252
|
|
|
array('gameId' => $gameId) |
253
|
|
|
) |
254
|
|
|
); |
255
|
|
|
$form->get('instant_win_id')->setAttribute('value', $gameId); |
256
|
|
|
|
257
|
|
|
// File validator |
258
|
|
|
$inputFilter = new InputFilter\InputFilter(); |
259
|
|
|
$fileFilter = new InputFilter\FileInput('file'); |
260
|
|
|
$validatorChain = new Validator\ValidatorChain(); |
261
|
|
|
$validatorChain->attach(new Validator\File\Exists()); |
262
|
|
|
$validatorChain->attach(new Validator\File\Extension('csv')); |
263
|
|
|
$fileFilter->setValidatorChain($validatorChain); |
264
|
|
|
$fileFilter->setRequired(true); |
265
|
|
|
|
266
|
|
|
$prizeFilter = new InputFilter\Input('prize'); |
267
|
|
|
$prizeFilter->setRequired(false); |
268
|
|
|
|
269
|
|
|
$inputFilter->add($fileFilter); |
270
|
|
|
$inputFilter->add($prizeFilter); |
271
|
|
|
$form->setInputFilter($inputFilter); |
272
|
|
|
|
273
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
274
|
|
|
$data = array_merge_recursive( |
275
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
276
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
277
|
|
|
); |
278
|
|
|
$form->setData($data); |
279
|
|
|
if ($form->isValid()) { |
280
|
|
|
$data = $form->getData(); |
281
|
|
|
$created = $this->getAdminGameService()->importOccurrences($data); |
282
|
|
View Code Duplication |
if ($created) { |
|
|
|
|
283
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
284
|
|
|
$created.' occurrences were created !' |
285
|
|
|
); |
286
|
|
|
return $this->redirect()->toRoute( |
287
|
|
|
'admin/playgroundgame/instantwin-occurrence-list', |
288
|
|
|
array('gameId'=>$gameId) |
289
|
|
|
); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $viewModel->setVariables( |
295
|
|
|
array( |
296
|
|
|
'form' => $form, |
297
|
|
|
'title' => 'Import occurrences', |
298
|
|
|
) |
299
|
|
|
); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function editOccurrenceAction() |
303
|
|
|
{ |
304
|
|
|
$viewModel = new ViewModel(); |
305
|
|
|
$viewModel->setTemplate('playground-game/instant-win/occurrence'); |
306
|
|
|
$service = $this->getAdminGameService(); |
307
|
|
|
|
308
|
|
|
$occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId'); |
309
|
|
|
$gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
310
|
|
|
if (!$gameId) { |
311
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
312
|
|
|
} |
313
|
|
|
$game = $service->getGameMapper()->findById($gameId); |
314
|
|
|
$occurrence = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId); |
315
|
|
|
// Si l'occurrence a été utilisée, on ne peut plus la modifier |
316
|
|
|
if ($occurrence->getUser()) { |
317
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
318
|
|
|
'This occurrence has a winner, you can not update it.' |
319
|
|
|
); |
320
|
|
|
return $this->redirect()->toRoute( |
321
|
|
|
'admin/playgroundgame/instantwin-occurrence-list', |
322
|
|
|
array('gameId'=>$gameId) |
323
|
|
|
); |
324
|
|
|
} |
325
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form'); |
326
|
|
|
$form->remove('occurrences_file'); |
327
|
|
|
|
328
|
|
|
$form->get('submit')->setAttribute('label', 'Edit'); |
329
|
|
|
$form->setAttribute('action', ''); |
330
|
|
|
|
331
|
|
|
$form->get('instant_win_id')->setAttribute('value', $gameId); |
332
|
|
|
|
333
|
|
|
$form->bind($occurrence); |
334
|
|
|
|
335
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
336
|
|
|
$data = array_merge( |
337
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
338
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
339
|
|
|
); |
340
|
|
|
$occurrence = $service->updateOccurrence($data, $occurrence->getId()); |
341
|
|
|
|
342
|
|
View Code Duplication |
if ($occurrence) { |
|
|
|
|
343
|
|
|
// Redirect to list of games |
344
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was edited'); |
345
|
|
|
return $this->redirect()->toRoute( |
346
|
|
|
'admin/playgroundgame/instantwin-occurrence-list', |
347
|
|
|
array('gameId'=>$gameId) |
348
|
|
|
); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
return $viewModel->setVariables( |
352
|
|
|
array( |
353
|
|
|
'form' => $form, |
354
|
|
|
'game' => $game, |
355
|
|
|
'occurrence_id' => $occurrenceId, |
356
|
|
|
'title' => 'Edit occurrence', |
357
|
|
|
) |
358
|
|
|
); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
|
362
|
|
|
public function removeOccurrenceAction() |
363
|
|
|
{ |
364
|
|
|
$service = $this->getAdminGameService(); |
365
|
|
|
$occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId'); |
366
|
|
|
if (!$occurrenceId) { |
367
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
368
|
|
|
} |
369
|
|
|
$occurrence = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId); |
370
|
|
|
$instantwinId = $occurrence->getInstantWin()->getId(); |
371
|
|
|
|
372
|
|
|
if ($occurrence->getActive()) { |
373
|
|
|
$service->getInstantWinOccurrenceMapper()->remove($occurrence); |
374
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was deleted'); |
375
|
|
|
} else { |
376
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
377
|
|
|
'Il y a un participant à cet instant gagnant. Vous ne pouvez plus le supprimer' |
378
|
|
|
); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return $this->redirect()->toRoute( |
382
|
|
|
'admin/playgroundgame/instantwin-occurrence-list', |
383
|
|
|
array('gameId'=>$instantwinId) |
384
|
|
|
); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
public function exportOccurrencesAction() |
388
|
|
|
{ |
389
|
|
|
$gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
390
|
|
|
$game = $this->getAdminGameService()->getGameMapper()->findById($gameId); |
391
|
|
|
$service = $this->getAdminGameService(); |
392
|
|
|
|
393
|
|
|
$file = $service->setOccurencesToCSV($game); |
394
|
|
|
|
395
|
|
|
$response = new \Zend\Http\Response\Stream(); |
396
|
|
|
$response->setStream(fopen($file, 'r')); |
397
|
|
|
$response->setStatusCode(200); |
398
|
|
|
|
399
|
|
|
$headers = new \Zend\Http\Headers(); |
400
|
|
|
$headers->addHeaderLine('Content-Type', 'text/csv') |
401
|
|
|
->addHeaderLine('Content-Disposition', 'attachment; filename="' . $file . '"') |
402
|
|
|
->addHeaderLine('Content-Length', filesize($file)); |
403
|
|
|
|
404
|
|
|
$response->setHeaders($headers); |
405
|
|
|
unlink($file); |
406
|
|
|
return $response; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
public function getAdminGameService() |
410
|
|
|
{ |
411
|
|
|
if (!$this->adminGameService) { |
412
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_instantwin_service'); |
|
|
|
|
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
return $this->adminGameService; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
public function setAdminGameService(\PlaygroundGame\Service\Game $adminGameService) |
419
|
|
|
{ |
420
|
|
|
$this->adminGameService = $adminGameService; |
|
|
|
|
421
|
|
|
|
422
|
|
|
return $this; |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|
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: