|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VideoGamesRecords\CoreBundle\Admin; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
|
8
|
|
|
use Doctrine\ORM\Exception\NotSupported; |
|
9
|
|
|
use FOS\CKEditorBundle\Form\Type\CKEditorType; |
|
10
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
|
11
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
|
12
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
|
13
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
14
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
|
15
|
|
|
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType; |
|
16
|
|
|
use Sonata\AdminBundle\Form\Type\ModelListType; |
|
17
|
|
|
use Sonata\AdminBundle\Route\RouteCollectionInterface; |
|
18
|
|
|
use Sonata\AdminBundle\Show\ShowMapper; |
|
19
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter; |
|
20
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter; |
|
21
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\NullFilter; |
|
22
|
|
|
use Symfony\Bundle\SecurityBundle\Security; |
|
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
24
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
26
|
|
|
use Symfony\Component\Intl\Locale; |
|
27
|
|
|
use VideoGamesRecords\CoreBundle\Traits\Accessor\SetRequestStack; |
|
28
|
|
|
use VideoGamesRecords\CoreBundle\ValueObject\ProofStatus; |
|
29
|
|
|
|
|
30
|
|
|
class ProofAdmin extends AbstractAdmin |
|
31
|
|
|
{ |
|
32
|
|
|
use SetRequestStack; |
|
33
|
|
|
|
|
34
|
|
|
protected $baseRouteName = 'vgrcorebundle_admin_proof'; |
|
35
|
|
|
|
|
36
|
|
|
private Security $security; |
|
37
|
|
|
|
|
38
|
|
|
public function setSecurity(Security $security): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->security = $security; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
private function getLibGame(): string |
|
47
|
|
|
{ |
|
48
|
|
|
$locale = Locale::getDefault(); |
|
49
|
|
|
return ($locale == 'fr') ? 'libGameFr' : 'libGameEn'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
private function getLibGroup(): string |
|
56
|
|
|
{ |
|
57
|
|
|
$locale = Locale::getDefault(); |
|
58
|
|
|
return ($locale == 'fr') ? 'libGroupFr' : 'libGroupEn'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
private function getLibChart(): string |
|
65
|
|
|
{ |
|
66
|
|
|
$locale = Locale::getDefault(); |
|
67
|
|
|
return ($locale == 'fr') ? 'libChartFr' : 'libChartEn'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param ProxyQueryInterface $query |
|
72
|
|
|
* @return ProxyQueryInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface |
|
75
|
|
|
{ |
|
76
|
|
|
$query = parent::configureQuery($query); |
|
77
|
|
|
$query->innerJoin($query->getRootAliases()[0] . '.chart', 'chr') |
|
|
|
|
|
|
78
|
|
|
->addSelect('chr') |
|
79
|
|
|
->innerJoin('chr.group', 'grp') |
|
80
|
|
|
->addSelect('grp') |
|
81
|
|
|
->innerJoin('grp.game', 'gam') |
|
82
|
|
|
->addSelect('gam'); |
|
83
|
|
|
return $query; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param RouteCollectionInterface $collection |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function configureRoutes(RouteCollectionInterface $collection): void |
|
90
|
|
|
{ |
|
91
|
|
|
$collection |
|
92
|
|
|
->remove('create') |
|
93
|
|
|
->remove('delete') |
|
94
|
|
|
->remove('export') |
|
95
|
|
|
->add('stats', 'stats'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param FormMapper $form |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function configureFormFields(FormMapper $form): void |
|
103
|
|
|
{ |
|
104
|
|
|
$form |
|
105
|
|
|
->add( |
|
106
|
|
|
'playerResponding', |
|
107
|
|
|
ModelListType::class, |
|
108
|
|
|
[ |
|
109
|
|
|
'data_class' => null, |
|
110
|
|
|
'btn_add' => false, |
|
111
|
|
|
'btn_list' => false, |
|
112
|
|
|
'btn_edit' => false, |
|
113
|
|
|
'btn_delete' => false, |
|
114
|
|
|
'btn_catalogue' => false, |
|
115
|
|
|
'label' => 'label.player.responding', |
|
116
|
|
|
] |
|
117
|
|
|
) |
|
118
|
|
|
->add( |
|
119
|
|
|
'playerChart', |
|
120
|
|
|
ModelListType::class, |
|
121
|
|
|
[ |
|
122
|
|
|
'data_class' => null, |
|
123
|
|
|
'btn_add' => false, |
|
124
|
|
|
'btn_list' => false, |
|
125
|
|
|
'btn_edit' => false, |
|
126
|
|
|
'btn_delete' => false, |
|
127
|
|
|
'btn_catalogue' => false, |
|
128
|
|
|
'label' => 'label.playerChart.edit', |
|
129
|
|
|
] |
|
130
|
|
|
) |
|
131
|
|
|
->add( |
|
132
|
|
|
'status', |
|
133
|
|
|
ChoiceType::class, |
|
134
|
|
|
[ |
|
135
|
|
|
'label' => 'label.status', |
|
136
|
|
|
'choices' => ProofStatus::getStatusChoices(), |
|
137
|
|
|
'choice_translation_domain' => false, |
|
138
|
|
|
] |
|
139
|
|
|
) |
|
140
|
|
|
->add('response', CKEditorType::class, [ |
|
141
|
|
|
'label' => 'label.proof.response', |
|
142
|
|
|
'required' => false, |
|
143
|
|
|
'config' => array( |
|
144
|
|
|
'height' => '100', |
|
145
|
|
|
'toolbar' => 'standard' |
|
146
|
|
|
), |
|
147
|
|
|
]); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param DatagridMapper $filter |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function configureDatagridFilters(DatagridMapper $filter): void |
|
154
|
|
|
{ |
|
155
|
|
|
$filter |
|
156
|
|
|
->add('id', null, ['label' => 'label.id']) |
|
157
|
|
|
->add('player', ModelFilter::class, [ |
|
158
|
|
|
'field_type' => ModelAutocompleteType::class, |
|
159
|
|
|
'field_options' => ['property' => 'pseudo'], |
|
160
|
|
|
'label' => 'label.player' |
|
161
|
|
|
]) |
|
162
|
|
|
->add('player.pseudo', null, ['label' => 'label.pseudo']) |
|
163
|
|
|
->add('chart.group.game.platforms', ModelFilter::class, [ |
|
164
|
|
|
'field_type' => ModelAutocompleteType::class, |
|
165
|
|
|
'field_options' => ['property' => 'libPlatform'], |
|
166
|
|
|
'label' => 'label.platform' |
|
167
|
|
|
]) |
|
168
|
|
|
->add('chart.group.game', ModelFilter::class, [ |
|
169
|
|
|
'field_type' => ModelAutocompleteType::class, |
|
170
|
|
|
'field_options' => ['property' => $this->getLibGame()], |
|
171
|
|
|
'label' => 'label.game' |
|
172
|
|
|
]) |
|
173
|
|
|
->add('chart.group.game.libGameEn', null, ['label' => 'label.game.en']) |
|
174
|
|
|
->add('chart.group.game.libGameFr', null, ['label' => 'label.game.fr']) |
|
175
|
|
|
->add('status', ChoiceFilter::class, [ |
|
176
|
|
|
'label' => 'label.proof.status', |
|
177
|
|
|
'field_type' => ChoiceType::class, |
|
178
|
|
|
'field_options' => [ |
|
179
|
|
|
'choices' => ProofStatus::getStatusChoices(), |
|
180
|
|
|
'multiple' => false, |
|
181
|
|
|
] |
|
182
|
|
|
]) |
|
183
|
|
|
->add('playerChart.status', null, ['label' => 'label.playerChart.status']) |
|
184
|
|
|
->add('playerResponding', ModelFilter::class, [ |
|
185
|
|
|
'field_type' => ModelAutocompleteType::class, |
|
186
|
|
|
'field_options' => ['property' => 'pseudo'], |
|
187
|
|
|
'label' => 'label.player.responding' |
|
188
|
|
|
]) |
|
189
|
|
|
->add('video', NullFilter::class, ['label' => 'label.video_is_null']) |
|
190
|
|
|
->add('picture', NullFilter::class, ['label' => 'label.picture_is_null']) |
|
191
|
|
|
; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param ListMapper $list |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function configureListFields(ListMapper $list): void |
|
199
|
|
|
{ |
|
200
|
|
|
$list |
|
201
|
|
|
->addIdentifier('id', null, ['label' => 'label.id']) |
|
202
|
|
|
->add('player', null, [ |
|
203
|
|
|
'associated_property' => 'pseudo', |
|
204
|
|
|
'label' => 'label.player', |
|
205
|
|
|
]) |
|
206
|
|
|
->add('chart.group.game', null, [ |
|
207
|
|
|
'associated_property' => $this->getLibGame(), |
|
208
|
|
|
'label' => 'label.game', |
|
209
|
|
|
'sortable' => true, |
|
210
|
|
|
'sort_field_mapping' => array( |
|
211
|
|
|
'fieldName' => $this->getLibGame() |
|
212
|
|
|
), |
|
213
|
|
|
'sort_parent_association_mappings' => array( |
|
214
|
|
|
array('fieldName' => 'chart'), |
|
215
|
|
|
array('fieldName' => 'group'), |
|
216
|
|
|
array('fieldName' => 'game'), |
|
217
|
|
|
) |
|
218
|
|
|
]) |
|
219
|
|
|
->add('chart.group', null, [ |
|
220
|
|
|
'associated_property' => $this->getLibGroup(), |
|
221
|
|
|
'label' => 'label.group', |
|
222
|
|
|
'sortable' => true, |
|
223
|
|
|
'sort_field_mapping' => array( |
|
224
|
|
|
'fieldName' => $this->getLibGroup() |
|
225
|
|
|
), |
|
226
|
|
|
'sort_parent_association_mappings' => array( |
|
227
|
|
|
array('fieldName' => 'chart'), |
|
228
|
|
|
array('fieldName' => 'group') |
|
229
|
|
|
) |
|
230
|
|
|
]) |
|
231
|
|
|
->add('chart', null, [ |
|
232
|
|
|
'associated_property' => $this->getLibChart(), |
|
233
|
|
|
'label' => 'label.chart', |
|
234
|
|
|
]) |
|
235
|
|
|
->addIdentifier('type', null, ['label' => 'label.type']) |
|
236
|
|
|
->add('playerResponding', null, [ |
|
237
|
|
|
'associated_property' => 'pseudo', |
|
238
|
|
|
'label' => 'label.player.responding', |
|
239
|
|
|
]) |
|
240
|
|
|
->add( |
|
241
|
|
|
'status', |
|
242
|
|
|
'choice', |
|
243
|
|
|
[ |
|
244
|
|
|
'label' => 'label.status', |
|
245
|
|
|
'editable' => true, |
|
246
|
|
|
'choices' => ProofStatus::getStatusChoices(), |
|
247
|
|
|
'choice_translation_domain' => false, |
|
248
|
|
|
] |
|
249
|
|
|
) |
|
250
|
|
|
->add('playerChart.status', null, ['label' => 'label.playerChart.status']) |
|
251
|
|
|
->add('created_at', 'datetime', ['label' => 'label.createdAt']) |
|
252
|
|
|
->add('_action', 'actions', [ |
|
253
|
|
|
'actions' => [ |
|
254
|
|
|
'show' => [], |
|
255
|
|
|
'edit' => [], |
|
256
|
|
|
] |
|
257
|
|
|
]); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @param ShowMapper $show |
|
262
|
|
|
*/ |
|
263
|
|
|
protected function configureShowFields(ShowMapper $show): void |
|
264
|
|
|
{ |
|
265
|
|
|
$show |
|
266
|
|
|
->add('id', null, ['label' => 'label.id']) |
|
267
|
|
|
->add('Player', null, ['label' => 'label.player']) |
|
268
|
|
|
->add('chart.group.game', null, array( |
|
269
|
|
|
'associated_property' => $this->getLibGame(), |
|
270
|
|
|
'label' => 'label.game', |
|
271
|
|
|
)) |
|
272
|
|
|
->add('chart.group', null, array( |
|
273
|
|
|
'associated_property' => $this->getLibGroup(), |
|
274
|
|
|
'label' => 'label.group', |
|
275
|
|
|
)) |
|
276
|
|
|
->add('chart', null, array( |
|
277
|
|
|
'associated_property' => $this->getLibChart(), |
|
278
|
|
|
'label' => 'label.chart', |
|
279
|
|
|
)) |
|
280
|
|
|
->add('created_at', 'datetime', ['label' => 'label.createdAt']) |
|
281
|
|
|
->add('updated_at', 'datetime', ['label' => 'label.updatedAt']) |
|
282
|
|
|
->add('checkedAt', 'datetime', ['label' => 'label.checkedAt']) |
|
283
|
|
|
->add('playerChart', null, ['label' => 'label.score']) |
|
284
|
|
|
->add('picture', null, ['label' => 'label.picture']) |
|
285
|
|
|
->add('video', null, ['label' => 'label.video']) |
|
286
|
|
|
->add('playerResponding', null, ['label' => 'label.player.responding']) |
|
287
|
|
|
->add('status', null, ['label' => 'label.status']) |
|
288
|
|
|
->add('proofRequest.message', null, ['label' => 'label.message']); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* @param $object |
|
293
|
|
|
*/ |
|
294
|
|
|
public function preValidate($object): void |
|
295
|
|
|
{ |
|
296
|
|
|
$player = $this->getPlayer(); |
|
297
|
|
|
|
|
298
|
|
|
if ($object->getPlayerChart() != null) { |
|
299
|
|
|
if ($object->getPlayerChart()->getPlayer()->getId() === $player->getId()) { |
|
300
|
|
|
$this->requestStack->getSession()->getFlashBag()->add( |
|
301
|
|
|
'error', |
|
302
|
|
|
"You can't update this proof" |
|
303
|
|
|
); |
|
304
|
|
|
|
|
305
|
|
|
$response = new RedirectResponse( |
|
306
|
|
|
$this->generateUrl( |
|
307
|
|
|
'edit', |
|
308
|
|
|
array( |
|
309
|
|
|
'id' => $object->getId() |
|
310
|
|
|
) |
|
311
|
|
|
) |
|
312
|
|
|
); |
|
313
|
|
|
header('Location: ' . $response->getTargetUrl()); |
|
314
|
|
|
exit; |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @param $object |
|
321
|
|
|
* @return void |
|
322
|
|
|
*/ |
|
323
|
|
|
public function preUpdate($object): void |
|
324
|
|
|
{ |
|
325
|
|
|
/** @var EntityManager $em */ |
|
326
|
|
|
$em = $this->getModelManager()->getEntityManager($this->getClass()); |
|
|
|
|
|
|
327
|
|
|
$originalObject = $em->getUnitOfWork()->getOriginalEntityData($object); |
|
328
|
|
|
|
|
329
|
|
|
// Cant change status final (CLOSED & REFUSED) |
|
330
|
|
|
if (in_array($originalObject['status'], array(ProofStatus::CLOSED, ProofStatus::REFUSED), true)) { |
|
331
|
|
|
$object->setStatus($originalObject['status']); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
|
|
335
|
|
|
if ($object->getPlayerChart() == null) { |
|
336
|
|
|
$object->setStatus(ProofStatus::CLOSED); |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* @return mixed |
|
342
|
|
|
* @throws NotSupported |
|
343
|
|
|
*/ |
|
344
|
|
|
private function getPlayer() |
|
345
|
|
|
{ |
|
346
|
|
|
/** @var EntityManager $em */ |
|
347
|
|
|
$em = $this->getModelManager()->getEntityManager($this->getClass()); |
|
348
|
|
|
$user = $this->security->getUser(); |
|
349
|
|
|
return $em->getRepository('VideoGamesRecords\CoreBundle\Entity\Player')->getPlayerFromUser($user); |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|