|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\InformationCollectionBundle\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
|
6
|
|
|
use eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute; |
|
7
|
|
|
use eZ\Bundle\EzPublishCoreBundle\Controller; |
|
8
|
|
|
use Netgen\InformationCollection\API\Service\Exporter; |
|
9
|
|
|
use Netgen\InformationCollection\API\Value\Export\ExportCriteria; |
|
10
|
|
|
use Netgen\InformationCollection\Form\Type\ExportType; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\Form\ClickableInterface; |
|
13
|
|
|
use League\Csv\Writer; |
|
14
|
|
|
use SplTempFileObject; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
|
|
17
|
|
|
final class ExportController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \Netgen\InformationCollection\API\Service\Exporter |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $exporter; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* \Netgen\InformationCollection\API\Service\Exporter constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Exporter $exporter |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(Exporter $exporter) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->exporter = $exporter; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Handles export |
|
36
|
|
|
* |
|
37
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Content $content |
|
38
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
39
|
|
|
* |
|
40
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response |
|
41
|
|
|
*/ |
|
42
|
|
|
public function exportAction(Content $content, Request $request): Response |
|
43
|
|
|
{ |
|
44
|
|
|
$attribute = new Attribute('infocollector', 'export'); |
|
45
|
|
|
$this->denyAccessUnlessGranted($attribute); |
|
46
|
|
|
|
|
47
|
|
|
$form = $this->createForm(ExportType::class); |
|
48
|
|
|
$form->handleRequest($request); |
|
49
|
|
|
|
|
50
|
|
|
if ($form->get('cancel') instanceof ClickableInterface && $form->get('cancel')->isClicked()) { |
|
51
|
|
|
return $this->redirect($this->generateUrl('netgen_information_collection.route.admin.overview')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($form->isValid() && $form->get('export') instanceof ClickableInterface && $form->get('export')->isClicked()) { |
|
55
|
|
|
|
|
56
|
|
|
$exportCriteria = new ExportCriteria( |
|
57
|
|
|
$content, |
|
58
|
|
|
$form->getData()['dateFrom'], |
|
59
|
|
|
$form->getData()['dateTo'], |
|
60
|
|
|
$form->getData()['offset'], |
|
61
|
|
|
$form->getData()['limit'] |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$export = $this->exporter->export($exportCriteria); |
|
65
|
|
|
|
|
66
|
|
|
$writer = Writer::createFromFileObject(new SplTempFileObject()); |
|
67
|
|
|
$writer->setDelimiter(","); |
|
68
|
|
|
$writer->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries |
|
69
|
|
|
$writer->setOutputBOM(Writer::BOM_UTF8); //adding the BOM sequence on output |
|
70
|
|
|
$writer->insertOne($export->getHeader()); |
|
71
|
|
|
$writer->insertAll($export->getContents()); |
|
72
|
|
|
|
|
73
|
|
|
$writer->output('export.csv'); |
|
74
|
|
|
return new Response(''); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this->render("@NetgenInformationCollection/admin/export_menu.html.twig", |
|
78
|
|
|
[ |
|
79
|
|
|
'content' => $content, |
|
80
|
|
|
'form' => $form->createView(), |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|