|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter; |
|
6
|
|
|
use PlaygroundCore\ORM\Pagination\LargeTablePaginator; |
|
7
|
|
|
use PlaygroundGame\Service\Game as AdminGameService; |
|
8
|
|
|
use PlaygroundGame\Entity\Memory; |
|
9
|
|
|
use PlaygroundGame\Entity\MemoryCard; |
|
10
|
|
|
use PlaygroundGame\Controller\Admin\GameController; |
|
11
|
|
|
use Zend\View\Model\ViewModel; |
|
12
|
|
|
use Zend\Paginator\Paginator; |
|
13
|
|
|
|
|
14
|
|
|
class MemoryController extends GameController |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \PlaygroundGame\Service\Game |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $adminGameService; |
|
20
|
|
|
|
|
21
|
|
View Code Duplication |
public function createMemoryAction() |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
$service = $this->getAdminGameService(); |
|
24
|
|
|
$viewModel = new ViewModel(); |
|
25
|
|
|
$viewModel->setTemplate('playground-game/memory/memory'); |
|
26
|
|
|
|
|
27
|
|
|
$gameForm = new ViewModel(); |
|
28
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
|
29
|
|
|
|
|
30
|
|
|
$memory = new Memory(); |
|
31
|
|
|
|
|
32
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_memory_form'); |
|
33
|
|
|
$form->bind($memory); |
|
34
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
|
35
|
|
|
$form->setAttribute( |
|
36
|
|
|
'action', |
|
37
|
|
|
$this->adminUrl()->fromRoute( |
|
|
|
|
|
|
38
|
|
|
'playgroundgame/create-memory', |
|
39
|
|
|
array('gameId' => 0) |
|
40
|
|
|
) |
|
41
|
|
|
); |
|
42
|
|
|
$form->setAttribute('method', 'post'); |
|
43
|
|
|
|
|
44
|
|
|
$request = $this->getRequest(); |
|
45
|
|
|
if ($request->isPost()) { |
|
46
|
|
|
$data = array_replace_recursive( |
|
47
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
48
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
49
|
|
|
); |
|
50
|
|
|
if (empty($data['prizes'])) { |
|
51
|
|
|
$data['prizes'] = array(); |
|
52
|
|
|
} |
|
53
|
|
|
if (isset($data['drawDate']) && $data['drawDate']) { |
|
54
|
|
|
$data['drawDate'] = \DateTime::createFromFormat('d/m/Y', $data['drawDate']); |
|
55
|
|
|
} |
|
56
|
|
|
$game = $service->createOrUpdate($data, $memory, 'playgroundgame_memory_form'); |
|
57
|
|
|
if ($game) { |
|
58
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created'); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/list')); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $memory)); |
|
64
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
|
65
|
|
|
|
|
66
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Create memory')); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function editMemoryAction() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->checkGame(); |
|
72
|
|
|
|
|
73
|
|
|
return $this->editGame( |
|
74
|
|
|
'playground-game/memory/memory', |
|
75
|
|
|
'playgroundgame_memory_form' |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
View Code Duplication |
public function listCardAction() |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
$this->checkGame(); |
|
82
|
|
|
|
|
83
|
|
|
$adapter = new DoctrineAdapter( |
|
84
|
|
|
new LargeTablePaginator( |
|
85
|
|
|
$this->getAdminGameService()->getMemoryCardMapper()->queryByGame($this->game) |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
$paginator = new Paginator($adapter); |
|
89
|
|
|
$paginator->setItemCountPerPage(16); |
|
90
|
|
|
$paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
|
91
|
|
|
|
|
92
|
|
|
return new ViewModel( |
|
93
|
|
|
array( |
|
94
|
|
|
'cards' => $paginator, |
|
95
|
|
|
'gameId' => $this->game->getId(), |
|
96
|
|
|
'game' => $this->game, |
|
97
|
|
|
) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
View Code Duplication |
public function addCardAction() |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
$this->checkGame(); |
|
104
|
|
|
|
|
105
|
|
|
$viewModel = new ViewModel(); |
|
106
|
|
|
$viewModel->setTemplate('playground-game/memory/card'); |
|
107
|
|
|
|
|
108
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_memorycard_form'); |
|
109
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
|
110
|
|
|
|
|
111
|
|
|
$form->setAttribute( |
|
112
|
|
|
'action', |
|
113
|
|
|
$this->adminUrl()->fromRoute( |
|
|
|
|
|
|
114
|
|
|
'playgroundgame/memory-card-add', |
|
115
|
|
|
array('gameId' => $this->game->getId()) |
|
116
|
|
|
) |
|
117
|
|
|
); |
|
118
|
|
|
$form->setAttribute('method', 'post'); |
|
119
|
|
|
$form->get('memory_id')->setAttribute('value', $this->game->getId()); |
|
120
|
|
|
$card = new MemoryCard(); |
|
121
|
|
|
$form->bind($card); |
|
122
|
|
|
|
|
123
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
124
|
|
|
$data = array_merge( |
|
125
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
126
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$card = $this->getAdminGameService()->updateCard($data, $card); |
|
130
|
|
|
if ($card) { |
|
131
|
|
|
// Redirect to list of games |
|
132
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The card has been created'); |
|
|
|
|
|
|
133
|
|
|
return $this->redirect()->toUrl( |
|
134
|
|
|
$this->adminUrl()->fromRoute('playgroundgame/memory-card-list', array('gameId' => $this->game->getId())) |
|
|
|
|
|
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
return $viewModel->setVariables( |
|
139
|
|
|
array( |
|
140
|
|
|
'form' => $form, |
|
141
|
|
|
'game' => $this->game, |
|
142
|
|
|
'card_id' => 0, |
|
143
|
|
|
'title' => 'Add card', |
|
144
|
|
|
) |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
View Code Duplication |
public function editCardAction() |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
$this->checkGame(); |
|
151
|
|
|
|
|
152
|
|
|
$viewModel = new ViewModel(); |
|
153
|
|
|
$viewModel->setTemplate('playground-game/memory/card'); |
|
154
|
|
|
$service = $this->getAdminGameService(); |
|
155
|
|
|
|
|
156
|
|
|
$cardId = $this->getEvent()->getRouteMatch()->getParam('cardId'); |
|
157
|
|
|
$card = $service->getMemoryCardMapper()->findById($cardId); |
|
158
|
|
|
|
|
159
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_memorycard_form'); |
|
160
|
|
|
$form->remove('cards_file'); |
|
161
|
|
|
|
|
162
|
|
|
$form->get('submit')->setAttribute('label', 'Edit'); |
|
163
|
|
|
$form->setAttribute('action', ''); |
|
164
|
|
|
|
|
165
|
|
|
$form->get('memory_id')->setAttribute('value', $this->game->getId()); |
|
166
|
|
|
|
|
167
|
|
|
$form->bind($card); |
|
168
|
|
|
|
|
169
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
170
|
|
|
$data = array_merge( |
|
171
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
|
|
172
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
|
|
173
|
|
|
); |
|
174
|
|
|
$card = $service->updateCard($data, $card); |
|
175
|
|
|
|
|
176
|
|
|
if ($card) { |
|
177
|
|
|
// Redirect to list of games |
|
178
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The card has been edited'); |
|
|
|
|
|
|
179
|
|
|
return $this->redirect()->toUrl( |
|
180
|
|
|
$this->adminUrl()->fromRoute('playgroundgame/memory-card-list', array('gameId' => $this->game->getId())) |
|
|
|
|
|
|
181
|
|
|
); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
return $viewModel->setVariables( |
|
185
|
|
|
array( |
|
186
|
|
|
'form' => $form, |
|
187
|
|
|
'game' => $this->game, |
|
188
|
|
|
'card_id' => $cardId, |
|
189
|
|
|
'title' => 'Edit card', |
|
190
|
|
|
) |
|
191
|
|
|
); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
View Code Duplication |
public function removeCardAction() |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
|
|
$service = $this->getAdminGameService(); |
|
197
|
|
|
$cardId = $this->getEvent()->getRouteMatch()->getParam('cardId'); |
|
198
|
|
|
if (!$cardId) { |
|
199
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/list')); |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
$card = $service->getMemoryCardMapper()->findById($cardId); |
|
202
|
|
|
$memoryId = $card->getGame()->getId(); |
|
203
|
|
|
|
|
204
|
|
|
$service->getMemoryCardMapper()->remove($card); |
|
205
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The card has been deleted'); |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/memory-card-list', array('gameId' => $memoryId))); |
|
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function getAdminGameService() |
|
211
|
|
|
{ |
|
212
|
|
|
if (!$this->adminGameService) { |
|
213
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_memory_service'); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $this->adminGameService; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function setAdminGameService(AdminGameService $adminGameService) |
|
220
|
|
|
{ |
|
221
|
|
|
$this->adminGameService = $adminGameService; |
|
222
|
|
|
|
|
223
|
|
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
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.