1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use PlaygroundGame\Entity\Mission; |
6
|
|
|
use PlaygroundGame\Controller\Admin\GameController; |
7
|
|
|
use PlaygroundGame\Service\Game as AdminGameService; |
8
|
|
|
use Zend\View\Model\ViewModel; |
9
|
|
|
|
10
|
|
|
class MissionController extends GameController |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var GameService |
14
|
|
|
*/ |
15
|
|
|
protected $adminGameService; |
16
|
|
|
|
17
|
|
|
protected $mission; |
18
|
|
|
|
19
|
|
View Code Duplication |
public function createMissionAction() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$service = $this->getAdminGameService(); |
22
|
|
|
$viewModel = new ViewModel(); |
23
|
|
|
$viewModel->setTemplate('playground-game/game/mission'); |
24
|
|
|
|
25
|
|
|
$gameForm = new ViewModel(); |
26
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
27
|
|
|
|
28
|
|
|
$mission = new Mission(); |
29
|
|
|
|
30
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_mission_form'); |
31
|
|
|
$form->bind($mission); |
32
|
|
|
$form->get('submit')->setAttribute('label', 'Add'); |
33
|
|
|
$form->setAttribute( |
34
|
|
|
'action', |
35
|
|
|
$this->url()->fromRoute( |
36
|
|
|
'admin/playgroundgame/create-mission', |
37
|
|
|
array('gameId' => 0) |
38
|
|
|
) |
39
|
|
|
); |
40
|
|
|
$form->setAttribute('method', 'post'); |
41
|
|
|
|
42
|
|
|
$request = $this->getRequest(); |
43
|
|
|
if ($request->isPost()) { |
44
|
|
|
$data = array_replace_recursive( |
45
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
46
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
47
|
|
|
); |
48
|
|
|
if (empty($data['prizes'])) { |
49
|
|
|
$data['prizes'] = array(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$game = $service->createOrUpdate($data, $mission, 'playgroundgame_mission_form'); |
53
|
|
|
if ($game) { |
54
|
|
|
|
55
|
|
|
$this->flashMessenger()->setNamespace('mission')->addMessage('The game was created'); |
56
|
|
|
|
57
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $mission)); |
61
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
62
|
|
|
|
63
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Create mission', 'mission' => $mission)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function editMissionAction() |
67
|
|
|
{ |
68
|
|
|
$service = $this->getAdminGameService(); |
69
|
|
|
$gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
70
|
|
|
|
71
|
|
|
if (!$gameId) { |
72
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/createMission'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$game = $service->getGameMapper()->findById($gameId); |
76
|
|
|
|
77
|
|
|
$viewModel = new ViewModel(); |
78
|
|
|
$viewModel->setTemplate('playground-game/mission/mission'); |
79
|
|
|
|
80
|
|
|
$gameForm = new ViewModel(); |
81
|
|
|
$gameForm->setTemplate('playground-game/game/game-form'); |
82
|
|
|
|
83
|
|
|
$form = $this->getServiceLocator()->get('playgroundgame_mission_form'); |
84
|
|
|
$form->setAttribute( |
85
|
|
|
'action', |
86
|
|
|
$this->url()->fromRoute( |
87
|
|
|
'admin/playgroundgame/edit-mission', |
88
|
|
|
array('gameId' => $gameId) |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
$form->setAttribute('method', 'post'); |
92
|
|
|
if ($game->getFbAppId()) { |
93
|
|
|
$appIds = $form->get('fbAppId')->getOption('value_options'); |
94
|
|
|
$appIds[$game->getFbAppId()] = $game->getFbAppId(); |
95
|
|
|
$form->get('fbAppId')->setAttribute('options', $appIds); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$gameOptions = $this->getAdminGameService()->getOptions(); |
99
|
|
|
$gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $game->getId(). '.css'; |
100
|
|
View Code Duplication |
if (is_file($gameStylesheet)) { |
|
|
|
|
101
|
|
|
$values = $form->get('stylesheet')->getValueOptions(); |
102
|
|
|
$values[$gameStylesheet] = 'Style personnalisé de ce jeu'; |
103
|
|
|
|
104
|
|
|
$form->get('stylesheet')->setAttribute('options', $values); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$form->bind($game); |
108
|
|
|
|
109
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
110
|
|
|
$data = array_replace_recursive( |
111
|
|
|
$this->getRequest()->getPost()->toArray(), |
|
|
|
|
112
|
|
|
$this->getRequest()->getFiles()->toArray() |
|
|
|
|
113
|
|
|
); |
114
|
|
|
if (empty($data['prizes'])) { |
115
|
|
|
$data['prizes'] = array(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$result = $service->createOrUpdate($data, $game, 'playgroundgame_mission_form'); |
119
|
|
|
|
120
|
|
|
if ($result) { |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* The following work has to be done there because doctrine hydration with nested objects is a mess |
124
|
|
|
* I've been obliged to proceed this way... haven't found the bugfix. |
125
|
|
|
*/ |
126
|
|
|
/*$cmg = count($result->getMissionGames()); |
127
|
|
|
if( $cmg < count($data['missionGames']) ){ |
128
|
|
|
$i=0; |
129
|
|
|
foreach($data['missionGames'] as $m){ |
130
|
|
|
if( $i >= $cmg ){ |
131
|
|
|
$g = $service->getGameMapper()->findById($m['game']); |
132
|
|
|
$mg = new \PlaygroundGame\Entity\MissionGame(); |
133
|
|
|
$mg->setGame($g); |
134
|
|
|
$mg->setPosition($m['position']); |
135
|
|
|
$mg->setMission($result); |
136
|
|
|
$result->addMissionGame($mg); |
137
|
|
|
} |
138
|
|
|
$i++; |
139
|
|
|
} |
140
|
|
|
$service->getGameMapper()->update($result); |
141
|
|
|
}*/ |
142
|
|
|
return $this->redirect()->toRoute('admin/playgroundgame/list'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$gameForm->setVariables(array('form' => $form, 'game' => $game)); |
147
|
|
|
$viewModel->addChild($gameForm, 'game_form'); |
148
|
|
|
|
149
|
|
|
return $viewModel->setVariables(array('form' => $form, 'title' => 'Edit mission', 'mission' => $game)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getAdminGameService() |
153
|
|
|
{ |
154
|
|
|
if (!$this->adminGameService) { |
155
|
|
|
$this->adminGameService = $this->getServiceLocator()->get('playgroundgame_mission_service'); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->adminGameService; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function setAdminGameService(AdminGameService $adminGameService) |
162
|
|
|
{ |
163
|
|
|
$this->adminGameService = $adminGameService; |
|
|
|
|
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
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.