1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* @author Nicolas CARPi <[email protected]> |
4
|
|
|
* @copyright 2012 Nicolas CARPi |
5
|
|
|
* @see https://www.elabftw.net Official website |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* @package elabftw |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Elabftw\Controllers; |
11
|
|
|
|
12
|
|
|
use Elabftw\Elabftw\App; |
13
|
|
|
use Elabftw\Exceptions\IllegalActionException; |
14
|
|
|
use Elabftw\Interfaces\ControllerInterface; |
15
|
|
|
use Elabftw\Interfaces\FileMakerInterface; |
16
|
|
|
use Elabftw\Interfaces\MpdfProviderInterface; |
17
|
|
|
use Elabftw\Models\AbstractEntity; |
18
|
|
|
use Elabftw\Models\Experiments; |
19
|
|
|
use Elabftw\Models\Items; |
20
|
|
|
use Elabftw\Models\Teams; |
21
|
|
|
use Elabftw\Services\MakeCsv; |
22
|
|
|
use Elabftw\Services\MakeJson; |
23
|
|
|
use Elabftw\Services\MakeMultiPdf; |
24
|
|
|
use Elabftw\Services\MakePdf; |
25
|
|
|
use Elabftw\Services\MakeQrPdf; |
26
|
|
|
use Elabftw\Services\MakeReport; |
27
|
|
|
use Elabftw\Services\MakeStreamZip; |
28
|
|
|
use Elabftw\Services\MpdfProvider; |
29
|
|
|
use function substr_count; |
30
|
|
|
use Symfony\Component\HttpFoundation\Response; |
31
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
32
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create zip, csv, pdf or report |
36
|
|
|
*/ |
37
|
|
|
class MakeController implements ControllerInterface |
38
|
|
|
{ |
39
|
|
|
/** @var AbstractEntity $Entity */ |
40
|
|
|
private $Entity; |
41
|
|
|
|
42
|
|
|
public function __construct(private App $App) |
43
|
|
|
{ |
44
|
|
|
$this->Entity = new Items($this->App->Users); |
45
|
|
|
if ($this->App->Request->query->get('type') === 'experiments') { |
46
|
|
|
$this->Entity = new Experiments($this->App->Users); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getResponse(): Response |
51
|
|
|
{ |
52
|
|
|
switch ($this->App->Request->query->get('what')) { |
53
|
|
|
case 'csv': |
54
|
|
|
return $this->makeCsv(); |
55
|
|
|
|
56
|
|
|
case 'json': |
57
|
|
|
return $this->makeJson(); |
58
|
|
|
|
59
|
|
|
case 'pdf': |
60
|
|
|
return $this->makePdf(); |
61
|
|
|
|
62
|
|
|
case 'multiPdf': |
63
|
|
|
if (substr_count((string) $this->App->Request->query->get('id'), ' ') === 0) { |
64
|
|
|
return $this->makePdf(); |
65
|
|
|
} |
66
|
|
|
return $this->makeMultiPdf(); |
67
|
|
|
|
68
|
|
|
case 'qrPdf': |
69
|
|
|
return $this->makeQrPdf(); |
70
|
|
|
|
71
|
|
|
case 'report': |
72
|
|
|
if (!$this->App->Session->get('is_sysadmin')) { |
73
|
|
|
throw new IllegalActionException('Non sysadmin user tried to generate report.'); |
74
|
|
|
} |
75
|
|
|
return $this->makeReport(); |
76
|
|
|
|
77
|
|
|
case 'zip': |
78
|
|
|
return $this->makeZip(); |
79
|
|
|
|
80
|
|
|
default: |
81
|
|
|
throw new IllegalActionException('Bad make what value'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function makeCsv(): Response |
86
|
|
|
{ |
87
|
|
|
return $this->getFileResponse(new MakeCsv($this->Entity, (string) $this->App->Request->query->get('id'))); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function makeJson(): Response |
91
|
|
|
{ |
92
|
|
|
return $this->getFileResponse(new MakeJson($this->Entity, (string) $this->App->Request->query->get('id'))); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function makePdf(): Response |
96
|
|
|
{ |
97
|
|
|
$this->Entity->setId((int) $this->App->Request->query->get('id')); |
98
|
|
|
$this->Entity->canOrExplode('read'); |
99
|
|
|
return $this->getFileResponse(new MakePdf($this->getMpdfProvider(), $this->Entity, true)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function makeMultiPdf(): Response |
103
|
|
|
{ |
104
|
|
|
return $this->getFileResponse(new MakeMultiPdf($this->getMpdfProvider(), $this->Entity, (string) $this->App->Request->query->get('id'))); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function makeQrPdf(): Response |
108
|
|
|
{ |
109
|
|
|
return $this->getFileResponse(new MakeQrPdf($this->getMpdfProvider(), $this->Entity, (string) $this->App->Request->query->get('id'))); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function makeReport(): Response |
113
|
|
|
{ |
114
|
|
|
return $this->getFileResponse(new MakeReport(new Teams($this->App->Users))); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function makeZip(): Response |
118
|
|
|
{ |
119
|
|
|
$Make = new MakeStreamZip($this->Entity, (string) $this->App->Request->query->get('id')); |
120
|
|
|
$Response = new StreamedResponse(); |
121
|
|
|
$Response->headers->set('X-Accel-Buffering', 'no'); |
122
|
|
|
$Response->headers->set('Content-Type', 'application/zip'); |
123
|
|
|
$Response->headers->set('Cache-Control', 'no-store'); |
124
|
|
|
$contentDisposition = $Response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $Make->getFileName(), 'elabftw-export.zip'); |
125
|
|
|
$Response->headers->set('Content-Disposition', $contentDisposition); |
126
|
|
|
$Response->setCallback(function () use ($Make) { |
127
|
|
|
$Make->getZip(); |
128
|
|
|
}); |
129
|
|
|
return $Response; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function getMpdfProvider(): MpdfProviderInterface |
133
|
|
|
{ |
134
|
|
|
$userData = $this->App->Users->userData; |
135
|
|
|
return new MpdfProvider( |
136
|
|
|
$userData['fullname'], |
137
|
|
|
$userData['pdf_format'], |
138
|
|
|
(bool) $userData['pdfa'], |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function getFileResponse(FileMakerInterface $Maker): Response |
143
|
|
|
{ |
144
|
|
|
return new Response( |
145
|
|
|
$Maker->getFileContent(), |
146
|
|
|
200, |
147
|
|
|
array( |
148
|
|
|
'Content-Type' => $Maker->getContentType(), |
149
|
|
|
'Content-disposition' => 'inline; filename="' . $Maker->getFileName() . '"', |
150
|
|
|
'Cache-Control' => 'no-store', |
151
|
|
|
'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT', |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|