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