1
|
|
|
<?php |
2
|
|
|
namespace PlaygroundGame\Service; |
3
|
|
|
|
4
|
|
|
use PlaygroundGame\Entity\Entry; |
5
|
|
|
use Zend\Session\Container; |
6
|
|
|
use Zend\ServiceManager\ServiceManager; |
7
|
|
|
use Zend\EventManager\EventManagerAwareTrait; |
8
|
|
|
use Zend\EventManager\EventManager; |
9
|
|
|
use PlaygroundGame\Options\ModuleOptions; |
10
|
|
|
use PlaygroundGame\Mapper\GameInterface as GameMapperInterface; |
11
|
|
|
use DoctrineModule\Validator\NoObjectExists as NoObjectExistsValidator; |
12
|
|
|
use Zend\Validator\File\Size; |
13
|
|
|
use Zend\Validator\File\IsImage; |
14
|
|
|
use Zend\Stdlib\ErrorHandler; |
15
|
|
|
use PlaygroundCore\Filter\Sanitize; |
16
|
|
|
use Zend\Form\Element; |
17
|
|
|
use Zend\Form\Form; |
18
|
|
|
use Zend\InputFilter\Factory as InputFactory; |
19
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
20
|
|
|
use ZfcDatagrid\Column; |
21
|
|
|
use ZfcDatagrid\Action; |
22
|
|
|
use ZfcDatagrid\Column\Formatter; |
23
|
|
|
use ZfcDatagrid\Column\Type; |
24
|
|
|
use ZfcDatagrid\Column\Style; |
25
|
|
|
use ZfcDatagrid\Filter; |
26
|
|
|
use Doctrine\ORM\Query\Expr; |
27
|
|
|
|
28
|
|
|
class Game |
29
|
|
|
{ |
30
|
|
|
use EventManagerAwareTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @var GameMapperInterface |
35
|
|
|
*/ |
36
|
|
|
protected $gameMapper; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @var EntryMapperInterface |
41
|
|
|
*/ |
42
|
|
|
protected $entryMapper; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @var UserServiceOptionsInterface |
47
|
|
|
*/ |
48
|
|
|
protected $options; |
49
|
|
|
|
50
|
|
|
protected $playerformMapper; |
51
|
|
|
|
52
|
|
|
protected $invitationMapper; |
53
|
|
|
|
54
|
|
|
protected $userMapper; |
55
|
|
|
|
56
|
|
|
protected $anonymousIdentifier = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
* @var ServiceManager |
61
|
|
|
*/ |
62
|
|
|
protected $serviceLocator; |
63
|
|
|
|
64
|
|
|
protected $event; |
65
|
|
|
|
66
|
|
|
public function __construct(ServiceLocatorInterface $locator) |
67
|
|
|
{ |
68
|
|
|
$this->serviceLocator = $locator; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function getEventManager() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
if (null === $this->event) { |
74
|
|
|
$this->event = new EventManager($this->serviceLocator->get('SharedEventManager'), [get_class($this)]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->event; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function getGameUserPath($game, $user) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR; |
83
|
|
|
$path .= 'game' . $game->getId() . DIRECTORY_SEPARATOR; |
84
|
|
|
if (!is_dir($path)) { |
85
|
|
|
mkdir($path, 0777, true); |
86
|
|
|
} |
87
|
|
|
$path .= 'user'. $user->getId() . DIRECTORY_SEPARATOR; |
88
|
|
|
if (!is_dir($path)) { |
89
|
|
|
mkdir($path, 0777, true); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $path; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function getGameUserMediaUrl($game, $user) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$media_url = $this->getOptions()->getMediaUrl() . '/'; |
98
|
|
|
$media_url .= 'game' . $game->getId() . '/' . 'user'. $user->getId() . '/'; |
99
|
|
|
|
100
|
|
|
return $media_url; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
* This service is ready for all types of games |
106
|
|
|
* |
107
|
|
|
* @param array $data |
108
|
|
|
* @param string $formClass |
109
|
|
|
* @return \PlaygroundGame\Entity\Game |
110
|
|
|
*/ |
111
|
|
|
public function createOrUpdate(array $data, $game, $formClass) |
112
|
|
|
{ |
113
|
|
|
$entityManager = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
114
|
|
|
|
115
|
|
|
$form = $this->serviceLocator->get($formClass); |
116
|
|
|
$form->get('publicationDate')->setOptions(array( |
117
|
|
|
'format' => 'Y-m-d H:i:s' |
118
|
|
|
)); |
119
|
|
|
$form->get('startDate')->setOptions(array( |
120
|
|
|
'format' => 'Y-m-d H:i:s' |
121
|
|
|
)); |
122
|
|
|
$form->get('endDate')->setOptions(array( |
123
|
|
|
'format' => 'Y-m-d H:i:s' |
124
|
|
|
)); |
125
|
|
|
$form->get('closeDate')->setOptions(array( |
126
|
|
|
'format' => 'Y-m-d H:i:s' |
127
|
|
|
)); |
128
|
|
|
|
129
|
|
|
$form->bind($game); |
130
|
|
|
|
131
|
|
|
$path = $this->getOptions()->getMediaPath() . '/'; |
132
|
|
|
if (!is_dir($path)) { |
133
|
|
|
mkdir($path, 0777, true); |
134
|
|
|
} |
135
|
|
|
$media_url = $this->getOptions()->getMediaUrl() . '/'; |
136
|
|
|
|
137
|
|
|
$identifierInput = $form->getInputFilter()->get('identifier'); |
138
|
|
|
$noObjectExistsValidator = new NoObjectExistsValidator(array( |
139
|
|
|
'object_repository' => $entityManager->getRepository('PlaygroundGame\Entity\Game'), |
140
|
|
|
'fields' => 'identifier', |
141
|
|
|
'messages' => array( |
142
|
|
|
'objectFound' => 'This url already exists !' |
143
|
|
|
) |
144
|
|
|
)); |
145
|
|
|
|
146
|
|
|
if ($game->getIdentifier() != $data['identifier']) { |
147
|
|
|
$identifierInput->getValidatorChain()->addValidator($noObjectExistsValidator); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// I must switch from original format to the Y-m-d format because |
151
|
|
|
// this is the only one accepted by new DateTime($value) |
152
|
|
View Code Duplication |
if (isset($data['publicationDate']) && $data['publicationDate']) { |
|
|
|
|
153
|
|
|
$tmpDate = \DateTime::createFromFormat('d/m/Y H:i:s', $data['publicationDate']); |
154
|
|
|
$data['publicationDate'] = $tmpDate->format('Y-m-d H:i:s'); |
155
|
|
|
} |
156
|
|
View Code Duplication |
if (isset($data['startDate']) && $data['startDate']) { |
|
|
|
|
157
|
|
|
$tmpDate = \DateTime::createFromFormat('d/m/Y H:i:s', $data['startDate']); |
158
|
|
|
$data['startDate'] = $tmpDate->format('Y-m-d H:i:s'); |
159
|
|
|
} |
160
|
|
View Code Duplication |
if (isset($data['endDate']) && $data['endDate']) { |
|
|
|
|
161
|
|
|
$tmpDate = \DateTime::createFromFormat('d/m/Y H:i:s', $data['endDate']); |
162
|
|
|
$data['endDate'] = $tmpDate->format('Y-m-d H:i:s'); |
163
|
|
|
} |
164
|
|
View Code Duplication |
if (isset($data['closeDate']) && $data['closeDate']) { |
|
|
|
|
165
|
|
|
$tmpDate = \DateTime::createFromFormat('d/m/Y H:i:s', $data['closeDate']); |
166
|
|
|
$data['closeDate'] = $tmpDate->format('Y-m-d H:i:s'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// If publicationDate is null, I update it with the startDate if not null neither |
170
|
|
|
if ((! isset($data['publicationDate']) || $data['publicationDate'] == '') && |
171
|
|
|
(isset($data['startDate']) && $data['startDate'] != '') |
172
|
|
|
) { |
173
|
|
|
$data['publicationDate'] = $data['startDate']; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// If the identifier has not been set, I use the title to create one. |
177
|
|
View Code Duplication |
if ((! isset($data['identifier']) || empty($data['identifier'])) && isset($data['title'])) { |
|
|
|
|
178
|
|
|
$data['identifier'] = $data['title']; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$form->setData($data); |
182
|
|
|
|
183
|
|
|
// If someone want to claim... It's time to do it ! used for exemple by PlaygroundFacebook Module |
184
|
|
|
$result = $this->getEventManager()->trigger(__FUNCTION__ . '.validate', $this, array( |
185
|
|
|
'game' => $game, |
186
|
|
|
'data' => $data |
187
|
|
|
)); |
188
|
|
|
if (is_array($result) && ! $result[0]) { |
189
|
|
|
$form->get('fbAppId')->setMessages(array( |
190
|
|
|
'Vous devez d\'abord désinstaller l\'appli Facebook' |
191
|
|
|
)); |
192
|
|
|
|
193
|
|
|
return false; |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (! $form->isValid()) { |
197
|
|
View Code Duplication |
if (isset($data['publicationDate']) && $data['publicationDate']) { |
|
|
|
|
198
|
|
|
$tmpDate = \DateTime::createFromFormat('Y-m-d H:i:s', $data['publicationDate']); |
199
|
|
|
$data['publicationDate'] = $tmpDate->format('d/m/Y H:i:s'); |
200
|
|
|
$form->setData(array( |
201
|
|
|
'publicationDate' => $data['publicationDate'] |
202
|
|
|
)); |
203
|
|
|
} |
204
|
|
View Code Duplication |
if (isset($data['startDate']) && $data['startDate']) { |
|
|
|
|
205
|
|
|
$tmpDate = \DateTime::createFromFormat('Y-m-d H:i:s', $data['startDate']); |
206
|
|
|
$data['startDate'] = $tmpDate->format('d/m/Y H:i:s'); |
207
|
|
|
$form->setData(array( |
208
|
|
|
'startDate' => $data['startDate'] |
209
|
|
|
)); |
210
|
|
|
} |
211
|
|
View Code Duplication |
if (isset($data['endDate']) && $data['endDate']) { |
|
|
|
|
212
|
|
|
$tmpDate = \DateTime::createFromFormat('Y-m-d H:i:s', $data['endDate']); |
213
|
|
|
$data['endDate'] = $tmpDate->format('d/m/Y H:i:s'); |
214
|
|
|
$form->setData(array( |
215
|
|
|
'endDate' => $data['endDate'] |
216
|
|
|
)); |
217
|
|
|
} |
218
|
|
View Code Duplication |
if (isset($data['closeDate']) && $data['closeDate']) { |
|
|
|
|
219
|
|
|
$tmpDate = \DateTime::createFromFormat('Y-m-d H:i:s', $data['closeDate']); |
220
|
|
|
$data['closeDate'] = $tmpDate->format('d/m/Y H:i:s'); |
221
|
|
|
$form->setData(array( |
222
|
|
|
'closeDate' => $data['closeDate'] |
223
|
|
|
)); |
224
|
|
|
} |
225
|
|
|
return false; |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$game = $form->getData(); |
229
|
|
|
$game = $this->getGameMapper()->insert($game); |
230
|
|
|
|
231
|
|
|
// I wait for the game to be saved to obtain its ID. |
232
|
|
View Code Duplication |
if (! empty($data['uploadMainImage']['tmp_name'])) { |
|
|
|
|
233
|
|
|
ErrorHandler::start(); |
234
|
|
|
$data['uploadMainImage']['name'] = $this->fileNewname( |
235
|
|
|
$path, |
236
|
|
|
$game->getId() . "-" . $data['uploadMainImage']['name'] |
237
|
|
|
); |
238
|
|
|
move_uploaded_file($data['uploadMainImage']['tmp_name'], $path . $data['uploadMainImage']['name']); |
239
|
|
|
$game->setMainImage($media_url . $data['uploadMainImage']['name']); |
240
|
|
|
ErrorHandler::stop(true); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if (isset($data['deleteMainImage']) && |
244
|
|
|
$data['deleteMainImage'] && |
245
|
|
|
empty($data['uploadMainImage']['tmp_name']) |
246
|
|
|
) { |
247
|
|
|
ErrorHandler::start(); |
248
|
|
|
$image = $game->getMainImage(); |
249
|
|
|
$image = str_replace($media_url, '', $image); |
250
|
|
|
unlink($path . $image); |
251
|
|
|
$game->setMainImage(null); |
252
|
|
|
ErrorHandler::stop(true); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
View Code Duplication |
if (! empty($data['uploadSecondImage']['tmp_name'])) { |
|
|
|
|
256
|
|
|
ErrorHandler::start(); |
257
|
|
|
$data['uploadSecondImage']['name'] = $this->fileNewname( |
258
|
|
|
$path, |
259
|
|
|
$game->getId() . "-" . $data['uploadSecondImage']['name'] |
260
|
|
|
); |
261
|
|
|
move_uploaded_file($data['uploadSecondImage']['tmp_name'], $path . $data['uploadSecondImage']['name']); |
262
|
|
|
$game->setSecondImage($media_url . $data['uploadSecondImage']['name']); |
263
|
|
|
ErrorHandler::stop(true); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
if (isset($data['deleteSecondImage']) && |
267
|
|
|
$data['deleteSecondImage'] && |
268
|
|
|
empty($data['uploadSecondImage']['tmp_name']) |
269
|
|
|
) { |
270
|
|
|
ErrorHandler::start(); |
271
|
|
|
$image = $game->getSecondImage(); |
272
|
|
|
$image = str_replace($media_url, '', $image); |
273
|
|
|
unlink($path . $image); |
274
|
|
|
$game->setSecondImage(null); |
275
|
|
|
ErrorHandler::stop(true); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
if (! empty($data['uploadStylesheet']['tmp_name'])) { |
279
|
|
|
ErrorHandler::start(); |
280
|
|
|
move_uploaded_file($data['uploadStylesheet']['tmp_name'], $path . 'stylesheet_' . $game->getId() . '.css'); |
281
|
|
|
$game->setStylesheet($media_url . 'stylesheet_' . $game->getId() . '.css'); |
282
|
|
|
ErrorHandler::stop(true); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
View Code Duplication |
if (! empty($data['uploadFbShareImage']['tmp_name'])) { |
|
|
|
|
286
|
|
|
ErrorHandler::start(); |
287
|
|
|
$data['uploadFbShareImage']['name'] = $this->fileNewname( |
288
|
|
|
$path, |
289
|
|
|
$game->getId() . "-" . $data['uploadFbShareImage']['name'] |
290
|
|
|
); |
291
|
|
|
move_uploaded_file($data['uploadFbShareImage']['tmp_name'], $path . $data['uploadFbShareImage']['name']); |
292
|
|
|
$game->setFbShareImage($media_url . $data['uploadFbShareImage']['name']); |
293
|
|
|
ErrorHandler::stop(true); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
if (isset($data['deleteFbShareImage']) && |
297
|
|
|
$data['deleteFbShareImage'] && |
298
|
|
|
empty($data['uploadFbShareImage']['tmp_name']) |
299
|
|
|
) { |
300
|
|
|
ErrorHandler::start(); |
301
|
|
|
$image = $game->getFbShareImage(); |
302
|
|
|
$image = str_replace($media_url, '', $image); |
303
|
|
|
unlink($path . $image); |
304
|
|
|
$game->setFbShareImage(null); |
305
|
|
|
ErrorHandler::stop(true); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if (! empty($data['uploadFbPageTabImage']['tmp_name'])) { |
309
|
|
|
ErrorHandler::start(); |
310
|
|
|
$extension = $this->getExtension(strtolower($data['uploadFbPageTabImage']['name'])); |
311
|
|
|
$src = $this->getSrc($extension, $data['uploadFbPageTabImage']['tmp_name']); |
312
|
|
|
$this->resize( |
313
|
|
|
$data['uploadFbPageTabImage']['tmp_name'], |
314
|
|
|
$extension, |
315
|
|
|
$path . $game->getId() . "-" . $data['uploadFbPageTabImage']['name'], |
316
|
|
|
$src, |
317
|
|
|
111, |
318
|
|
|
74 |
319
|
|
|
); |
320
|
|
|
|
321
|
|
|
$game->setFbPageTabImage($media_url . $game->getId() . "-" . $data['uploadFbPageTabImage']['name']); |
322
|
|
|
ErrorHandler::stop(true); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if (isset($data['deleteFbPageTabImage']) && |
326
|
|
|
$data['deleteFbPageTabImage'] && |
327
|
|
|
empty($data['uploadFbPageTabImage']['tmp_name']) |
328
|
|
|
) { |
329
|
|
|
ErrorHandler::start(); |
330
|
|
|
$image = $game->getFbPageTabImage(); |
331
|
|
|
$image = str_replace($media_url, '', $image); |
332
|
|
|
unlink($path . $image); |
333
|
|
|
$game->setFbPageTabImage(null); |
334
|
|
|
ErrorHandler::stop(true); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
// Let's remove the fbPostId if there is no post to send anymore |
338
|
|
|
if ($data['broadcastPostFacebook'] == 0) { |
339
|
|
|
$game->setFbPostId(null); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$game = $this->getGameMapper()->update($game); |
343
|
|
|
|
344
|
|
|
$prize_mapper = $this->serviceLocator->get('playgroundgame_prize_mapper'); |
345
|
|
|
if (isset($data['prizes'])) { |
346
|
|
|
foreach ($data['prizes'] as $prize_data) { |
347
|
|
|
if (! empty($prize_data['picture_file']['tmp_name']) && ! $prize_data['picture_file']['error']) { |
348
|
|
|
if ($prize_data['id']) { |
349
|
|
|
$prize = $prize_mapper->findById($prize_data['id']); |
350
|
|
|
} else { |
351
|
|
|
$some_prizes = $prize_mapper->findBy(array( |
352
|
|
|
'game' => $game, |
353
|
|
|
'title' => $prize_data['title'] |
354
|
|
|
)); |
355
|
|
|
if (count($some_prizes) == 1) { |
356
|
|
|
$prize = $some_prizes[0]; |
357
|
|
|
} else { |
358
|
|
|
return false; |
|
|
|
|
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
// Remove if existing image |
362
|
|
|
if ($prize->getPicture() && file_exists($prize->getPicture())) { |
363
|
|
|
unlink($prize->getPicture()); |
364
|
|
|
$prize->getPicture(null); |
365
|
|
|
} |
366
|
|
|
// Upload and set new |
367
|
|
|
ErrorHandler::start(); |
368
|
|
|
$filename = "game-" . $game->getId() . "-prize-"; |
369
|
|
|
$filename .= $prize->getId() . "-" . $prize_data['picture_file']['name']; |
370
|
|
|
move_uploaded_file($prize_data['picture_file']['tmp_name'], $path . $filename); |
371
|
|
|
$prize->setPicture($media_url . $filename); |
372
|
|
|
ErrorHandler::stop(true); |
373
|
|
|
$prize_mapper->update($prize); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
// If I receive false, it means that the FB Id was not available anymore |
378
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
379
|
|
|
'game' => $game |
380
|
|
|
)); |
381
|
|
|
|
382
|
|
|
return $game; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* getNextGames |
387
|
|
|
* |
388
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
389
|
|
|
*/ |
390
|
|
|
public function getNextGames($dateStart = null, $dateEnd = null, $classType = null, $cost = null, $order = null, $dir = 'DESC') |
391
|
|
|
{ |
392
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
393
|
|
|
if ($dateStart === null) { |
394
|
|
|
$today = new \DateTime("now"); |
395
|
|
|
} else { |
396
|
|
|
$today = new \DateTime($dateStart); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
400
|
|
|
$orderBy = 'g.startDate'; |
401
|
|
|
if ($order != null) { |
402
|
|
|
$orderBy = 'g.'.$order; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
$qb = $em->createQueryBuilder(); |
406
|
|
|
$and = $qb->expr()->andx(); |
407
|
|
|
$and->add($qb->expr()->gte('g.startDate', ':date')); |
408
|
|
|
$qb->setParameter('date', $today); |
409
|
|
|
if ($dateEnd != null) { |
410
|
|
|
$dateEnd = new \DateTime($dateEnd); |
411
|
|
|
$end = $dateEnd->format('Y-m-d H:i:s'); |
412
|
|
|
$and->add($qb->expr()->lte('g.endDate', ':datefin')); |
413
|
|
|
$qb->setParameter('datefin', $end); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
$and->add($qb->expr()->eq('g.active', '1')); |
417
|
|
|
$and->add($qb->expr()->eq('g.broadcastPlatform', '1')); |
418
|
|
|
|
419
|
|
|
if ($classType != '') { |
420
|
|
|
$and->add($qb->expr()->eq('g.classType', ':classType')); |
421
|
|
|
$qb->setParameter('classType', $classType); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
if ($cost !== null) { |
425
|
|
|
$and->add($qb->expr()->eq('g.costToPlay', ':cost')); |
426
|
|
|
$qb->setParameter('cost', $cost); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
$qb->select('g') |
430
|
|
|
->from('PlaygroundGame\Entity\Game', 'g') |
431
|
|
|
->where($and) |
432
|
|
|
->orderBy($orderBy, $dir); |
433
|
|
|
|
434
|
|
|
$query = $qb->getQuery(); |
435
|
|
|
//echo $query->getSql(); |
436
|
|
|
$games = $query->getResult(); |
437
|
|
|
|
438
|
|
|
// je les classe par date de publication (date comme clé dans le tableau afin de pouvoir merger les objets |
439
|
|
|
// de type article avec le même procédé en les classant naturellement par date asc ou desc |
440
|
|
|
$arrayGames = array(); |
441
|
|
View Code Duplication |
foreach ($games as $game) { |
|
|
|
|
442
|
|
|
if ($game->getPublicationDate()) { |
443
|
|
|
$key = $game->getPublicationDate()->format('Ymd'); |
444
|
|
|
} elseif ($game->getStartDate()) { |
445
|
|
|
$key = $game->getStartDate()->format('Ymd'); |
446
|
|
|
} else { |
447
|
|
|
$key = $game->getUpdatedAt()->format('Ymd'); |
448
|
|
|
} |
449
|
|
|
$key .= $game->getUpdatedAt()->format('Ymd') . '-' . $game->getId(); |
450
|
|
|
$arrayGames[$key] = $game; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
return $arrayGames; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* getActiveGames |
458
|
|
|
* |
459
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
460
|
|
|
*/ |
461
|
|
|
public function getActiveGames($displayHome = null, $classType = '', $order = '', $dir = 'DESC') |
462
|
|
|
{ |
463
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
464
|
|
|
$today = new \DateTime("now"); |
465
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
466
|
|
|
$orderBy = 'g.publicationDate'; |
467
|
|
|
if ($order != '') { |
468
|
|
|
$orderBy = 'g.'.$order; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
$qb = $em->createQueryBuilder(); |
472
|
|
|
$and = $qb->expr()->andx(); |
473
|
|
|
$and->add( |
474
|
|
|
$qb->expr()->orX( |
475
|
|
|
$qb->expr()->lte('g.publicationDate', ':date'), |
476
|
|
|
$qb->expr()->isNull('g.publicationDate') |
477
|
|
|
) |
478
|
|
|
); |
479
|
|
|
$and->add( |
480
|
|
|
$qb->expr()->orX( |
481
|
|
|
$qb->expr()->gte('g.closeDate', ':date'), |
482
|
|
|
$qb->expr()->isNull('g.closeDate') |
483
|
|
|
) |
484
|
|
|
); |
485
|
|
|
$qb->setParameter('date', $today); |
486
|
|
|
|
487
|
|
|
$and->add($qb->expr()->eq('g.active', '1')); |
488
|
|
|
$and->add($qb->expr()->eq('g.broadcastPlatform', '1')); |
489
|
|
|
|
490
|
|
|
if ($classType != '') { |
491
|
|
|
$and->add($qb->expr()->eq('g.classType', ':classType')); |
492
|
|
|
$qb->setParameter('classType', $classType); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
if ($displayHome !== null) { |
496
|
|
|
$and->add($qb->expr()->eq('g.displayHome', ':key')); |
497
|
|
|
$qb->setParameter('key', $displayHome); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
$qb->select('g') |
501
|
|
|
->from('PlaygroundGame\Entity\Game', 'g') |
502
|
|
|
->where($and) |
503
|
|
|
->orderBy($orderBy, $dir); |
504
|
|
|
|
505
|
|
|
$query = $qb->getQuery(); |
506
|
|
|
$games = $query->getResult(); |
507
|
|
|
|
508
|
|
|
// je les classe par date de publication (date comme clé dans le tableau afin de pouvoir merger les objets |
509
|
|
|
// de type article avec le même procédé en les classant naturellement par date asc ou desc |
510
|
|
|
$arrayGames = array(); |
511
|
|
View Code Duplication |
foreach ($games as $game) { |
|
|
|
|
512
|
|
|
if ($game->getPublicationDate()) { |
513
|
|
|
$key = $game->getPublicationDate()->format('Ymd'); |
514
|
|
|
} elseif ($game->getStartDate()) { |
515
|
|
|
$key = $game->getStartDate()->format('Ymd'); |
516
|
|
|
} else { |
517
|
|
|
$key = $game->getUpdatedAt()->format('Ymd'); |
518
|
|
|
} |
519
|
|
|
$key .= $game->getUpdatedAt()->format('Ymd') . '-' . $game->getId(); |
520
|
|
|
$arrayGames[$key] = $game; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
return $arrayGames; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* getAvailableGames : Games OnLine and not already played by $user |
528
|
|
|
* |
529
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
530
|
|
|
*/ |
531
|
|
|
public function getAvailableGames($user, $maxResults = 2) |
532
|
|
|
{ |
533
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
534
|
|
|
$today = new \DateTime("now"); |
535
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
536
|
|
|
|
537
|
|
|
// Game active with a start_date before today (or without start_date) |
538
|
|
|
// and end_date after today (or without end-date) |
539
|
|
|
$query = $em->createQuery('SELECT g FROM PlaygroundGame\Entity\Game g |
540
|
|
|
WHERE NOT EXISTS (SELECT l FROM PlaygroundGame\Entity\Entry l WHERE l.game = g AND l.user = :user) |
541
|
|
|
AND (g.startDate <= :date OR g.startDate IS NULL) |
542
|
|
|
AND (g.endDate >= :date OR g.endDate IS NULL) |
543
|
|
|
AND g.active = 1 AND g.broadcastPlatform = 1 |
544
|
|
|
ORDER BY g.startDate ASC'); |
545
|
|
|
$query->setParameter('date', $today); |
546
|
|
|
$query->setParameter('user', $user); |
547
|
|
|
$query->setMaxResults($maxResults); |
548
|
|
|
$games = $query->getResult(); |
549
|
|
|
|
550
|
|
|
return $games; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
public function getGrid($game) |
554
|
|
|
{ |
555
|
|
|
$qb = $this->getEntriesQuery($game); |
556
|
|
|
|
557
|
|
|
/* @var $grid \ZfcDatagrid\Datagrid */ |
558
|
|
|
$grid = $this->serviceLocator->get('ZfcDatagrid\Datagrid'); |
559
|
|
|
$grid->setTitle('Entries'); |
560
|
|
|
$grid->setDataSource($qb); |
561
|
|
|
|
562
|
|
|
$col = new Column\Select('id', 'u'); |
563
|
|
|
$col->setLabel('Id'); |
564
|
|
|
$col->setIdentity(true); |
565
|
|
|
$grid->addColumn($col); |
566
|
|
|
|
567
|
|
|
$colType = new Type\DateTime( |
568
|
|
|
'Y-m-d H:i:s', |
569
|
|
|
\IntlDateFormatter::MEDIUM, |
570
|
|
|
\IntlDateFormatter::MEDIUM |
571
|
|
|
); |
572
|
|
|
$colType->setSourceTimezone('UTC'); |
573
|
|
|
|
574
|
|
|
$col = new Column\Select('created_at', 'u'); |
575
|
|
|
$col->setLabel('Created'); |
576
|
|
|
$col->setType($colType); |
577
|
|
|
$grid->addColumn($col); |
578
|
|
|
|
579
|
|
|
$col = new Column\Select('username', 'u'); |
580
|
|
|
$col->setLabel('Username'); |
581
|
|
|
$grid->addColumn($col); |
582
|
|
|
|
583
|
|
|
$col = new Column\Select('email', 'u'); |
584
|
|
|
$col->setLabel('Email'); |
585
|
|
|
$grid->addColumn($col); |
586
|
|
|
|
587
|
|
|
$col = new Column\Select('firstname', 'u'); |
588
|
|
|
$col->setLabel('Firstname'); |
589
|
|
|
$grid->addColumn($col); |
590
|
|
|
|
591
|
|
|
$col = new Column\Select('lastname', 'u'); |
592
|
|
|
$col->setLabel('Lastname'); |
593
|
|
|
$grid->addColumn($col); |
594
|
|
|
|
595
|
|
|
$col = new Column\Select('winner', 'e'); |
596
|
|
|
$col->setLabel('Status'); |
597
|
|
|
$col->setReplaceValues( |
598
|
|
|
[ |
599
|
|
|
0 => 'looser', |
600
|
|
|
1 => 'winner', |
601
|
|
|
] |
602
|
|
|
); |
603
|
|
|
$grid->addColumn($col); |
604
|
|
|
|
605
|
|
|
if (method_exists($game, 'getDrawAuto') && $game->getDrawAuto()) { |
606
|
|
|
$adminUrl = $this->serviceLocator->get('ControllerPluginManager')->get('adminUrl'); |
607
|
|
|
|
608
|
|
|
$grid->setToolbarTemplateVariables( |
609
|
|
|
['globalActions' => |
610
|
|
|
[ |
611
|
|
|
$this->serviceLocator->get('MvcTranslator')->translate('Draw') => |
612
|
|
|
$adminUrl->fromRoute($game->getClassType() .'/draw', array('gameId' => $game->getId())) |
613
|
|
|
] |
614
|
|
|
] |
615
|
|
|
); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
// $actions = new Column\Action(); |
619
|
|
|
// $actions->setLabel(''); |
620
|
|
|
|
621
|
|
|
// $viewAction = new Column\Action\Button(); |
622
|
|
|
// $viewAction->setLabel('Reset Password'); |
623
|
|
|
// $rowId = $viewAction->getRowIdPlaceholder(); |
624
|
|
|
// $viewAction->setLink('/admin/user/reset/'.$rowId); |
625
|
|
|
// $actions->addAction($viewAction); |
626
|
|
|
|
627
|
|
|
// $grid->addColumn($actions); |
628
|
|
|
|
629
|
|
|
// $action = new Action\Mass(); |
630
|
|
|
// $action->setTitle('This is incredible'); |
631
|
|
|
// $action->setLink('/test'); |
632
|
|
|
// $action->setConfirm(true); |
633
|
|
|
// $grid->addMassAction($action); |
634
|
|
|
|
635
|
|
|
return $grid; |
636
|
|
|
} |
637
|
|
|
|
638
|
|
View Code Duplication |
public function getEntriesQuery($game) |
|
|
|
|
639
|
|
|
{ |
640
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
641
|
|
|
|
642
|
|
|
$qb = $em->createQueryBuilder(); |
643
|
|
|
$qb->select(' |
644
|
|
|
e.id, |
645
|
|
|
u.username, |
646
|
|
|
u.title, |
647
|
|
|
u.firstname, |
648
|
|
|
u.lastname, |
649
|
|
|
u.email, |
650
|
|
|
u.optin, |
651
|
|
|
u.optinPartner, |
652
|
|
|
u.address, |
653
|
|
|
u.address2, |
654
|
|
|
u.postalCode, |
655
|
|
|
u.city, |
656
|
|
|
u.country, |
657
|
|
|
u.telephone, |
658
|
|
|
u.mobile, |
659
|
|
|
u.created_at, |
660
|
|
|
u.dob, |
661
|
|
|
e.winner, |
662
|
|
|
e.socialShares, |
663
|
|
|
e.playerData, |
664
|
|
|
e.geoloc, |
665
|
|
|
e.updated_at |
666
|
|
|
') |
667
|
|
|
->from('PlaygroundGame\Entity\Entry', 'e') |
668
|
|
|
->leftJoin('e.user', 'u') |
669
|
|
|
->where($qb->expr()->eq('e.game', ':game')); |
670
|
|
|
|
671
|
|
|
$qb->setParameter('game', $game); |
672
|
|
|
|
673
|
|
|
return $qb; |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
/** |
677
|
|
|
* DEPRECATED |
678
|
|
|
*/ |
679
|
|
|
public function getEntriesHeader($game) |
680
|
|
|
{ |
681
|
|
|
if ($game->getAnonymousAllowed() && $game->getPlayerForm()) { |
682
|
|
|
$formPV = json_decode($game->getPlayerForm()->getForm(), true); |
683
|
|
|
$header = array('id'=> 1); |
684
|
|
View Code Duplication |
foreach ($formPV as $element) { |
|
|
|
|
685
|
|
|
foreach ($element as $k => $v) { |
686
|
|
|
if ($k !== 'form_properties') { |
687
|
|
|
$header[$v[0]['name']] = 1; |
688
|
|
|
} |
689
|
|
|
} |
690
|
|
|
} |
691
|
|
|
} elseif ($game->getAnonymousAllowed()) { |
692
|
|
|
$header = array( |
693
|
|
|
'u.id' => 1, |
694
|
|
|
'u.ip' => 1 |
695
|
|
|
); |
696
|
|
|
} else { |
697
|
|
|
$header = array( |
698
|
|
|
'u.id' => 1, |
699
|
|
|
'u.username' => 1, |
700
|
|
|
'u.title' => 1, |
701
|
|
|
'u.firstname' => 1, |
702
|
|
|
'u.lastname' => 1, |
703
|
|
|
'u.displayName' => 1, |
704
|
|
|
'u.email' => 1, |
705
|
|
|
'u.optin' => 1, |
706
|
|
|
'u.optinPartner' => 1, |
707
|
|
|
'u.address' => 1, |
708
|
|
|
'u.address2' => 1, |
709
|
|
|
'u.postalCode' => 1, |
710
|
|
|
'u.city' => 1, |
711
|
|
|
'u.country' => 1, |
712
|
|
|
'u.telephone' => 1, |
713
|
|
|
'u.mobile' => 1, |
714
|
|
|
'u.created_at' => 1, |
715
|
|
|
'u.dob' => 1 |
716
|
|
|
); |
717
|
|
|
} |
718
|
|
|
$header['e.geoloc'] = 1; |
719
|
|
|
$header['e.winner'] = 1; |
720
|
|
|
$header['e.socialShares'] = 1; |
721
|
|
|
$header['e.updated_at'] = 1; |
722
|
|
|
|
723
|
|
|
return $header; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* DEPRECATED |
728
|
|
|
* getGameEntries : I create an array of entries based on playerData + header |
729
|
|
|
* |
730
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
731
|
|
|
*/ |
732
|
|
|
public function getGameEntries($header, $entries, $game) |
733
|
|
|
{ |
734
|
|
|
$header = $this->getEntriesHeader($game); |
735
|
|
|
|
736
|
|
|
$results = array(); |
737
|
|
|
|
738
|
|
|
foreach ($entries as $k => $entry) { |
739
|
|
|
$entryData = json_decode($entry['playerData'], true); |
740
|
|
View Code Duplication |
foreach ($header as $key => $v) { |
|
|
|
|
741
|
|
|
if (isset($entryData[$key])) { |
742
|
|
|
$results[$k][$key] = (is_array($entryData[$key]))?implode(', ', $entryData[$key]):$entryData[$key]; |
743
|
|
|
} elseif (array_key_exists($key, $entry)) { |
744
|
|
|
$results[$k][$key] = ($entry[$key] instanceof \DateTime)? |
745
|
|
|
$entry[$key]->format('Y-m-d H:i:s'): |
746
|
|
|
$entry[$key]; |
747
|
|
|
} else { |
748
|
|
|
$results[$k][$key] = ''; |
749
|
|
|
} |
750
|
|
|
} |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
return $results; |
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
/** |
757
|
|
|
* getActiveSliderGames |
758
|
|
|
* |
759
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
760
|
|
|
*/ |
761
|
|
|
public function getActiveSliderGames() |
762
|
|
|
{ |
763
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
764
|
|
|
$today = new \DateTime("now"); |
765
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
766
|
|
|
|
767
|
|
|
// Game active with a start_date before today (or without start_date) |
768
|
|
|
// and end_date after today (or without end-date) |
769
|
|
|
$query = $em->createQuery('SELECT g FROM PlaygroundGame\Entity\Game g |
770
|
|
|
WHERE (g.publicationDate <= :date OR g.publicationDate IS NULL) |
771
|
|
|
AND (g.closeDate >= :date OR g.closeDate IS NULL) |
772
|
|
|
AND g.active = true AND g.broadcastPlatform = 1 AND g.pushHome = true'); |
773
|
|
|
$query->setParameter('date', $today); |
774
|
|
|
$games = $query->getResult(); |
775
|
|
|
|
776
|
|
|
// je les classe par date de publication (date comme clé dans le tableau afin de pouvoir merger les objets |
777
|
|
|
// de type article avec le même procédé en les classant naturellement par date asc ou desc |
778
|
|
|
$arrayGames = array(); |
779
|
|
View Code Duplication |
foreach ($games as $game) { |
|
|
|
|
780
|
|
|
if ($game->getPublicationDate()) { |
781
|
|
|
$key = $game->getPublicationDate()->format('Ymd'); |
782
|
|
|
} elseif ($game->getStartDate()) { |
783
|
|
|
$key = $game->getStartDate()->format('Ymd'); |
784
|
|
|
} else { |
785
|
|
|
$key = $game->getUpdatedAt()->format('Ymd'); |
786
|
|
|
} |
787
|
|
|
$key .= $game->getUpdatedAt()->format('Ymd') . '-' . $game->getId(); |
788
|
|
|
$arrayGames[$key] = $game; |
789
|
|
|
} |
790
|
|
|
|
791
|
|
|
return $arrayGames; |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
/** |
795
|
|
|
* getPrizeCategoryGames |
796
|
|
|
* |
797
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
798
|
|
|
*/ |
799
|
|
View Code Duplication |
public function getPrizeCategoryGames($categoryid) |
|
|
|
|
800
|
|
|
{ |
801
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
802
|
|
|
|
803
|
|
|
$query = $em->createQuery('SELECT g FROM PlaygroundGame\Entity\Game g |
804
|
|
|
WHERE (g.prizeCategory = :categoryid AND g.broadcastPlatform = 1) |
805
|
|
|
ORDER BY g.publicationDate DESC'); |
806
|
|
|
$query->setParameter('categoryid', $categoryid); |
807
|
|
|
$games = $query->getResult(); |
808
|
|
|
|
809
|
|
|
return $games; |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
public function getGameIdentifierFromFacebook($fbPageId) |
813
|
|
|
{ |
814
|
|
|
$identifier = null; |
815
|
|
|
$game = $this->getGameMapper()->findOneBy(array('fbPageId' => $fbPageId, 'broadcastFacebook' => 1)); |
816
|
|
|
|
817
|
|
|
if ($game && $game->getIdentifier() !== null) { |
818
|
|
|
$identifier = $game->getIdentifier(); |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
return $identifier; |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
public function checkGame($identifier, $checkIfStarted = true) |
825
|
|
|
{ |
826
|
|
|
$gameMapper = $this->getGameMapper(); |
827
|
|
|
|
828
|
|
|
if (! $identifier) { |
829
|
|
|
return false; |
830
|
|
|
} |
831
|
|
|
|
832
|
|
|
$game = $gameMapper->findByIdentifier($identifier); |
833
|
|
|
|
834
|
|
|
// the game has not been found |
835
|
|
|
if (! $game) { |
836
|
|
|
return false; |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
// for preview stuff as admin |
840
|
|
|
if ($this->isAllowed('game', 'edit')) { |
841
|
|
|
//$r =$this->serviceLocator->get('request'); |
842
|
|
|
//if ($r->getQuery()->get('preview')) { |
843
|
|
|
$game->setActive(true); |
844
|
|
|
$game->setStartDate(null); |
845
|
|
|
$game->setEndDate(null); |
846
|
|
|
$game->setPublicationDate(null); |
847
|
|
|
$game->setBroadcastPlatform(true); |
848
|
|
|
|
849
|
|
|
// I don't want the game to be updated through any update during the preview mode. |
850
|
|
|
// I mark it as readonly for Doctrine |
851
|
|
|
$this->serviceLocator |
852
|
|
|
->get('doctrine.entitymanager.orm_default') |
853
|
|
|
->getUnitOfWork() |
854
|
|
|
->markReadOnly($game); |
855
|
|
|
|
856
|
|
|
return $game; |
857
|
|
|
//} |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
// The game is inactive |
861
|
|
|
if (! $game->getActive()) { |
862
|
|
|
return false; |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
// the game has not begun yet |
866
|
|
|
if (! $game->isOpen()) { |
867
|
|
|
return false; |
868
|
|
|
} |
869
|
|
|
|
870
|
|
|
// the game is finished and closed |
871
|
|
|
if (! $game->isStarted() && $checkIfStarted) { |
872
|
|
|
return false; |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
return $game; |
876
|
|
|
} |
877
|
|
|
|
878
|
|
|
/** |
879
|
|
|
* Return the last entry of the user on this game, if it exists. |
880
|
|
|
* An entry can be associated to : |
881
|
|
|
* - A user account (a real one, linked to PlaygroundUser |
882
|
|
|
* - An anonymous Identifier (based on one value of playerData (generally email)) |
883
|
|
|
* - A cookie set on the player device (the less secure) |
884
|
|
|
* If the active param is set, it can check if the entry is active or not. |
885
|
|
|
* If the bonus param is set, it can check if the entry is a bonus or not. |
886
|
|
|
* |
887
|
|
|
* @param unknown $game |
888
|
|
|
* @param string $user |
889
|
|
|
* @param boolean $active |
890
|
|
|
* @param boolean $bonus |
891
|
|
|
* @return boolean |
892
|
|
|
*/ |
893
|
|
|
public function checkExistingEntry($game, $user = null, $active = null, $bonus = null) |
894
|
|
|
{ |
895
|
|
|
$search = array('game' => $game); |
896
|
|
|
|
897
|
|
|
if ($user) { |
|
|
|
|
898
|
|
|
$search['user'] = $user; |
899
|
|
|
} elseif ($this->getAnonymousIdentifier()) { |
900
|
|
|
$search['anonymousIdentifier'] = $this->getAnonymousIdentifier(); |
901
|
|
|
$search['user'] = null; |
902
|
|
|
} else { |
903
|
|
|
$search['anonymousId'] = $this->getAnonymousId(); |
904
|
|
|
$search['user'] = null; |
905
|
|
|
} |
906
|
|
|
|
907
|
|
|
if (! is_null($active)) { |
908
|
|
|
$search['active'] = $active; |
909
|
|
|
} |
910
|
|
|
if (! is_null($bonus)) { |
911
|
|
|
$search['bonus'] = $bonus; |
912
|
|
|
} |
913
|
|
|
|
914
|
|
|
$entry = $this->getEntryMapper()->findOneBy($search, array('updated_at' => 'desc')); |
915
|
|
|
|
916
|
|
|
return $entry; |
917
|
|
|
} |
918
|
|
|
|
919
|
|
|
/* |
920
|
|
|
* This function updates the entry with the player data after checking |
921
|
|
|
* that the data are compliant with the formUser Game attribute |
922
|
|
|
* |
923
|
|
|
* The $data has to be a json object |
924
|
|
|
*/ |
925
|
|
|
public function updateEntryPlayerForm($data, $game, $user, $entry, $mandatory = true) |
|
|
|
|
926
|
|
|
{ |
927
|
|
|
$form = $this->createFormFromJson($game->getPlayerForm()->getForm(), 'playerForm'); |
928
|
|
|
$form->setData($data); |
929
|
|
|
|
930
|
|
|
if (!$mandatory) { |
931
|
|
|
$filter = $form->getInputFilter(); |
932
|
|
|
foreach ($form->getElements() as $element) { |
933
|
|
|
try { |
934
|
|
|
$elementInput = $filter->get($element->getName()); |
935
|
|
|
$elementInput->setRequired(false); |
936
|
|
|
$form->get($element->getName())->setAttribute('required', false); |
937
|
|
|
} catch (\Zend\Form\Exception\InvalidElementException $e) { |
|
|
|
|
938
|
|
|
} |
939
|
|
|
} |
940
|
|
|
} |
941
|
|
|
|
942
|
|
|
if ($form->isValid()) { |
943
|
|
|
$dataJson = json_encode($form->getData()); |
944
|
|
|
|
945
|
|
|
if ($game->getAnonymousAllowed() && |
946
|
|
|
$game->getAnonymousIdentifier() && |
947
|
|
|
isset($data[$game->getAnonymousIdentifier()]) |
948
|
|
|
) { |
949
|
|
|
$session = new \Zend\Session\Container('anonymous_identifier'); |
950
|
|
|
$anonymousIdentifier = $data[$game->getAnonymousIdentifier()]; |
951
|
|
|
$entry->setAnonymousIdentifier($anonymousIdentifier); |
952
|
|
|
if (empty($session->offsetGet('anonymous_identifier'))) { |
953
|
|
|
$session->offsetSet('anonymous_identifier', $anonymousIdentifier); |
954
|
|
|
} |
955
|
|
|
} |
956
|
|
|
|
957
|
|
|
$entry->setPlayerData($dataJson); |
958
|
|
|
$this->getEntryMapper()->update($entry); |
959
|
|
|
} else { |
960
|
|
|
return false; |
961
|
|
|
} |
962
|
|
|
|
963
|
|
|
return true; |
964
|
|
|
} |
965
|
|
|
|
966
|
|
|
|
967
|
|
|
public function checkIsFan($game) |
|
|
|
|
968
|
|
|
{ |
969
|
|
|
// If on Facebook, check if you have to be a FB fan to play the game |
970
|
|
|
$session = new Container('facebook'); |
971
|
|
|
|
972
|
|
|
if ($session->offsetExists('signed_request')) { |
973
|
|
|
// I'm on Facebook |
974
|
|
|
$sr = $session->offsetGet('signed_request'); |
975
|
|
|
if ($sr['page']['liked'] == 1) { |
976
|
|
|
return true; |
977
|
|
|
} |
978
|
|
|
} else { |
979
|
|
|
// I'm not on Facebook |
980
|
|
|
return true; |
981
|
|
|
} |
982
|
|
|
|
983
|
|
|
return false; |
984
|
|
|
} |
985
|
|
|
|
986
|
|
|
public function getAnonymousIdentifier() |
987
|
|
|
{ |
988
|
|
|
if (is_null($this->anonymousIdentifier) || $this->anonymousIdentifier === false) { |
989
|
|
|
$session = new Container('anonymous_identifier'); |
990
|
|
|
|
991
|
|
|
if ($session->offsetExists('anonymous_identifier')) { |
992
|
|
|
$this->anonymousIdentifier = $session->offsetGet('anonymous_identifier'); |
993
|
|
|
} else { |
994
|
|
|
$this->anonymousIdentifier = false; |
995
|
|
|
} |
996
|
|
|
} |
997
|
|
|
|
998
|
|
|
return $this->anonymousIdentifier; |
999
|
|
|
} |
1000
|
|
|
|
1001
|
|
|
/** |
1002
|
|
|
* setTermsOptin |
1003
|
|
|
* |
1004
|
|
|
* @return Boolean the result of the terms optin update |
1005
|
|
|
*/ |
1006
|
|
|
public function setTermsOptin($optin, $game, $user) |
1007
|
|
|
{ |
1008
|
|
|
$entry = $this->checkExistingEntry($game, $user, true); |
1009
|
|
|
|
1010
|
|
|
if ($entry) { |
1011
|
|
|
$entry->setTermsOptin($optin); |
|
|
|
|
1012
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1013
|
|
|
} else { |
1014
|
|
|
if (!$this->hasReachedPlayLimit($game, $user) |
1015
|
|
|
&& $this->canPayToPlay($game, $user) |
1016
|
|
|
) { |
1017
|
|
|
$ip = $this->getIp(); |
1018
|
|
|
$geoloc = $this->getGeoloc($ip); |
1019
|
|
|
$entry = new Entry(); |
1020
|
|
|
$entry->setGame($game); |
1021
|
|
|
$entry->setUser($user); |
1022
|
|
|
$entry->setPoints(0); |
1023
|
|
|
$entry->setIp($ip); |
1024
|
|
|
$entry->setGeoloc($geoloc); |
1025
|
|
|
$entry->setAnonymousId($this->getAnonymousId()); |
1026
|
|
|
if ($this->getAnonymousIdentifier()) { |
1027
|
|
|
$entry->setAnonymousIdentifier($this->getAnonymousIdentifier()); |
1028
|
|
|
} |
1029
|
|
|
$entry->setTermsOptin($optin); |
1030
|
|
|
$entry = $this->getEntryMapper()->insert($entry); |
1031
|
|
|
} |
1032
|
|
|
} |
1033
|
|
|
|
1034
|
|
|
if ($entry) { |
1035
|
|
|
$this->getEventManager()->trigger( |
1036
|
|
|
__FUNCTION__ . '.post', |
1037
|
|
|
$this, |
1038
|
|
|
[ |
1039
|
|
|
'user' => $user, |
1040
|
|
|
'game' => $game, |
1041
|
|
|
'entry' => $entry, |
1042
|
|
|
] |
1043
|
|
|
); |
1044
|
|
|
|
1045
|
|
|
return true; |
1046
|
|
|
} |
1047
|
|
|
|
1048
|
|
|
return false; |
1049
|
|
|
} |
1050
|
|
|
|
1051
|
|
|
/** |
1052
|
|
|
* errors : |
1053
|
|
|
* -1 : limit entry games for this user reached |
1054
|
|
|
* -2 : no terms optin |
1055
|
|
|
* -3 : no payment |
1056
|
|
|
* |
1057
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
1058
|
|
|
* @param \PlaygroundUser\Entity\UserInterface $user |
1059
|
|
|
* @return number unknown |
1060
|
|
|
*/ |
1061
|
|
|
public function play($game, $user, &$error = null) |
1062
|
|
|
{ |
1063
|
|
|
//some entries can stay open during days. |
1064
|
|
|
// The player can take back the entry to go on playing |
1065
|
|
|
// like in a postvote or for quizzes |
1066
|
|
|
// We create also an entry before the game when an optin is needed |
1067
|
|
|
$entry = $this->checkExistingEntry($game, $user, true); |
|
|
|
|
1068
|
|
|
|
1069
|
|
|
// If the game needs an optin and the player has not opted in |
1070
|
|
|
if ($entry && $game->getTermsOptin() && !$entry->getTermsOptin()) { |
|
|
|
|
1071
|
|
|
$error = -2; |
1072
|
|
|
return false; |
|
|
|
|
1073
|
|
|
} |
1074
|
|
|
|
1075
|
|
|
// If the game has a cost and the player has not paid yet |
1076
|
|
|
// (this case occurs when the entry has been created by the termsOptin |
1077
|
|
|
// and the game has a price to play) |
1078
|
|
|
if ($entry && !$entry->getPaid() && $game->getCostToPlay() > 0) { |
|
|
|
|
1079
|
|
|
if (!$this->payToPlay($game, $user, $entry)) { |
1080
|
|
|
$error = -3; |
1081
|
|
|
return false; |
|
|
|
|
1082
|
|
|
} else { |
1083
|
|
|
$entry->setPaid(1); |
|
|
|
|
1084
|
|
|
$entry->setPaidAmount($game->getCostToPlay()); |
|
|
|
|
1085
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1086
|
|
|
} |
1087
|
|
|
} |
1088
|
|
|
|
1089
|
|
|
if (! $entry) { |
1090
|
|
|
// Play limit reached |
1091
|
|
|
if ($this->hasReachedPlayLimit($game, $user)) { |
1092
|
|
|
$error = -1; |
1093
|
|
|
return false; |
|
|
|
|
1094
|
|
|
} |
1095
|
|
|
// Can't pay for the game |
1096
|
|
|
if (!$this->canPayToPlay($game, $user)) { |
1097
|
|
|
$error = -3; |
1098
|
|
|
return false; |
|
|
|
|
1099
|
|
|
} |
1100
|
|
|
// Terms optin not checked |
1101
|
|
|
if ($game->getTermsOptin()) { |
1102
|
|
|
$error = -2; |
1103
|
|
|
return false; |
|
|
|
|
1104
|
|
|
} |
1105
|
|
|
|
1106
|
|
|
$ip = $this->getIp(); |
1107
|
|
|
$geoloc = $this->getGeoloc($ip); |
1108
|
|
|
$entry = new Entry(); |
1109
|
|
|
$entry->setGame($game); |
1110
|
|
|
$entry->setUser($user); |
1111
|
|
|
$entry->setPoints(0); |
1112
|
|
|
$entry->setIp($ip); |
1113
|
|
|
$entry->setGeoloc($geoloc); |
1114
|
|
|
$entry->setAnonymousId($this->getAnonymousId()); |
1115
|
|
|
|
1116
|
|
|
if ($this->getAnonymousIdentifier()) { |
1117
|
|
|
$entry->setAnonymousIdentifier($this->getAnonymousIdentifier()); |
1118
|
|
|
} |
1119
|
|
|
if ($game->getCostToPlay() === 0) { |
1120
|
|
|
$entry->setPaid(true); |
1121
|
|
|
$entry = $this->getEntryMapper()->insert($entry); |
1122
|
|
|
$this->getEventManager()->trigger( |
1123
|
|
|
__FUNCTION__ . '.post', |
1124
|
|
|
$this, |
1125
|
|
|
[ |
1126
|
|
|
'user' => $user, |
1127
|
|
|
'game' => $game, |
1128
|
|
|
'entry' => $entry, |
1129
|
|
|
] |
1130
|
|
|
); |
1131
|
|
|
} else { |
1132
|
|
|
$entry = $this->getEntryMapper()->insert($entry); |
1133
|
|
|
$entryPaid = $this->payToPlay($game, $user, $entry); |
1134
|
|
|
if ($entryPaid) { |
1135
|
|
|
$entry = $this->getEntryMapper()->update($entryPaid); |
1136
|
|
|
$this->getEventManager()->trigger( |
1137
|
|
|
__FUNCTION__ . '.post', |
1138
|
|
|
$this, |
1139
|
|
|
[ |
1140
|
|
|
'user' => $user, |
1141
|
|
|
'game' => $game, |
1142
|
|
|
'entry' => $entry, |
1143
|
|
|
] |
1144
|
|
|
); |
1145
|
|
|
} else { |
1146
|
|
|
$entry->setActive(false); |
1147
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
|
|
|
|
1148
|
|
|
$error = -3; |
1149
|
|
|
return false; |
|
|
|
|
1150
|
|
|
} |
1151
|
|
|
} |
1152
|
|
|
} |
1153
|
|
|
|
1154
|
|
|
return $entry; |
1155
|
|
|
} |
1156
|
|
|
|
1157
|
|
|
/** |
1158
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
1159
|
|
|
* @param \PlaygroundUser\Entity\UserInterface $user |
1160
|
|
|
*/ |
1161
|
|
|
public function hasReachedPlayLimit($game, $user) |
1162
|
|
|
{ |
1163
|
|
|
// Is there a limitation on the game ? |
1164
|
|
|
$limitAmount = $game->getPlayLimit(); |
1165
|
|
|
if ($limitAmount) { |
1166
|
|
|
$limitScale = $game->getPlayLimitScale(); |
1167
|
|
|
$userEntries = $this->findLastEntries($game, $user, $limitScale); |
1168
|
|
|
|
1169
|
|
|
// player has reached the game limit |
1170
|
|
|
if ($userEntries >= $limitAmount) { |
1171
|
|
|
return true; |
1172
|
|
|
} |
1173
|
|
|
} |
1174
|
|
|
return false; |
1175
|
|
|
} |
1176
|
|
|
|
1177
|
|
|
/** |
1178
|
|
|
* If the game has a cost to be played (costToPlay>0) |
1179
|
|
|
* I check and decrement the price from the leaderboard all of the user |
1180
|
|
|
* |
1181
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
1182
|
|
|
* @param \PlaygroundUser\Entity\UserInterface $user |
1183
|
|
|
*/ |
1184
|
|
|
public function canPayToPlay($game, $user) |
1185
|
|
|
{ |
1186
|
|
|
// Is there a limitation on the game ? |
1187
|
|
|
$cost = $game->getCostToPlay(); |
1188
|
|
|
if ($cost && $cost > 0) { |
1189
|
|
|
$availableAmount = $this->getEventManager()->trigger( |
1190
|
|
|
'leaderboardUserTotal', |
1191
|
|
|
$this, |
1192
|
|
|
[ |
1193
|
|
|
'user' => $user, |
1194
|
|
|
] |
1195
|
|
|
)->last(); |
1196
|
|
|
if ($availableAmount && $availableAmount >= $cost) { |
1197
|
|
|
return true; |
1198
|
|
|
} |
1199
|
|
|
} else { |
1200
|
|
|
return true; |
1201
|
|
|
} |
1202
|
|
|
|
1203
|
|
|
return false; |
1204
|
|
|
} |
1205
|
|
|
|
1206
|
|
|
/** |
1207
|
|
|
* If the game has a cost to be played (costToPlay>0) |
1208
|
|
|
* I check and decrement the price from the leaderboard all of the user |
1209
|
|
|
* |
1210
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
1211
|
|
|
* @param \PlaygroundUser\Entity\UserInterface $user |
1212
|
|
|
*/ |
1213
|
|
|
public function payToPlay($game, $user, $entry) |
1214
|
|
|
{ |
1215
|
|
|
// Is there a limitation on the game ? |
1216
|
|
|
$cost = $game->getCostToPlay(); |
1217
|
|
|
if ($cost && $cost > 0) { |
1218
|
|
|
if ($this->canPayToPlay($game, $user)) { |
1219
|
|
|
$entry->setPaid(true); |
1220
|
|
|
$entry->setPaidAmount(-$cost); |
1221
|
|
|
$leaderboard = $this->getEventManager()->trigger( |
|
|
|
|
1222
|
|
|
'leaderboardUserUpdate', |
1223
|
|
|
$this, |
1224
|
|
|
[ |
1225
|
|
|
'user' => $user, |
1226
|
|
|
'game' => $game, |
1227
|
|
|
'points' => -$cost, |
1228
|
|
|
'entry' => $entry, |
1229
|
|
|
] |
1230
|
|
|
)->last(); |
1231
|
|
|
|
1232
|
|
|
return $entry; |
1233
|
|
|
} |
1234
|
|
|
} else { |
1235
|
|
|
$entry->setPaid(true); |
1236
|
|
|
return $entry; |
1237
|
|
|
} |
1238
|
|
|
|
1239
|
|
|
return false; |
1240
|
|
|
} |
1241
|
|
|
|
1242
|
|
|
/** |
1243
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
1244
|
|
|
* @param \PlaygroundUser\Entity\UserInterface $user |
1245
|
|
|
*/ |
1246
|
|
|
public function findLastEntries($game, $user, $limitScale) |
1247
|
|
|
{ |
1248
|
|
|
$limitDate = $this->getLimitDate($limitScale); |
1249
|
|
|
|
1250
|
|
|
if ($user) { |
1251
|
|
|
return $this->getEntryMapper()->countLastEntriesByUser($game, $user, $limitDate); |
1252
|
|
|
} elseif ($this->getAnonymousIdentifier()) { |
1253
|
|
|
$entries = $this->getEntryMapper()->countLastEntriesByAnonymousIdentifier( |
1254
|
|
|
$game, |
1255
|
|
|
$this->getAnonymousIdentifier(), |
1256
|
|
|
$limitDate |
1257
|
|
|
); |
1258
|
|
|
|
1259
|
|
|
return $entries; |
1260
|
|
View Code Duplication |
} else { |
|
|
|
|
1261
|
|
|
// If the game is supposed to be a reguler user game or an anonymous identified game, |
1262
|
|
|
// it means that the registration/login is at the end of the game |
1263
|
|
|
if ((!$user && !$game->getAnonymousAllowed()) || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) { |
1264
|
|
|
return 0; |
1265
|
|
|
} |
1266
|
|
|
return $this->getEntryMapper()->countLastEntriesByIp($game, $this->getIp(), $limitDate); |
1267
|
|
|
} |
1268
|
|
|
} |
1269
|
|
|
|
1270
|
|
|
/** |
1271
|
|
|
* |
1272
|
|
|
* |
1273
|
|
|
*/ |
1274
|
|
|
public function getLimitDate($limitScale) |
1275
|
|
|
{ |
1276
|
|
|
$now = new \DateTime("now"); |
1277
|
|
|
switch ($limitScale) { |
1278
|
|
|
case 'always': |
1279
|
|
|
$interval = 'P100Y'; |
1280
|
|
|
$now->sub(new \DateInterval($interval)); |
1281
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
1282
|
|
|
break; |
1283
|
|
|
case 'day': |
1284
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
1285
|
|
|
break; |
1286
|
|
View Code Duplication |
case 'week': |
|
|
|
|
1287
|
|
|
$interval = 'P7D'; |
1288
|
|
|
$now->sub(new \DateInterval($interval)); |
1289
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
1290
|
|
|
break; |
1291
|
|
View Code Duplication |
case 'month': |
|
|
|
|
1292
|
|
|
$interval = 'P1M'; |
1293
|
|
|
$now->sub(new \DateInterval($interval)); |
1294
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
1295
|
|
|
break; |
1296
|
|
View Code Duplication |
case 'year': |
|
|
|
|
1297
|
|
|
$interval = 'P1Y'; |
1298
|
|
|
$now->sub(new \DateInterval($interval)); |
1299
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
1300
|
|
|
break; |
1301
|
|
View Code Duplication |
default: |
|
|
|
|
1302
|
|
|
$interval = 'P100Y'; |
1303
|
|
|
$now->sub(new \DateInterval($interval)); |
1304
|
|
|
$dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
1305
|
|
|
} |
1306
|
|
|
|
1307
|
|
|
return $dateLimit; |
1308
|
|
|
} |
1309
|
|
|
|
1310
|
|
|
public function findLastActiveEntry($game, $user) |
1311
|
|
|
{ |
1312
|
|
|
return $this->checkExistingEntry($game, $user, true); |
1313
|
|
|
} |
1314
|
|
|
|
1315
|
|
|
public function findLastInactiveEntry($game, $user) |
1316
|
|
|
{ |
1317
|
|
|
return $this->checkExistingEntry($game, $user, false, false); |
1318
|
|
|
} |
1319
|
|
|
|
1320
|
|
|
public function findLastEntry($game, $user) |
1321
|
|
|
{ |
1322
|
|
|
return $this->checkExistingEntry($game, $user, null, false); |
1323
|
|
|
} |
1324
|
|
|
|
1325
|
|
|
public function inviteToTeam($data, $game, $user) |
1326
|
|
|
{ |
1327
|
|
|
$mailService = $this->serviceLocator->get('playgroundgame_message'); |
1328
|
|
|
$invitationMapper = $this->serviceLocator->get('playgroundgame_invitation_mapper'); |
1329
|
|
|
|
1330
|
|
|
$sentInvitations = $invitationMapper->findBy(array('host' => $user, 'game' => $game)); |
1331
|
|
|
$nbInvitations = count($sentInvitations); |
1332
|
|
|
$to = $data['email']; |
1333
|
|
|
if (empty($to)) { |
1334
|
|
|
return ['result'=>false, 'message'=>'no email']; |
1335
|
|
|
} |
1336
|
|
|
|
1337
|
|
|
if ($nbInvitations < 20) { |
1338
|
|
|
$alreadyInvited = $invitationMapper->findBy(array('requestKey' => $to, 'game' => $game)); |
1339
|
|
|
if (!$alreadyInvited) { |
1340
|
|
|
$alreadyInvited = $this->getUserMapper()->findByEmail($to); |
1341
|
|
|
} |
1342
|
|
|
|
1343
|
|
|
if (empty($alreadyInvited)) { |
1344
|
|
|
$invitation = new \PlaygroundGame\Entity\Invitation(); |
1345
|
|
|
$invitation->setRequestKey($to); |
1346
|
|
|
$invitation->setGame($game); |
1347
|
|
|
$invitation->setHost($user); |
1348
|
|
|
$invitationMapper->insert($invitation); |
1349
|
|
|
|
1350
|
|
|
$from = $this->getOptions()->getEmailFromAddress(); |
1351
|
|
|
$subject = $this->serviceLocator->get('MvcTranslator')->translate( |
1352
|
|
|
$this->getOptions()->getInviteToTeamSubjectLine(), |
1353
|
|
|
'playgroundgame' |
1354
|
|
|
); |
1355
|
|
|
$message = $mailService->createHtmlMessage( |
1356
|
|
|
$from, |
1357
|
|
|
$to, |
1358
|
|
|
$subject, |
1359
|
|
|
'playground-game/email/invite_team', |
1360
|
|
|
array( |
1361
|
|
|
'game' => $game, |
1362
|
|
|
'user' => $user, |
1363
|
|
|
'data' => $data, |
1364
|
|
|
'from' => $from |
1365
|
|
|
) |
1366
|
|
|
); |
1367
|
|
|
try { |
1368
|
|
|
$mailService->send($message); |
1369
|
|
|
} catch (\Zend\Mail\Protocol\Exception\RuntimeException $e) { |
|
|
|
|
1370
|
|
|
return ['result' => true, 'message' => $this->serviceLocator->get('MvcTranslator')->translate( |
1371
|
|
|
'mail error' |
1372
|
|
|
)]; |
1373
|
|
|
} |
1374
|
|
|
|
1375
|
|
|
return ['result' => true, 'message' => '']; |
1376
|
|
|
} else { |
1377
|
|
|
return ['result' => false, 'message' => 'already invited']; |
1378
|
|
|
} |
1379
|
|
|
} else { |
1380
|
|
|
return [ |
1381
|
|
|
'result' => false, |
1382
|
|
|
'message' => $this->serviceLocator->get('MvcTranslator')->translate( |
1383
|
|
|
'Too many invitations for this user' |
1384
|
|
|
) |
1385
|
|
|
]; |
1386
|
|
|
} |
1387
|
|
|
} |
1388
|
|
|
|
1389
|
|
|
public function sendShareMail( |
1390
|
|
|
$data, |
1391
|
|
|
$game, |
1392
|
|
|
$user, |
1393
|
|
|
$entry, |
1394
|
|
|
$template = 'share_game', |
1395
|
|
|
$subject = '' |
1396
|
|
|
) { |
1397
|
|
|
$mailService = $this->serviceLocator->get('playgroundgame_message'); |
1398
|
|
|
$mailSent = false; |
1399
|
|
|
$from = $this->getOptions()->getEmailFromAddress(); |
1400
|
|
|
|
1401
|
|
|
if (empty($subject) && $game) { |
1402
|
|
|
$subject = $game->getEmailShareSubject(); |
1403
|
|
|
} |
1404
|
|
|
|
1405
|
|
|
$message = ''; |
1406
|
|
|
if ($game && !empty($game->getEmailShareMessage())) { |
1407
|
|
|
$message = $game->getEmailShareMessage(); |
1408
|
|
|
} |
1409
|
|
|
|
1410
|
|
|
$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface'); |
1411
|
|
|
$skinUrl = $renderer->url( |
1412
|
|
|
'frontend', |
1413
|
|
|
array(), |
1414
|
|
|
array('force_canonical' => true) |
1415
|
|
|
); |
1416
|
|
|
$secretKey = strtoupper(substr(sha1(uniqid('pg_', true) . '####' . time()), 0, 15)); |
1417
|
|
|
|
1418
|
|
|
if (isset($data['email']) && !is_array($data['email'])) { |
1419
|
|
|
$data['email'] = array($data['email']); |
1420
|
|
|
} |
1421
|
|
|
|
1422
|
|
|
foreach ($data['email'] as $to) { |
1423
|
|
|
$mailSent = true; |
1424
|
|
|
if (!empty($to)) { |
1425
|
|
|
$message = $mailService->createHtmlMessage( |
1426
|
|
|
$from, |
1427
|
|
|
$to, |
1428
|
|
|
$subject, |
1429
|
|
|
'playground-game/email/' . $template, |
1430
|
|
|
array( |
1431
|
|
|
'game' => $game, |
1432
|
|
|
'data' => $data, |
1433
|
|
|
'from' => $from, |
1434
|
|
|
'to' => $to, |
1435
|
|
|
'secretKey' => $secretKey, |
1436
|
|
|
'skinUrl' => $skinUrl, |
1437
|
|
|
'message' => $message, |
1438
|
|
|
) |
1439
|
|
|
); |
1440
|
|
|
try { |
1441
|
|
|
$mailService->send($message); |
1442
|
|
|
} catch (\Zend\Mail\Protocol\Exception\RuntimeException $e) { |
|
|
|
|
1443
|
|
|
} |
1444
|
|
|
|
1445
|
|
|
if ($entry) { |
1446
|
|
|
$shares = json_decode($entry->getSocialShares(), true); |
1447
|
|
|
(!isset($shares['mail']))? $shares['mail'] = 1:$shares['mail'] += 1; |
1448
|
|
|
} |
1449
|
|
|
$this->getEventManager()->trigger( |
1450
|
|
|
__FUNCTION__ . '.post', |
1451
|
|
|
$this, |
1452
|
|
|
array( |
1453
|
|
|
'user' => $user, |
1454
|
|
|
'secretKey' => $secretKey, |
1455
|
|
|
'data' => $data, |
1456
|
|
|
'game' => $game, |
1457
|
|
|
'entry' => $entry, |
1458
|
|
|
'message' => $message, |
1459
|
|
|
'to' => $to, |
1460
|
|
|
) |
1461
|
|
|
); |
1462
|
|
|
} |
1463
|
|
|
} |
1464
|
|
|
|
1465
|
|
|
if ($mailSent) { |
1466
|
|
|
if ($entry) { |
1467
|
|
|
$sharesJson = json_encode($shares); |
|
|
|
|
1468
|
|
|
$entry->setSocialShares($sharesJson); |
1469
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
|
|
|
|
1470
|
|
|
} |
1471
|
|
|
|
1472
|
|
|
return true; |
1473
|
|
|
} |
1474
|
|
|
|
1475
|
|
|
return false; |
1476
|
|
|
} |
1477
|
|
|
|
1478
|
|
|
/** |
1479
|
|
|
* New standard function to send mails |
1480
|
|
|
* |
1481
|
|
|
*/ |
1482
|
|
|
public function mail($from, $to, $subject, $template, $data) |
1483
|
|
|
{ |
1484
|
|
|
$mailService = $this->serviceLocator->get('playgroundgame_message'); |
1485
|
|
|
$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface'); |
1486
|
|
|
$skinUrl = $renderer->url( |
1487
|
|
|
'frontend', |
1488
|
|
|
array(), |
1489
|
|
|
array('force_canonical' => true) |
1490
|
|
|
); |
1491
|
|
|
$data['skinUrl'] = $skinUrl; |
1492
|
|
|
$template = 'playground-game/email/' . $template; |
1493
|
|
|
$message = $mailService->createHtmlMessage($from, $to, $subject, $template, $data); |
1494
|
|
|
$mailService->send($message); |
1495
|
|
|
} |
1496
|
|
|
|
1497
|
|
|
/** |
1498
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
1499
|
|
|
* @param \PlaygroundUser\Entity\User $user |
1500
|
|
|
* @param Entry $entry |
1501
|
|
|
* @param \PlaygroundGame\Entity\Prize $prize |
1502
|
|
|
*/ |
1503
|
|
|
public function sendResultMail($game, $user, $entry, $template = 'entry', $prize = null) |
1504
|
|
|
{ |
1505
|
|
|
$mailService = $this->serviceLocator->get('playgroundgame_message'); |
1506
|
|
|
$from = $this->getOptions()->getEmailFromAddress(); |
1507
|
|
|
if ($entry->getAnonymousIdentifier()) { |
1508
|
|
|
$to = $entry->getAnonymousIdentifier(); |
1509
|
|
|
} elseif ($user) { |
1510
|
|
|
$to = $user->getEmail(); |
1511
|
|
|
} else { |
1512
|
|
|
return false; |
1513
|
|
|
} |
1514
|
|
|
$subject = $this->serviceLocator->get('MvcTranslator')->translate( |
1515
|
|
|
$this->getOptions()->getParticipationSubjectLine(), |
1516
|
|
|
'playgroundgame' |
1517
|
|
|
); |
1518
|
|
|
$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface'); |
1519
|
|
|
$skinUrl = $renderer->url( |
1520
|
|
|
'frontend', |
1521
|
|
|
array(), |
1522
|
|
|
array('force_canonical' => true) |
1523
|
|
|
); |
1524
|
|
|
$message = $mailService->createHtmlMessage($from, $to, $subject, 'playground-game/email/' . $template, array( |
1525
|
|
|
'game' => $game, |
1526
|
|
|
'entry' => $entry, |
1527
|
|
|
'skinUrl' => $skinUrl, |
1528
|
|
|
'prize' => $prize |
1529
|
|
|
)); |
1530
|
|
|
$mailService->send($message); |
1531
|
|
|
} |
1532
|
|
|
|
1533
|
|
|
public function sendGameMail($game, $user, $post, $template = 'postvote') |
1534
|
|
|
{ |
1535
|
|
|
$mailService = $this->serviceLocator->get('playgroundgame_message'); |
1536
|
|
|
$from = $this->getOptions()->getEmailFromAddress(); |
1537
|
|
|
$to = $user->getEmail(); |
1538
|
|
|
$subject = $this->serviceLocator->get('MvcTranslator')->translate( |
1539
|
|
|
$this->getOptions()->getParticipationSubjectLine(), |
1540
|
|
|
'playgroundgame' |
1541
|
|
|
); |
1542
|
|
|
$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface'); |
1543
|
|
|
$skinUrl = $renderer->url( |
1544
|
|
|
'frontend', |
1545
|
|
|
array(), |
1546
|
|
|
array('force_canonical' => true) |
1547
|
|
|
); |
1548
|
|
|
|
1549
|
|
|
$message = $mailService->createHtmlMessage($from, $to, $subject, 'playground-game/email/' . $template, array( |
1550
|
|
|
'game' => $game, |
1551
|
|
|
'post' => $post, |
1552
|
|
|
'skinUrl' => $skinUrl |
1553
|
|
|
)); |
1554
|
|
|
$mailService->send($message); |
1555
|
|
|
} |
1556
|
|
|
|
1557
|
|
View Code Duplication |
public function postFbWall($secretKey, $game, $user, $entry) |
|
|
|
|
1558
|
|
|
{ |
1559
|
|
|
$topic = $game->getTitle(); |
1560
|
|
|
|
1561
|
|
|
$shares = json_decode($entry->getSocialShares(), true); |
1562
|
|
|
if (!isset($shares['fbwall'])) { |
1563
|
|
|
$shares['fbwall'] = 1; |
1564
|
|
|
} else { |
1565
|
|
|
$shares['fbwall'] += 1; |
1566
|
|
|
} |
1567
|
|
|
$sharesJson = json_encode($shares); |
1568
|
|
|
$entry->setSocialShares($sharesJson); |
1569
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1570
|
|
|
|
1571
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
1572
|
|
|
'user' => $user, |
1573
|
|
|
'game' => $game, |
1574
|
|
|
'secretKey' => $secretKey, |
1575
|
|
|
'topic' => $topic, |
1576
|
|
|
'entry' => $entry |
1577
|
|
|
)); |
1578
|
|
|
|
1579
|
|
|
return true; |
1580
|
|
|
} |
1581
|
|
|
|
1582
|
|
|
public function postFbRequest($secretKey, $game, $user, $entry, $to) |
1583
|
|
|
{ |
1584
|
|
|
$shares = json_decode($entry->getSocialShares(), true); |
1585
|
|
|
$to = explode(',', $to); |
1586
|
|
|
if (!isset($shares['fbrequest'])) { |
1587
|
|
|
$shares['fbrequest'] = count($to); |
1588
|
|
|
} else { |
1589
|
|
|
$shares['fbrequest'] += count($to); |
1590
|
|
|
} |
1591
|
|
|
$sharesJson = json_encode($shares); |
1592
|
|
|
$entry->setSocialShares($sharesJson); |
1593
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1594
|
|
|
|
1595
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
1596
|
|
|
'user' => $user, |
1597
|
|
|
'game' => $game, |
1598
|
|
|
'secretKey' => $secretKey, |
1599
|
|
|
'entry' => $entry, |
1600
|
|
|
'invites' => count($to) |
1601
|
|
|
)); |
1602
|
|
|
|
1603
|
|
|
return true; |
1604
|
|
|
} |
1605
|
|
|
|
1606
|
|
View Code Duplication |
public function postTwitter($secretKey, $game, $user, $entry) |
|
|
|
|
1607
|
|
|
{ |
1608
|
|
|
$topic = $game->getTitle(); |
1609
|
|
|
|
1610
|
|
|
$shares = json_decode($entry->getSocialShares(), true); |
1611
|
|
|
if (!isset($shares['fbrequest'])) { |
1612
|
|
|
$shares['tweet'] = 1; |
1613
|
|
|
} else { |
1614
|
|
|
$shares['tweet'] += 1; |
1615
|
|
|
} |
1616
|
|
|
$sharesJson = json_encode($shares); |
1617
|
|
|
$entry->setSocialShares($sharesJson); |
1618
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1619
|
|
|
|
1620
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
1621
|
|
|
'user' => $user, |
1622
|
|
|
'game' => $game, |
1623
|
|
|
'secretKey' => $secretKey, |
1624
|
|
|
'topic' => $topic, |
1625
|
|
|
'entry' => $entry |
1626
|
|
|
)); |
1627
|
|
|
|
1628
|
|
|
return true; |
1629
|
|
|
} |
1630
|
|
|
|
1631
|
|
View Code Duplication |
public function postGoogle($secretKey, $game, $user, $entry) |
|
|
|
|
1632
|
|
|
{ |
1633
|
|
|
$topic = $game->getTitle(); |
1634
|
|
|
|
1635
|
|
|
$shares = json_decode($entry->getSocialShares(), true); |
1636
|
|
|
if (!isset($shares['fbrequest'])) { |
1637
|
|
|
$shares['google'] = 1; |
1638
|
|
|
} else { |
1639
|
|
|
$shares['google'] += 1; |
1640
|
|
|
} |
1641
|
|
|
$sharesJson = json_encode($shares); |
1642
|
|
|
$entry->setSocialShares($sharesJson); |
1643
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1644
|
|
|
|
1645
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
1646
|
|
|
'user' => $user, |
1647
|
|
|
'game' => $game, |
1648
|
|
|
'secretKey' => $secretKey, |
1649
|
|
|
'topic' => $topic, |
1650
|
|
|
'entry' => $entry |
1651
|
|
|
)); |
1652
|
|
|
|
1653
|
|
|
return true; |
1654
|
|
|
} |
1655
|
|
|
|
1656
|
|
|
/** |
1657
|
|
|
* Is it possible to trigger a bonus entry ? |
1658
|
|
|
* |
1659
|
|
|
* @param unknown_type $game |
1660
|
|
|
* @param unknown_type $user |
1661
|
|
|
*/ |
1662
|
|
|
public function allowBonus($game, $user) |
1663
|
|
|
{ |
1664
|
|
|
if (! $game->getPlayBonus() || $game->getPlayBonus() == 'none') { |
1665
|
|
|
return false; |
1666
|
|
|
} elseif ($game->getPlayBonus() == 'one') { |
1667
|
|
|
if ($this->getEntryMapper()->findOneBy(array( |
1668
|
|
|
'game' => $game, |
1669
|
|
|
'user' => $user, |
1670
|
|
|
'bonus' => 1 |
1671
|
|
|
))) { |
1672
|
|
|
return false; |
1673
|
|
|
} else { |
1674
|
|
|
return true; |
1675
|
|
|
} |
1676
|
|
|
} elseif ($game->getPlayBonus() == 'per_entry') { |
1677
|
|
|
return $this->getEntryMapper()->checkBonusEntry($game, $user); |
1678
|
|
|
} |
1679
|
|
|
|
1680
|
|
|
return false; |
1681
|
|
|
} |
1682
|
|
|
|
1683
|
|
|
public function addAnotherEntry($game, $user, $winner = 0) |
1684
|
|
|
{ |
1685
|
|
|
$entry = new Entry(); |
1686
|
|
|
$entry->setGame($game); |
1687
|
|
|
$entry->setUser($user); |
1688
|
|
|
$entry->setPoints(0); |
1689
|
|
|
$entry->setIp($this->getIp()); |
1690
|
|
|
$entry->setActive(0); |
1691
|
|
|
$entry->setBonus(1); |
1692
|
|
|
$entry->setWinner($winner); |
1693
|
|
|
$entry = $this->getEntryMapper()->insert($entry); |
1694
|
|
|
|
1695
|
|
|
return $entry; |
1696
|
|
|
} |
1697
|
|
|
|
1698
|
|
|
/** |
1699
|
|
|
* This bonus entry doesn't give points nor badges |
1700
|
|
|
* It's just there to increase the chances during the Draw |
1701
|
|
|
* Old Name playBonus |
1702
|
|
|
* |
1703
|
|
|
* @param PlaygroundGame\Entity\Game $game |
1704
|
|
|
* @param unknown $user |
1705
|
|
|
* @return boolean unknown |
1706
|
|
|
*/ |
1707
|
|
|
public function addAnotherChance($game, $user, $winner = 0) |
1708
|
|
|
{ |
1709
|
|
|
if ($this->allowBonus($game, $user)) { |
1710
|
|
|
$this->addAnotherEntry($game, $user, $winner); |
1711
|
|
|
|
1712
|
|
|
return true; |
1713
|
|
|
} |
1714
|
|
|
|
1715
|
|
|
return false; |
1716
|
|
|
} |
1717
|
|
|
|
1718
|
|
|
/** |
1719
|
|
|
* This bonus entry doesn't give points nor badges but can play again |
1720
|
|
|
* |
1721
|
|
|
* @param PlaygroundGame\Entity\Game $game |
1722
|
|
|
* @param user $user |
1723
|
|
|
* @return boolean unknown |
1724
|
|
|
*/ |
1725
|
|
|
public function playAgain($game, $user, $winner = 0) |
1726
|
|
|
{ |
1727
|
|
|
if ($this->allowBonus($game, $user)) { |
1728
|
|
|
$entry = $this->addAnotherEntry($game, $user, $winner); |
1729
|
|
|
$entry->setActive(1); |
1730
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1731
|
|
|
if ($entry->getActive() == 1) { |
1732
|
|
|
return true; |
1733
|
|
|
} |
1734
|
|
|
} |
1735
|
|
|
|
1736
|
|
|
return false; |
1737
|
|
|
} |
1738
|
|
|
|
1739
|
|
|
/** |
1740
|
|
|
* @param string $path |
1741
|
|
|
* @param boolean $noReplace : If the image name already exist, don't replace it but change the name |
1742
|
|
|
*/ |
1743
|
|
|
public function uploadFile($path, $file, $noReplace = true) |
1744
|
|
|
{ |
1745
|
|
|
$err = $file["error"]; |
1746
|
|
|
$message = ''; |
1747
|
|
|
if ($err > 0) { |
1748
|
|
|
switch ($err) { |
1749
|
|
|
case '1': |
1750
|
|
|
$message .= 'Max file size exceeded. (php.ini)'; |
1751
|
|
|
break; |
1752
|
|
|
case '2': |
1753
|
|
|
$message .= 'Max file size exceeded.'; |
1754
|
|
|
break; |
1755
|
|
|
case '3': |
1756
|
|
|
$message .= 'File upload was only partial.'; |
1757
|
|
|
break; |
1758
|
|
|
case '4': |
1759
|
|
|
$message .= 'No file was attached.'; |
1760
|
|
|
break; |
1761
|
|
|
case '7': |
1762
|
|
|
$message .= 'File permission denied.'; |
1763
|
|
|
break; |
1764
|
|
|
default: |
1765
|
|
|
$message .= 'Unexpected error occurs.'; |
1766
|
|
|
} |
1767
|
|
|
|
1768
|
|
|
return $err; |
1769
|
|
|
} else { |
1770
|
|
|
$fileNewname = $this->fileNewname($path, $file['name'], $noReplace); |
1771
|
|
|
|
1772
|
|
|
if (isset($file["base64"])) { |
1773
|
|
|
list(, $img) = explode(',', $file["base64"]); |
1774
|
|
|
$img = str_replace(' ', '+', $img); |
1775
|
|
|
$im = base64_decode($img); |
1776
|
|
|
if ($im !== false) { |
1777
|
|
|
// getimagesizefromstring |
1778
|
|
|
file_put_contents($path . $fileNewname, $im); |
1779
|
|
|
} else { |
1780
|
|
|
return 1; |
1781
|
|
|
} |
1782
|
|
|
|
1783
|
|
|
return $fileNewname; |
1784
|
|
|
} else { |
1785
|
|
|
$adapter = new \Zend\File\Transfer\Adapter\Http(); |
1786
|
|
|
// 1Mo |
1787
|
|
|
$size = new Size(array( |
1788
|
|
|
'max' => 1024000 |
1789
|
|
|
)); |
1790
|
|
|
$is_image = new IsImage('jpeg,png,gif,jpg'); |
1791
|
|
|
$adapter->setValidators(array( |
1792
|
|
|
$size, |
1793
|
|
|
$is_image |
1794
|
|
|
), $fileNewname); |
1795
|
|
|
|
1796
|
|
|
if (! $adapter->isValid()) { |
1797
|
|
|
return false; |
1798
|
|
|
} |
1799
|
|
|
|
1800
|
|
|
@move_uploaded_file($file["tmp_name"], $path . $fileNewname); |
|
|
|
|
1801
|
|
|
} |
1802
|
|
|
|
1803
|
|
|
|
1804
|
|
|
if (class_exists("Imagick")) { |
1805
|
|
|
$ext = pathinfo($fileNewname, PATHINFO_EXTENSION); |
1806
|
|
|
$img = new \Imagick($path . $fileNewname); |
1807
|
|
|
$img->cropThumbnailImage(100, 100); |
1808
|
|
|
$img->setImageCompression(\Imagick::COMPRESSION_JPEG); |
1809
|
|
|
$img->setImageCompressionQuality(75); |
1810
|
|
|
// Strip out unneeded meta data |
1811
|
|
|
$img->stripImage(); |
1812
|
|
|
$img->writeImage($path . str_replace('.'.$ext, '-thumbnail.'.$ext, $fileNewname)); |
1813
|
|
|
ErrorHandler::stop(true); |
1814
|
|
|
} |
1815
|
|
|
} |
1816
|
|
|
|
1817
|
|
|
return $fileNewname; |
1818
|
|
|
} |
1819
|
|
|
|
1820
|
|
|
/** |
1821
|
|
|
* @param string $path |
1822
|
|
|
*/ |
1823
|
|
|
public function fileNewname($path, $filename, $generate = false) |
1824
|
|
|
{ |
1825
|
|
|
$sanitize = new Sanitize(); |
1826
|
|
|
$name = $sanitize->filter($filename); |
1827
|
|
|
$newpath = $path . $name; |
1828
|
|
|
|
1829
|
|
|
if ($generate) { |
1830
|
|
|
if (file_exists($newpath)) { |
1831
|
|
|
$filename = pathinfo($name, PATHINFO_FILENAME); |
1832
|
|
|
$ext = pathinfo($name, PATHINFO_EXTENSION); |
1833
|
|
|
|
1834
|
|
|
$name = $filename . '_' . rand(0, 99) . '.' . $ext; |
1835
|
|
|
} |
1836
|
|
|
} |
1837
|
|
|
|
1838
|
|
|
unset($sanitize); |
1839
|
|
|
|
1840
|
|
|
return $name; |
1841
|
|
|
} |
1842
|
|
|
|
1843
|
|
|
/** |
1844
|
|
|
* This function returns the list of games, ordered by $type |
1845
|
|
|
*/ |
1846
|
|
|
public function getQueryGamesOrderBy($type = 'createdAt', $order = 'DESC') |
1847
|
|
|
{ |
1848
|
|
|
$em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
1849
|
|
|
$today = new \DateTime("now"); |
1850
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
1851
|
|
|
|
1852
|
|
|
$onlineGames = '( |
1853
|
|
|
( |
1854
|
|
|
CASE WHEN ( |
1855
|
|
|
g.active = 1 |
1856
|
|
|
AND g.broadcastPlatform = 1 |
1857
|
|
|
AND (g.startDate <= :date OR g.startDate IS NULL) |
1858
|
|
|
AND (g.closeDate >= :date OR g.closeDate IS NULL) |
1859
|
|
|
) THEN 1 ELSE 0 END |
1860
|
|
|
) + |
1861
|
|
|
( |
1862
|
|
|
CASE WHEN ( |
1863
|
|
|
g.active = 0 |
1864
|
|
|
AND (g.broadcastPlatform = 0 OR g.broadcastPlatform IS NULL) |
1865
|
|
|
AND g.startDate > :date |
1866
|
|
|
AND g.closeDate < :date |
1867
|
|
|
) THEN 1 ELSE 0 END |
1868
|
|
|
) |
1869
|
|
|
)'; |
1870
|
|
|
|
1871
|
|
|
$qb = $em->createQueryBuilder(); |
1872
|
|
|
$qb->select('g')->from('PlaygroundGame\Entity\Game', 'g'); |
1873
|
|
|
|
1874
|
|
|
switch ($type) { |
1875
|
|
|
case 'startDate': |
1876
|
|
|
$qb->orderBy('g.startDate', $order); |
1877
|
|
|
break; |
1878
|
|
|
case 'activeGames': |
1879
|
|
|
$qb->orderBy('g.active', $order); |
1880
|
|
|
break; |
1881
|
|
|
case 'onlineGames': |
1882
|
|
|
$qb->orderBy($onlineGames, $order); |
1883
|
|
|
$qb->setParameter('date', $today); |
1884
|
|
|
break; |
1885
|
|
|
case 'createdAt': |
1886
|
|
|
$qb->orderBy('g.createdAt', $order); |
1887
|
|
|
break; |
1888
|
|
|
} |
1889
|
|
|
|
1890
|
|
|
$query = $qb->getQuery(); |
1891
|
|
|
|
1892
|
|
|
return $query; |
1893
|
|
|
} |
1894
|
|
|
|
1895
|
|
|
public function draw($game) |
1896
|
|
|
{ |
1897
|
|
|
$total = $game->getWinners(); |
1898
|
|
|
|
1899
|
|
|
// I Have to know what is the User Class used |
1900
|
|
|
$zfcUserOptions = $this->serviceLocator->get('zfcuser_module_options'); |
1901
|
|
|
$userClass = $zfcUserOptions->getUserEntityClass(); |
1902
|
|
|
|
1903
|
|
|
$result = $this->getEntryMapper()->draw($game, $userClass, $total); |
1904
|
|
|
|
1905
|
|
|
foreach ($result as $e) { |
1906
|
|
|
$e->setWinner(1); |
1907
|
|
|
$e = $this->getEntryMapper()->update($e); |
1908
|
|
|
$this->getEventManager()->trigger('win_lottery.post', $this, array( |
1909
|
|
|
'user' => $e->getUser(), |
1910
|
|
|
'game' => $game, |
1911
|
|
|
'entry' => $e |
1912
|
|
|
)); |
1913
|
|
|
} |
1914
|
|
|
|
1915
|
|
|
return $result; |
1916
|
|
|
} |
1917
|
|
|
|
1918
|
|
|
/** |
1919
|
|
|
* getGameMapper |
1920
|
|
|
* |
1921
|
|
|
* @return GameMapperInterface |
1922
|
|
|
*/ |
1923
|
|
View Code Duplication |
public function getGameMapper() |
|
|
|
|
1924
|
|
|
{ |
1925
|
|
|
if (null === $this->gameMapper) { |
1926
|
|
|
$this->gameMapper = $this->serviceLocator->get('playgroundgame_game_mapper'); |
1927
|
|
|
} |
1928
|
|
|
|
1929
|
|
|
return $this->gameMapper; |
1930
|
|
|
} |
1931
|
|
|
|
1932
|
|
|
/** |
1933
|
|
|
* setGameMapper |
1934
|
|
|
* |
1935
|
|
|
* @param GameMapperInterface $gameMapper |
1936
|
|
|
* @return Game |
1937
|
|
|
*/ |
1938
|
|
|
public function setGameMapper(GameMapperInterface $gameMapper) |
1939
|
|
|
{ |
1940
|
|
|
$this->gameMapper = $gameMapper; |
1941
|
|
|
|
1942
|
|
|
return $this; |
1943
|
|
|
} |
1944
|
|
|
|
1945
|
|
|
/** |
1946
|
|
|
* getEntryMapper |
1947
|
|
|
* |
1948
|
|
|
* @return EntryMapperInterface |
1949
|
|
|
*/ |
1950
|
|
|
public function getEntryMapper() |
1951
|
|
|
{ |
1952
|
|
|
if (null === $this->entryMapper) { |
1953
|
|
|
$this->entryMapper = $this->serviceLocator->get('playgroundgame_entry_mapper'); |
1954
|
|
|
} |
1955
|
|
|
|
1956
|
|
|
return $this->entryMapper; |
1957
|
|
|
} |
1958
|
|
|
|
1959
|
|
|
/** |
1960
|
|
|
* setEntryMapper |
1961
|
|
|
* |
1962
|
|
|
* @param EntryMapperInterface $entryMapper |
1963
|
|
|
* @return Game |
1964
|
|
|
*/ |
1965
|
|
|
public function setEntryMapper($entryMapper) |
1966
|
|
|
{ |
1967
|
|
|
$this->entryMapper = $entryMapper; |
1968
|
|
|
|
1969
|
|
|
return $this; |
1970
|
|
|
} |
1971
|
|
|
|
1972
|
|
|
public function setOptions(ModuleOptions $options) |
1973
|
|
|
{ |
1974
|
|
|
$this->options = $options; |
|
|
|
|
1975
|
|
|
|
1976
|
|
|
return $this; |
1977
|
|
|
} |
1978
|
|
|
|
1979
|
|
|
public function getOptions() |
1980
|
|
|
{ |
1981
|
|
|
if (! $this->options instanceof ModuleOptions) { |
1982
|
|
|
$this->setOptions($this->serviceLocator |
1983
|
|
|
->get('playgroundgame_module_options')); |
1984
|
|
|
} |
1985
|
|
|
|
1986
|
|
|
return $this->options; |
1987
|
|
|
} |
1988
|
|
|
|
1989
|
|
|
/** |
1990
|
|
|
* @param string $str |
1991
|
|
|
*/ |
1992
|
|
|
public function getExtension($str) |
1993
|
|
|
{ |
1994
|
|
|
$i = strrpos($str, '.'); |
1995
|
|
|
|
1996
|
|
|
$l = strlen($str) - $i; |
1997
|
|
|
$ext = substr($str, $i + 1, $l); |
1998
|
|
|
|
1999
|
|
|
return $ext; |
2000
|
|
|
} |
2001
|
|
|
|
2002
|
|
|
/** |
2003
|
|
|
* @param string $extension |
2004
|
|
|
*/ |
2005
|
|
|
public function getSrc($extension, $temp_path) |
2006
|
|
|
{ |
2007
|
|
|
$image_src = ''; |
2008
|
|
|
switch ($extension) { |
2009
|
|
|
case 'jpg': |
2010
|
|
|
$image_src = imagecreatefromjpeg($temp_path); |
2011
|
|
|
break; |
2012
|
|
|
case 'jpeg': |
2013
|
|
|
$image_src = imagecreatefromjpeg($temp_path); |
2014
|
|
|
break; |
2015
|
|
|
case 'png': |
2016
|
|
|
$image_src = imagecreatefrompng($temp_path); |
2017
|
|
|
break; |
2018
|
|
|
case 'gif': |
2019
|
|
|
$image_src = imagecreatefromgif($temp_path); |
2020
|
|
|
break; |
2021
|
|
|
} |
2022
|
|
|
|
2023
|
|
|
return $image_src; |
2024
|
|
|
} |
2025
|
|
|
|
2026
|
|
|
/** |
2027
|
|
|
* @param string $extension |
2028
|
|
|
* @param string $rep |
2029
|
|
|
* @param integer $mini_width |
2030
|
|
|
* @param integer $mini_height |
2031
|
|
|
*/ |
2032
|
|
|
public function resize($tmp_file, $extension, $rep, $src, $mini_width, $mini_height) |
|
|
|
|
2033
|
|
|
{ |
2034
|
|
|
list($src_width, $src_height) = getimagesize($tmp_file); |
2035
|
|
|
|
2036
|
|
|
$ratio_src = $src_width / $src_height; |
2037
|
|
|
$ratio_mini = $mini_width / $mini_height; |
2038
|
|
|
|
2039
|
|
|
if ($ratio_src >= $ratio_mini) { |
2040
|
|
|
$new_height_mini = $mini_height; |
2041
|
|
|
$new_width_mini = $src_width / ($src_height / $mini_height); |
2042
|
|
|
} else { |
2043
|
|
|
$new_width_mini = $mini_width; |
2044
|
|
|
$new_height_mini = $src_height / ($src_width / $mini_width); |
2045
|
|
|
} |
2046
|
|
|
|
2047
|
|
|
$new_image_mini = imagecreatetruecolor($mini_width, $mini_height); |
2048
|
|
|
|
2049
|
|
|
imagecopyresampled( |
2050
|
|
|
$new_image_mini, |
2051
|
|
|
$src, |
2052
|
|
|
0 - ($new_width_mini - $mini_width) / 2, |
2053
|
|
|
0 - ($new_height_mini - $mini_height) / 2, |
2054
|
|
|
0, |
2055
|
|
|
0, |
2056
|
|
|
$new_width_mini, |
2057
|
|
|
$new_height_mini, |
2058
|
|
|
$src_width, |
2059
|
|
|
$src_height |
2060
|
|
|
); |
2061
|
|
|
imagejpeg($new_image_mini, $rep); |
2062
|
|
|
|
2063
|
|
|
imagedestroy($new_image_mini); |
2064
|
|
|
} |
2065
|
|
|
|
2066
|
|
|
public function getGameEntity() |
2067
|
|
|
{ |
2068
|
|
|
return new \PlaygroundGame\Entity\Game(); |
2069
|
|
|
} |
2070
|
|
|
|
2071
|
|
|
/** |
2072
|
|
|
* @param string $resource |
2073
|
|
|
* @param string $privilege |
2074
|
|
|
*/ |
2075
|
|
|
public function isAllowed($resource, $privilege = null) |
2076
|
|
|
{ |
2077
|
|
|
$auth = $this->serviceLocator->get('BjyAuthorize\Service\Authorize'); |
2078
|
|
|
|
2079
|
|
|
return $auth->isAllowed($resource, $privilege); |
2080
|
|
|
} |
2081
|
|
|
|
2082
|
|
|
public function getIp() |
2083
|
|
|
{ |
2084
|
|
|
$ipaddress = ''; |
|
|
|
|
2085
|
|
|
if (isset($_SERVER['HTTP_CLIENT_IP'])) { |
2086
|
|
|
$ipaddress = $_SERVER['HTTP_CLIENT_IP']; |
2087
|
|
|
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
2088
|
|
|
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; |
2089
|
|
|
} elseif (isset($_SERVER['HTTP_X_FORWARDED'])) { |
2090
|
|
|
$ipaddress = $_SERVER['HTTP_X_FORWARDED']; |
2091
|
|
|
} elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) { |
2092
|
|
|
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; |
2093
|
|
|
} elseif (isset($_SERVER['HTTP_FORWARDED'])) { |
2094
|
|
|
$ipaddress = $_SERVER['HTTP_FORWARDED']; |
2095
|
|
|
} elseif (isset($_SERVER['REMOTE_ADDR'])) { |
2096
|
|
|
$ipaddress = $_SERVER['REMOTE_ADDR']; |
2097
|
|
|
} else { |
2098
|
|
|
$ipaddress = 'UNKNOWN'; |
2099
|
|
|
} |
2100
|
|
|
|
2101
|
|
|
return $ipaddress; |
2102
|
|
|
} |
2103
|
|
|
|
2104
|
|
|
public function getGeoloc($ip) |
2105
|
|
|
{ |
2106
|
|
|
$geoloc = ''; |
2107
|
|
|
try { |
2108
|
|
|
$res = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip)); |
2109
|
|
|
if ($res['geoplugin_latitude'] != '') { |
2110
|
|
|
$geoloc = $res['geoplugin_latitude'] . ',' . $res['geoplugin_longitude']; |
2111
|
|
|
} |
2112
|
|
|
} catch (\Exception $e) { |
|
|
|
|
2113
|
|
|
} |
2114
|
|
|
|
2115
|
|
|
return $geoloc; |
2116
|
|
|
} |
2117
|
|
|
|
2118
|
|
|
public function getAnonymousId() |
2119
|
|
|
{ |
2120
|
|
|
$anonymousId = ''; |
2121
|
|
|
if ($_COOKIE && array_key_exists('pg_anonymous', $_COOKIE)) { |
2122
|
|
|
$anonymousId = $_COOKIE['pg_anonymous']; |
2123
|
|
|
} |
2124
|
|
|
|
2125
|
|
|
return $anonymousId; |
2126
|
|
|
} |
2127
|
|
|
|
2128
|
|
|
/** |
2129
|
|
|
* |
2130
|
|
|
* |
2131
|
|
|
* This service is ready for all types of games |
2132
|
|
|
* |
2133
|
|
|
* @param array $data |
2134
|
|
|
* @return \PlaygroundGame\Entity\Game |
2135
|
|
|
*/ |
2136
|
|
View Code Duplication |
public function createForm(array $data, $game, $form = null) |
|
|
|
|
2137
|
|
|
{ |
2138
|
|
|
$title = ''; |
2139
|
|
|
$description = ''; |
2140
|
|
|
|
2141
|
|
|
if ($data['form_jsonified']) { |
2142
|
|
|
$jsonPV = json_decode($data['form_jsonified']); |
2143
|
|
|
foreach ($jsonPV as $element) { |
2144
|
|
|
if ($element->form_properties) { |
2145
|
|
|
$attributes = $element->form_properties[0]; |
2146
|
|
|
$title = $attributes->title; |
2147
|
|
|
$description = $attributes->description; |
2148
|
|
|
|
2149
|
|
|
break; |
2150
|
|
|
} |
2151
|
|
|
} |
2152
|
|
|
} |
2153
|
|
|
if (! $form) { |
2154
|
|
|
$form = new \PlaygroundGame\Entity\PlayerForm(); |
2155
|
|
|
} |
2156
|
|
|
$form->setGame($game); |
2157
|
|
|
$form->setTitle($title); |
2158
|
|
|
$form->setDescription($description); |
2159
|
|
|
$form->setForm($data['form_jsonified']); |
2160
|
|
|
$form->setFormTemplate($data['form_template']); |
2161
|
|
|
|
2162
|
|
|
$form = $this->getPlayerFormMapper()->insert($form); |
2163
|
|
|
|
2164
|
|
|
return $form; |
2165
|
|
|
} |
2166
|
|
|
|
2167
|
|
|
/** |
2168
|
|
|
* getCSV creates lines of CSV and returns it. |
2169
|
|
|
*/ |
2170
|
|
|
public function getCSV($array) |
2171
|
|
|
{ |
2172
|
|
|
ob_start(); // buffer the output ... |
2173
|
|
|
$out = fopen('php://output', 'w'); |
2174
|
|
|
fputcsv($out, array_keys($array[0]), ";"); |
2175
|
|
|
foreach ($array as $line) { |
2176
|
|
|
fputcsv($out, $line, ";"); |
2177
|
|
|
} |
2178
|
|
|
fclose($out); |
2179
|
|
|
return ob_get_clean(); // ... then return it as a string! |
2180
|
|
|
} |
2181
|
|
|
|
2182
|
|
|
public function getAttributes($attributes) |
2183
|
|
|
{ |
2184
|
|
|
$a = array(); |
2185
|
|
|
|
2186
|
|
|
$a['name'] = isset($attributes->name)? $attributes->name : ''; |
2187
|
|
|
$a['placeholder'] = isset($attributes->data->placeholder)? $attributes->data->placeholder : ''; |
2188
|
|
|
$a['label'] = isset($attributes->data->label)? $attributes->data->label : ''; |
2189
|
|
|
$a['required'] = (isset($attributes->data->required) && $attributes->data->required == 'true')? |
2190
|
|
|
true: |
2191
|
|
|
false; |
2192
|
|
|
$a['class'] = isset($attributes->data->class)? $attributes->data->class : ''; |
2193
|
|
|
$a['id'] = isset($attributes->data->id)? $attributes->data->id : ''; |
2194
|
|
|
$a['lengthMin'] = isset($attributes->data->length)? $attributes->data->length->min : ''; |
2195
|
|
|
$a['lengthMax'] = isset($attributes->data->length)? $attributes->data->length->max : ''; |
2196
|
|
|
$a['validator'] = isset($attributes->data->validator)? $attributes->data->validator : ''; |
2197
|
|
|
$a['innerData'] = isset($attributes->data->innerData)? $attributes->data->innerData : array(); |
2198
|
|
|
$a['dropdownValues']= isset($attributes->data->dropdownValues)? |
2199
|
|
|
$attributes->data->dropdownValues : |
2200
|
|
|
array(); |
2201
|
|
|
$a['filesizeMin'] = isset($attributes->data->filesize)? $attributes->data->filesize->min : 0; |
2202
|
|
|
$a['filesizeMax'] = isset($attributes->data->filesize)? $attributes->data->filesize->max : 10*1024*1024; |
2203
|
|
|
$a['fileextension'] = isset($attributes->data->fileextension)? |
2204
|
|
|
str_replace(', ', ',', $attributes->data->fileextension) : |
2205
|
|
|
'png,jpg,jpeg,gif'; |
2206
|
|
|
$a['key'] = isset($attributes->data->key)? |
2207
|
|
|
$attributes->data->key : null; |
2208
|
|
|
|
2209
|
|
|
// hiddenRequired('fileexcludeextension', '').appendTo(li); |
2210
|
|
|
// hiddenRequired('filemimetype', '').appendTo(li); |
2211
|
|
|
// hiddenRequired('fileexcludemimetype', '').appendTo(li); |
2212
|
|
|
// hiddenRequired('fileexists', '').appendTo(li); |
2213
|
|
|
// hiddenRequired('fileimagesize_minheight', '').appendTo(li); |
2214
|
|
|
// hiddenRequired('fileimagesize_maxheight', '').appendTo(li); |
2215
|
|
|
// hiddenRequired('fileimagesize_minwidth', '').appendTo(li); |
2216
|
|
|
// hiddenRequired('fileimagesize_maxwidth', '').appendTo(li); |
2217
|
|
|
// hiddenRequired('fileiscompressed', '').appendTo(li); |
2218
|
|
|
// hiddenRequired('fileisimage', '').appendTo(li); |
2219
|
|
|
// hiddenRequired('filewordcount_min', '').appendTo(li); |
2220
|
|
|
// hiddenRequired('filewordcount_max', '').appendTo(li); |
2221
|
|
|
|
2222
|
|
|
return $a; |
2223
|
|
|
} |
2224
|
|
|
|
2225
|
|
|
/** |
2226
|
|
|
* @param \Zend\InputFilter\InputFilter $inputFilter |
2227
|
|
|
*/ |
2228
|
|
|
public function decorate($element, $attr, $inputFilter) |
2229
|
|
|
{ |
2230
|
|
|
$factory = new InputFactory(); |
2231
|
|
|
$element->setLabel($attr['label']); |
2232
|
|
|
$element->setAttributes( |
2233
|
|
|
array( |
2234
|
|
|
'placeholder' => $attr['placeholder'], |
2235
|
|
|
'required' => $attr['required'], |
2236
|
|
|
'class' => $attr['class'], |
2237
|
|
|
'id' => $attr['id'] |
2238
|
|
|
) |
2239
|
|
|
); |
2240
|
|
|
|
2241
|
|
|
$options = array(); |
2242
|
|
|
$options['encoding'] = 'UTF-8'; |
2243
|
|
|
if ($attr['lengthMin'] && $attr['lengthMin'] > 0) { |
2244
|
|
|
$options['min'] = $attr['lengthMin']; |
2245
|
|
|
} |
2246
|
|
|
if ($attr['lengthMax'] && $attr['lengthMax'] > $attr['lengthMin']) { |
2247
|
|
|
$options['max'] = $attr['lengthMax']; |
2248
|
|
|
$element->setAttribute('maxlength', $attr['lengthMax']); |
2249
|
|
|
$options['messages'] = array( |
2250
|
|
|
\Zend\Validator\StringLength::TOO_LONG => sprintf( |
2251
|
|
|
$this->serviceLocator->get('MvcTranslator')->translate( |
2252
|
|
|
'This field contains more than %s characters', |
2253
|
|
|
'playgroundgame' |
2254
|
|
|
), |
2255
|
|
|
$attr['lengthMax'] |
2256
|
|
|
) |
2257
|
|
|
); |
2258
|
|
|
} |
2259
|
|
|
|
2260
|
|
|
$validators = array( |
2261
|
|
|
array( |
2262
|
|
|
'name' => 'StringLength', |
2263
|
|
|
'options' => $options, |
2264
|
|
|
), |
2265
|
|
|
); |
2266
|
|
|
if ($attr['validator']) { |
2267
|
|
|
$regex = "/.*\(([^)]*)\)/"; |
2268
|
|
|
preg_match($regex, $attr['validator'], $matches); |
2269
|
|
|
$valArray = array( |
2270
|
|
|
'name' => str_replace( |
2271
|
|
|
'('.$matches[1].')', |
2272
|
|
|
'', |
2273
|
|
|
$attr['validator'] |
2274
|
|
|
), |
2275
|
|
|
'options' => array($matches[1]) |
2276
|
|
|
); |
2277
|
|
|
$validators[] = $valArray; |
2278
|
|
|
} |
2279
|
|
|
|
2280
|
|
|
$inputFilter->add($factory->createInput(array( |
2281
|
|
|
'name' => $attr['name'], |
2282
|
|
|
'required' => $attr['required'], |
2283
|
|
|
'filters' => array( |
2284
|
|
|
array('name' => 'StripTags'), |
2285
|
|
|
array('name' => 'StringTrim'), |
2286
|
|
|
), |
2287
|
|
|
'validators' => $validators, |
2288
|
|
|
))); |
2289
|
|
|
|
2290
|
|
|
return $element; |
2291
|
|
|
} |
2292
|
|
|
|
2293
|
|
|
/** |
2294
|
|
|
* Create a ZF2 Form from json data |
2295
|
|
|
* @return Form |
2296
|
|
|
*/ |
2297
|
|
|
public function createFormFromJson($jsonForm, $id = 'jsonForm') |
2298
|
|
|
{ |
2299
|
|
|
$formPV = json_decode($jsonForm); |
2300
|
|
|
|
2301
|
|
|
$form = new Form(); |
2302
|
|
|
$form->setAttribute('id', $id); |
2303
|
|
|
$form->setAttribute('enctype', 'multipart/form-data'); |
2304
|
|
|
|
2305
|
|
|
$inputFilter = new \Zend\InputFilter\InputFilter(); |
2306
|
|
|
$factory = new InputFactory(); |
2307
|
|
|
|
2308
|
|
|
foreach ($formPV as $element) { |
2309
|
|
View Code Duplication |
if (isset($element->line_text)) { |
|
|
|
|
2310
|
|
|
$attr = $this->getAttributes($element->line_text[0]); |
2311
|
|
|
$element = new Element\Text($attr['name']); |
2312
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
2313
|
|
|
$form->add($element); |
2314
|
|
|
} |
2315
|
|
|
if (isset($element->line_key)) { |
2316
|
|
|
$attr = $this->getAttributes($element->line_key[0]); |
2317
|
|
|
$element = new Element\Text($attr['name']); |
2318
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
2319
|
|
|
$form->add($element); |
2320
|
|
|
|
2321
|
|
|
$inputFilter->add($factory->createInput(array( |
2322
|
|
|
'name' => $attr['name'], |
2323
|
|
|
'required' => $attr['required'], |
2324
|
|
|
'validators' => array( |
2325
|
|
|
array( |
2326
|
|
|
'name' => 'InArray', |
2327
|
|
|
'options' => array( |
2328
|
|
|
'haystack' => array($attr['key']), |
2329
|
|
|
), |
2330
|
|
|
), |
2331
|
|
|
), |
2332
|
|
|
))); |
2333
|
|
|
} |
2334
|
|
View Code Duplication |
if (isset($element->line_password)) { |
|
|
|
|
2335
|
|
|
$attr = $this->getAttributes($element->line_password[0]); |
2336
|
|
|
$element = new Element\Password($attr['name']); |
2337
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
2338
|
|
|
$form->add($element); |
2339
|
|
|
} |
2340
|
|
View Code Duplication |
if (isset($element->line_hidden)) { |
|
|
|
|
2341
|
|
|
$attr = $this->getAttributes($element->line_hidden[0]); |
2342
|
|
|
$element = new Element\Hidden($attr['name']); |
2343
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
2344
|
|
|
$form->add($element); |
2345
|
|
|
} |
2346
|
|
View Code Duplication |
if (isset($element->line_email)) { |
|
|
|
|
2347
|
|
|
$attr = $this->getAttributes($element->line_email[0]); |
2348
|
|
|
$element = new Element\Email($attr['name']); |
2349
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
2350
|
|
|
$form->add($element); |
2351
|
|
|
} |
2352
|
|
View Code Duplication |
if (isset($element->line_radio)) { |
|
|
|
|
2353
|
|
|
$attr = $this->getAttributes($element->line_radio[0]); |
2354
|
|
|
$element = new Element\Radio($attr['name']); |
2355
|
|
|
|
2356
|
|
|
$element->setLabel($attr['label']); |
2357
|
|
|
$element->setAttributes( |
2358
|
|
|
array( |
2359
|
|
|
'name' => $attr['name'], |
2360
|
|
|
'required' => $attr['required'], |
2361
|
|
|
'allowEmpty'=> !$attr['required'], |
2362
|
|
|
'class' => $attr['class'], |
2363
|
|
|
'id' => $attr['id'] |
2364
|
|
|
) |
2365
|
|
|
); |
2366
|
|
|
$values = array(); |
2367
|
|
|
foreach ($attr['innerData'] as $value) { |
2368
|
|
|
$values[] = $value->label; |
2369
|
|
|
} |
2370
|
|
|
$element->setValueOptions($values); |
2371
|
|
|
|
2372
|
|
|
$options = array(); |
2373
|
|
|
$options['encoding'] = 'UTF-8'; |
2374
|
|
|
$options['disable_inarray_validator'] = true; |
2375
|
|
|
|
2376
|
|
|
$element->setOptions($options); |
2377
|
|
|
|
2378
|
|
|
$form->add($element); |
2379
|
|
|
|
2380
|
|
|
$inputFilter->add($factory->createInput(array( |
2381
|
|
|
'name' => $attr['name'], |
2382
|
|
|
'required' => $attr['required'], |
2383
|
|
|
'allowEmpty' => !$attr['required'], |
2384
|
|
|
))); |
2385
|
|
|
} |
2386
|
|
View Code Duplication |
if (isset($element->line_checkbox)) { |
|
|
|
|
2387
|
|
|
$attr = $this->getAttributes($element->line_checkbox[0]); |
2388
|
|
|
$element = new Element\MultiCheckbox($attr['name']); |
2389
|
|
|
|
2390
|
|
|
$element->setLabel($attr['label']); |
2391
|
|
|
$element->setAttributes( |
2392
|
|
|
array( |
2393
|
|
|
'name' => $attr['name'], |
2394
|
|
|
'required' => $attr['required'], |
2395
|
|
|
'allowEmpty'=> !$attr['required'], |
2396
|
|
|
'class' => $attr['class'], |
2397
|
|
|
'id' => $attr['id'] |
2398
|
|
|
) |
2399
|
|
|
); |
2400
|
|
|
|
2401
|
|
|
$values = array(); |
2402
|
|
|
foreach ($attr['innerData'] as $value) { |
2403
|
|
|
$values[] = $value->label; |
2404
|
|
|
} |
2405
|
|
|
$element->setValueOptions($values); |
2406
|
|
|
$form->add($element); |
2407
|
|
|
|
2408
|
|
|
$options = array(); |
2409
|
|
|
$options['encoding'] = 'UTF-8'; |
2410
|
|
|
$options['disable_inarray_validator'] = true; |
2411
|
|
|
|
2412
|
|
|
$element->setOptions($options); |
2413
|
|
|
|
2414
|
|
|
$inputFilter->add($factory->createInput(array( |
2415
|
|
|
'name' => $attr['name'], |
2416
|
|
|
'required' => $attr['required'], |
2417
|
|
|
'allowEmpty'=> !$attr['required'], |
2418
|
|
|
))); |
2419
|
|
|
} |
2420
|
|
View Code Duplication |
if (isset($element->line_dropdown)) { |
|
|
|
|
2421
|
|
|
$attr = $this->getAttributes($element->line_dropdown[0]); |
2422
|
|
|
$element = new Element\Select($attr['name']); |
2423
|
|
|
|
2424
|
|
|
$element->setLabel($attr['label']); |
2425
|
|
|
$element->setAttributes( |
2426
|
|
|
array( |
2427
|
|
|
'name' => $attr['name'], |
2428
|
|
|
'required' => $attr['required'], |
2429
|
|
|
'allowEmpty'=> !$attr['required'], |
2430
|
|
|
'class' => $attr['class'], |
2431
|
|
|
'id' => $attr['id'] |
2432
|
|
|
) |
2433
|
|
|
); |
2434
|
|
|
$values = array(); |
2435
|
|
|
foreach ($attr['dropdownValues'] as $value) { |
2436
|
|
|
$values[] = $value->dropdown_label; |
2437
|
|
|
} |
2438
|
|
|
$element->setValueOptions($values); |
2439
|
|
|
$form->add($element); |
2440
|
|
|
|
2441
|
|
|
$options = array(); |
2442
|
|
|
$options['encoding'] = 'UTF-8'; |
2443
|
|
|
$options['disable_inarray_validator'] = true; |
2444
|
|
|
|
2445
|
|
|
$element->setOptions($options); |
2446
|
|
|
|
2447
|
|
|
$inputFilter->add($factory->createInput(array( |
2448
|
|
|
'name' => $attr['name'], |
2449
|
|
|
'required' => $attr['required'], |
2450
|
|
|
'allowEmpty' => !$attr['required'], |
2451
|
|
|
))); |
2452
|
|
|
} |
2453
|
|
View Code Duplication |
if (isset($element->line_paragraph)) { |
|
|
|
|
2454
|
|
|
$attr = $this->getAttributes($element->line_paragraph[0]); |
2455
|
|
|
$element = new Element\Textarea($attr['name']); |
2456
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
2457
|
|
|
$form->add($element); |
2458
|
|
|
} |
2459
|
|
|
if (isset($element->line_upload)) { |
2460
|
|
|
$attr = $this->getAttributes($element->line_upload[0]); |
2461
|
|
|
$element = new Element\File($attr['name']); |
2462
|
|
|
|
2463
|
|
|
$element->setLabel($attr['label']); |
2464
|
|
|
$element->setAttributes( |
2465
|
|
|
array( |
2466
|
|
|
'name' => $attr['name'], |
2467
|
|
|
'required' => $attr['required'], |
2468
|
|
|
'class' => $attr['class'], |
2469
|
|
|
'id' => $attr['id'] |
2470
|
|
|
) |
2471
|
|
|
); |
2472
|
|
|
$form->add($element); |
2473
|
|
|
|
2474
|
|
|
$inputFilter->add($factory->createInput(array( |
2475
|
|
|
'name' => $attr['name'], |
2476
|
|
|
'required' => $attr['required'], |
2477
|
|
|
'validators' => array( |
2478
|
|
|
array( |
2479
|
|
|
'name' => '\Zend\Validator\File\Size', |
2480
|
|
|
'options' => array('min' => $attr['filesizeMin'], 'max' => $attr['filesizeMax']) |
2481
|
|
|
), |
2482
|
|
|
array( |
2483
|
|
|
'name' => '\Zend\Validator\File\Extension', |
2484
|
|
|
'options' => array( |
2485
|
|
|
$attr['fileextension'], |
2486
|
|
|
'messages' => array( |
2487
|
|
|
\Zend\Validator\File\Extension::FALSE_EXTENSION =>'Veuillez télécharger un fichier avec la bonne extension' |
2488
|
|
|
) |
2489
|
|
|
) |
2490
|
|
|
), |
2491
|
|
|
), |
2492
|
|
|
))); |
2493
|
|
|
} |
2494
|
|
|
} |
2495
|
|
|
|
2496
|
|
|
$form->setInputFilter($inputFilter); |
2497
|
|
|
|
2498
|
|
|
return $form; |
2499
|
|
|
} |
2500
|
|
|
|
2501
|
|
|
/** |
2502
|
|
|
* Send mail for winner and/or loser |
2503
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
2504
|
|
|
* @param \PlaygroundUser\Entity\User $user |
2505
|
|
|
* @param \PlaygroundGame\Entity\Entry $lastEntry |
2506
|
|
|
* @param \PlaygroundGame\Entity\Prize $prize |
2507
|
|
|
*/ |
2508
|
|
|
public function sendMail($game, $user, $lastEntry, $prize = null) |
2509
|
|
|
{ |
2510
|
|
View Code Duplication |
if (($user || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) && |
|
|
|
|
2511
|
|
|
$game->getMailWinner() && |
2512
|
|
|
$lastEntry->getWinner() |
2513
|
|
|
) { |
2514
|
|
|
$this->sendResultMail($game, $user, $lastEntry, 'winner', $prize); |
2515
|
|
|
} |
2516
|
|
|
|
2517
|
|
View Code Duplication |
if (($user || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) && |
|
|
|
|
2518
|
|
|
$game->getMailLooser() && |
2519
|
|
|
!$lastEntry->getWinner() |
2520
|
|
|
) { |
2521
|
|
|
$this->sendResultMail($game, $user, $lastEntry, 'looser'); |
2522
|
|
|
} |
2523
|
|
|
|
2524
|
|
|
if (($user || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) && |
2525
|
|
|
$game->getMailEntry() |
2526
|
|
|
) { |
2527
|
|
|
$this->sendResultMail($game, $user, $lastEntry); |
2528
|
|
|
} |
2529
|
|
|
} |
2530
|
|
|
|
2531
|
|
|
public function getPlayerFormMapper() |
2532
|
|
|
{ |
2533
|
|
|
if (null === $this->playerformMapper) { |
2534
|
|
|
$this->playerformMapper = $this->serviceLocator->get('playgroundgame_playerform_mapper'); |
2535
|
|
|
} |
2536
|
|
|
|
2537
|
|
|
return $this->playerformMapper; |
2538
|
|
|
} |
2539
|
|
|
|
2540
|
|
|
public function setPlayerFormMapper($playerformMapper) |
2541
|
|
|
{ |
2542
|
|
|
$this->playerformMapper = $playerformMapper; |
2543
|
|
|
|
2544
|
|
|
return $this; |
2545
|
|
|
} |
2546
|
|
|
|
2547
|
|
|
public function getInvitationMapper() |
2548
|
|
|
{ |
2549
|
|
|
if (null === $this->invitationMapper) { |
2550
|
|
|
$this->invitationMapper = $this->serviceLocator->get('playgroundgame_invitation_mapper'); |
2551
|
|
|
} |
2552
|
|
|
|
2553
|
|
|
return $this->invitationMapper; |
2554
|
|
|
} |
2555
|
|
|
|
2556
|
|
|
public function setInvitationMapper($invitationMapper) |
2557
|
|
|
{ |
2558
|
|
|
$this->invitationMapper = $invitationMapper; |
2559
|
|
|
|
2560
|
|
|
return $this; |
2561
|
|
|
} |
2562
|
|
|
|
2563
|
|
|
/** |
2564
|
|
|
* getUserMapper |
2565
|
|
|
* |
2566
|
|
|
* @return UserMapperInterface |
2567
|
|
|
*/ |
2568
|
|
|
public function getUserMapper() |
2569
|
|
|
{ |
2570
|
|
|
if (null === $this->userMapper) { |
2571
|
|
|
$this->userMapper = $this->serviceLocator->get('zfcuser_user_mapper'); |
2572
|
|
|
} |
2573
|
|
|
|
2574
|
|
|
return $this->userMapper; |
2575
|
|
|
} |
2576
|
|
|
|
2577
|
|
|
/** |
2578
|
|
|
* getUserMapper |
2579
|
|
|
* |
2580
|
|
|
* @return ServiceManager |
2581
|
|
|
*/ |
2582
|
|
|
public function getServiceManager() |
2583
|
|
|
{ |
2584
|
|
|
return $this->serviceLocator; |
2585
|
|
|
} |
2586
|
|
|
} |
2587
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.