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\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
|
|
|
|
42
|
|
|
/** @var DataFetcherInterface $dataFetcher */ |
43
|
|
|
$dataFetcher = $this->getReportDataFetcher()->getDataFetcher($report); |
44
|
|
|
/** @var FormInterface $configurationForm */ |
45
|
|
|
$configurationForm = $this->container->get('form.factory')->createNamed( |
46
|
|
|
'configuration', |
47
|
|
|
$dataFetcher->getType(), |
48
|
|
|
$report->getDataFetcherConfiguration() |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
if ($request->query->has('configuration')) { |
52
|
|
|
$configurationForm->handleRequest($request); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $report); |
56
|
|
|
|
57
|
|
|
$view = View::create($report); |
58
|
|
|
|
59
|
|
|
if ($configuration->isHtmlRequest()) { |
60
|
|
|
$view |
61
|
|
|
->setTemplate($configuration->getTemplate(ResourceActions::SHOW . '.html')) |
62
|
|
|
->setTemplateVar($this->metadata->getName()) |
63
|
|
|
->setData([ |
64
|
|
|
'configuration' => $configuration, |
65
|
|
|
'metadata' => $this->metadata, |
66
|
|
|
'resource' => $report, |
67
|
|
|
'form' => $configurationForm->createView(), |
68
|
|
|
'configurationForm' => $configurationForm->getData(), |
69
|
|
|
$this->metadata->getName() => $report, |
70
|
|
|
]) |
71
|
|
|
; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->viewHandler->handle($configuration, $view); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Request $request |
79
|
|
|
* |
80
|
|
|
* @return Response |
81
|
|
|
*/ |
82
|
|
|
public function exportAction(Request $request) |
83
|
|
|
{ |
84
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
85
|
|
|
|
86
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::SHOW); |
87
|
|
|
|
88
|
|
|
/** @var ReportInterface $report */ |
89
|
|
|
$report = $this->findOr404($configuration); |
90
|
|
|
|
91
|
|
|
$type = $request->get('type'); |
92
|
|
|
$configuration = $request->get('configuration'); |
93
|
|
|
$currencyProvider = $this->get('sylius.currency_provider'); |
94
|
|
|
|
95
|
|
|
if(isset($configuration['timePeriod'])) |
96
|
|
|
{ |
97
|
|
|
$configuration['timePeriod']['start'] = new \DateTime($configuration['timePeriod']['start']['date'], new \DateTimeZone($configuration['timePeriod']['start']['timezone'])); |
98
|
|
|
$configuration['timePeriod']['end'] = new \DateTime($configuration['timePeriod']['end']['date'], new \DateTimeZone($configuration['timePeriod']['end']['timezone'])); |
99
|
|
|
} |
100
|
|
|
$configuration['baseCurrency'] = $currencyProvider->getBaseCurrency(); |
101
|
|
|
|
102
|
|
|
/** @var Data $data */ |
103
|
|
|
$data = $this->getReportDataFetcher()->fetch($report, $configuration); |
104
|
|
|
|
105
|
|
|
$response = null; |
106
|
|
|
switch ($type) |
107
|
|
|
{ |
108
|
|
|
case 'json': |
109
|
|
|
$response = $this->createJsonResponse($data); |
110
|
|
|
break; |
111
|
|
|
case 'csv': |
112
|
|
|
default: |
113
|
|
|
$response = $this->createCsvResponse($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
|
|
|
return $dataFetcher; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Data $data |
162
|
|
|
* |
163
|
|
|
* @return Response |
164
|
|
|
*/ |
165
|
|
|
protected function createJsonResponse($data) |
166
|
|
|
{ |
167
|
|
|
$responseData = []; |
168
|
|
|
foreach ($data->getData() as $key => $value) |
169
|
|
|
{ |
170
|
|
|
$responseData[] = [$key, $value]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$response = JsonResponse::create($responseData); |
174
|
|
|
|
175
|
|
|
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', 'export.json')); |
176
|
|
|
|
177
|
|
|
if (!$response->headers->has('Content-Type')) { |
178
|
|
|
$response->headers->set('Content-Type', 'text/json'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $response; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param Data $data |
186
|
|
|
* |
187
|
|
|
* @return Response |
188
|
|
|
*/ |
189
|
|
|
protected function createCsvResponse($data) |
190
|
|
|
{ |
191
|
|
|
$responseData = []; |
192
|
|
|
foreach ($data->getData() as $key => $value) |
193
|
|
|
{ |
194
|
|
|
if(is_array($value)) |
195
|
|
|
{ |
196
|
|
|
foreach ($value as $k => $v) |
197
|
|
|
{ |
198
|
|
|
$responseData[] = [$k, $v]; |
199
|
|
|
} |
200
|
|
|
}else { |
201
|
|
|
$responseData[] = [$key, $value]; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return new CsvResponse($responseData); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|