|
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 \PlaygroundGame\Service\Game |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $adminGameService; |
|
21
|
|
|
|
|
22
|
|
|
public function removeAction() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->checkGame(); |
|
25
|
|
|
$this->getAdminGameService()->getGameMapper()->remove($this->game); |
|
26
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game has been removed'); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/list')); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function createInstantWinAction() |
|
32
|
|
|
{ |
|
33
|
|
|
$service = $this->getAdminGameService(); |
|
34
|
|
|
$viewModel = new ViewModel(); |
|
35
|
|
|
$viewModel->setTemplate('playground-game/instant-win/instantwin'); |
|
36
|
|
|
|
|
37
|
|
|
$gameForm = new ViewModel(); |
|
38
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
|
39
|
|
|
|
|
40
|
|
|
$instantwin = new InstantWin(); |
|
41
|
|
|
|
|
42
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwin_form'); |
|
43
|
|
|
$form->bind($instantwin); |
|
44
|
|
|
$form->setAttribute( |
|
45
|
|
|
'action', |
|
46
|
|
|
$this->adminUrl()->fromRoute('playgroundgame/create-instantwin', array('gameId' => 0)) |
|
|
|
|
|
|
47
|
|
|
); |
|
48
|
|
|
$form->setAttribute('method', 'post'); |
|
49
|
|
|
|
|
50
|
|
|
$request = $this->getRequest(); |
|
51
|
|
|
if ($request->isPost()) { |
|
52
|
|
|
$data = array_replace_recursive( |
|
53
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
54
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
55
|
|
|
); |
|
56
|
|
|
if (empty($data['prizes'])) { |
|
57
|
|
|
$data['prizes'] = array(); |
|
58
|
|
|
} |
|
59
|
|
|
$game = $service->createOrUpdate($data, $instantwin, 'playgroundgame_instantwin_form'); |
|
60
|
|
|
if ($game) { |
|
61
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created'); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/list')); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $instantwin)); |
|
67
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
|
68
|
|
|
|
|
69
|
|
|
return $viewModel->setVariables( |
|
70
|
|
|
array( |
|
71
|
|
|
'form' => $form, |
|
72
|
|
|
'title' => 'Create instant win', |
|
73
|
|
|
) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function editInstantWinAction() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->checkGame(); |
|
80
|
|
|
|
|
81
|
|
|
return $this->editGame( |
|
82
|
|
|
'playground-game/instant-win/instantwin', |
|
83
|
|
|
'playgroundgame_instantwin_form' |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
View Code Duplication |
public function listOccurrenceAction() |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$this->checkGame(); |
|
90
|
|
|
|
|
91
|
|
|
$adapter = new DoctrineAdapter( |
|
92
|
|
|
new LargeTablePaginator( |
|
93
|
|
|
$this->getAdminGameService()->getInstantWinOccurrenceMapper()->queryByGame($this->game) |
|
94
|
|
|
) |
|
95
|
|
|
); |
|
96
|
|
|
$paginator = new Paginator($adapter); |
|
97
|
|
|
$paginator->setItemCountPerPage(25); |
|
98
|
|
|
$paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
|
99
|
|
|
|
|
100
|
|
|
return new ViewModel( |
|
101
|
|
|
array( |
|
102
|
|
|
'occurrences' => $paginator, |
|
103
|
|
|
'gameId' => $this->game->getId(), |
|
104
|
|
|
'game' => $this->game, |
|
105
|
|
|
) |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function addOccurrenceAction() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->checkGame(); |
|
112
|
|
|
|
|
113
|
|
|
$viewModel = new ViewModel(); |
|
114
|
|
|
$viewModel->setTemplate('playground-game/instant-win/occurrence'); |
|
115
|
|
|
|
|
116
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form'); |
|
117
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
|
118
|
|
|
|
|
119
|
|
|
$form->setAttribute( |
|
120
|
|
|
'action', |
|
121
|
|
|
$this->adminUrl()->fromRoute( |
|
|
|
|
|
|
122
|
|
|
'playgroundgame/instantwin-occurrence-add', |
|
123
|
|
|
array('gameId' => $this->game->getId()) |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
$form->setAttribute('method', 'post'); |
|
127
|
|
|
$form->get('instant_win_id')->setAttribute('value', $this->game->getId()); |
|
128
|
|
|
$occurrence = new InstantWinOccurrence(); |
|
129
|
|
|
$form->bind($occurrence); |
|
130
|
|
|
|
|
131
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
132
|
|
|
$data = array_merge( |
|
133
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
134
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
// Change the format of the date |
|
138
|
|
|
$value = \DateTime::createFromFormat('d/m/Y H:i:s', $data['value']); |
|
139
|
|
|
$data['value'] = $value->format('Y-m-d H:i:s'); |
|
140
|
|
|
|
|
141
|
|
|
$occurrence = $this->getAdminGameService()->updateOccurrence($data, $occurrence->getId()); |
|
142
|
|
|
if ($occurrence) { |
|
143
|
|
|
// Redirect to list of games |
|
144
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was created'); |
|
|
|
|
|
|
145
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/instantwin-occurrence-list', array('gameId' => $this->game->getId()))); |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
return $viewModel->setVariables( |
|
149
|
|
|
array( |
|
150
|
|
|
'form' => $form, |
|
151
|
|
|
'game' => $this->game, |
|
152
|
|
|
'occurrence_id' => 0, |
|
153
|
|
|
'title' => 'Add occurrence', |
|
154
|
|
|
) |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function importOccurrencesAction() |
|
159
|
|
|
{ |
|
160
|
|
|
$this->checkGame(); |
|
161
|
|
|
|
|
162
|
|
|
$viewModel = new ViewModel(); |
|
163
|
|
|
$viewModel->setTemplate('playground-game/instant-win/import-occurrences'); |
|
164
|
|
|
|
|
165
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrenceimport_form'); |
|
166
|
|
|
|
|
167
|
|
|
$form->get('submit')->setAttribute('label', 'Import'); |
|
168
|
|
|
$form->setAttribute( |
|
169
|
|
|
'action', |
|
170
|
|
|
$this->adminUrl()->fromRoute( |
|
|
|
|
|
|
171
|
|
|
'playgroundgame/instantwin-occurrences-import', |
|
172
|
|
|
array('gameId' => $this->game->getId()) |
|
173
|
|
|
) |
|
174
|
|
|
); |
|
175
|
|
|
$form->get('instant_win_id')->setAttribute('value', $this->game->getId()); |
|
176
|
|
|
|
|
177
|
|
|
// File validator |
|
178
|
|
|
$inputFilter = new InputFilter\InputFilter(); |
|
179
|
|
|
$fileFilter = new InputFilter\FileInput('file'); |
|
180
|
|
|
$validatorChain = new Validator\ValidatorChain(); |
|
181
|
|
|
$validatorChain->attach(new Validator\File\Exists()); |
|
182
|
|
|
$validatorChain->attach(new Validator\File\Extension('csv')); |
|
183
|
|
|
$fileFilter->setValidatorChain($validatorChain); |
|
184
|
|
|
$fileFilter->setRequired(true); |
|
185
|
|
|
|
|
186
|
|
|
$prizeFilter = new InputFilter\Input('prize'); |
|
187
|
|
|
$prizeFilter->setRequired(false); |
|
188
|
|
|
|
|
189
|
|
|
$inputFilter->add($fileFilter); |
|
190
|
|
|
$inputFilter->add($prizeFilter); |
|
191
|
|
|
$form->setInputFilter($inputFilter); |
|
192
|
|
|
|
|
193
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
194
|
|
|
$data = array_merge_recursive( |
|
195
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
196
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
197
|
|
|
); |
|
198
|
|
|
$form->setData($data); |
|
199
|
|
|
if ($form->isValid()) { |
|
200
|
|
|
$data = $form->getData(); |
|
201
|
|
|
$created = $this->getAdminGameService()->importOccurrences($data); |
|
202
|
|
|
if ($created) { |
|
203
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
|
|
|
|
|
|
204
|
|
|
$created.' occurrences were created !' |
|
205
|
|
|
); |
|
206
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/instantwin-occurrence-list', array('gameId' => $this->game->getId()))); |
|
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $viewModel->setVariables( |
|
212
|
|
|
array( |
|
213
|
|
|
'form' => $form, |
|
214
|
|
|
'title' => 'Import occurrences', |
|
215
|
|
|
) |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function editOccurrenceAction() |
|
220
|
|
|
{ |
|
221
|
|
|
$this->checkGame(); |
|
222
|
|
|
|
|
223
|
|
|
$viewModel = new ViewModel(); |
|
224
|
|
|
$viewModel->setTemplate('playground-game/instant-win/occurrence'); |
|
225
|
|
|
$service = $this->getAdminGameService(); |
|
226
|
|
|
|
|
227
|
|
|
$occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId'); |
|
228
|
|
|
$occurrence = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId); |
|
229
|
|
|
// Si l'occurrence a été utilisée, on ne peut plus la modifier |
|
230
|
|
|
if ($occurrence->getUser()) { |
|
231
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
|
|
|
|
|
|
232
|
|
|
'This occurrence has a winner, you can not update it.' |
|
233
|
|
|
); |
|
234
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/instantwin-occurrence-list', array('gameId'=>$this->game->getId()))); |
|
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form'); |
|
237
|
|
|
$form->remove('occurrences_file'); |
|
238
|
|
|
|
|
239
|
|
|
$form->get('submit')->setAttribute('label', 'Edit'); |
|
240
|
|
|
$form->setAttribute('action', ''); |
|
241
|
|
|
|
|
242
|
|
|
$form->get('instant_win_id')->setAttribute('value', $this->game->getId()); |
|
243
|
|
|
|
|
244
|
|
|
$form->bind($occurrence); |
|
245
|
|
|
|
|
246
|
|
View Code Duplication |
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
247
|
|
|
$data = array_merge( |
|
248
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
249
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
250
|
|
|
); |
|
251
|
|
|
$occurrence = $service->updateOccurrence($data, $occurrence->getId()); |
|
252
|
|
|
|
|
253
|
|
|
if ($occurrence) { |
|
254
|
|
|
// Redirect to list of games |
|
255
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was edited'); |
|
|
|
|
|
|
256
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/instantwin-occurrence-list', array('gameId' => $this->game->getId()))); |
|
|
|
|
|
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
return $viewModel->setVariables( |
|
260
|
|
|
array( |
|
261
|
|
|
'form' => $form, |
|
262
|
|
|
'game' => $this->game, |
|
263
|
|
|
'occurrence_id' => $occurrenceId, |
|
264
|
|
|
'title' => 'Edit occurrence', |
|
265
|
|
|
) |
|
266
|
|
|
); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function removeAllOccurrencesAction() |
|
270
|
|
|
{ |
|
271
|
|
|
$this->checkGame(); |
|
272
|
|
|
$service = $this->getAdminGameService(); |
|
273
|
|
|
$occurrences = $service->getInstantWinOccurrenceMapper()->findBy(['instantwin' => $this->game, 'entry' => null]); |
|
274
|
|
|
$service->getInstantWinOccurrenceMapper()->removeAll($occurrences); |
|
275
|
|
|
|
|
276
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/instantwin-occurrence-list', array('gameId'=>$this->game->getId()))); |
|
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
public function removeOccurrenceAction() |
|
280
|
|
|
{ |
|
281
|
|
|
$service = $this->getAdminGameService(); |
|
282
|
|
|
$occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId'); |
|
283
|
|
|
if (!$occurrenceId) { |
|
284
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/list')); |
|
|
|
|
|
|
285
|
|
|
} |
|
286
|
|
|
$occurrence = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId); |
|
287
|
|
|
$instantwinId = $occurrence->getInstantWin()->getId(); |
|
288
|
|
|
|
|
289
|
|
|
if ($occurrence->getActive()) { |
|
290
|
|
|
$service->getInstantWinOccurrenceMapper()->remove($occurrence); |
|
291
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence has been deleted'); |
|
|
|
|
|
|
292
|
|
|
} else { |
|
293
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
|
|
|
|
|
|
294
|
|
|
"A player has already won this occurrence. You can't delete it anymore" |
|
295
|
|
|
); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/instantwin-occurrence-list', array('gameId'=>$instantwinId))); |
|
|
|
|
|
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
public function exportOccurrencesAction() |
|
302
|
|
|
{ |
|
303
|
|
|
$this->checkGame(); |
|
304
|
|
|
|
|
305
|
|
|
$file = $this->getAdminGameService()->setOccurencesToCSV($this->game); |
|
306
|
|
|
|
|
307
|
|
|
$response = new \Zend\Http\Response\Stream(); |
|
308
|
|
|
$response->setStream(fopen($file, 'r')); |
|
309
|
|
|
$response->setStatusCode(200); |
|
310
|
|
|
|
|
311
|
|
|
$headers = new \Zend\Http\Headers(); |
|
312
|
|
|
$headers->addHeaderLine('Content-Type', 'text/csv') |
|
313
|
|
|
->addHeaderLine('Content-Disposition', 'attachment; filename="' . $file . '"') |
|
314
|
|
|
->addHeaderLine('Content-Length', filesize($file)); |
|
315
|
|
|
|
|
316
|
|
|
$response->setHeaders($headers); |
|
317
|
|
|
unlink($file); |
|
318
|
|
|
return $response; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
public function getAdminGameService() |
|
322
|
|
|
{ |
|
323
|
|
|
if (!$this->adminGameService) { |
|
324
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_instantwin_service'); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
return $this->adminGameService; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
public function setAdminGameService(\PlaygroundGame\Service\Game $adminGameService) |
|
331
|
|
|
{ |
|
332
|
|
|
$this->adminGameService = $adminGameService; |
|
333
|
|
|
|
|
334
|
|
|
return $this; |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: