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
|
|
|
$this->checkGame(); |
25
|
|
|
$service->getGameMapper()->remove($this->game); |
|
|
|
|
26
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game has been removed'); |
27
|
|
|
|
28
|
|
|
return $this->redirect()->toRoute('admin/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->url()->fromRoute('admin/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->create($data, $instantwin, 'playgroundgame_instantwin_form'); |
60
|
|
|
if ($game) { |
61
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created'); |
62
|
|
|
|
63
|
|
|
return $this->redirect()->toRoute('admin/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
|
|
|
public function listOccurrenceAction() |
88
|
|
|
{ |
89
|
|
|
$this->checkGame(); |
90
|
|
|
|
91
|
|
|
$adapter = new DoctrineAdapter( |
92
|
|
|
new LargeTablePaginator( |
93
|
|
|
$service->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->url()->fromRoute( |
122
|
|
|
'admin/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', $data['value']); |
139
|
|
|
$data['value'] = $value->format('Y-m-d H:i:s'); |
140
|
|
|
|
141
|
|
|
$occurrence = $service->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()->toRoute( |
146
|
|
|
'admin/playgroundgame/instantwin-occurrence-list', |
147
|
|
|
array('gameId'=>$this->game->getId()) |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
return $viewModel->setVariables( |
152
|
|
|
array( |
153
|
|
|
'form' => $form, |
154
|
|
|
'game' => $this->game, |
155
|
|
|
'occurrence_id' => 0, |
156
|
|
|
'title' => 'Add occurrence', |
157
|
|
|
) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function importOccurrencesAction() |
162
|
|
|
{ |
163
|
|
|
$this->checkGame(); |
164
|
|
|
|
165
|
|
|
$viewModel = new ViewModel(); |
166
|
|
|
$viewModel->setTemplate('playground-game/instant-win/import-occurrences'); |
167
|
|
|
|
168
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrenceimport_form'); |
169
|
|
|
|
170
|
|
|
$form->get('submit')->setAttribute('label', 'Import'); |
171
|
|
|
$form->setAttribute( |
172
|
|
|
'action', |
173
|
|
|
$this->url()->fromRoute( |
174
|
|
|
'admin/playgroundgame/instantwin-occurrences-import', |
175
|
|
|
array('gameId' => $this->game->getId()) |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
$form->get('instant_win_id')->setAttribute('value', $this->game->getId()); |
179
|
|
|
|
180
|
|
|
// File validator |
181
|
|
|
$inputFilter = new InputFilter\InputFilter(); |
182
|
|
|
$fileFilter = new InputFilter\FileInput('file'); |
183
|
|
|
$validatorChain = new Validator\ValidatorChain(); |
184
|
|
|
$validatorChain->attach(new Validator\File\Exists()); |
185
|
|
|
$validatorChain->attach(new Validator\File\Extension('csv')); |
186
|
|
|
$fileFilter->setValidatorChain($validatorChain); |
187
|
|
|
$fileFilter->setRequired(true); |
188
|
|
|
|
189
|
|
|
$prizeFilter = new InputFilter\Input('prize'); |
190
|
|
|
$prizeFilter->setRequired(false); |
191
|
|
|
|
192
|
|
|
$inputFilter->add($fileFilter); |
193
|
|
|
$inputFilter->add($prizeFilter); |
194
|
|
|
$form->setInputFilter($inputFilter); |
195
|
|
|
|
196
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
197
|
|
|
$data = array_merge_recursive( |
198
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
199
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
200
|
|
|
); |
201
|
|
|
$form->setData($data); |
202
|
|
|
if ($form->isValid()) { |
203
|
|
|
$data = $form->getData(); |
204
|
|
|
$created = $this->getAdminGameService()->importOccurrences($data); |
205
|
|
|
if ($created) { |
206
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
207
|
|
|
$created.' occurrences were created !' |
208
|
|
|
); |
209
|
|
|
return $this->redirect()->toRoute( |
210
|
|
|
'admin/playgroundgame/instantwin-occurrence-list', |
211
|
|
|
array('gameId'=>$this->game->getId()) |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $viewModel->setVariables( |
218
|
|
|
array( |
219
|
|
|
'form' => $form, |
220
|
|
|
'title' => 'Import occurrences', |
221
|
|
|
) |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function editOccurrenceAction() |
226
|
|
|
{ |
227
|
|
|
$this->checkGame(); |
228
|
|
|
|
229
|
|
|
$viewModel = new ViewModel(); |
230
|
|
|
$viewModel->setTemplate('playground-game/instant-win/occurrence'); |
231
|
|
|
$service = $this->getAdminGameService(); |
232
|
|
|
|
233
|
|
|
$occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId'); |
234
|
|
|
$occurrence = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId); |
235
|
|
|
// Si l'occurrence a été utilisée, on ne peut plus la modifier |
236
|
|
|
if ($occurrence->getUser()) { |
237
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
238
|
|
|
'This occurrence has a winner, you can not update it.' |
239
|
|
|
); |
240
|
|
|
return $this->redirect()->toRoute( |
241
|
|
|
'admin/playgroundgame/instantwin-occurrence-list', |
242
|
|
|
array('gameId'=>$this->game->getId()) |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form'); |
246
|
|
|
$form->remove('occurrences_file'); |
247
|
|
|
|
248
|
|
|
$form->get('submit')->setAttribute('label', 'Edit'); |
249
|
|
|
$form->setAttribute('action', ''); |
250
|
|
|
|
251
|
|
|
$form->get('instant_win_id')->setAttribute('value', $this->game->getId()); |
252
|
|
|
|
253
|
|
|
$form->bind($occurrence); |
254
|
|
|
|
255
|
|
View Code Duplication |
if ($this->getRequest()->isPost()) { |
|
|
|
|
256
|
|
|
$data = array_merge( |
257
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
258
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
259
|
|
|
); |
260
|
|
|
$occurrence = $service->updateOccurrence($data, $occurrence->getId()); |
261
|
|
|
|
262
|
|
|
if ($occurrence) { |
263
|
|
|
// Redirect to list of games |
264
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was edited'); |
265
|
|
|
return $this->redirect()->toRoute( |
266
|
|
|
'admin/playgroundgame/instantwin-occurrence-list', |
267
|
|
|
array('gameId'=>$this->game->getId()) |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
return $viewModel->setVariables( |
272
|
|
|
array( |
273
|
|
|
'form' => $form, |
274
|
|
|
'game' => $this->game, |
275
|
|
|
'occurrence_id' => $occurrenceId, |
276
|
|
|
'title' => 'Edit occurrence', |
277
|
|
|
) |
278
|
|
|
); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
public function removeOccurrenceAction() |
283
|
|
|
{ |
284
|
|
|
$service = $this->getAdminGameService(); |
285
|
|
|
$occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId'); |
286
|
|
|
if (!$occurrenceId) { |
287
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
288
|
|
|
} |
289
|
|
|
$occurrence = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId); |
290
|
|
|
$instantwinId = $occurrence->getInstantWin()->getId(); |
291
|
|
|
|
292
|
|
|
if ($occurrence->getActive()) { |
293
|
|
|
$service->getInstantWinOccurrenceMapper()->remove($occurrence); |
294
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was deleted'); |
295
|
|
|
} else { |
296
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
297
|
|
|
'Il y a un participant à cet instant gagnant. Vous ne pouvez plus le supprimer' |
298
|
|
|
); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $this->redirect()->toRoute( |
302
|
|
|
'admin/playgroundgame/instantwin-occurrence-list', |
303
|
|
|
array('gameId'=>$instantwinId) |
304
|
|
|
); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function exportOccurrencesAction() |
308
|
|
|
{ |
309
|
|
|
$this->checkGame(); |
310
|
|
|
|
311
|
|
|
$file = $this->getAdminGameService()->setOccurencesToCSV($this->game); |
312
|
|
|
|
313
|
|
|
$response = new \Zend\Http\Response\Stream(); |
314
|
|
|
$response->setStream(fopen($file, 'r')); |
315
|
|
|
$response->setStatusCode(200); |
316
|
|
|
|
317
|
|
|
$headers = new \Zend\Http\Headers(); |
318
|
|
|
$headers->addHeaderLine('Content-Type', 'text/csv') |
319
|
|
|
->addHeaderLine('Content-Disposition', 'attachment; filename="' . $file . '"') |
320
|
|
|
->addHeaderLine('Content-Length', filesize($file)); |
321
|
|
|
|
322
|
|
|
$response->setHeaders($headers); |
323
|
|
|
unlink($file); |
324
|
|
|
return $response; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function getAdminGameService() |
328
|
|
|
{ |
329
|
|
|
if (!$this->adminGameService) { |
330
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_instantwin_service'); |
|
|
|
|
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return $this->adminGameService; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function setAdminGameService(\PlaygroundGame\Service\Game $adminGameService) |
337
|
|
|
{ |
338
|
|
|
$this->adminGameService = $adminGameService; |
|
|
|
|
339
|
|
|
|
340
|
|
|
return $this; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.