Completed
Push — master ( 888359...e58691 )
by greg
03:08 queued 32s
created

TradingCardController::createTradingcardAction()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 44

Duplication

Lines 44
Ratio 100 %

Importance

Changes 0
Metric Value
dl 44
loc 44
rs 9.216
c 0
b 0
f 0
cc 4
nc 5
nop 0
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
    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
    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
        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($this->adminUrl()->fromRoute('playgroundgame/tradingcard-model-list', array('gameId' => $this->game->getId()));
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ';', expecting ',' or ')'
Loading history...
135
            }
136
        }
137
        return $viewModel->setVariables(
138
            array(
139
                'form'     => $form,
140
                'game'     => $this->game,
141
                'model_id' => 0,
142
                'title'    => 'Add model',
143
            )
144
        );
145
    }
146
147
    public function editModelAction()
148
    {
149
        $this->checkGame();
150
151
        $viewModel = new ViewModel();
152
        $viewModel->setTemplate('playground-game/trading-card/model');
153
        $service = $this->getAdminGameService();
154
155
        $modelId = $this->getEvent()->getRouteMatch()->getParam('modelId');
156
        $model   = $service->getTradingCardModelMapper()->findById($modelId);
157
158
        $form = $this->getServiceLocator()->get('playgroundgame_tradingcardmodel_form');
159
        $form->remove('models_file');
160
161
        $form->get('submit')->setAttribute('label', 'Edit');
162
        $form->setAttribute('action', '');
163
164
        $form->get('trading_card_id')->setAttribute('value', $this->game->getId());
165
166
        $form->bind($model);
167
168
        if ($this->getRequest()->isPost()) {
169
            $data = array_merge(
170
                $this->getRequest()->getPost()->toArray(),
171
                $this->getRequest()->getFiles()->toArray()
172
            );
173
            $model = $service->updateModel($data, $model);
174
175
            if ($model) {
176
                // Redirect to list of games
177
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been edited');
178
                return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/tradingcard-model-list', array('gameId' => $this->game->getId()));
179
            }
180
        }
181
        return $viewModel->setVariables(
182
            array(
183
                'form'     => $form,
184
                'game'     => $this->game,
185
                'model_id' => $modelId,
186
                'title'    => 'Edit model',
187
            )
188
        );
189
    }
190
191
    /**
192
     * This function helps the admin to position UGC image on a card model
193
     */
194
    public function cooAction()
195
    {
196
        // get models
197
        $sg = $this->getAdminGameService();
198
        $this->checkGame();
199
200
        $models = $sg->getTradingCardModelMapper()->findBy(array('game' => $this->game));
201
202
        if ($this->getRequest()->isXmlHttpRequest() && $this->getRequest()->isPost()) {
203
            $position = json_decode($this->getRequest()->getPost()->get('position'));
204
            $i        = 0;
205
            foreach ($models as $model) {
206
                $jsonData = json_decode($model->getJsonData());
207
                if (isset($jsonData->coo)) {
208
                    $jsonData->coo = $position[$i];
209
                } else {
210
                    $jsonData = array('coo' => $position[$i]);
211
                }
212
213
                $model->setJsonData(json_encode($jsonData));
214
                $models = $sg->getTradingCardModelMapper()->update($model);
215
                ++$i;
216
            }
217
218
            // update coo of Model
219
            $jsonModel = new \Zend\View\Model\JsonModel();
220
            $jsonModel->setVariables(array(
221
                    'success' => true,
222
                ));
223
224
            return $jsonModel;
225
        }
226
227
        // ajout des images au tableau
228
        $bgs = [];
229
        $coo = [];
230
        foreach ($models as $model) {
231
            $json     = json_decode($model->getJsonData());
232
            $modelCoo = (isset($json->coo))?$json->coo:'';
233
            $bgs[]    = '/'.$model->getImage();
234
            $coo[]    = $modelCoo;
235
        }
236
237
        return array(
238
            'backgrounds' => $bgs,
239
            'face'        => 'img-test/photo.png',
240
            'coo'         => $coo,
241
        );
242
    }
243
244
    public function removeOccurrenceAction()
245
    {
246
        $service = $this->getAdminGameService();
247
        $modelId = $this->getEvent()->getRouteMatch()->getParam('modelId');
248
        if (!$modelId) {
249
            return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/list'));
250
        }
251
        $model         = $service->getTradingCardModelMapper()->findById($modelId);
252
        $tradingcardId = $model->getTradingCard()->getId();
253
254
        if ($model->getActive()) {
255
            $service->getTradingCardModelMapper()->remove($model);
256
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been deleted');
257
        } else {
258
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
259
                'cards have already been created with this model'
260
            );
261
        }
262
263
        return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/tradingcard-model-list', array('gameId' => $tradingcardId)));
264
    }
265
266
    public function getAdminGameService()
267
    {
268
        if (!$this->adminGameService) {
269
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_tradingcard_service');
270
        }
271
272
        return $this->adminGameService;
273
    }
274
275
    public function setAdminGameService(AdminGameService $adminGameService)
276
    {
277
        $this->adminGameService = $adminGameService;
278
279
        return $this;
280
    }
281
}
282