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\Controller\Admin\GameController; |
8
|
|
|
use PlaygroundGame\Entity\TradingCard; |
9
|
|
|
use PlaygroundGame\Entity\TradingCardModel; |
10
|
|
|
use PlaygroundGame\Service\Game as AdminGameService; |
11
|
|
|
use Zend\Paginator\Paginator; |
12
|
|
|
use Zend\View\Model\ViewModel; |
13
|
|
|
|
14
|
|
|
class TradingCardController extends GameController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \PlaygroundGame\Service\Game |
18
|
|
|
*/ |
19
|
|
|
protected $adminGameService; |
20
|
|
|
|
21
|
|
View Code Duplication |
public function createTradingcardAction() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$service = $this->getAdminGameService(); |
24
|
|
|
$viewModel = new ViewModel(); |
25
|
|
|
$viewModel->setTemplate('playground-game/trading-card/tradingcard'); |
26
|
|
|
|
27
|
|
|
$gameForm = new ViewModel(); |
28
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
29
|
|
|
|
30
|
|
|
$tradingcard = new TradingCard(); |
31
|
|
|
|
32
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_tradingcard_form'); |
33
|
|
|
$form->bind($tradingcard); |
34
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
35
|
|
|
$form->setAttribute( |
36
|
|
|
'action', |
37
|
|
|
$this->adminUrl()->fromRoute( |
|
|
|
|
38
|
|
|
'playgroundgame/create-tradingcard', |
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
|
|
|
$game = $service->createOrUpdate($data, $tradingcard, 'playgroundgame_tradingcard_form'); |
54
|
|
|
if ($game) { |
55
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game has been created'); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/list')); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $tradingcard)); |
61
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
62
|
|
|
|
63
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Create trading card')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function editTradingcardAction() |
67
|
|
|
{ |
68
|
|
|
$this->checkGame(); |
69
|
|
|
|
70
|
|
|
return $this->editGame( |
71
|
|
|
'playground-game/trading-card/tradingcard', |
72
|
|
|
'playgroundgame_tradingcard_form' |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function listModelAction() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$this->checkGame(); |
79
|
|
|
|
80
|
|
|
$adapter = new DoctrineAdapter( |
81
|
|
|
new LargeTablePaginator( |
82
|
|
|
$this->getAdminGameService()->getTradingCardModelMapper()->queryByGame($this->game) |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
$paginator = new Paginator($adapter); |
86
|
|
|
$paginator->setItemCountPerPage(25); |
87
|
|
|
$paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
88
|
|
|
|
89
|
|
|
return new ViewModel( |
90
|
|
|
array( |
91
|
|
|
'models' => $paginator, |
92
|
|
|
'gameId' => $this->game->getId(), |
93
|
|
|
'game' => $this->game, |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function addModelAction() |
99
|
|
|
{ |
100
|
|
|
$this->checkGame(); |
101
|
|
|
|
102
|
|
|
$viewModel = new ViewModel(); |
103
|
|
|
$viewModel->setTemplate('playground-game/trading-card/model'); |
104
|
|
|
|
105
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_tradingcardmodel_form'); |
106
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
107
|
|
|
|
108
|
|
|
// $form->get('availability')->setOptions(array( |
109
|
|
|
// 'format' => 'Y-m-d H:i:s' |
110
|
|
|
// )); |
111
|
|
|
|
112
|
|
|
$form->setAttribute( |
113
|
|
|
'action', |
114
|
|
|
$this->adminUrl()->fromRoute( |
|
|
|
|
115
|
|
|
'playgroundgame/tradingcard-model-add', |
116
|
|
|
array('gameId' => $this->game->getId()) |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
$form->setAttribute('method', 'post'); |
120
|
|
|
$form->get('trading_card_id')->setAttribute('value', $this->game->getId()); |
121
|
|
|
$model = new TradingCardModel(); |
122
|
|
|
$form->bind($model); |
123
|
|
|
|
124
|
|
View Code Duplication |
if ($this->getRequest()->isPost()) { |
|
|
|
|
125
|
|
|
$data = array_merge( |
126
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
127
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$model = $this->getAdminGameService()->updateModel($data, $model); |
131
|
|
|
if ($model) { |
132
|
|
|
// Redirect to list of games |
133
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been created'); |
|
|
|
|
134
|
|
|
return $this->redirect()->toUrl( |
135
|
|
|
$this->adminUrl()->fromRoute('playgroundgame/tradingcard-model-list', array('gameId' => $this->game->getId())) |
|
|
|
|
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
return $viewModel->setVariables( |
140
|
|
|
array( |
141
|
|
|
'form' => $form, |
142
|
|
|
'game' => $this->game, |
143
|
|
|
'model_id' => 0, |
144
|
|
|
'title' => 'Add model', |
145
|
|
|
) |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function editModelAction() |
150
|
|
|
{ |
151
|
|
|
$this->checkGame(); |
152
|
|
|
|
153
|
|
|
$viewModel = new ViewModel(); |
154
|
|
|
$viewModel->setTemplate('playground-game/trading-card/model'); |
155
|
|
|
$service = $this->getAdminGameService(); |
156
|
|
|
|
157
|
|
|
$modelId = $this->getEvent()->getRouteMatch()->getParam('modelId'); |
158
|
|
|
$model = $service->getTradingCardModelMapper()->findById($modelId); |
159
|
|
|
|
160
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_tradingcardmodel_form'); |
161
|
|
|
$form->remove('models_file'); |
162
|
|
|
|
163
|
|
|
$form->get('submit')->setAttribute('label', 'Edit'); |
164
|
|
|
$form->setAttribute('action', ''); |
165
|
|
|
|
166
|
|
|
$form->get('trading_card_id')->setAttribute('value', $this->game->getId()); |
167
|
|
|
|
168
|
|
|
$form->bind($model); |
169
|
|
|
|
170
|
|
View Code Duplication |
if ($this->getRequest()->isPost()) { |
|
|
|
|
171
|
|
|
$data = array_merge( |
172
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
173
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
174
|
|
|
); |
175
|
|
|
$model = $service->updateModel($data, $model); |
176
|
|
|
|
177
|
|
|
if ($model) { |
178
|
|
|
// Redirect to list of games |
179
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been edited'); |
|
|
|
|
180
|
|
|
return $this->redirect()->toUrl( |
181
|
|
|
$this->adminUrl()->fromRoute('playgroundgame/tradingcard-model-list', array('gameId' => $this->game->getId())) |
|
|
|
|
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
return $viewModel->setVariables( |
186
|
|
|
array( |
187
|
|
|
'form' => $form, |
188
|
|
|
'game' => $this->game, |
189
|
|
|
'model_id' => $modelId, |
190
|
|
|
'title' => 'Edit model', |
191
|
|
|
) |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* This function helps the admin to position UGC image on a card model |
197
|
|
|
*/ |
198
|
|
|
public function cooAction() |
199
|
|
|
{ |
200
|
|
|
// get models |
201
|
|
|
$sg = $this->getAdminGameService(); |
202
|
|
|
$this->checkGame(); |
203
|
|
|
|
204
|
|
|
$models = $sg->getTradingCardModelMapper()->findBy(array('game' => $this->game)); |
205
|
|
|
|
206
|
|
|
if ($this->getRequest()->isXmlHttpRequest() && $this->getRequest()->isPost()) { |
|
|
|
|
207
|
|
|
$position = json_decode($this->getRequest()->getPost()->get('position')); |
|
|
|
|
208
|
|
|
$i = 0; |
209
|
|
|
foreach ($models as $model) { |
210
|
|
|
$jsonData = json_decode($model->getJsonData()); |
211
|
|
|
if (isset($jsonData->coo)) { |
212
|
|
|
$jsonData->coo = $position[$i]; |
213
|
|
|
} else { |
214
|
|
|
$jsonData = array('coo' => $position[$i]); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$model->setJsonData(json_encode($jsonData)); |
218
|
|
|
$models = $sg->getTradingCardModelMapper()->update($model); |
219
|
|
|
++$i; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// update coo of Model |
223
|
|
|
$jsonModel = new \Zend\View\Model\JsonModel(); |
224
|
|
|
$jsonModel->setVariables(array( |
225
|
|
|
'success' => true, |
226
|
|
|
)); |
227
|
|
|
|
228
|
|
|
return $jsonModel; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// ajout des images au tableau |
232
|
|
|
$bgs = []; |
233
|
|
|
$coo = []; |
234
|
|
|
foreach ($models as $model) { |
235
|
|
|
$json = json_decode($model->getJsonData()); |
236
|
|
|
$modelCoo = (isset($json->coo))?$json->coo:''; |
237
|
|
|
$bgs[] = '/'.$model->getImage(); |
238
|
|
|
$coo[] = $modelCoo; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return array( |
242
|
|
|
'backgrounds' => $bgs, |
243
|
|
|
'face' => 'img-test/photo.png', |
244
|
|
|
'coo' => $coo, |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
View Code Duplication |
public function removeOccurrenceAction() |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$service = $this->getAdminGameService(); |
251
|
|
|
$modelId = $this->getEvent()->getRouteMatch()->getParam('modelId'); |
252
|
|
|
if (!$modelId) { |
253
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/list')); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
$model = $service->getTradingCardModelMapper()->findById($modelId); |
256
|
|
|
$tradingcardId = $model->getTradingCard()->getId(); |
257
|
|
|
|
258
|
|
|
if ($model->getActive()) { |
259
|
|
|
$service->getTradingCardModelMapper()->remove($model); |
260
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been deleted'); |
|
|
|
|
261
|
|
|
} else { |
262
|
|
|
$this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
|
|
|
|
263
|
|
|
'cards have already been created with this model' |
264
|
|
|
); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/tradingcard-model-list', array('gameId' => $tradingcardId))); |
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function getAdminGameService() |
271
|
|
|
{ |
272
|
|
|
if (!$this->adminGameService) { |
273
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_tradingcard_service'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $this->adminGameService; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function setAdminGameService(AdminGameService $adminGameService) |
280
|
|
|
{ |
281
|
|
|
$this->adminGameService = $adminGameService; |
282
|
|
|
|
283
|
|
|
return $this; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
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.