1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Odiseo\SyliusReportPlugin\Controller; |
4
|
|
|
|
5
|
|
|
use FOS\RestBundle\View\View; |
6
|
|
|
use Odiseo\SyliusReportPlugin\DataFetcher\Data; |
7
|
|
|
use Odiseo\SyliusReportPlugin\DataFetcher\DelegatingDataFetcherInterface; |
8
|
|
|
use Odiseo\SyliusReportPlugin\Model\ReportInterface; |
9
|
|
|
use Odiseo\SyliusReportPlugin\Renderer\DelegatingRendererInterface; |
10
|
|
|
use Odiseo\SyliusReportPlugin\Response\CsvResponse; |
11
|
|
|
use Odiseo\SyliusReportPlugin\Form\Type\ReportDataFetcherConfigurationType; |
12
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
13
|
|
|
use Sylius\Component\Resource\ResourceActions; |
14
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
15
|
|
|
use Symfony\Component\Form\FormInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Mateusz Zalewski <[email protected]> |
22
|
|
|
* @author Łukasz Chruściel <[email protected]> |
23
|
|
|
* @author Fernando Caraballo Ortiz <[email protected]> |
24
|
|
|
* @author Diego D'amico <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ReportController extends ResourceController |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param Request $request |
30
|
|
|
* |
31
|
|
|
* @return Response |
32
|
|
|
*/ |
33
|
|
|
public function renderAction(Request $request) |
34
|
|
|
{ |
35
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
36
|
|
|
|
37
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::SHOW); |
38
|
|
|
|
39
|
|
|
/** @var ReportInterface $report */ |
40
|
|
|
$report = $this->findOr404($configuration); |
41
|
|
|
$reportDataFetcherConfigurationForm = $this->getReportDataFetcherConfigurationForm($report, $request); |
42
|
|
|
$report = $reportDataFetcherConfigurationForm->getData(); |
43
|
|
|
|
44
|
|
|
$this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $report); |
45
|
|
|
|
46
|
|
|
$view = View::create($report); |
47
|
|
|
|
48
|
|
|
$view |
49
|
|
|
->setTemplate($configuration->getTemplate(ResourceActions::SHOW . '.html')) |
50
|
|
|
->setTemplateVar($this->metadata->getName()) |
51
|
|
|
->setData([ |
52
|
|
|
'configuration' => $configuration, |
53
|
|
|
'metadata' => $this->metadata, |
54
|
|
|
'resource' => $report, |
55
|
|
|
'form' => $reportDataFetcherConfigurationForm->createView(), |
56
|
|
|
$this->metadata->getName() => $report, |
57
|
|
|
]) |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
return $this->viewHandler->handle($configuration, $view); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Request $request |
65
|
|
|
* |
66
|
|
|
* @return Response |
67
|
|
|
*/ |
68
|
|
|
public function exportAction(Request $request) |
69
|
|
|
{ |
70
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
71
|
|
|
|
72
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::SHOW); |
73
|
|
|
|
74
|
|
|
/** @var ReportInterface $report */ |
75
|
|
|
$report = $this->findOr404($configuration); |
76
|
|
|
$reportDataFetcherConfigurationForm = $this->getReportDataFetcherConfigurationForm($report, $request); |
77
|
|
|
$report = $reportDataFetcherConfigurationForm->getData(); |
78
|
|
|
|
79
|
|
|
$dataFetcherConfiguration = $report->getDataFetcherConfiguration(); |
80
|
|
|
|
81
|
|
|
/** @var Data $data */ |
82
|
|
|
$data = $this->getReportDataFetcher()->fetch($report, $dataFetcherConfiguration); |
83
|
|
|
|
84
|
|
|
$filename = $this->slugify($report->getName()); |
85
|
|
|
|
86
|
|
|
$format = $request->get('_format'); |
87
|
|
|
$response = null; |
88
|
|
|
switch ($format) { |
89
|
|
|
case 'json': |
90
|
|
|
$response = $this->createJsonResponse($filename, $data); |
91
|
|
|
break; |
92
|
|
|
case 'csv': |
93
|
|
|
default: |
94
|
|
|
$response = $this->createCsvResponse($filename, $data); |
95
|
|
|
break; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $response; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param ReportInterface $report |
103
|
|
|
* @param array $dataFetcherConfiguration |
104
|
|
|
* |
105
|
|
|
* @return Response |
106
|
|
|
*/ |
107
|
|
|
public function embedAction(ReportInterface $report, array $dataFetcherConfiguration = []) |
108
|
|
|
{ |
109
|
|
|
$data = $this->getReportDataFetcher()->fetch($report, $dataFetcherConfiguration); |
110
|
|
|
|
111
|
|
|
return new Response($this->getReportRenderer()->render($report, $data)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return DelegatingRendererInterface |
116
|
|
|
*/ |
117
|
|
|
private function getReportRenderer() |
118
|
|
|
{ |
119
|
|
|
/** @var DelegatingRendererInterface $renderer */ |
120
|
|
|
$renderer = $this->container->get('odiseo_sylius_report.renderer'); |
121
|
|
|
|
122
|
|
|
return $renderer; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return DelegatingDataFetcherInterface |
127
|
|
|
*/ |
128
|
|
|
private function getReportDataFetcher() |
129
|
|
|
{ |
130
|
|
|
/** @var DelegatingDataFetcherInterface $dataFetcher */ |
131
|
|
|
$dataFetcher = $this->container->get('odiseo_sylius_report.data_fetcher'); |
132
|
|
|
|
133
|
|
|
return $dataFetcher; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $filename |
138
|
|
|
* @param Data $data |
139
|
|
|
* |
140
|
|
|
* @return Response |
141
|
|
|
*/ |
142
|
|
|
protected function createJsonResponse($filename, $data) |
143
|
|
|
{ |
144
|
|
|
$responseData = []; |
145
|
|
|
foreach ($data->getData() as $key => $value) { |
146
|
|
|
$responseData[] = [$key, $value]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$response = JsonResponse::create($responseData); |
150
|
|
|
|
151
|
|
|
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $filename.'.json')); |
152
|
|
|
|
153
|
|
|
if (!$response->headers->has('Content-Type')) { |
154
|
|
|
$response->headers->set('Content-Type', 'text/json'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $response; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $filename |
162
|
|
|
* @param Data $data |
163
|
|
|
* |
164
|
|
|
* @return Response |
165
|
|
|
*/ |
166
|
|
|
protected function createCsvResponse($filename, $data) |
167
|
|
|
{ |
168
|
|
|
$labels = [$data->getLabels()]; |
169
|
|
|
|
170
|
|
|
$responseData = array_merge($labels, $data->getData()); |
171
|
|
|
|
172
|
|
|
$response = new CsvResponse($responseData); |
173
|
|
|
$response->setFilename($filename.'.csv'); |
174
|
|
|
|
175
|
|
|
return $response; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param ReportInterface $report |
180
|
|
|
* @param Request $request |
181
|
|
|
* @return FormInterface |
182
|
|
|
*/ |
183
|
|
|
protected function getReportDataFetcherConfigurationForm(ReportInterface $report, Request $request): FormInterface |
184
|
|
|
{ |
185
|
|
|
/** @var FormFactoryInterface $formFactory */ |
186
|
|
|
$formFactory = $this->container->get('form.factory'); |
187
|
|
|
|
188
|
|
|
/** @var FormInterface $configurationForm */ |
189
|
|
|
$configurationForm = $formFactory->create(ReportDataFetcherConfigurationType::class, $report); |
190
|
|
|
|
191
|
|
|
if ($request->query->has($configurationForm->getName())) { |
192
|
|
|
$configurationForm->handleRequest($request); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $configurationForm; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $string |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
private function slugify($string) |
204
|
|
|
{ |
205
|
|
|
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string))); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|