1
|
|
|
<?php |
2
|
|
|
namespace PlaygroundGame\Service; |
3
|
|
|
|
4
|
|
|
use PlaygroundGame\Entity\Entry; |
5
|
|
|
use Zend\Session\Container; |
6
|
|
|
use Zend\ServiceManager\ServiceManagerAwareInterface; |
7
|
|
|
use Zend\ServiceManager\ServiceManager; |
8
|
|
|
use ZfcBase\EventManager\EventProvider; |
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
|
|
|
|
20
|
|
|
class Game extends EventProvider implements ServiceManagerAwareInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @var GameMapperInterface |
25
|
|
|
*/ |
26
|
|
|
protected $gameMapper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @var EntryMapperInterface |
31
|
|
|
*/ |
32
|
|
|
protected $entryMapper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
* @var ServiceManager |
37
|
|
|
*/ |
38
|
|
|
protected $serviceManager; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
* @var UserServiceOptionsInterface |
43
|
|
|
*/ |
44
|
|
|
protected $options; |
45
|
|
|
|
46
|
|
|
protected $playerformMapper; |
47
|
|
|
|
48
|
|
|
protected $invitationMapper; |
49
|
|
|
|
50
|
|
|
protected $userMapper; |
51
|
|
|
|
52
|
|
|
protected $anonymousIdentifier = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* |
57
|
|
|
* This service is ready for all types of games |
58
|
|
|
* |
59
|
|
|
* @param array $data |
60
|
|
|
* @param string $formClass |
61
|
|
|
* @return \PlaygroundGame\Entity\Game |
62
|
|
|
*/ |
63
|
|
|
public function createOrUpdate(array $data, $game, $formClass) |
64
|
|
|
{ |
65
|
|
|
$entityManager = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
66
|
|
|
|
67
|
|
|
$form = $this->getServiceManager()->get($formClass); |
68
|
|
|
$form->get('publicationDate')->setOptions(array( |
69
|
|
|
'format' => 'Y-m-d H:i:s' |
70
|
|
|
)); |
71
|
|
|
$form->get('startDate')->setOptions(array( |
72
|
|
|
'format' => 'Y-m-d H:i:s' |
73
|
|
|
)); |
74
|
|
|
$form->get('endDate')->setOptions(array( |
75
|
|
|
'format' => 'Y-m-d H:i:s' |
76
|
|
|
)); |
77
|
|
|
$form->get('closeDate')->setOptions(array( |
78
|
|
|
'format' => 'Y-m-d H:i:s' |
79
|
|
|
)); |
80
|
|
|
|
81
|
|
|
$form->bind($game); |
82
|
|
|
|
83
|
|
|
$path = $this->getOptions()->getMediaPath() . '/'; |
84
|
|
|
$media_url = $this->getOptions()->getMediaUrl() . '/'; |
85
|
|
|
|
86
|
|
|
$identifierInput = $form->getInputFilter()->get('identifier'); |
87
|
|
|
$noObjectExistsValidator = new NoObjectExistsValidator(array( |
88
|
|
|
'object_repository' => $entityManager->getRepository('PlaygroundGame\Entity\Game'), |
89
|
|
|
'fields' => 'identifier', |
90
|
|
|
'messages' => array( |
91
|
|
|
'objectFound' => 'This url already exists !' |
92
|
|
|
) |
93
|
|
|
)); |
94
|
|
|
|
95
|
|
|
if ($game->getIdentifier() != $data['identifier']) { |
96
|
|
|
$identifierInput->getValidatorChain()->addValidator($noObjectExistsValidator); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// I must switch from original format to the Y-m-d format because |
100
|
|
|
// this is the only one accepted by new DateTime($value) |
101
|
|
View Code Duplication |
if (isset($data['publicationDate']) && $data['publicationDate']) { |
|
|
|
|
102
|
|
|
$tmpDate = \DateTime::createFromFormat('d/m/Y H:i:s', $data['publicationDate']); |
103
|
|
|
$data['publicationDate'] = $tmpDate->format('Y-m-d H:i:s'); |
104
|
|
|
} |
105
|
|
View Code Duplication |
if (isset($data['startDate']) && $data['startDate']) { |
|
|
|
|
106
|
|
|
$tmpDate = \DateTime::createFromFormat('d/m/Y H:i:s', $data['startDate']); |
107
|
|
|
$data['startDate'] = $tmpDate->format('Y-m-d H:i:s'); |
108
|
|
|
} |
109
|
|
View Code Duplication |
if (isset($data['endDate']) && $data['endDate']) { |
|
|
|
|
110
|
|
|
$tmpDate = \DateTime::createFromFormat('d/m/Y H:i:s', $data['endDate']); |
111
|
|
|
$data['endDate'] = $tmpDate->format('Y-m-d H:i:s'); |
112
|
|
|
} |
113
|
|
View Code Duplication |
if (isset($data['closeDate']) && $data['closeDate']) { |
|
|
|
|
114
|
|
|
$tmpDate = \DateTime::createFromFormat('d/m/Y H:i:s', $data['closeDate']); |
115
|
|
|
$data['closeDate'] = $tmpDate->format('Y-m-d H:i:s'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// If publicationDate is null, I update it with the startDate if not null neither |
119
|
|
|
if ((! isset($data['publicationDate']) || $data['publicationDate'] == '') && |
120
|
|
|
(isset($data['startDate']) && $data['startDate'] != '') |
121
|
|
|
) { |
122
|
|
|
$data['publicationDate'] = $data['startDate']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// If the identifier has not been set, I use the title to create one. |
126
|
|
View Code Duplication |
if ((! isset($data['identifier']) || empty($data['identifier'])) && isset($data['title'])) { |
|
|
|
|
127
|
|
|
$data['identifier'] = $data['title']; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$form->setData($data); |
131
|
|
|
|
132
|
|
|
// If someone want to claim... It's time to do it ! used for exemple by PlaygroundFacebook Module |
133
|
|
|
$result = $this->getEventManager()->trigger(__FUNCTION__ . '.validate', $this, array( |
134
|
|
|
'game' => $game, |
135
|
|
|
'data' => $data |
136
|
|
|
)); |
137
|
|
|
if (is_array($result) && ! $result[0]) { |
138
|
|
|
$form->get('fbAppId')->setMessages(array( |
139
|
|
|
'Vous devez d\'abord désinstaller l\'appli Facebook' |
140
|
|
|
)); |
141
|
|
|
|
142
|
|
|
return false; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (! $form->isValid()) { |
146
|
|
View Code Duplication |
if (isset($data['publicationDate']) && $data['publicationDate']) { |
|
|
|
|
147
|
|
|
$tmpDate = \DateTime::createFromFormat('Y-m-d H:i:s', $data['publicationDate']); |
148
|
|
|
$data['publicationDate'] = $tmpDate->format('d/m/Y H:i:s'); |
149
|
|
|
$form->setData(array( |
150
|
|
|
'publicationDate' => $data['publicationDate'] |
151
|
|
|
)); |
152
|
|
|
} |
153
|
|
View Code Duplication |
if (isset($data['startDate']) && $data['startDate']) { |
|
|
|
|
154
|
|
|
$tmpDate = \DateTime::createFromFormat('Y-m-d H:i:s', $data['startDate']); |
155
|
|
|
$data['startDate'] = $tmpDate->format('d/m/Y H:i:s'); |
156
|
|
|
$form->setData(array( |
157
|
|
|
'startDate' => $data['startDate'] |
158
|
|
|
)); |
159
|
|
|
} |
160
|
|
View Code Duplication |
if (isset($data['endDate']) && $data['endDate']) { |
|
|
|
|
161
|
|
|
$tmpDate = \DateTime::createFromFormat('Y-m-d H:i:s', $data['endDate']); |
162
|
|
|
$data['endDate'] = $tmpDate->format('d/m/Y H:i:s'); |
163
|
|
|
$form->setData(array( |
164
|
|
|
'endDate' => $data['endDate'] |
165
|
|
|
)); |
166
|
|
|
} |
167
|
|
View Code Duplication |
if (isset($data['closeDate']) && $data['closeDate']) { |
|
|
|
|
168
|
|
|
$tmpDate = \DateTime::createFromFormat('Y-m-d H:i:s', $data['closeDate']); |
169
|
|
|
$data['closeDate'] = $tmpDate->format('d/m/Y H:i:s'); |
170
|
|
|
$form->setData(array( |
171
|
|
|
'closeDate' => $data['closeDate'] |
172
|
|
|
)); |
173
|
|
|
} |
174
|
|
|
return false; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$game = $form->getData(); |
178
|
|
|
$game = $this->getGameMapper()->insert($game); |
179
|
|
|
|
180
|
|
|
// I wait for the game to be saved to obtain its ID. |
181
|
|
View Code Duplication |
if (! empty($data['uploadMainImage']['tmp_name'])) { |
|
|
|
|
182
|
|
|
ErrorHandler::start(); |
183
|
|
|
$data['uploadMainImage']['name'] = $this->fileNewname( |
184
|
|
|
$path, |
185
|
|
|
$game->getId() . "-" . $data['uploadMainImage']['name'] |
186
|
|
|
); |
187
|
|
|
move_uploaded_file($data['uploadMainImage']['tmp_name'], $path . $data['uploadMainImage']['name']); |
188
|
|
|
$game->setMainImage($media_url . $data['uploadMainImage']['name']); |
189
|
|
|
ErrorHandler::stop(true); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
View Code Duplication |
if (isset($data['deleteMainImage']) && |
|
|
|
|
193
|
|
|
$data['deleteMainImage'] && |
194
|
|
|
empty($data['uploadMainImage']['tmp_name']) |
195
|
|
|
) { |
196
|
|
|
ErrorHandler::start(); |
197
|
|
|
$image = $game->getMainImage(); |
198
|
|
|
$image = str_replace($media_url, '', $image); |
199
|
|
|
unlink($path . $image); |
200
|
|
|
$game->setMainImage(null); |
201
|
|
|
ErrorHandler::stop(true); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
View Code Duplication |
if (! empty($data['uploadSecondImage']['tmp_name'])) { |
|
|
|
|
205
|
|
|
ErrorHandler::start(); |
206
|
|
|
$data['uploadSecondImage']['name'] = $this->fileNewname( |
207
|
|
|
$path, |
208
|
|
|
$game->getId() . "-" . $data['uploadSecondImage']['name'] |
209
|
|
|
); |
210
|
|
|
move_uploaded_file($data['uploadSecondImage']['tmp_name'], $path . $data['uploadSecondImage']['name']); |
211
|
|
|
$game->setSecondImage($media_url . $data['uploadSecondImage']['name']); |
212
|
|
|
ErrorHandler::stop(true); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
View Code Duplication |
if (isset($data['deleteSecondImage']) && |
|
|
|
|
216
|
|
|
$data['deleteSecondImage'] && |
217
|
|
|
empty($data['uploadSecondImage']['tmp_name']) |
218
|
|
|
) { |
219
|
|
|
ErrorHandler::start(); |
220
|
|
|
$image = $game->getSecondImage(); |
221
|
|
|
$image = str_replace($media_url, '', $image); |
222
|
|
|
unlink($path . $image); |
223
|
|
|
$game->setSecondImage(null); |
224
|
|
|
ErrorHandler::stop(true); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if (! empty($data['uploadStylesheet']['tmp_name'])) { |
228
|
|
|
ErrorHandler::start(); |
229
|
|
|
move_uploaded_file($data['uploadStylesheet']['tmp_name'], $path . 'stylesheet_' . $game->getId() . '.css'); |
230
|
|
|
$game->setStylesheet($media_url . 'stylesheet_' . $game->getId() . '.css'); |
231
|
|
|
ErrorHandler::stop(true); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
View Code Duplication |
if (! empty($data['uploadFbShareImage']['tmp_name'])) { |
|
|
|
|
235
|
|
|
ErrorHandler::start(); |
236
|
|
|
$data['uploadFbShareImage']['name'] = $this->fileNewname( |
237
|
|
|
$path, |
238
|
|
|
$game->getId() . "-" . $data['uploadFbShareImage']['name'] |
239
|
|
|
); |
240
|
|
|
move_uploaded_file($data['uploadFbShareImage']['tmp_name'], $path . $data['uploadFbShareImage']['name']); |
241
|
|
|
$game->setFbShareImage($media_url . $data['uploadFbShareImage']['name']); |
242
|
|
|
ErrorHandler::stop(true); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
View Code Duplication |
if (isset($data['deleteFbShareImage']) && |
|
|
|
|
246
|
|
|
$data['deleteFbShareImage'] && |
247
|
|
|
empty($data['uploadFbShareImage']['tmp_name']) |
248
|
|
|
) { |
249
|
|
|
ErrorHandler::start(); |
250
|
|
|
$image = $game->getFbShareImage(); |
251
|
|
|
$image = str_replace($media_url, '', $image); |
252
|
|
|
unlink($path . $image); |
253
|
|
|
$game->setFbShareImage(null); |
254
|
|
|
ErrorHandler::stop(true); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (! empty($data['uploadFbPageTabImage']['tmp_name'])) { |
258
|
|
|
ErrorHandler::start(); |
259
|
|
|
$extension = $this->getExtension(strtolower($data['uploadFbPageTabImage']['name'])); |
260
|
|
|
$src = $this->getSrc($extension, $data['uploadFbPageTabImage']['tmp_name']); |
261
|
|
|
$this->resize( |
262
|
|
|
$data['uploadFbPageTabImage']['tmp_name'], |
263
|
|
|
$extension, |
264
|
|
|
$path . $game->getId() . "-" . $data['uploadFbPageTabImage']['name'], |
265
|
|
|
$src, |
266
|
|
|
111, |
267
|
|
|
74 |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
$game->setFbPageTabImage($media_url . $game->getId() . "-" . $data['uploadFbPageTabImage']['name']); |
271
|
|
|
ErrorHandler::stop(true); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
View Code Duplication |
if (isset($data['deleteFbPageTabImage']) && |
|
|
|
|
275
|
|
|
$data['deleteFbPageTabImage'] && |
276
|
|
|
empty($data['uploadFbPageTabImage']['tmp_name']) |
277
|
|
|
) { |
278
|
|
|
ErrorHandler::start(); |
279
|
|
|
$image = $game->getFbPageTabImage(); |
280
|
|
|
$image = str_replace($media_url, '', $image); |
281
|
|
|
unlink($path . $image); |
282
|
|
|
$game->setFbPageTabImage(null); |
283
|
|
|
ErrorHandler::stop(true); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$game = $this->getGameMapper()->update($game); |
287
|
|
|
|
288
|
|
|
$prize_mapper = $this->getServiceManager()->get('playgroundgame_prize_mapper'); |
289
|
|
|
if (isset($data['prizes'])) { |
290
|
|
|
foreach ($data['prizes'] as $prize_data) { |
291
|
|
|
if (! empty($prize_data['picture_file']['tmp_name']) && ! $prize_data['picture_file']['error']) { |
292
|
|
|
if ($prize_data['id']) { |
293
|
|
|
$prize = $prize_mapper->findById($prize_data['id']); |
294
|
|
|
} else { |
295
|
|
|
$some_prizes = $prize_mapper->findBy(array( |
296
|
|
|
'game' => $game, |
297
|
|
|
'title' => $prize_data['title'] |
298
|
|
|
)); |
299
|
|
|
if (count($some_prizes) == 1) { |
300
|
|
|
$prize = $some_prizes[0]; |
301
|
|
|
} else { |
302
|
|
|
return false; |
|
|
|
|
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
// Remove if existing image |
306
|
|
|
if ($prize->getPicture() && file_exists($prize->getPicture())) { |
307
|
|
|
unlink($prize->getPicture()); |
308
|
|
|
$prize->getPicture(null); |
309
|
|
|
} |
310
|
|
|
// Upload and set new |
311
|
|
|
ErrorHandler::start(); |
312
|
|
|
$filename = "game-" . $game->getId() . "-prize-"; |
313
|
|
|
$filename .= $prize->getId() . "-" . $prize_data['picture_file']['name']; |
314
|
|
|
move_uploaded_file($prize_data['picture_file']['tmp_name'], $path . $filename); |
315
|
|
|
$prize->setPicture($media_url . $filename); |
316
|
|
|
ErrorHandler::stop(true); |
317
|
|
|
$prize_mapper->update($prize); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
// If I receive false, it means that the FB Id was not available anymore |
322
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
323
|
|
|
'game' => $game |
324
|
|
|
)); |
325
|
|
|
|
326
|
|
|
return $game; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* getActiveGames |
331
|
|
|
* |
332
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
333
|
|
|
*/ |
334
|
|
|
public function getActiveGames($displayHome = true, $classType = '', $order = '') |
335
|
|
|
{ |
336
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
337
|
|
|
$today = new \DateTime("now"); |
338
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
339
|
|
|
$orderBy = 'g.publicationDate'; |
340
|
|
|
if ($order != '') { |
341
|
|
|
$orderBy = 'g.'.$order; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
$qb = $em->createQueryBuilder(); |
345
|
|
|
$and = $qb->expr()->andx(); |
346
|
|
|
$and->add( |
347
|
|
|
$qb->expr()->orX( |
348
|
|
|
$qb->expr()->lte('g.publicationDate', ':date'), |
349
|
|
|
$qb->expr()->isNull('g.publicationDate') |
350
|
|
|
) |
351
|
|
|
); |
352
|
|
|
$and->add( |
353
|
|
|
$qb->expr()->orX( |
354
|
|
|
$qb->expr()->gte('g.closeDate', ':date'), |
355
|
|
|
$qb->expr()->isNull('g.closeDate') |
356
|
|
|
) |
357
|
|
|
); |
358
|
|
|
$qb->setParameter('date', $today); |
359
|
|
|
|
360
|
|
|
$and->add($qb->expr()->eq('g.active', '1')); |
361
|
|
|
$and->add($qb->expr()->eq('g.broadcastPlatform', '1')); |
362
|
|
|
|
363
|
|
|
if ($classType != '') { |
364
|
|
|
$and->add($qb->expr()->eq('g.classType', ':classType')); |
365
|
|
|
$qb->setParameter('classType', $classType); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
if ($displayHome) { |
369
|
|
|
$and->add($qb->expr()->eq('g.displayHome', true)); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
$qb->select('g') |
373
|
|
|
->from('PlaygroundGame\Entity\Game', 'g') |
374
|
|
|
->where($and) |
375
|
|
|
->orderBy($orderBy, 'DESC'); |
376
|
|
|
|
377
|
|
|
$query = $qb->getQuery(); |
378
|
|
|
$games = $query->getResult(); |
379
|
|
|
|
380
|
|
|
// je les classe par date de publication (date comme clé dans le tableau afin de pouvoir merger les objets |
381
|
|
|
// de type article avec le même procédé en les classant naturellement par date asc ou desc |
382
|
|
|
$arrayGames = array(); |
383
|
|
View Code Duplication |
foreach ($games as $game) { |
|
|
|
|
384
|
|
|
if ($game->getPublicationDate()) { |
385
|
|
|
$key = $game->getPublicationDate()->format('Ymd'); |
386
|
|
|
} elseif ($game->getStartDate()) { |
387
|
|
|
$key = $game->getStartDate()->format('Ymd'); |
388
|
|
|
} else { |
389
|
|
|
$key = $game->getUpdatedAt()->format('Ymd'); |
390
|
|
|
} |
391
|
|
|
$key .= $game->getUpdatedAt()->format('Ymd') . '-' . $game->getId(); |
392
|
|
|
$arrayGames[$key] = $game; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
return $arrayGames; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* getAvailableGames : Games OnLine and not already played by $user |
400
|
|
|
* |
401
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
402
|
|
|
*/ |
403
|
|
|
public function getAvailableGames($user, $maxResults = 2) |
404
|
|
|
{ |
405
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
406
|
|
|
$today = new \DateTime("now"); |
407
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
408
|
|
|
|
409
|
|
|
// Game active with a start_date before today (or without start_date) |
410
|
|
|
// and end_date after today (or without end-date) |
411
|
|
|
$query = $em->createQuery('SELECT g FROM PlaygroundGame\Entity\Game g |
412
|
|
|
WHERE NOT EXISTS (SELECT l FROM PlaygroundGame\Entity\Entry l WHERE l.game = g AND l.user = :user) |
413
|
|
|
AND (g.startDate <= :date OR g.startDate IS NULL) |
414
|
|
|
AND (g.endDate >= :date OR g.endDate IS NULL) |
415
|
|
|
AND g.active = 1 AND g.broadcastPlatform = 1 |
416
|
|
|
ORDER BY g.startDate ASC'); |
417
|
|
|
$query->setParameter('date', $today); |
418
|
|
|
$query->setParameter('user', $user); |
419
|
|
|
$query->setMaxResults($maxResults); |
420
|
|
|
$games = $query->getResult(); |
421
|
|
|
|
422
|
|
|
return $games; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
View Code Duplication |
public function getEntriesQuery($game) |
|
|
|
|
426
|
|
|
{ |
427
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
428
|
|
|
|
429
|
|
|
$qb = $em->createQueryBuilder(); |
430
|
|
|
$qb->select(' |
431
|
|
|
e.id, |
432
|
|
|
u.username, |
433
|
|
|
u.title, |
434
|
|
|
u.firstname, |
435
|
|
|
u.lastname, |
436
|
|
|
u.email, |
437
|
|
|
u.optin, |
438
|
|
|
u.optinPartner, |
439
|
|
|
u.address, |
440
|
|
|
u.address2, |
441
|
|
|
u.postalCode, |
442
|
|
|
u.city, |
443
|
|
|
u.telephone, |
444
|
|
|
u.mobile, |
445
|
|
|
u.created_at, |
446
|
|
|
u.dob, |
447
|
|
|
e.winner, |
448
|
|
|
e.socialShares, |
449
|
|
|
e.playerData, |
450
|
|
|
e.updated_at |
451
|
|
|
') |
452
|
|
|
->from('PlaygroundGame\Entity\Entry', 'e') |
453
|
|
|
->leftJoin('e.user', 'u') |
454
|
|
|
->where($qb->expr()->eq('e.game', ':game')); |
455
|
|
|
|
456
|
|
|
$qb->setParameter('game', $game); |
457
|
|
|
|
458
|
|
|
return $qb->getQuery(); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
public function getEntriesHeader($game) |
462
|
|
|
{ |
463
|
|
|
if ($game->getPlayerForm()) { |
464
|
|
|
$formPV = json_decode($game->getPlayerForm()->getForm(), true); |
465
|
|
|
$header = array('id'=> 1); |
466
|
|
View Code Duplication |
foreach ($formPV as $element) { |
|
|
|
|
467
|
|
|
foreach ($element as $k => $v) { |
468
|
|
|
if ($k !== 'form_properties') { |
469
|
|
|
$header[$v[0]['name']] = 1; |
470
|
|
|
} |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
} else { |
474
|
|
|
$header = array( |
475
|
|
|
'id' => 1, |
476
|
|
|
'username' => 1, |
477
|
|
|
'title' => 1, |
478
|
|
|
'firstname' => 1, |
479
|
|
|
'lastname' => 1, |
480
|
|
|
'email' => 1, |
481
|
|
|
'optin' => 1, |
482
|
|
|
'optinPartner' => 1, |
483
|
|
|
'address' => 1, |
484
|
|
|
'address2' => 1, |
485
|
|
|
'postalCode' => 1, |
486
|
|
|
'city' => 1, |
487
|
|
|
'telephone' => 1, |
488
|
|
|
'mobile' => 1, |
489
|
|
|
'created_at' => 1, |
490
|
|
|
'dob' => 1, |
491
|
|
|
'winner' => 1 |
492
|
|
|
); |
493
|
|
|
} |
494
|
|
|
$header['winner'] = 1; |
495
|
|
|
$header['socialShares'] = 1; |
496
|
|
|
$header['updated_at'] = 1; |
497
|
|
|
|
498
|
|
|
return $header; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* getGameEntries : I create an array of entries based on playerData + header |
503
|
|
|
* |
504
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
505
|
|
|
*/ |
506
|
|
|
public function getGameEntries($header, $entries, $game) |
507
|
|
|
{ |
508
|
|
|
$header = $this->getEntriesHeader($game); |
509
|
|
|
|
510
|
|
|
$results = array(); |
511
|
|
|
|
512
|
|
|
foreach ($entries as $k => $entry) { |
513
|
|
|
$entryData = json_decode($entry['playerData'], true); |
514
|
|
View Code Duplication |
foreach ($header as $key => $v) { |
|
|
|
|
515
|
|
|
if (isset($entryData[$key])) { |
516
|
|
|
$results[$k][$key] = (is_array($entryData[$key]))?implode(', ', $entryData[$key]):$entryData[$key]; |
517
|
|
|
} elseif (array_key_exists($key, $entry)) { |
518
|
|
|
$results[$k][$key] = ($entry[$key] instanceof \DateTime)? |
519
|
|
|
$entry[$key]->format('Y-m-d H:i:s'): |
520
|
|
|
$entry[$key]; |
521
|
|
|
} else { |
522
|
|
|
$results[$k][$key] = ''; |
523
|
|
|
} |
524
|
|
|
} |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
return $results; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* getActiveSliderGames |
532
|
|
|
* |
533
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
534
|
|
|
*/ |
535
|
|
|
public function getActiveSliderGames() |
536
|
|
|
{ |
537
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
538
|
|
|
$today = new \DateTime("now"); |
539
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
540
|
|
|
|
541
|
|
|
// Game active with a start_date before today (or without start_date) |
542
|
|
|
// and end_date after today (or without end-date) |
543
|
|
|
$query = $em->createQuery('SELECT g FROM PlaygroundGame\Entity\Game g |
544
|
|
|
WHERE (g.publicationDate <= :date OR g.publicationDate IS NULL) |
545
|
|
|
AND (g.closeDate >= :date OR g.closeDate IS NULL) |
546
|
|
|
AND g.active = true AND g.broadcastPlatform = 1 AND g.pushHome = true'); |
547
|
|
|
$query->setParameter('date', $today); |
548
|
|
|
$games = $query->getResult(); |
549
|
|
|
|
550
|
|
|
// je les classe par date de publication (date comme clé dans le tableau afin de pouvoir merger les objets |
551
|
|
|
// de type article avec le même procédé en les classant naturellement par date asc ou desc |
552
|
|
|
$arrayGames = array(); |
553
|
|
View Code Duplication |
foreach ($games as $game) { |
|
|
|
|
554
|
|
|
if ($game->getPublicationDate()) { |
555
|
|
|
$key = $game->getPublicationDate()->format('Ymd'); |
556
|
|
|
} elseif ($game->getStartDate()) { |
557
|
|
|
$key = $game->getStartDate()->format('Ymd'); |
558
|
|
|
} else { |
559
|
|
|
$key = $game->getUpdatedAt()->format('Ymd'); |
560
|
|
|
} |
561
|
|
|
$key .= $game->getUpdatedAt()->format('Ymd') . '-' . $game->getId(); |
562
|
|
|
$arrayGames[$key] = $game; |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
return $arrayGames; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* getPrizeCategoryGames |
570
|
|
|
* |
571
|
|
|
* @return Array of PlaygroundGame\Entity\Game |
572
|
|
|
*/ |
573
|
|
View Code Duplication |
public function getPrizeCategoryGames($categoryid) |
|
|
|
|
574
|
|
|
{ |
575
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
576
|
|
|
|
577
|
|
|
$query = $em->createQuery('SELECT g FROM PlaygroundGame\Entity\Game g |
578
|
|
|
WHERE (g.prizeCategory = :categoryid AND g.broadcastPlatform = 1) |
579
|
|
|
ORDER BY g.publicationDate DESC'); |
580
|
|
|
$query->setParameter('categoryid', $categoryid); |
581
|
|
|
$games = $query->getResult(); |
582
|
|
|
|
583
|
|
|
return $games; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
public function checkGame($identifier, $checkIfStarted = true) |
587
|
|
|
{ |
588
|
|
|
$gameMapper = $this->getGameMapper(); |
589
|
|
|
|
590
|
|
|
if (! $identifier) { |
591
|
|
|
return false; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
$game = $gameMapper->findByIdentifier($identifier); |
595
|
|
|
|
596
|
|
|
// the game has not been found |
597
|
|
|
if (! $game) { |
598
|
|
|
return false; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
// for preview stuff as admin |
602
|
|
|
if ($this->isAllowed('game', 'edit')) { |
603
|
|
|
$r =$this->getServiceManager()->get('request'); |
604
|
|
|
if ($r->getQuery()->get('preview')) { |
605
|
|
|
$game->setActive(true); |
606
|
|
|
$game->setStartDate(null); |
607
|
|
|
$game->setEndDate(null); |
608
|
|
|
$game->setPublicationDate(null); |
609
|
|
|
$game->setBroadcastPlatform(true); |
610
|
|
|
|
611
|
|
|
// I don't want the game to be updated through any update during the preview mode. |
612
|
|
|
// I mark it as readonly for Doctrine |
613
|
|
|
$this->getServiceManager() |
614
|
|
|
->get('doctrine.entitymanager.orm_default') |
615
|
|
|
->getUnitOfWork() |
616
|
|
|
->markReadOnly($game); |
617
|
|
|
|
618
|
|
|
return $game; |
619
|
|
|
} |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
// The game is inactive |
623
|
|
|
if (! $game->getActive()) { |
624
|
|
|
return false; |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
// the game has not begun yet |
628
|
|
|
if (! $game->isOpen()) { |
629
|
|
|
return false; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
// the game is finished and closed |
633
|
|
|
if (! $game->isStarted() && $checkIfStarted) { |
634
|
|
|
return false; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
return $game; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Return the last entry of the user on this game, if it exists. |
642
|
|
|
* An entry can be associated to : |
643
|
|
|
* - A user account (a real one, linked to PlaygroundUser |
644
|
|
|
* - An anonymous Identifier (based on one value of playerData (generally email)) |
645
|
|
|
* - A cookie set on the player device (the less secure) |
646
|
|
|
* If the active param is set, it can check if the entry is active or not. |
647
|
|
|
* If the bonus param is set, it can check if the entry is a bonus or not. |
648
|
|
|
* |
649
|
|
|
* @param unknown $game |
650
|
|
|
* @param string $user |
651
|
|
|
* @param boolean $active |
652
|
|
|
* @param boolean $bonus |
653
|
|
|
* @return boolean |
654
|
|
|
*/ |
655
|
|
|
public function checkExistingEntry($game, $user = null, $active = null, $bonus = null) |
656
|
|
|
{ |
657
|
|
|
$search = array('game' => $game); |
658
|
|
|
|
659
|
|
|
if ($user) { |
|
|
|
|
660
|
|
|
$search['user'] = $user; |
661
|
|
|
} elseif ($this->getAnonymousIdentifier()) { |
662
|
|
|
$search['anonymousIdentifier'] = $this->getAnonymousIdentifier(); |
663
|
|
|
$search['user'] = null; |
664
|
|
|
} else { |
665
|
|
|
$search['anonymousId'] = $this->getAnonymousId(); |
666
|
|
|
$search['user'] = null; |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
if (! is_null($active)) { |
670
|
|
|
$search['active'] = $active; |
671
|
|
|
} |
672
|
|
|
if (! is_null($bonus)) { |
673
|
|
|
$search['bonus'] = $bonus; |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
$entry = $this->getEntryMapper()->findOneBy($search, array('updated_at' => 'desc')); |
677
|
|
|
|
678
|
|
|
return $entry; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/* |
682
|
|
|
* This function updates the entry with the player data after checking |
683
|
|
|
* that the data are compliant with the formUser Game attribute |
684
|
|
|
* |
685
|
|
|
* The $data has to be a json object |
686
|
|
|
*/ |
687
|
|
|
public function updateEntryPlayerForm($data, $game, $user, $entry, $mandatory = true) |
|
|
|
|
688
|
|
|
{ |
689
|
|
|
$form = $this->createFormFromJson($game->getPlayerForm()->getForm(), 'playerForm'); |
690
|
|
|
$form->setData($data); |
691
|
|
|
|
692
|
|
|
if (!$mandatory) { |
693
|
|
|
$filter = $form->getInputFilter(); |
694
|
|
|
foreach ($form->getElements() as $element) { |
695
|
|
|
try { |
696
|
|
|
$elementInput = $filter->get($element->getName()); |
697
|
|
|
$elementInput->setRequired(false); |
|
|
|
|
698
|
|
|
$form->get($element->getName())->setAttribute('required', false); |
699
|
|
|
} catch (\Zend\Form\Exception\InvalidElementException $e) { |
|
|
|
|
700
|
|
|
} |
701
|
|
|
} |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
if ($form->isValid()) { |
705
|
|
|
$dataJson = json_encode($form->getData()); |
706
|
|
|
|
707
|
|
|
if ($game->getAnonymousAllowed() && |
708
|
|
|
$game->getAnonymousIdentifier() && |
709
|
|
|
isset($data[$game->getAnonymousIdentifier()]) |
710
|
|
|
) { |
711
|
|
|
$session = new \Zend\Session\Container('anonymous_identifier'); |
712
|
|
|
if (empty($session->offsetGet('anonymous_identifier'))) { |
713
|
|
|
$anonymousIdentifier = $data[$game->getAnonymousIdentifier()]; |
714
|
|
|
|
715
|
|
|
$entry->setAnonymousIdentifier($anonymousIdentifier); |
716
|
|
|
|
717
|
|
|
// I must transmit this info during the whole game workflow |
718
|
|
|
$session->offsetSet('anonymous_identifier', $anonymousIdentifier); |
719
|
|
|
} |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
$entry->setPlayerData($dataJson); |
723
|
|
|
$this->getEntryMapper()->update($entry); |
724
|
|
|
} else { |
725
|
|
|
return false; |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
return true; |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
|
732
|
|
|
public function checkIsFan($game) |
|
|
|
|
733
|
|
|
{ |
734
|
|
|
// If on Facebook, check if you have to be a FB fan to play the game |
735
|
|
|
$session = new Container('facebook'); |
736
|
|
|
|
737
|
|
|
if ($session->offsetExists('signed_request')) { |
738
|
|
|
// I'm on Facebook |
739
|
|
|
$sr = $session->offsetGet('signed_request'); |
740
|
|
|
if ($sr['page']['liked'] == 1) { |
741
|
|
|
return true; |
742
|
|
|
} |
743
|
|
|
} else { |
744
|
|
|
// I'm not on Facebook |
745
|
|
|
return true; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
return false; |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
public function getAnonymousIdentifier() |
752
|
|
|
{ |
753
|
|
|
if (is_null($this->anonymousIdentifier)) { |
754
|
|
|
// If on Facebook, check if you have to be a FB fan to play the game |
755
|
|
|
$session = new Container('anonymous_identifier'); |
756
|
|
|
|
757
|
|
|
if ($session->offsetExists('anonymous_identifier')) { |
758
|
|
|
$this->anonymousIdentifier = $session->offsetGet('anonymous_identifier'); |
759
|
|
|
} else { |
760
|
|
|
$this->anonymousIdentifier = false; |
761
|
|
|
} |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
return $this->anonymousIdentifier; |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
/** |
768
|
|
|
* errors : |
769
|
|
|
* -1 : user not connected |
770
|
|
|
* -2 : limit entry games for this user reached |
771
|
|
|
* |
772
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
773
|
|
|
* @param \PlaygroundUser\Entity\UserInterface $user |
774
|
|
|
* @return number unknown |
775
|
|
|
*/ |
776
|
|
|
public function play($game, $user) |
777
|
|
|
{ |
778
|
|
|
|
779
|
|
|
// certaines participations peuvent rester ouvertes. |
780
|
|
|
// On autorise alors le joueur à reprendre là ou il en était |
781
|
|
|
// par exemple les postvote... |
782
|
|
|
$entry = $this->checkExistingEntry($game, $user, true); |
|
|
|
|
783
|
|
|
|
784
|
|
|
if (! $entry) { |
785
|
|
|
if ($this->hasReachedPlayLimit($game, $user)) { |
786
|
|
|
return false; |
|
|
|
|
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
$entry = new Entry(); |
790
|
|
|
$entry->setGame($game); |
791
|
|
|
$entry->setUser($user); |
792
|
|
|
$entry->setPoints(0); |
793
|
|
|
$entry->setIp($this->getIp()); |
794
|
|
|
$entry->setAnonymousId($this->getAnonymousId()); |
795
|
|
|
if ($this->getAnonymousIdentifier()) { |
796
|
|
|
$entry->setAnonymousIdentifier($this->getAnonymousIdentifier()); |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
$entry = $this->getEntryMapper()->insert($entry); |
800
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
801
|
|
|
'user' => $user, |
802
|
|
|
'game' => $game, |
803
|
|
|
'entry' => $entry |
804
|
|
|
)); |
805
|
|
|
} |
806
|
|
|
|
807
|
|
|
return $entry; |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
/** |
811
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
812
|
|
|
* @param \PlaygroundUser\Entity\UserInterface $user |
813
|
|
|
*/ |
814
|
|
|
public function hasReachedPlayLimit($game, $user) |
815
|
|
|
{ |
816
|
|
|
// Is there a limitation on the game ? |
817
|
|
|
$limitAmount = $game->getPlayLimit(); |
818
|
|
|
if ($limitAmount) { |
819
|
|
|
$limitScale = $game->getPlayLimitScale(); |
820
|
|
|
$userEntries = $this->findLastEntries($game, $user, $limitScale); |
821
|
|
|
|
822
|
|
|
// player has reached the game limit |
823
|
|
|
if ($userEntries >= $limitAmount) { |
824
|
|
|
return true; |
825
|
|
|
} |
826
|
|
|
} |
827
|
|
|
return false; |
828
|
|
|
} |
829
|
|
|
|
830
|
|
|
/** |
831
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
832
|
|
|
* @param \PlaygroundUser\Entity\UserInterface $user |
833
|
|
|
*/ |
834
|
|
|
public function findLastEntries($game, $user, $limitScale) |
835
|
|
|
{ |
836
|
|
|
$limitDate = $this->getLimitDate($limitScale); |
837
|
|
|
|
838
|
|
|
if ($user) { |
839
|
|
|
return $this->getEntryMapper()->findLastEntriesByUser($game, $user, $limitDate); |
840
|
|
|
} elseif ($this->getAnonymousIdentifier()) { |
841
|
|
|
return $this->getEntryMapper()->findLastEntriesByAnonymousIdentifier( |
842
|
|
|
$game, |
843
|
|
|
$this->getAnonymousIdentifier(), |
844
|
|
|
$limitDate |
845
|
|
|
); |
846
|
|
|
} else { |
847
|
|
|
return $this->getEntryMapper()->findLastEntriesByIp($game, $this->getIp(), $limitDate); |
848
|
|
|
} |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
/** |
852
|
|
|
* |
853
|
|
|
* |
854
|
|
|
*/ |
855
|
|
|
public function getLimitDate($limitScale) |
856
|
|
|
{ |
857
|
|
|
$now = new \DateTime("now"); |
858
|
|
|
switch ($limitScale) { |
859
|
|
|
case 'always': |
860
|
|
|
$interval = 'P100Y'; |
861
|
|
|
$now->sub(new \DateInterval($interval)); |
862
|
|
|
$dateLimit = $now->format('Y-m-d H:i:s'); |
863
|
|
|
break; |
864
|
|
|
case 'day': |
865
|
|
|
$dateLimit = $now->format('Y-m-d H:i:s'); |
866
|
|
|
break; |
867
|
|
View Code Duplication |
case 'week': |
|
|
|
|
868
|
|
|
$interval = 'P7D'; |
869
|
|
|
$now->sub(new \DateInterval($interval)); |
870
|
|
|
$dateLimit = $now->format('Y-m-d H:i:s'); |
871
|
|
|
break; |
872
|
|
View Code Duplication |
case 'month': |
|
|
|
|
873
|
|
|
$interval = 'P1M'; |
874
|
|
|
$now->sub(new \DateInterval($interval)); |
875
|
|
|
$dateLimit = $now->format('Y-m-d H:i:s'); |
876
|
|
|
break; |
877
|
|
View Code Duplication |
case 'year': |
|
|
|
|
878
|
|
|
$interval = 'P1Y'; |
879
|
|
|
$now->sub(new \DateInterval($interval)); |
880
|
|
|
$dateLimit = $now->format('Y-m-d H:i:s'); |
881
|
|
|
break; |
882
|
|
|
default: |
883
|
|
|
$interval = 'P100Y'; |
884
|
|
|
$now->sub(new \DateInterval($interval)); |
885
|
|
|
$dateLimit = $now->format('Y-m-d H:i:s'); |
886
|
|
|
} |
887
|
|
|
|
888
|
|
|
return $dateLimit; |
889
|
|
|
} |
890
|
|
|
|
891
|
|
|
public function findLastActiveEntry($game, $user) |
892
|
|
|
{ |
893
|
|
|
return $this->checkExistingEntry($game, $user, true); |
894
|
|
|
} |
895
|
|
|
|
896
|
|
|
public function findLastInactiveEntry($game, $user) |
897
|
|
|
{ |
898
|
|
|
return $this->checkExistingEntry($game, $user, false, false); |
899
|
|
|
} |
900
|
|
|
|
901
|
|
|
public function findLastEntry($game, $user) |
902
|
|
|
{ |
903
|
|
|
return $this->checkExistingEntry($game, $user, null, false); |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
public function inviteToTeam($data, $game, $user){ |
907
|
|
|
$mailService = $this->getServiceManager()->get('playgroundgame_message'); |
908
|
|
|
$invitationMapper = $this->getServiceManager()->get('playgroundgame_invitation_mapper'); |
909
|
|
|
|
910
|
|
|
$sentInvitations = $invitationMapper->findBy(array('host' => $user, 'game' => $game)); |
911
|
|
|
$nbInvitations = count($sentInvitations); |
912
|
|
|
$to = $data['email']; |
913
|
|
|
if(empty($to)) return ['result'=>false, 'message'=>'no email']; |
914
|
|
|
|
915
|
|
|
if($nbInvitations < 20){ |
916
|
|
|
$alreadyInvited = $invitationMapper->findBy(array('requestKey' => $to, 'game' => $game)); |
917
|
|
|
if(!$alreadyInvited) $alreadyInvited = $this->getUserMapper()->findByEmail($to); |
918
|
|
|
|
919
|
|
|
if(empty($alreadyInvited)){ |
920
|
|
|
$invitation = new \PlaygroundGame\Entity\Invitation(); |
921
|
|
|
$invitation->setRequestKey($to); |
922
|
|
|
$invitation->setGame($game); |
923
|
|
|
$invitation->setHost($user); |
924
|
|
|
$invitationMapper->insert($invitation); |
925
|
|
|
|
926
|
|
|
$from = $this->getOptions()->getEmailFromAddress(); |
927
|
|
|
$subject = $this->getServiceManager()->get('translator')->translate( |
928
|
|
|
$this->getOptions()->getInviteToTeamSubjectLine(), |
929
|
|
|
'playgroundgame' |
930
|
|
|
); |
931
|
|
|
$message = $mailService->createHtmlMessage( |
932
|
|
|
$from, |
933
|
|
|
$to, |
934
|
|
|
$subject, |
935
|
|
|
'playground-game/email/invite_team', |
936
|
|
|
array( |
937
|
|
|
'game' => $game, |
938
|
|
|
'user' => $user, |
939
|
|
|
'data' => $data, |
940
|
|
|
'from' => $from |
941
|
|
|
) |
942
|
|
|
); |
943
|
|
|
try { |
944
|
|
|
$mailService->send($message); |
945
|
|
|
} catch (\Zend\Mail\Protocol\Exception\RuntimeException $e) { |
946
|
|
|
return ['result' => true, 'message' => $this->getServiceManager()->get('translator')->translate( |
947
|
|
|
'mail error' |
948
|
|
|
)]; |
949
|
|
|
} |
950
|
|
|
|
951
|
|
|
return ['result' => true, 'message' => '']; |
952
|
|
|
|
953
|
|
|
} else { |
954
|
|
|
return ['result' => false, 'message' => 'already invited']; |
955
|
|
|
} |
956
|
|
|
} else { |
957
|
|
|
return [ |
958
|
|
|
'result' => false, |
959
|
|
|
'message' => $this->getServiceManager()->get('translator')->translate( |
960
|
|
|
'Too many invitations for this user' |
961
|
|
|
) |
962
|
|
|
]; |
963
|
|
|
} |
964
|
|
|
|
965
|
|
|
} |
966
|
|
|
|
967
|
|
|
public function sendShareMail( |
968
|
|
|
$data, |
969
|
|
|
$game, |
970
|
|
|
$user, |
971
|
|
|
$entry, |
972
|
|
|
$template = 'share_game', |
973
|
|
|
$topic = null, |
974
|
|
|
$userTimer = array(), |
975
|
|
|
$subject = '' |
976
|
|
|
) { |
977
|
|
|
$mailService = $this->getServiceManager()->get('playgroundgame_message'); |
978
|
|
|
$mailSent = false; |
979
|
|
|
$from = $this->getOptions()->getEmailFromAddress(); |
980
|
|
|
|
981
|
|
|
if(empty($subject)){ |
982
|
|
|
$subject = $this->getServiceManager()->get('translator')->translate( |
983
|
|
|
$this->getOptions()->getShareSubjectLine(), |
984
|
|
|
'playgroundgame' |
985
|
|
|
); |
986
|
|
|
} else { |
987
|
|
|
$subject = $this->getServiceManager()->get('translator')->translate( |
988
|
|
|
$subject, |
989
|
|
|
'playgroundgame' |
990
|
|
|
); |
991
|
|
|
} |
992
|
|
|
|
993
|
|
|
$renderer = $this->getServiceManager()->get('Zend\View\Renderer\RendererInterface'); |
994
|
|
|
$skinUrl = $renderer->url( |
995
|
|
|
'frontend', |
996
|
|
|
array(), |
997
|
|
|
array('force_canonical' => true) |
998
|
|
|
); |
999
|
|
|
$secretKey = strtoupper(substr(sha1(uniqid('pg_', true) . '####' . time()), 0, 15)); |
1000
|
|
|
|
1001
|
|
|
if (! $topic) { |
1002
|
|
|
$topic = $game->getTitle(); |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
if(isset($data['email']) && !is_array($data['email'])) $data['email'] = array($data['email']); |
1006
|
|
|
|
1007
|
|
|
foreach ($data['email'] as $to) { |
1008
|
|
|
$mailSent = true; |
1009
|
|
|
if (!empty($to)) { |
1010
|
|
|
$message = $mailService->createHtmlMessage( |
1011
|
|
|
$from, |
1012
|
|
|
$to, |
1013
|
|
|
$subject, |
1014
|
|
|
'playground-game/email/' . $template, |
1015
|
|
|
array( |
1016
|
|
|
'game' => $game, |
1017
|
|
|
'data' => $data, |
1018
|
|
|
'from' => $from, |
1019
|
|
|
'to' => $to, |
1020
|
|
|
'secretKey' => $secretKey, |
1021
|
|
|
'skinUrl' => $skinUrl, |
1022
|
|
|
'userTimer' => $userTimer |
1023
|
|
|
) |
1024
|
|
|
); |
1025
|
|
|
try { |
1026
|
|
|
$mailService->send($message); |
1027
|
|
|
} catch (\Zend\Mail\Protocol\Exception\RuntimeException $e) { |
1028
|
|
|
//$mailSent = false; |
1029
|
|
|
} |
1030
|
|
|
|
1031
|
|
|
if ($entry) { |
1032
|
|
|
$shares = json_decode($entry->getSocialShares(), true); |
1033
|
|
|
(!isset($shares['mail']))? $shares['mail'] = 1:$shares['mail'] += 1; |
1034
|
|
|
} |
1035
|
|
|
} |
1036
|
|
|
} |
1037
|
|
|
|
1038
|
|
|
if ($mailSent) { |
1039
|
|
|
if ($entry) { |
1040
|
|
|
$sharesJson = json_encode($shares); |
|
|
|
|
1041
|
|
|
$entry->setSocialShares($sharesJson); |
1042
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1043
|
|
|
} |
1044
|
|
|
|
1045
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
1046
|
|
|
'user' => $user, |
1047
|
|
|
'topic' => $topic, |
1048
|
|
|
'secretKey' => $secretKey, |
1049
|
|
|
'data' => $data, |
1050
|
|
|
'game' => $game, |
1051
|
|
|
'entry' => $entry |
1052
|
|
|
)); |
1053
|
|
|
|
1054
|
|
|
return true; |
1055
|
|
|
} |
1056
|
|
|
|
1057
|
|
|
return false; |
1058
|
|
|
} |
1059
|
|
|
|
1060
|
|
|
/** |
1061
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
1062
|
|
|
* @param \PlaygroundUser\Entity\User $user |
1063
|
|
|
* @param Entry $entry |
1064
|
|
|
* @param \PlaygroundGame\Entity\Prize $prize |
1065
|
|
|
*/ |
1066
|
|
|
public function sendResultMail($game, $user, $entry, $template = 'entry', $prize = null) |
1067
|
|
|
{ |
1068
|
|
|
$mailService = $this->getServiceManager()->get('playgroundgame_message'); |
1069
|
|
|
$from = $this->getOptions()->getEmailFromAddress(); |
1070
|
|
|
if ($user) { |
1071
|
|
|
$to = $user->getEmail(); |
1072
|
|
|
} elseif ($entry->getAnonymousIdentifier()) { |
1073
|
|
|
$to = $entry->getAnonymousIdentifier(); |
1074
|
|
|
} else { |
1075
|
|
|
return false; |
1076
|
|
|
} |
1077
|
|
|
$subject = $game->getTitle(); |
1078
|
|
|
$renderer = $this->getServiceManager()->get('Zend\View\Renderer\RendererInterface'); |
1079
|
|
|
$skinUrl = $renderer->url( |
1080
|
|
|
'frontend', |
1081
|
|
|
array(), |
1082
|
|
|
array('force_canonical' => true) |
1083
|
|
|
); |
1084
|
|
|
$message = $mailService->createHtmlMessage($from, $to, $subject, 'playground-game/email/' . $template, array( |
1085
|
|
|
'game' => $game, |
1086
|
|
|
'entry' => $entry, |
1087
|
|
|
'skinUrl' => $skinUrl, |
1088
|
|
|
'prize' => $prize |
1089
|
|
|
)); |
1090
|
|
|
$mailService->send($message); |
1091
|
|
|
} |
1092
|
|
|
|
1093
|
|
|
public function sendGameMail($game, $user, $post, $template = 'postvote') |
1094
|
|
|
{ |
1095
|
|
|
$mailService = $this->getServiceManager()->get('playgroundgame_message'); |
1096
|
|
|
$from = $this->getOptions()->getEmailFromAddress(); |
1097
|
|
|
$to = $user->getEmail(); |
1098
|
|
|
$subject = $this->getServiceManager()->get('translator')->translate( |
1099
|
|
|
$this->getOptions()->getParticipationSubjectLine(), 'playgroundgame' |
1100
|
|
|
); |
1101
|
|
|
$renderer = $this->getServiceManager()->get('Zend\View\Renderer\RendererInterface'); |
1102
|
|
|
$skinUrl = $renderer->url( |
1103
|
|
|
'frontend', |
1104
|
|
|
array(), |
1105
|
|
|
array('force_canonical' => true) |
1106
|
|
|
); |
1107
|
|
|
|
1108
|
|
|
$message = $mailService->createHtmlMessage($from, $to, $subject, 'playground-game/email/' . $template, array( |
1109
|
|
|
'game' => $game, |
1110
|
|
|
'post' => $post, |
1111
|
|
|
'skinUrl' => $skinUrl |
1112
|
|
|
)); |
1113
|
|
|
$mailService->send($message); |
1114
|
|
|
} |
1115
|
|
|
|
1116
|
|
View Code Duplication |
public function postFbWall($secretKey, $game, $user, $entry) |
|
|
|
|
1117
|
|
|
{ |
1118
|
|
|
$topic = $game->getTitle(); |
1119
|
|
|
|
1120
|
|
|
$shares = json_decode($entry->getSocialShares(), true); |
1121
|
|
|
if (!isset($shares['fbwall'])) { |
1122
|
|
|
$shares['fbwall'] = 1; |
1123
|
|
|
} else { |
1124
|
|
|
$shares['fbwall'] += 1; |
1125
|
|
|
} |
1126
|
|
|
$sharesJson = json_encode($shares); |
1127
|
|
|
$entry->setSocialShares($sharesJson); |
1128
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1129
|
|
|
|
1130
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
1131
|
|
|
'user' => $user, |
1132
|
|
|
'game' => $game, |
1133
|
|
|
'secretKey' => $secretKey, |
1134
|
|
|
'topic' => $topic, |
1135
|
|
|
'entry' => $entry |
1136
|
|
|
)); |
1137
|
|
|
|
1138
|
|
|
return true; |
1139
|
|
|
} |
1140
|
|
|
|
1141
|
|
|
public function postFbRequest($secretKey, $game, $user, $entry, $to) |
1142
|
|
|
{ |
1143
|
|
|
$shares = json_decode($entry->getSocialShares(), true); |
1144
|
|
|
$to = explode(',', $to); |
1145
|
|
|
if (!isset($shares['fbrequest'])) { |
1146
|
|
|
$shares['fbrequest'] = count($to); |
1147
|
|
|
} else { |
1148
|
|
|
$shares['fbrequest'] += count($to); |
1149
|
|
|
} |
1150
|
|
|
$sharesJson = json_encode($shares); |
1151
|
|
|
$entry->setSocialShares($sharesJson); |
1152
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1153
|
|
|
|
1154
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
1155
|
|
|
'user' => $user, |
1156
|
|
|
'game' => $game, |
1157
|
|
|
'secretKey' => $secretKey, |
1158
|
|
|
'entry' => $entry, |
1159
|
|
|
'invites' => count($to) |
1160
|
|
|
)); |
1161
|
|
|
|
1162
|
|
|
return true; |
1163
|
|
|
} |
1164
|
|
|
|
1165
|
|
View Code Duplication |
public function postTwitter($secretKey, $game, $user, $entry) |
|
|
|
|
1166
|
|
|
{ |
1167
|
|
|
$topic = $game->getTitle(); |
1168
|
|
|
|
1169
|
|
|
$shares = json_decode($entry->getSocialShares(), true); |
1170
|
|
|
if (!isset($shares['fbrequest'])) { |
1171
|
|
|
$shares['tweet'] = 1; |
1172
|
|
|
} else { |
1173
|
|
|
$shares['tweet'] += 1; |
1174
|
|
|
} |
1175
|
|
|
$sharesJson = json_encode($shares); |
1176
|
|
|
$entry->setSocialShares($sharesJson); |
1177
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1178
|
|
|
|
1179
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
1180
|
|
|
'user' => $user, |
1181
|
|
|
'game' => $game, |
1182
|
|
|
'secretKey' => $secretKey, |
1183
|
|
|
'topic' => $topic, |
1184
|
|
|
'entry' => $entry |
1185
|
|
|
)); |
1186
|
|
|
|
1187
|
|
|
return true; |
1188
|
|
|
} |
1189
|
|
|
|
1190
|
|
View Code Duplication |
public function postGoogle($secretKey, $game, $user, $entry) |
|
|
|
|
1191
|
|
|
{ |
1192
|
|
|
$topic = $game->getTitle(); |
1193
|
|
|
|
1194
|
|
|
$shares = json_decode($entry->getSocialShares(), true); |
1195
|
|
|
if (!isset($shares['fbrequest'])) { |
1196
|
|
|
$shares['google'] = 1; |
1197
|
|
|
} else { |
1198
|
|
|
$shares['google'] += 1; |
1199
|
|
|
} |
1200
|
|
|
$sharesJson = json_encode($shares); |
1201
|
|
|
$entry->setSocialShares($sharesJson); |
1202
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1203
|
|
|
|
1204
|
|
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
1205
|
|
|
'user' => $user, |
1206
|
|
|
'game' => $game, |
1207
|
|
|
'secretKey' => $secretKey, |
1208
|
|
|
'topic' => $topic, |
1209
|
|
|
'entry' => $entry |
1210
|
|
|
)); |
1211
|
|
|
|
1212
|
|
|
return true; |
1213
|
|
|
} |
1214
|
|
|
|
1215
|
|
|
/** |
1216
|
|
|
* Is it possible to trigger a bonus entry ? |
1217
|
|
|
* |
1218
|
|
|
* @param unknown_type $game |
1219
|
|
|
* @param unknown_type $user |
1220
|
|
|
*/ |
1221
|
|
|
public function allowBonus($game, $user) |
1222
|
|
|
{ |
1223
|
|
|
if (! $game->getPlayBonus() || $game->getPlayBonus() == 'none') { |
1224
|
|
|
return false; |
1225
|
|
|
} elseif ($game->getPlayBonus() == 'one') { |
1226
|
|
|
if ($this->getEntryMapper()->findOneBy(array( |
1227
|
|
|
'game' => $game, |
1228
|
|
|
'user' => $user, |
1229
|
|
|
'bonus' => 1 |
1230
|
|
|
))) { |
1231
|
|
|
return false; |
1232
|
|
|
} else { |
1233
|
|
|
return true; |
1234
|
|
|
} |
1235
|
|
|
} elseif ($game->getPlayBonus() == 'per_entry') { |
1236
|
|
|
return $this->getEntryMapper()->checkBonusEntry($game, $user); |
1237
|
|
|
} |
1238
|
|
|
|
1239
|
|
|
return false; |
1240
|
|
|
} |
1241
|
|
|
|
1242
|
|
|
public function addAnotherEntry($game, $user, $winner = 0) |
1243
|
|
|
{ |
1244
|
|
|
$entry = new Entry(); |
1245
|
|
|
$entry->setGame($game); |
1246
|
|
|
$entry->setUser($user); |
1247
|
|
|
$entry->setPoints(0); |
1248
|
|
|
$entry->setIp($this->getIp()); |
1249
|
|
|
$entry->setActive(0); |
1250
|
|
|
$entry->setBonus(1); |
1251
|
|
|
$entry->setWinner($winner); |
1252
|
|
|
$entry = $this->getEntryMapper()->insert($entry); |
1253
|
|
|
|
1254
|
|
|
return $entry; |
1255
|
|
|
} |
1256
|
|
|
|
1257
|
|
|
/** |
1258
|
|
|
* This bonus entry doesn't give points nor badges |
1259
|
|
|
* It's just there to increase the chances during the Draw |
1260
|
|
|
* Old Name playBonus |
1261
|
|
|
* |
1262
|
|
|
* @param PlaygroundGame\Entity\Game $game |
1263
|
|
|
* @param unknown $user |
1264
|
|
|
* @return boolean unknown |
1265
|
|
|
*/ |
1266
|
|
|
public function addAnotherChance($game, $user, $winner = 0) |
1267
|
|
|
{ |
1268
|
|
|
if ($this->allowBonus($game, $user)) { |
1269
|
|
|
$this->addAnotherEntry($game, $user, $winner); |
1270
|
|
|
|
1271
|
|
|
return true; |
1272
|
|
|
} |
1273
|
|
|
|
1274
|
|
|
return false; |
1275
|
|
|
} |
1276
|
|
|
|
1277
|
|
|
/** |
1278
|
|
|
* This bonus entry doesn't give points nor badges but can play again |
1279
|
|
|
* |
1280
|
|
|
* @param PlaygroundGame\Entity\Game $game |
1281
|
|
|
* @param user $user |
1282
|
|
|
* @return boolean unknown |
1283
|
|
|
*/ |
1284
|
|
|
public function playAgain($game, $user, $winner = 0) |
1285
|
|
|
{ |
1286
|
|
|
if ($this->allowBonus($game, $user)) { |
1287
|
|
|
$entry = $this->addAnotherEntry($game, $user, $winner); |
1288
|
|
|
$entry->setActive(1); |
1289
|
|
|
$entry = $this->getEntryMapper()->update($entry); |
1290
|
|
|
if ($entry->getActive() == 1) { |
1291
|
|
|
return true; |
1292
|
|
|
} |
1293
|
|
|
} |
1294
|
|
|
|
1295
|
|
|
return false; |
1296
|
|
|
} |
1297
|
|
|
|
1298
|
|
|
/** |
1299
|
|
|
* @param string $path |
1300
|
|
|
*/ |
1301
|
|
|
public function uploadFile($path, $file) |
1302
|
|
|
{ |
1303
|
|
|
$err = $file["error"]; |
1304
|
|
|
$message = ''; |
1305
|
|
|
if ($err > 0) { |
1306
|
|
|
switch ($err) { |
1307
|
|
|
case '1': |
1308
|
|
|
$message .= 'Max file size exceeded. (php.ini)'; |
1309
|
|
|
break; |
1310
|
|
|
case '2': |
1311
|
|
|
$message .= 'Max file size exceeded.'; |
1312
|
|
|
break; |
1313
|
|
|
case '3': |
1314
|
|
|
$message .= 'File upload was only partial.'; |
1315
|
|
|
break; |
1316
|
|
|
case '4': |
1317
|
|
|
$message .= 'No file was attached.'; |
1318
|
|
|
break; |
1319
|
|
|
case '7': |
1320
|
|
|
$message .= 'File permission denied.'; |
1321
|
|
|
break; |
1322
|
|
|
default: |
1323
|
|
|
$message .= 'Unexpected error occurs.'; |
1324
|
|
|
} |
1325
|
|
|
|
1326
|
|
|
return $err; |
1327
|
|
|
} else { |
1328
|
|
|
$fileNewname = $this->fileNewname($path, $file['name'], true); |
1329
|
|
|
|
1330
|
|
|
if (isset($file["base64"])) { |
1331
|
|
|
list(, $img) = explode(',', $file["base64"]); |
1332
|
|
|
$img = str_replace(' ', '+', $img); |
1333
|
|
|
$im = base64_decode($img); |
1334
|
|
|
if ($im !== false) { |
1335
|
|
|
// getimagesizefromstring |
1336
|
|
|
file_put_contents($path . $fileNewname, $im); |
1337
|
|
|
} else { |
1338
|
|
|
return 1; |
1339
|
|
|
} |
1340
|
|
|
|
1341
|
|
|
return $fileNewname; |
1342
|
|
|
} else { |
1343
|
|
|
$adapter = new \Zend\File\Transfer\Adapter\Http(); |
1344
|
|
|
// 1Mo |
1345
|
|
|
$size = new Size(array( |
1346
|
|
|
'max' => 1024000 |
1347
|
|
|
)); |
1348
|
|
|
$is_image = new IsImage('jpeg,png,gif,jpg'); |
1349
|
|
|
$adapter->setValidators(array( |
1350
|
|
|
$size, |
1351
|
|
|
$is_image |
1352
|
|
|
), $fileNewname); |
1353
|
|
|
|
1354
|
|
|
if (! $adapter->isValid()) { |
1355
|
|
|
return false; |
1356
|
|
|
} |
1357
|
|
|
|
1358
|
|
|
@move_uploaded_file($file["tmp_name"], $path . $fileNewname); |
|
|
|
|
1359
|
|
|
} |
1360
|
|
|
|
1361
|
|
|
|
1362
|
|
|
if (class_exists("Imagick")) { |
1363
|
|
|
$ext = pathinfo($fileNewname, PATHINFO_EXTENSION); |
1364
|
|
|
$img = new \Imagick($path . $fileNewname); |
1365
|
|
|
$img->cropThumbnailImage(100, 100); |
1366
|
|
|
$img->setImageCompression(\Imagick::COMPRESSION_JPEG); |
1367
|
|
|
$img->setImageCompressionQuality(75); |
1368
|
|
|
// Strip out unneeded meta data |
1369
|
|
|
$img->stripImage(); |
1370
|
|
|
$img->writeImage($path . str_replace('.'.$ext, '-thumbnail.'.$ext, $fileNewname)); |
1371
|
|
|
ErrorHandler::stop(true); |
1372
|
|
|
} |
1373
|
|
|
} |
1374
|
|
|
|
1375
|
|
|
return $fileNewname; |
1376
|
|
|
} |
1377
|
|
|
|
1378
|
|
|
/** |
1379
|
|
|
* @param string $path |
1380
|
|
|
*/ |
1381
|
|
|
public function fileNewname($path, $filename, $generate = false) |
1382
|
|
|
{ |
1383
|
|
|
$sanitize = new Sanitize(); |
1384
|
|
|
$name = $sanitize->filter($filename); |
1385
|
|
|
$newpath = $path . $name; |
1386
|
|
|
|
1387
|
|
|
if ($generate) { |
1388
|
|
|
if (file_exists($newpath)) { |
1389
|
|
|
$filename = pathinfo($name, PATHINFO_FILENAME); |
1390
|
|
|
$ext = pathinfo($name, PATHINFO_EXTENSION); |
1391
|
|
|
|
1392
|
|
|
$name = $filename . '_' . rand(0, 99) . '.' . $ext; |
1393
|
|
|
} |
1394
|
|
|
} |
1395
|
|
|
|
1396
|
|
|
unset($sanitize); |
1397
|
|
|
|
1398
|
|
|
return $name; |
1399
|
|
|
} |
1400
|
|
|
|
1401
|
|
|
/** |
1402
|
|
|
* This function returns the list of games, order by $type |
1403
|
|
|
*/ |
1404
|
|
|
public function getQueryGamesOrderBy($type = 'createdAt', $order = 'DESC') |
1405
|
|
|
{ |
1406
|
|
|
$em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
1407
|
|
|
$today = new \DateTime("now"); |
1408
|
|
|
$today = $today->format('Y-m-d H:i:s'); |
1409
|
|
|
|
1410
|
|
|
$onlineGames = '( |
1411
|
|
|
( |
1412
|
|
|
CASE WHEN ( |
1413
|
|
|
g.active = 1 |
1414
|
|
|
AND g.broadcastPlatform = 1 |
1415
|
|
|
AND (g.startDate <= :date OR g.startDate IS NULL) |
1416
|
|
|
AND (g.closeDate >= :date OR g.closeDate IS NULL) |
1417
|
|
|
) THEN 1 ELSE 0 END |
1418
|
|
|
) + |
1419
|
|
|
( |
1420
|
|
|
CASE WHEN ( |
1421
|
|
|
g.active = 0 |
1422
|
|
|
AND (g.broadcastPlatform = 0 OR g.broadcastPlatform IS NULL) |
1423
|
|
|
AND g.startDate > :date |
1424
|
|
|
AND g.closeDate < :date |
1425
|
|
|
) THEN 1 ELSE 0 END |
1426
|
|
|
) |
1427
|
|
|
)'; |
1428
|
|
|
|
1429
|
|
|
$qb = $em->createQueryBuilder(); |
1430
|
|
|
$qb->select('g')->from('PlaygroundGame\Entity\Game', 'g'); |
1431
|
|
|
|
1432
|
|
|
switch ($type) { |
1433
|
|
|
case 'startDate': |
1434
|
|
|
$qb->orderBy('g.startDate', $order); |
1435
|
|
|
break; |
1436
|
|
|
case 'activeGames': |
1437
|
|
|
$qb->orderBy('g.active', $order); |
1438
|
|
|
break; |
1439
|
|
|
case 'onlineGames': |
1440
|
|
|
$qb->orderBy($onlineGames, $order); |
1441
|
|
|
$qb->setParameter('date', $today); |
1442
|
|
|
break; |
1443
|
|
|
case 'createdAt': |
1444
|
|
|
$qb->orderBy('g.createdAt', $order); |
1445
|
|
|
break; |
1446
|
|
|
} |
1447
|
|
|
|
1448
|
|
|
$query = $qb->getQuery(); |
1449
|
|
|
|
1450
|
|
|
return $query; |
1451
|
|
|
} |
1452
|
|
|
|
1453
|
|
|
public function draw($game) |
1454
|
|
|
{ |
1455
|
|
|
$total = $game->getWinners(); |
1456
|
|
|
|
1457
|
|
|
// I Have to know what is the User Class used |
1458
|
|
|
$zfcUserOptions = $this->getServiceManager()->get('zfcuser_module_options'); |
1459
|
|
|
$userClass = $zfcUserOptions->getUserEntityClass(); |
1460
|
|
|
|
1461
|
|
|
$result = $this->getEntryMapper()->draw($game, $userClass, $total); |
1462
|
|
|
|
1463
|
|
|
foreach ($result as $e) { |
1464
|
|
|
$e->setWinner(1); |
1465
|
|
|
$e = $this->getEntryMapper()->update($e); |
1466
|
|
|
$this->getEventManager()->trigger('win_lottery.post', $this, array( |
1467
|
|
|
'user' => $e->getUser(), |
1468
|
|
|
'game' => $game, |
1469
|
|
|
'entry' => $e |
1470
|
|
|
)); |
1471
|
|
|
} |
1472
|
|
|
|
1473
|
|
|
return $result; |
1474
|
|
|
} |
1475
|
|
|
|
1476
|
|
|
/** |
1477
|
|
|
* getGameMapper |
1478
|
|
|
* |
1479
|
|
|
* @return GameMapperInterface |
1480
|
|
|
*/ |
1481
|
|
View Code Duplication |
public function getGameMapper() |
|
|
|
|
1482
|
|
|
{ |
1483
|
|
|
if (null === $this->gameMapper) { |
1484
|
|
|
$this->gameMapper = $this->getServiceManager()->get('playgroundgame_game_mapper'); |
|
|
|
|
1485
|
|
|
} |
1486
|
|
|
|
1487
|
|
|
return $this->gameMapper; |
|
|
|
|
1488
|
|
|
} |
1489
|
|
|
|
1490
|
|
|
/** |
1491
|
|
|
* setGameMapper |
1492
|
|
|
* |
1493
|
|
|
* @param GameMapperInterface $gameMapper |
1494
|
|
|
* @return Game |
1495
|
|
|
*/ |
1496
|
|
|
public function setGameMapper(GameMapperInterface $gameMapper) |
1497
|
|
|
{ |
1498
|
|
|
$this->gameMapper = $gameMapper; |
1499
|
|
|
|
1500
|
|
|
return $this; |
1501
|
|
|
} |
1502
|
|
|
|
1503
|
|
|
/** |
1504
|
|
|
* getEntryMapper |
1505
|
|
|
* |
1506
|
|
|
* @return EntryMapperInterface |
1507
|
|
|
*/ |
1508
|
|
|
public function getEntryMapper() |
1509
|
|
|
{ |
1510
|
|
|
if (null === $this->entryMapper) { |
1511
|
|
|
$this->entryMapper = $this->getServiceManager()->get('playgroundgame_entry_mapper'); |
|
|
|
|
1512
|
|
|
} |
1513
|
|
|
|
1514
|
|
|
return $this->entryMapper; |
|
|
|
|
1515
|
|
|
} |
1516
|
|
|
|
1517
|
|
|
/** |
1518
|
|
|
* setEntryMapper |
1519
|
|
|
* |
1520
|
|
|
* @param EntryMapperInterface $entryMapper |
1521
|
|
|
* @return Game |
1522
|
|
|
*/ |
1523
|
|
|
public function setEntryMapper($entryMapper) |
1524
|
|
|
{ |
1525
|
|
|
$this->entryMapper = $entryMapper; |
1526
|
|
|
|
1527
|
|
|
return $this; |
1528
|
|
|
} |
1529
|
|
|
|
1530
|
|
|
public function setOptions(ModuleOptions $options) |
1531
|
|
|
{ |
1532
|
|
|
$this->options = $options; |
|
|
|
|
1533
|
|
|
|
1534
|
|
|
return $this; |
1535
|
|
|
} |
1536
|
|
|
|
1537
|
|
|
public function getOptions() |
1538
|
|
|
{ |
1539
|
|
|
if (! $this->options instanceof ModuleOptions) { |
1540
|
|
|
$this->setOptions($this->getServiceManager() |
|
|
|
|
1541
|
|
|
->get('playgroundgame_module_options')); |
1542
|
|
|
} |
1543
|
|
|
|
1544
|
|
|
return $this->options; |
1545
|
|
|
} |
1546
|
|
|
|
1547
|
|
|
/** |
1548
|
|
|
* Retrieve service manager instance |
1549
|
|
|
* |
1550
|
|
|
* @return ServiceManager |
1551
|
|
|
*/ |
1552
|
|
|
public function getServiceManager() |
1553
|
|
|
{ |
1554
|
|
|
return $this->serviceManager; |
1555
|
|
|
} |
1556
|
|
|
|
1557
|
|
|
/** |
1558
|
|
|
* Set service manager instance |
1559
|
|
|
* |
1560
|
|
|
* @param ServiceManager $serviceManager |
1561
|
|
|
* @return Game |
1562
|
|
|
*/ |
1563
|
|
|
public function setServiceManager(ServiceManager $serviceManager) |
1564
|
|
|
{ |
1565
|
|
|
$this->serviceManager = $serviceManager; |
1566
|
|
|
|
1567
|
|
|
return $this; |
1568
|
|
|
} |
1569
|
|
|
|
1570
|
|
|
/** |
1571
|
|
|
* @param string $str |
1572
|
|
|
*/ |
1573
|
|
|
public function getExtension($str) |
1574
|
|
|
{ |
1575
|
|
|
$i = strrpos($str, '.'); |
1576
|
|
|
|
1577
|
|
|
$l = strlen($str) - $i; |
1578
|
|
|
$ext = substr($str, $i + 1, $l); |
1579
|
|
|
|
1580
|
|
|
return $ext; |
1581
|
|
|
} |
1582
|
|
|
|
1583
|
|
|
/** |
1584
|
|
|
* @param string $extension |
1585
|
|
|
*/ |
1586
|
|
|
public function getSrc($extension, $temp_path) |
1587
|
|
|
{ |
1588
|
|
|
$image_src = ''; |
1589
|
|
|
switch ($extension) { |
1590
|
|
|
case 'jpg': |
1591
|
|
|
$image_src = imagecreatefromjpeg($temp_path); |
1592
|
|
|
break; |
1593
|
|
|
case 'jpeg': |
1594
|
|
|
$image_src = imagecreatefromjpeg($temp_path); |
1595
|
|
|
break; |
1596
|
|
|
case 'png': |
1597
|
|
|
$image_src = imagecreatefrompng($temp_path); |
1598
|
|
|
break; |
1599
|
|
|
case 'gif': |
1600
|
|
|
$image_src = imagecreatefromgif($temp_path); |
1601
|
|
|
break; |
1602
|
|
|
} |
1603
|
|
|
|
1604
|
|
|
return $image_src; |
1605
|
|
|
} |
1606
|
|
|
|
1607
|
|
|
/** |
1608
|
|
|
* @param string $extension |
1609
|
|
|
* @param string $rep |
1610
|
|
|
* @param integer $mini_width |
1611
|
|
|
* @param integer $mini_height |
1612
|
|
|
*/ |
1613
|
|
|
public function resize($tmp_file, $extension, $rep, $src, $mini_width, $mini_height) |
|
|
|
|
1614
|
|
|
{ |
1615
|
|
|
list($src_width, $src_height) = getimagesize($tmp_file); |
1616
|
|
|
|
1617
|
|
|
$ratio_src = $src_width / $src_height; |
1618
|
|
|
$ratio_mini = $mini_width / $mini_height; |
1619
|
|
|
|
1620
|
|
|
if ($ratio_src >= $ratio_mini) { |
1621
|
|
|
$new_height_mini = $mini_height; |
1622
|
|
|
$new_width_mini = $src_width / ($src_height / $mini_height); |
1623
|
|
|
} else { |
1624
|
|
|
$new_width_mini = $mini_width; |
1625
|
|
|
$new_height_mini = $src_height / ($src_width / $mini_width); |
1626
|
|
|
} |
1627
|
|
|
|
1628
|
|
|
$new_image_mini = imagecreatetruecolor($mini_width, $mini_height); |
1629
|
|
|
|
1630
|
|
|
imagecopyresampled( |
1631
|
|
|
$new_image_mini, |
1632
|
|
|
$src, |
1633
|
|
|
0 - ($new_width_mini - $mini_width) / 2, |
1634
|
|
|
0 - ($new_height_mini - $mini_height) / 2, |
1635
|
|
|
0, |
1636
|
|
|
0, |
1637
|
|
|
$new_width_mini, |
1638
|
|
|
$new_height_mini, |
1639
|
|
|
$src_width, |
1640
|
|
|
$src_height |
1641
|
|
|
); |
1642
|
|
|
imagejpeg($new_image_mini, $rep); |
1643
|
|
|
|
1644
|
|
|
imagedestroy($new_image_mini); |
1645
|
|
|
} |
1646
|
|
|
|
1647
|
|
|
public function getGameEntity() |
1648
|
|
|
{ |
1649
|
|
|
return new \PlaygroundGame\Entity\Game(); |
1650
|
|
|
} |
1651
|
|
|
|
1652
|
|
|
/** |
1653
|
|
|
* @param string $resource |
1654
|
|
|
* @param string $privilege |
1655
|
|
|
*/ |
1656
|
|
|
public function isAllowed($resource, $privilege = null) |
1657
|
|
|
{ |
1658
|
|
|
$auth = $this->getServiceManager()->get('BjyAuthorize\Service\Authorize'); |
1659
|
|
|
|
1660
|
|
|
return $auth->isAllowed($resource, $privilege); |
1661
|
|
|
} |
1662
|
|
|
|
1663
|
|
|
public function getIp() |
1664
|
|
|
{ |
1665
|
|
|
$ipaddress = ''; |
|
|
|
|
1666
|
|
|
if (isset($_SERVER['HTTP_CLIENT_IP'])) { |
1667
|
|
|
$ipaddress = $_SERVER['HTTP_CLIENT_IP']; |
1668
|
|
|
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
1669
|
|
|
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; |
1670
|
|
|
} elseif (isset($_SERVER['HTTP_X_FORWARDED'])) { |
1671
|
|
|
$ipaddress = $_SERVER['HTTP_X_FORWARDED']; |
1672
|
|
|
} elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) { |
1673
|
|
|
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; |
1674
|
|
|
} elseif (isset($_SERVER['HTTP_FORWARDED'])) { |
1675
|
|
|
$ipaddress = $_SERVER['HTTP_FORWARDED']; |
1676
|
|
|
} elseif (isset($_SERVER['REMOTE_ADDR'])) { |
1677
|
|
|
$ipaddress = $_SERVER['REMOTE_ADDR']; |
1678
|
|
|
} else { |
1679
|
|
|
$ipaddress = 'UNKNOWN'; |
1680
|
|
|
} |
1681
|
|
|
|
1682
|
|
|
return $ipaddress; |
1683
|
|
|
} |
1684
|
|
|
|
1685
|
|
|
public function getAnonymousId() |
1686
|
|
|
{ |
1687
|
|
|
$anonymousId = ''; |
1688
|
|
|
if ($_COOKIE && array_key_exists('pg_anonymous', $_COOKIE)) { |
1689
|
|
|
$anonymousId = $_COOKIE['pg_anonymous']; |
1690
|
|
|
} |
1691
|
|
|
|
1692
|
|
|
return $anonymousId; |
1693
|
|
|
} |
1694
|
|
|
|
1695
|
|
|
/** |
1696
|
|
|
* |
1697
|
|
|
* |
1698
|
|
|
* This service is ready for all types of games |
1699
|
|
|
* |
1700
|
|
|
* @param array $data |
1701
|
|
|
* @return \PlaygroundGame\Entity\Game |
1702
|
|
|
*/ |
1703
|
|
View Code Duplication |
public function createForm(array $data, $game, $form = null) |
|
|
|
|
1704
|
|
|
{ |
1705
|
|
|
$title = ''; |
1706
|
|
|
$description = ''; |
1707
|
|
|
|
1708
|
|
|
if ($data['form_jsonified']) { |
1709
|
|
|
$jsonPV = json_decode($data['form_jsonified']); |
1710
|
|
|
foreach ($jsonPV as $element) { |
1711
|
|
|
if ($element->form_properties) { |
1712
|
|
|
$attributes = $element->form_properties[0]; |
1713
|
|
|
$title = $attributes->title; |
1714
|
|
|
$description = $attributes->description; |
1715
|
|
|
|
1716
|
|
|
break; |
1717
|
|
|
} |
1718
|
|
|
} |
1719
|
|
|
} |
1720
|
|
|
if (! $form) { |
1721
|
|
|
$form = new \PlaygroundGame\Entity\PlayerForm(); |
1722
|
|
|
} |
1723
|
|
|
$form->setGame($game); |
1724
|
|
|
$form->setTitle($title); |
1725
|
|
|
$form->setDescription($description); |
1726
|
|
|
$form->setForm($data['form_jsonified']); |
1727
|
|
|
$form->setFormTemplate($data['form_template']); |
1728
|
|
|
|
1729
|
|
|
$form = $this->getPlayerFormMapper()->insert($form); |
1730
|
|
|
|
1731
|
|
|
return $form; |
1732
|
|
|
} |
1733
|
|
|
|
1734
|
|
|
/** |
1735
|
|
|
* getCSV creates lines of CSV and returns it. |
1736
|
|
|
*/ |
1737
|
|
|
public function getCSV($array) |
1738
|
|
|
{ |
1739
|
|
|
ob_start(); // buffer the output ... |
1740
|
|
|
$out = fopen('php://output', 'w'); |
1741
|
|
|
fputcsv($out, array_keys($array[0]), ";"); |
1742
|
|
|
foreach ($array as $line) { |
1743
|
|
|
fputcsv($out, $line, ";"); |
1744
|
|
|
} |
1745
|
|
|
fclose($out); |
1746
|
|
|
return ob_get_clean(); // ... then return it as a string! |
1747
|
|
|
} |
1748
|
|
|
|
1749
|
|
|
public function getAttributes($attributes) |
1750
|
|
|
{ |
1751
|
|
|
$a = array(); |
1752
|
|
|
|
1753
|
|
|
$a['name'] = isset($attributes->name)? $attributes->name : ''; |
1754
|
|
|
$a['placeholder'] = isset($attributes->data->placeholder)? $attributes->data->placeholder : ''; |
1755
|
|
|
$a['label'] = isset($attributes->data->label)? $attributes->data->label : ''; |
1756
|
|
|
$a['required'] = (isset($attributes->data->required) && $attributes->data->required == 'true')? |
1757
|
|
|
true: |
1758
|
|
|
false; |
1759
|
|
|
$a['class'] = isset($attributes->data->class)? $attributes->data->class : ''; |
1760
|
|
|
$a['id'] = isset($attributes->data->id)? $attributes->data->id : ''; |
1761
|
|
|
$a['lengthMin'] = isset($attributes->data->length)? $attributes->data->length->min : ''; |
1762
|
|
|
$a['lengthMax'] = isset($attributes->data->length)? $attributes->data->length->max : ''; |
1763
|
|
|
$a['validator'] = isset($attributes->data->validator)? $attributes->data->validator : ''; |
1764
|
|
|
$a['innerData'] = isset($attributes->data->innerData)? $attributes->data->innerData : array(); |
1765
|
|
|
$a['dropdownValues']= isset($attributes->data->dropdownValues)? |
1766
|
|
|
$attributes->data->dropdownValues : |
1767
|
|
|
array(); |
1768
|
|
|
$a['filesizeMin'] = isset($attributes->data->filesize)? $attributes->data->filesize->min : 0; |
1769
|
|
|
$a['filesizeMax'] = isset($attributes->data->filesize)? $attributes->data->filesize->max : 10*1024*1024; |
1770
|
|
|
|
1771
|
|
|
return $a; |
1772
|
|
|
} |
1773
|
|
|
|
1774
|
|
|
/** |
1775
|
|
|
* @param \Zend\InputFilter\InputFilter $inputFilter |
1776
|
|
|
*/ |
1777
|
|
|
public function decorate($element, $attr, $inputFilter) |
1778
|
|
|
{ |
1779
|
|
|
$factory = new InputFactory(); |
1780
|
|
|
$element->setAttributes( |
1781
|
|
|
array( |
1782
|
|
|
'placeholder' => $attr['placeholder'], |
1783
|
|
|
'required' => $attr['required'], |
1784
|
|
|
'class' => $attr['class'], |
1785
|
|
|
'id' => $attr['id'] |
1786
|
|
|
) |
1787
|
|
|
); |
1788
|
|
|
|
1789
|
|
|
$options = array(); |
1790
|
|
|
$options['encoding'] = 'UTF-8'; |
1791
|
|
|
if ($attr['lengthMin'] && $attr['lengthMin'] > 0) { |
1792
|
|
|
$options['min'] = $attr['lengthMin']; |
1793
|
|
|
} |
1794
|
|
|
if ($attr['lengthMax'] && $attr['lengthMax'] > $attr['lengthMin']) { |
1795
|
|
|
$options['max'] = $attr['lengthMax']; |
1796
|
|
|
$element->setAttribute('maxlength', $attr['lengthMax']); |
1797
|
|
|
$options['messages'] = array( |
1798
|
|
|
\Zend\Validator\StringLength::TOO_LONG => sprintf( |
1799
|
|
|
$this->getServiceManager()->get('translator')->translate( |
1800
|
|
|
'This field contains more than %s characters', |
1801
|
|
|
'playgroundgame' |
1802
|
|
|
), |
1803
|
|
|
$attr['lengthMax'] |
1804
|
|
|
) |
1805
|
|
|
); |
1806
|
|
|
} |
1807
|
|
|
|
1808
|
|
|
$validators = array( |
1809
|
|
|
array( |
1810
|
|
|
'name' => 'StringLength', |
1811
|
|
|
'options' => $options, |
1812
|
|
|
), |
1813
|
|
|
); |
1814
|
|
|
if ($attr['validator']) { |
1815
|
|
|
$regex = "/.*\(([^)]*)\)/"; |
1816
|
|
|
preg_match($regex, $attr['validator'], $matches); |
1817
|
|
|
$valArray = array( |
1818
|
|
|
'name' => str_replace( |
1819
|
|
|
'('.$matches[1].')', |
1820
|
|
|
'', |
1821
|
|
|
$attr['validator'] |
1822
|
|
|
), |
1823
|
|
|
'options' => array($matches[1]) |
1824
|
|
|
); |
1825
|
|
|
$validators[] = $valArray; |
1826
|
|
|
} |
1827
|
|
|
|
1828
|
|
|
$inputFilter->add($factory->createInput(array( |
1829
|
|
|
'name' => $attr['name'], |
1830
|
|
|
'required' => $attr['required'], |
1831
|
|
|
'filters' => array( |
1832
|
|
|
array('name' => 'StripTags'), |
1833
|
|
|
array('name' => 'StringTrim'), |
1834
|
|
|
), |
1835
|
|
|
'validators' => $validators, |
1836
|
|
|
))); |
1837
|
|
|
|
1838
|
|
|
return $element; |
1839
|
|
|
} |
1840
|
|
|
/** |
1841
|
|
|
* Create a ZF2 Form from json data |
1842
|
|
|
* @return Form |
1843
|
|
|
*/ |
1844
|
|
|
public function createFormFromJson($jsonForm, $id = 'jsonForm') |
1845
|
|
|
{ |
1846
|
|
|
$formPV = json_decode($jsonForm); |
1847
|
|
|
|
1848
|
|
|
$form = new Form(); |
1849
|
|
|
$form->setAttribute('id', $id); |
1850
|
|
|
$form->setAttribute('enctype', 'multipart/form-data'); |
1851
|
|
|
|
1852
|
|
|
$inputFilter = new \Zend\InputFilter\InputFilter(); |
1853
|
|
|
$factory = new InputFactory(); |
1854
|
|
|
|
1855
|
|
|
foreach ($formPV as $element) { |
1856
|
|
View Code Duplication |
if (isset($element->line_text)) { |
|
|
|
|
1857
|
|
|
$attr = $this->getAttributes($element->line_text[0]); |
1858
|
|
|
$element = new Element\Text($attr['name']); |
1859
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
1860
|
|
|
$form->add($element); |
1861
|
|
|
} |
1862
|
|
View Code Duplication |
if (isset($element->line_password)) { |
|
|
|
|
1863
|
|
|
$attr = $this->getAttributes($element->line_password[0]); |
1864
|
|
|
$element = new Element\Password($attr['name']); |
1865
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
1866
|
|
|
$form->add($element); |
1867
|
|
|
} |
1868
|
|
View Code Duplication |
if (isset($element->line_hidden)) { |
|
|
|
|
1869
|
|
|
$attr = $this->getAttributes($element->line_hidden[0]); |
1870
|
|
|
$element = new Element\Hidden($attr['name']); |
1871
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
1872
|
|
|
$form->add($element); |
1873
|
|
|
} |
1874
|
|
View Code Duplication |
if (isset($element->line_email)) { |
|
|
|
|
1875
|
|
|
$attr = $this->getAttributes($element->line_email[0]); |
1876
|
|
|
$element = new Element\Email($attr['name']); |
1877
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
1878
|
|
|
$form->add($element); |
1879
|
|
|
} |
1880
|
|
View Code Duplication |
if (isset($element->line_radio)) { |
|
|
|
|
1881
|
|
|
$attr = $this->getAttributes($element->line_radio[0]); |
1882
|
|
|
$element = new Element\Radio($attr['name']); |
1883
|
|
|
|
1884
|
|
|
$element->setLabel($attr['label']); |
1885
|
|
|
$element->setAttributes( |
1886
|
|
|
array( |
1887
|
|
|
'name' => $attr['name'], |
1888
|
|
|
'required' => $attr['required'], |
1889
|
|
|
'allowEmpty'=> !$attr['required'], |
1890
|
|
|
'class' => $attr['class'], |
1891
|
|
|
'id' => $attr['id'] |
1892
|
|
|
) |
1893
|
|
|
); |
1894
|
|
|
$values = array(); |
1895
|
|
|
foreach ($attr['innerData'] as $value) { |
1896
|
|
|
$values[] = $value->label; |
1897
|
|
|
} |
1898
|
|
|
$element->setValueOptions($values); |
1899
|
|
|
|
1900
|
|
|
$options = array(); |
1901
|
|
|
$options['encoding'] = 'UTF-8'; |
1902
|
|
|
$options['disable_inarray_validator'] = true; |
1903
|
|
|
|
1904
|
|
|
$element->setOptions($options); |
1905
|
|
|
|
1906
|
|
|
$form->add($element); |
1907
|
|
|
|
1908
|
|
|
$inputFilter->add($factory->createInput(array( |
1909
|
|
|
'name' => $attr['name'], |
1910
|
|
|
'required' => $attr['required'], |
1911
|
|
|
'allowEmpty' => !$attr['required'], |
1912
|
|
|
))); |
1913
|
|
|
} |
1914
|
|
View Code Duplication |
if (isset($element->line_checkbox)) { |
|
|
|
|
1915
|
|
|
$attr = $this->getAttributes($element->line_checkbox[0]); |
1916
|
|
|
$element = new Element\MultiCheckbox($attr['name']); |
1917
|
|
|
|
1918
|
|
|
$element->setLabel($attr['label']); |
1919
|
|
|
$element->setAttributes( |
1920
|
|
|
array( |
1921
|
|
|
'name' => $attr['name'], |
1922
|
|
|
'required' => $attr['required'], |
1923
|
|
|
'allowEmpty'=> !$attr['required'], |
1924
|
|
|
'class' => $attr['class'], |
1925
|
|
|
'id' => $attr['id'] |
1926
|
|
|
) |
1927
|
|
|
); |
1928
|
|
|
|
1929
|
|
|
$values = array(); |
1930
|
|
|
foreach ($attr['innerData'] as $value) { |
1931
|
|
|
$values[] = $value->label; |
1932
|
|
|
} |
1933
|
|
|
$element->setValueOptions($values); |
1934
|
|
|
$form->add($element); |
1935
|
|
|
|
1936
|
|
|
$options = array(); |
1937
|
|
|
$options['encoding'] = 'UTF-8'; |
1938
|
|
|
$options['disable_inarray_validator'] = true; |
1939
|
|
|
|
1940
|
|
|
$element->setOptions($options); |
1941
|
|
|
|
1942
|
|
|
$inputFilter->add($factory->createInput(array( |
1943
|
|
|
'name' => $attr['name'], |
1944
|
|
|
'required' => $attr['required'], |
1945
|
|
|
'allowEmpty'=> !$attr['required'], |
1946
|
|
|
))); |
1947
|
|
|
} |
1948
|
|
View Code Duplication |
if (isset($element->line_dropdown)) { |
|
|
|
|
1949
|
|
|
$attr = $this->getAttributes($element->line_dropdown[0]); |
1950
|
|
|
$element = new Element\Select($attr['name']); |
1951
|
|
|
|
1952
|
|
|
$element->setLabel($attr['label']); |
1953
|
|
|
$element->setAttributes( |
1954
|
|
|
array( |
1955
|
|
|
'name' => $attr['name'], |
1956
|
|
|
'required' => $attr['required'], |
1957
|
|
|
'allowEmpty'=> !$attr['required'], |
1958
|
|
|
'class' => $attr['class'], |
1959
|
|
|
'id' => $attr['id'] |
1960
|
|
|
) |
1961
|
|
|
); |
1962
|
|
|
$values = array(); |
1963
|
|
|
foreach ($attr['dropdownValues'] as $value) { |
1964
|
|
|
$values[] = $value->dropdown_label; |
1965
|
|
|
} |
1966
|
|
|
$element->setValueOptions($values); |
1967
|
|
|
$form->add($element); |
1968
|
|
|
|
1969
|
|
|
$options = array(); |
1970
|
|
|
$options['encoding'] = 'UTF-8'; |
1971
|
|
|
$options['disable_inarray_validator'] = true; |
1972
|
|
|
|
1973
|
|
|
$element->setOptions($options); |
1974
|
|
|
|
1975
|
|
|
$inputFilter->add($factory->createInput(array( |
1976
|
|
|
'name' => $attr['name'], |
1977
|
|
|
'required' => $attr['required'], |
1978
|
|
|
'allowEmpty' => !$attr['required'], |
1979
|
|
|
))); |
1980
|
|
|
} |
1981
|
|
View Code Duplication |
if (isset($element->line_paragraph)) { |
|
|
|
|
1982
|
|
|
$attr = $this->getAttributes($element->line_paragraph[0]); |
1983
|
|
|
$element = new Element\Textarea($attr['name']); |
1984
|
|
|
$element = $this->decorate($element, $attr, $inputFilter); |
1985
|
|
|
$form->add($element); |
1986
|
|
|
} |
1987
|
|
|
if (isset($element->line_upload)) { |
1988
|
|
|
$attr = $this->getAttributes($element->line_upload[0]); |
1989
|
|
|
$element = new Element\File($attr['name']); |
1990
|
|
|
|
1991
|
|
|
$element->setLabel($attr['label']); |
1992
|
|
|
$element->setAttributes( |
1993
|
|
|
array( |
1994
|
|
|
'name' => $attr['name'], |
1995
|
|
|
'required' => $attr['required'], |
1996
|
|
|
'class' => $attr['class'], |
1997
|
|
|
'id' => $attr['id'] |
1998
|
|
|
) |
1999
|
|
|
); |
2000
|
|
|
$form->add($element); |
2001
|
|
|
|
2002
|
|
|
$inputFilter->add($factory->createInput(array( |
2003
|
|
|
'name' => $attr['name'], |
2004
|
|
|
'required' => $attr['required'], |
2005
|
|
|
'validators' => array( |
2006
|
|
|
array( |
2007
|
|
|
'name' => '\Zend\Validator\File\Size', |
2008
|
|
|
'options' => array('min' => $attr['filesizeMin'], 'max' => $attr['filesizeMax']) |
2009
|
|
|
), |
2010
|
|
|
array( |
2011
|
|
|
'name' => '\Zend\Validator\File\Extension', |
2012
|
|
|
'options' => array( |
2013
|
|
|
'png,PNG,jpg,JPG,jpeg,JPEG,gif,GIF', |
2014
|
|
|
'messages' => array( |
2015
|
|
|
\Zend\Validator\File\Extension::FALSE_EXTENSION =>'Veuillez télécharger une image' |
2016
|
|
|
) |
2017
|
|
|
) |
2018
|
|
|
), |
2019
|
|
|
), |
2020
|
|
|
))); |
2021
|
|
|
} |
2022
|
|
|
} |
2023
|
|
|
|
2024
|
|
|
$form->setInputFilter($inputFilter); |
2025
|
|
|
|
2026
|
|
|
return $form; |
2027
|
|
|
} |
2028
|
|
|
|
2029
|
|
|
/** |
2030
|
|
|
* Send mail for winner and/or loser |
2031
|
|
|
* @param \PlaygroundGame\Entity\Game $game |
2032
|
|
|
* @param \PlaygroundUser\Entity\User $user |
2033
|
|
|
* @param \PlaygroundGame\Entity\Entry $lastEntry |
2034
|
|
|
* @param \PlaygroundGame\Entity\Prize $prize |
2035
|
|
|
*/ |
2036
|
|
|
public function sendMail($game, $user, $lastEntry, $prize = null) |
2037
|
|
|
{ |
2038
|
|
View Code Duplication |
if (($user || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) && |
|
|
|
|
2039
|
|
|
$game->getMailWinner() && |
2040
|
|
|
$lastEntry->getWinner() |
2041
|
|
|
) { |
2042
|
|
|
$this->sendResultMail($game, $user, $lastEntry, 'winner', $prize); |
2043
|
|
|
} |
2044
|
|
|
|
2045
|
|
View Code Duplication |
if (($user || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) && |
|
|
|
|
2046
|
|
|
$game->getMailLooser() && |
2047
|
|
|
!$lastEntry->getWinner() |
2048
|
|
|
) { |
2049
|
|
|
$this->sendResultMail($game, $user, $lastEntry, 'looser'); |
2050
|
|
|
} |
2051
|
|
|
} |
2052
|
|
|
|
2053
|
|
|
public function getPlayerFormMapper() |
2054
|
|
|
{ |
2055
|
|
|
if (null === $this->playerformMapper) { |
2056
|
|
|
$this->playerformMapper = $this->getServiceManager()->get('playgroundgame_playerform_mapper'); |
2057
|
|
|
} |
2058
|
|
|
|
2059
|
|
|
return $this->playerformMapper; |
2060
|
|
|
} |
2061
|
|
|
|
2062
|
|
|
public function setPlayerFormMapper($playerformMapper) |
2063
|
|
|
{ |
2064
|
|
|
$this->playerformMapper = $playerformMapper; |
2065
|
|
|
|
2066
|
|
|
return $this; |
2067
|
|
|
} |
2068
|
|
|
|
2069
|
|
|
public function getInvitationMapper() |
2070
|
|
|
{ |
2071
|
|
|
if (null === $this->invitationMapper) { |
2072
|
|
|
$this->invitationMapper = $this->getServiceManager()->get('playgroundgame_invitation_mapper'); |
2073
|
|
|
} |
2074
|
|
|
|
2075
|
|
|
return $this->invitationMapper; |
2076
|
|
|
} |
2077
|
|
|
|
2078
|
|
|
public function setInvitationMapper($invitationMapper) |
2079
|
|
|
{ |
2080
|
|
|
$this->invitationMapper = $invitationMapper; |
2081
|
|
|
|
2082
|
|
|
return $this; |
2083
|
|
|
} |
2084
|
|
|
|
2085
|
|
|
/** |
2086
|
|
|
* getUserMapper |
2087
|
|
|
* |
2088
|
|
|
* @return UserMapperInterface |
2089
|
|
|
*/ |
2090
|
|
|
public function getUserMapper() |
2091
|
|
|
{ |
2092
|
|
|
if (null === $this->userMapper) { |
2093
|
|
|
$this->userMapper = $this->getServiceManager()->get('zfcuser_user_mapper'); |
2094
|
|
|
} |
2095
|
|
|
|
2096
|
|
|
return $this->userMapper; |
2097
|
|
|
} |
2098
|
|
|
} |
2099
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.