|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use PlaygroundGame\Service\Game as AdminGameService; |
|
6
|
|
|
use PlaygroundGame\Entity\Lottery; |
|
7
|
|
|
use PlaygroundGame\Controller\Admin\GameController; |
|
8
|
|
|
use Zend\View\Model\ViewModel; |
|
9
|
|
|
|
|
10
|
|
|
class LotteryController extends GameController |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var GameService |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $adminGameService; |
|
16
|
|
|
|
|
17
|
|
|
public function createLotteryAction() |
|
18
|
|
|
{ |
|
19
|
|
|
$service = $this->getAdminGameService(); |
|
20
|
|
|
$viewModel = new ViewModel(); |
|
21
|
|
|
$viewModel->setTemplate('playground-game/lottery/lottery'); |
|
22
|
|
|
|
|
23
|
|
|
$gameForm = new ViewModel(); |
|
24
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
|
25
|
|
|
|
|
26
|
|
|
$lottery = new Lottery(); |
|
27
|
|
|
|
|
28
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_lottery_form'); |
|
29
|
|
|
$form->bind($lottery); |
|
30
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
|
31
|
|
|
$form->setAttribute( |
|
32
|
|
|
'action', |
|
33
|
|
|
$this->url()->fromRoute( |
|
34
|
|
|
'admin/playgroundgame/create-lottery', |
|
35
|
|
|
array('gameId' => 0) |
|
36
|
|
|
) |
|
37
|
|
|
); |
|
38
|
|
|
$form->setAttribute('method', 'post'); |
|
39
|
|
|
|
|
40
|
|
|
$request = $this->getRequest(); |
|
41
|
|
|
if ($request->isPost()) { |
|
42
|
|
|
$data = array_replace_recursive( |
|
43
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
44
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
45
|
|
|
); |
|
46
|
|
|
if (empty($data['prizes'])) { |
|
47
|
|
|
$data['prizes'] = array(); |
|
48
|
|
|
} |
|
49
|
|
|
if (isset($data['drawDate']) && $data['drawDate']) { |
|
50
|
|
|
$data['drawDate'] = \DateTime::createFromFormat('d/m/Y', $data['drawDate']); |
|
51
|
|
|
} |
|
52
|
|
|
$game = $service->create($data, $lottery, 'playgroundgame_lottery_form'); |
|
53
|
|
|
if ($game) { |
|
54
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created'); |
|
55
|
|
|
|
|
56
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $lottery)); |
|
60
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
|
61
|
|
|
|
|
62
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Create lottery')); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function editLotteryAction() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->checkGame(); |
|
68
|
|
|
|
|
69
|
|
|
$viewModel = new ViewModel(); |
|
70
|
|
|
$viewModel->setTemplate('playground-game/lottery/lottery'); |
|
71
|
|
|
|
|
72
|
|
|
$gameForm = new ViewModel(); |
|
73
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
|
74
|
|
|
|
|
75
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_lottery_form'); |
|
76
|
|
|
$form->setAttribute( |
|
77
|
|
|
'action', |
|
78
|
|
|
$this->url()->fromRoute( |
|
79
|
|
|
'admin/playgroundgame/edit-lottery', |
|
80
|
|
|
array('gameId' => $this->game->getId()) |
|
81
|
|
|
) |
|
82
|
|
|
); |
|
83
|
|
|
$form->setAttribute('method', 'post'); |
|
84
|
|
|
if ($this->game->getFbAppId()) { |
|
85
|
|
|
$appIds = $form->get('fbAppId')->getOption('value_options'); |
|
86
|
|
|
$appIds[$this->game->getFbAppId()] = $this->game->getFbAppId(); |
|
87
|
|
|
$form->get('fbAppId')->setAttribute('options', $appIds); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$gameOptions = $this->getAdminGameService()->getOptions(); |
|
91
|
|
|
$gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $this->game->getId(). '.css'; |
|
92
|
|
|
if (is_file($gameStylesheet)) { |
|
93
|
|
|
$values = $form->get('stylesheet')->getValueOptions(); |
|
94
|
|
|
$values[$gameStylesheet] = 'Style personnalisé de ce jeu'; |
|
95
|
|
|
|
|
96
|
|
|
$form->get('stylesheet')->setAttribute('options', $values); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$form->bind($this->game); |
|
100
|
|
|
|
|
101
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
102
|
|
|
$data = array_replace_recursive( |
|
103
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
104
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
105
|
|
|
); |
|
106
|
|
|
if (empty($data['prizes'])) { |
|
107
|
|
|
$data['prizes'] = array(); |
|
108
|
|
|
} |
|
109
|
|
|
if (isset($data['drawDate']) && $data['drawDate']) { |
|
110
|
|
|
$data['drawDate'] = \DateTime::createFromFormat('d/m/Y', $data['drawDate']); |
|
111
|
|
|
} |
|
112
|
|
|
$result = $service->edit($data, $this->game, 'playgroundgame_lottery_form'); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
if ($result) { |
|
115
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $this->game)); |
|
120
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
|
121
|
|
|
|
|
122
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Edit lottery')); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getAdminGameService() |
|
126
|
|
|
{ |
|
127
|
|
|
if (!$this->adminGameService) { |
|
128
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_lottery_service'); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->adminGameService; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function setAdminGameService(AdminGameService $adminGameService) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->adminGameService = $adminGameService; |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: