1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Bundle\ResourceBundle\Rest\View\EventSubscriber; |
13
|
|
|
|
14
|
|
|
use Lug\Bundle\GridBundle\Form\Type\Batch\GridBatchType; |
15
|
|
|
use Lug\Bundle\GridBundle\View\GridViewInterface; |
16
|
|
|
use Lug\Bundle\ResourceBundle\Form\FormFactoryInterface; |
17
|
|
|
use Lug\Bundle\ResourceBundle\Rest\AbstractSubscriber; |
18
|
|
|
use Lug\Bundle\ResourceBundle\Rest\RestEvents; |
19
|
|
|
use Lug\Bundle\ResourceBundle\Rest\View\ViewEvent; |
20
|
|
|
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface; |
21
|
|
|
use Symfony\Component\Form\FormInterface; |
22
|
|
|
use Symfony\Component\Form\FormRendererInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class GridViewSubscriber extends AbstractSubscriber |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var FormFactoryInterface |
31
|
|
|
*/ |
32
|
|
|
private $formFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var FormRendererInterface |
36
|
|
|
*/ |
37
|
|
|
private $formRenderer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param ParameterResolverInterface $parameterResolver |
41
|
|
|
* @param FormFactoryInterface $formFactory |
42
|
|
|
* @param FormRendererInterface $formRenderer |
43
|
|
|
*/ |
44
|
11 |
|
public function __construct( |
45
|
|
|
ParameterResolverInterface $parameterResolver, |
46
|
|
|
FormFactoryInterface $formFactory, |
47
|
|
|
FormRendererInterface $formRenderer |
48
|
|
|
) { |
49
|
11 |
|
parent::__construct($parameterResolver); |
50
|
|
|
|
51
|
11 |
|
$this->formFactory = $formFactory; |
52
|
11 |
|
$this->formRenderer = $formRenderer; |
53
|
11 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ViewEvent $event |
57
|
|
|
*/ |
58
|
3 |
|
public function onApi(ViewEvent $event) |
59
|
|
|
{ |
60
|
3 |
|
if (!$this->getParameterResolver()->resolveApi()) { |
61
|
1 |
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$view = $event->getView(); |
65
|
2 |
|
$data = $view->getData(); |
66
|
|
|
|
67
|
2 |
View Code Duplication |
if (is_array($data) && isset($data['grid']) && $data['grid'] instanceof GridViewInterface) { |
|
|
|
|
68
|
1 |
|
$data = $data['grid']; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
2 |
|
if ($data instanceof GridViewInterface) { |
72
|
1 |
|
$view->setData($data->getDataSource()); |
73
|
1 |
|
} |
74
|
2 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ViewEvent $event |
78
|
|
|
*/ |
79
|
5 |
|
public function onView(ViewEvent $event) |
80
|
|
|
{ |
81
|
5 |
|
if ($this->getParameterResolver()->resolveApi()) { |
82
|
1 |
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
$view = $event->getView(); |
86
|
4 |
|
$data = $grid = $view->getData(); |
87
|
|
|
|
88
|
4 |
View Code Duplication |
if (is_array($data) && isset($data['grid']) && $data['grid'] instanceof GridViewInterface) { |
|
|
|
|
89
|
3 |
|
$grid = $data['grid']; |
90
|
3 |
|
} |
91
|
|
|
|
92
|
4 |
|
if (!$grid instanceof GridViewInterface) { |
93
|
1 |
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
if ($grid->getBatchForm() === null) { |
97
|
3 |
|
$batchForm = !isset($data['batch_form']) || !$data['batch_form'] instanceof FormInterface |
98
|
3 |
|
? $this->formFactory->create(GridBatchType::class, null, ['grid' => $grid]) |
99
|
3 |
|
: $data['batch_form']; |
100
|
|
|
|
101
|
3 |
|
$grid->setBatchForm($batchForm->createView()); |
102
|
3 |
|
} |
103
|
|
|
|
104
|
3 |
|
$themes = $this->getParameterResolver()->resolveThemes(); |
105
|
|
|
|
106
|
3 |
|
if (!empty($themes)) { |
107
|
1 |
|
$this->formRenderer->setTheme($grid->getForm(), $themes); |
|
|
|
|
108
|
1 |
|
$this->formRenderer->setTheme($grid->getBatchForm(), $themes); |
|
|
|
|
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
$view |
112
|
3 |
|
->setTemplateVar('grid') |
113
|
3 |
|
->setData($grid); |
114
|
3 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
1 |
View Code Duplication |
public static function getSubscribedEvents() |
|
|
|
|
120
|
|
|
{ |
121
|
1 |
|
return [RestEvents::VIEW => [ |
122
|
1 |
|
['onApi', -2000], |
123
|
1 |
|
['onView', -1000], |
124
|
1 |
|
]]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.