|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Frontend; |
|
4
|
|
|
|
|
5
|
|
|
class InstantWinController extends GameController |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var gameService |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $gameService; |
|
11
|
|
|
|
|
12
|
|
|
public function playAction() |
|
13
|
|
|
{ |
|
14
|
|
|
$sg = $this->getGameService(); |
|
15
|
|
|
|
|
16
|
|
|
$identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
|
17
|
|
|
$channel = $this->getEvent()->getRouteMatch()->getParam('channel'); |
|
18
|
|
|
|
|
19
|
|
|
$game = $sg->checkGame($identifier); |
|
20
|
|
|
if (!$game || $game->isClosed()) { |
|
21
|
|
|
return $this->notFoundAction(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$redirectFb = $this->checkFbRegistration($this->zfcUserAuthentication()->getIdentity(), $game, $channel); |
|
|
|
|
|
|
25
|
|
|
if ($redirectFb) { |
|
26
|
|
|
return $redirectFb; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$user = $this->zfcUserAuthentication()->getIdentity(); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
View Code Duplication |
if (!$user && !$game->getAnonymousAllowed()) { |
|
|
|
|
|
|
32
|
|
|
$redirect = urlencode( |
|
33
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
34
|
|
|
$game->getClassType() . '/play', |
|
35
|
|
|
array( |
|
36
|
|
|
'id' => $game->getIdentifier(), |
|
37
|
|
|
'channel' => $channel |
|
38
|
|
|
), |
|
39
|
|
|
array('force_canonical' => true) |
|
40
|
|
|
) |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
return $this->redirect()->toUrl( |
|
44
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
45
|
|
|
'zfcuser/register', |
|
46
|
|
|
array('channel' => $channel) |
|
47
|
|
|
) . '?redirect='.$redirect |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($game->getOccurrenceType()=='datetime') { |
|
52
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
53
|
|
|
// En post, je reçois la maj du form pour les gagnants. |
|
54
|
|
|
// Je n'ai pas à créer une nouvelle participation mais vérifier la précédente |
|
55
|
|
|
$lastEntry = $sg->findLastInactiveEntry($game, $user); |
|
56
|
|
View Code Duplication |
if (!$lastEntry) { |
|
|
|
|
|
|
57
|
|
|
return $this->redirect()->toUrl( |
|
58
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
59
|
|
|
'instantwin', |
|
60
|
|
|
array( |
|
61
|
|
|
'id' => $game->getIdentifier(), |
|
62
|
|
|
'channel' => $channel |
|
63
|
|
|
), |
|
64
|
|
|
array('force_canonical' => true) |
|
65
|
|
|
) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
$winner = $lastEntry->getWinner(); |
|
69
|
|
|
// if not winner, I'm not authorized to call this page in POST mode. |
|
70
|
|
View Code Duplication |
if (!$winner) { |
|
|
|
|
|
|
71
|
|
|
return $this->redirect()->toUrl( |
|
72
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
73
|
|
|
'instantwin', |
|
74
|
|
|
array( |
|
75
|
|
|
'id' => $game->getIdentifier(), |
|
76
|
|
|
'channel' => $channel |
|
77
|
|
|
), |
|
78
|
|
|
array('force_canonical' => true) |
|
79
|
|
|
) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
} else { |
|
83
|
|
|
// J'arrive sur le jeu, j'essaie donc de participer |
|
84
|
|
|
$entry = $sg->play($game, $user); |
|
85
|
|
View Code Duplication |
if (!$entry) { |
|
|
|
|
|
|
86
|
|
|
// the user has already taken part of this game and the participation limit has been reached |
|
87
|
|
|
$this->flashMessenger()->addMessage('Vous avez déjà participé'); |
|
88
|
|
|
|
|
89
|
|
|
return $this->redirect()->toUrl( |
|
90
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
91
|
|
|
'instantwin/result', |
|
92
|
|
|
array( |
|
93
|
|
|
'id' => $game->getIdentifier(), |
|
94
|
|
|
'channel' => $channel |
|
95
|
|
|
) |
|
96
|
|
|
) |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// update the winner attribute in entry. |
|
101
|
|
|
$winner = $sg->IsInstantWinner($game, $user); |
|
102
|
|
|
} |
|
103
|
|
|
$prize = null; |
|
104
|
|
|
if ($winner) { |
|
105
|
|
|
$prize = $winner->getPrize(); |
|
106
|
|
|
} |
|
107
|
|
|
$viewVariables = array( |
|
108
|
|
|
'winner' => $winner, |
|
109
|
|
|
'prize' => $prize, |
|
110
|
|
|
'over' => false, |
|
111
|
|
|
); |
|
112
|
|
|
} elseif ($game->getOccurrenceType()=='code') { |
|
113
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrencecode_form'); |
|
114
|
|
|
$form->setAttribute( |
|
115
|
|
|
'action', |
|
116
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
117
|
|
|
'instantwin/play', |
|
118
|
|
|
array( |
|
119
|
|
|
'id' => $game->getIdentifier(), |
|
120
|
|
|
'channel' => $channel |
|
121
|
|
|
), |
|
122
|
|
|
array('force_canonical' => true) |
|
123
|
|
|
) |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
$occurrence = null; |
|
|
|
|
|
|
127
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
128
|
|
|
$form->setData($this->getRequest()->getPost()); |
|
|
|
|
|
|
129
|
|
|
if ($form->isValid()) { |
|
130
|
|
|
$data = $form->getData('code-input'); |
|
131
|
|
|
$code = filter_var($data['code-input'], FILTER_SANITIZE_STRING); |
|
132
|
|
|
$occurrence = $this->getGameService()->isInstantWinner($game, $user, $code); |
|
133
|
|
|
if (!$occurrence) { |
|
134
|
|
|
$this->flashMessenger()->addMessage('Le code entré est invalide ou a déjà été utilisé !'); |
|
135
|
|
|
return $this->redirect()->toUrl( |
|
136
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
137
|
|
|
'instantwin/play', |
|
138
|
|
|
array( |
|
139
|
|
|
'id' => $game->getIdentifier(), |
|
140
|
|
|
'channel' => $channel |
|
141
|
|
|
), |
|
142
|
|
|
array('force_canonical' => true) |
|
143
|
|
|
) |
|
144
|
|
|
); |
|
145
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
146
|
|
|
return $this->redirect()->toUrl( |
|
147
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
148
|
|
|
'instantwin/result', |
|
149
|
|
|
array( |
|
150
|
|
|
'id' => $game->getIdentifier(), |
|
151
|
|
|
'channel' => $channel |
|
152
|
|
|
) |
|
153
|
|
|
) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
$viewVariables = array('form' => $form); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$viewModel = $this->buildView($game); |
|
162
|
|
|
if ($viewModel instanceof \Zend\View\Model\ViewModel) { |
|
163
|
|
|
$viewModel->setVariables($viewVariables); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $viewModel; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function resultAction() |
|
170
|
|
|
{ |
|
171
|
|
|
$identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
|
172
|
|
|
$channel = $this->getEvent()->getRouteMatch()->getParam('channel'); |
|
173
|
|
|
$user = $this->zfcUserAuthentication()->getIdentity(); |
|
|
|
|
|
|
174
|
|
|
$sg = $this->getGameService(); |
|
175
|
|
|
|
|
176
|
|
|
$game = $sg->checkGame($identifier); |
|
177
|
|
|
if (!$game || $game->isClosed()) { |
|
178
|
|
|
return $this->notFoundAction(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$lastEntry = $sg->findLastInactiveEntry($game, $user); |
|
182
|
|
View Code Duplication |
if (!$lastEntry) { |
|
|
|
|
|
|
183
|
|
|
return $this->redirect()->toUrl( |
|
184
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
185
|
|
|
'instantwin', |
|
186
|
|
|
array('id' => $game->getIdentifier(), 'channel' => $channel), |
|
187
|
|
|
array('force_canonical' => true) |
|
188
|
|
|
) |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
$winner = $lastEntry->getWinner(); |
|
192
|
|
|
$occurrence = null; |
|
193
|
|
|
|
|
194
|
|
|
// On tente de récupèrer l'occurrence si elle existe pour avoir accés au lot associé |
|
195
|
|
|
$occurrences = $sg->getInstantWinOccurrenceMapper()->findBy( |
|
196
|
|
|
array('instantwin' => $game->getId(), 'entry' => $lastEntry->getId()) |
|
197
|
|
|
); |
|
198
|
|
|
if (!empty($occurrences)) { |
|
199
|
|
|
$occurrence = current($occurrences); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
View Code Duplication |
if (!$user && !$game->getAnonymousAllowed()) { |
|
|
|
|
|
|
203
|
|
|
$redirect = urlencode( |
|
204
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
205
|
|
|
'instantwin/result', |
|
206
|
|
|
array('id' => $game->getIdentifier(), 'channel' => $channel) |
|
207
|
|
|
) |
|
208
|
|
|
); |
|
209
|
|
|
return $this->redirect()->toUrl( |
|
210
|
|
|
$this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
211
|
|
|
'zfcuser/register', |
|
212
|
|
|
array('channel' => $channel) |
|
213
|
|
|
) . '?redirect='.$redirect |
|
214
|
|
|
); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
|
218
|
|
|
$socialLinkUrl = $this->frontendUrl()->fromRoute( |
|
|
|
|
|
|
219
|
|
|
'instantwin', |
|
220
|
|
|
array('id' => $game->getIdentifier(), 'channel' => $channel), |
|
221
|
|
|
array('force_canonical' => true) |
|
222
|
|
|
).'?key='.$secretKey; |
|
223
|
|
|
// With core shortener helper |
|
224
|
|
|
$socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
|
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
|
227
|
|
|
$form->setAttribute('method', 'post'); |
|
228
|
|
|
|
|
229
|
|
|
$statusMail = null; |
|
230
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
231
|
|
|
$data = $this->getRequest()->getPost()->toArray(); |
|
|
|
|
|
|
232
|
|
|
$form->setData($data); |
|
233
|
|
|
if ($form->isValid()) { |
|
234
|
|
|
if (isset($data['email1']) || isset($data['email2']) || isset($data['email3'])) { |
|
235
|
|
|
$result = $this->getGameService()->sendShareMail($data, $game, $user, $lastEntry); |
|
236
|
|
|
if ($result) { |
|
237
|
|
|
$statusMail = true; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
$prize = null; |
|
244
|
|
|
if ($occurrence instanceof \PlaygroundGame\Entity\InstantWinOccurrence) { |
|
245
|
|
|
$prize = $occurrence->getPrize(); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
// buildView must be before sendMail because it adds the game template path to the templateStack |
|
249
|
|
|
$viewModel = $this->buildView($game); |
|
250
|
|
|
|
|
251
|
|
|
$this->getGameService()->sendMail($game, $user, $lastEntry, $prize); |
|
252
|
|
|
|
|
253
|
|
|
if ($viewModel instanceof \Zend\View\Model\ViewModel) { |
|
254
|
|
|
$viewModel->setVariables(array( |
|
255
|
|
|
'occurrence' => $occurrence, |
|
256
|
|
|
'statusMail' => $statusMail, |
|
257
|
|
|
'winner' => $winner, |
|
258
|
|
|
'form' => $form, |
|
259
|
|
|
'socialLinkUrl' => $socialLinkUrl, |
|
260
|
|
|
'secretKey' => $secretKey, |
|
261
|
|
|
)); |
|
262
|
|
|
} |
|
263
|
|
|
return $viewModel; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
public function getGameService() |
|
267
|
|
|
{ |
|
268
|
|
|
if (!$this->gameService) { |
|
269
|
|
|
$this->gameService = $this->getServiceLocator()->get('playgroundgame_instantwin_service'); |
|
|
|
|
|
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return $this->gameService; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
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: