1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusReportPlugin\Controller; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusReportPlugin\DataFetcher\Data; |
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 Odiseo\SyliusReportPlugin\Form\Type\ReportDataFetcherConfigurationType; |
13
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
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
|
|
|
public function renderAction(Request $request): Response |
30
|
|
|
{ |
31
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
32
|
|
|
|
33
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::SHOW); |
34
|
|
|
|
35
|
|
|
/** @var ReportInterface $report */ |
36
|
|
|
$report = $this->findOr404($configuration); |
37
|
|
|
$reportDataFetcherConfigurationForm = $this->getReportDataFetcherConfigurationForm($report, $request); |
38
|
|
|
$report = $reportDataFetcherConfigurationForm->getData(); |
39
|
|
|
|
40
|
|
|
$this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $report); |
41
|
|
|
|
42
|
|
|
if ($configuration->isHtmlRequest()) { |
43
|
|
|
return $this->render($configuration->getTemplate(ResourceActions::SHOW . '.html'), [ |
44
|
|
|
'configuration' => $configuration, |
45
|
|
|
'metadata' => $this->metadata, |
46
|
|
|
'resource' => $report, |
47
|
|
|
'form' => $reportDataFetcherConfigurationForm->createView(), |
48
|
|
|
$this->metadata->getName() => $report, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->createRestView($configuration, $report); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function exportAction(Request $request): Response |
56
|
|
|
{ |
57
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
58
|
|
|
|
59
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::SHOW); |
60
|
|
|
|
61
|
|
|
/** @var ReportInterface $report */ |
62
|
|
|
$report = $this->findOr404($configuration); |
63
|
|
|
$reportDataFetcherConfigurationForm = $this->getReportDataFetcherConfigurationForm($report, $request); |
64
|
|
|
$report = $reportDataFetcherConfigurationForm->getData(); |
65
|
|
|
|
66
|
|
|
$dataFetcherConfiguration = $report->getDataFetcherConfiguration(); |
67
|
|
|
|
68
|
|
|
/** @var Data $data */ |
69
|
|
|
$data = $this->getReportDataFetcher()->fetch($report, $dataFetcherConfiguration); |
70
|
|
|
|
71
|
|
|
$filename = $this->slugify($report->getName()); |
72
|
|
|
|
73
|
|
|
$format = $request->get('_format'); |
74
|
|
|
$response = null; |
75
|
|
|
switch ($format) { |
76
|
|
|
case 'json': |
77
|
|
|
$response = $this->createJsonResponse($filename, $data); |
78
|
|
|
break; |
79
|
|
|
case 'csv': |
80
|
|
|
default: |
81
|
|
|
$response = $this->createCsvResponse($filename, $data); |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $response; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function embedAction(ReportInterface $report, array $dataFetcherConfiguration = []): Response |
89
|
|
|
{ |
90
|
|
|
$data = $this->getReportDataFetcher()->fetch($report, $dataFetcherConfiguration); |
91
|
|
|
|
92
|
|
|
return new Response($this->getReportRenderer()->render($report, $data)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function getReportRenderer(): DelegatingRendererInterface |
96
|
|
|
{ |
97
|
|
|
/** @var DelegatingRendererInterface $renderer */ |
98
|
|
|
$renderer = $this->container->get('odiseo_sylius_report.renderer'); |
99
|
|
|
|
100
|
|
|
return $renderer; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function getReportDataFetcher(): DelegatingDataFetcherInterface |
104
|
|
|
{ |
105
|
|
|
/** @var DelegatingDataFetcherInterface $dataFetcher */ |
106
|
|
|
$dataFetcher = $this->container->get('odiseo_sylius_report.data_fetcher'); |
107
|
|
|
|
108
|
|
|
return $dataFetcher; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function createJsonResponse(string $filename, Data $data): Response |
112
|
|
|
{ |
113
|
|
|
$responseData = []; |
114
|
|
|
foreach ($data->getData() as $key => $value) { |
115
|
|
|
$responseData[] = [$key, $value]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$response = JsonResponse::create($responseData); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $filename.'.json')); |
121
|
|
|
|
122
|
|
|
if (!$response->headers->has('Content-Type')) { |
123
|
|
|
$response->headers->set('Content-Type', 'text/json'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $response; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function createCsvResponse(string $filename, Data $data): Response |
130
|
|
|
{ |
131
|
|
|
$response = new CsvResponse($data); |
132
|
|
|
$response->setFilename($filename.'.csv'); |
133
|
|
|
|
134
|
|
|
return $response; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function getReportDataFetcherConfigurationForm(ReportInterface $report, Request $request): FormInterface |
138
|
|
|
{ |
139
|
|
|
/** @var FormFactoryInterface $formFactory */ |
140
|
|
|
$formFactory = $this->container->get('form.factory'); |
141
|
|
|
|
142
|
|
|
/** @var FormInterface $configurationForm */ |
143
|
|
|
$configurationForm = $formFactory->create(ReportDataFetcherConfigurationType::class, $report); |
144
|
|
|
|
145
|
|
|
if ($request->query->has($configurationForm->getName())) { |
146
|
|
|
$configurationForm->handleRequest($request); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $configurationForm; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function slugify(string $string): string |
153
|
|
|
{ |
154
|
|
|
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string))); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.