1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Service; |
6
|
|
|
|
7
|
|
|
use Netgen\InformationCollection\API\Service\Exporter; |
8
|
|
|
use Netgen\InformationCollection\API\Service\InformationCollection; |
9
|
|
|
use Netgen\InformationCollection\API\Value\Attribute; |
10
|
|
|
use Netgen\InformationCollection\API\Value\Export\Export; |
11
|
|
|
use Netgen\InformationCollection\API\Value\Export\ExportCriteria; |
12
|
|
|
use Netgen\InformationCollection\Core\Persistence\ContentTypeUtils; |
13
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
14
|
|
|
|
15
|
|
|
class ExporterService implements Exporter |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Symfony\Component\Translation\TranslatorInterface |
19
|
|
|
*/ |
20
|
|
|
protected $translator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Netgen\InformationCollection\Core\Persistence\ContentTypeUtils |
24
|
|
|
*/ |
25
|
|
|
protected $contentTypeUtils; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Netgen\InformationCollection\API\Service\InformationCollection |
29
|
|
|
*/ |
30
|
|
|
protected $informationCollection; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
InformationCollection $informationCollection, |
34
|
|
|
TranslatorInterface $translator, |
35
|
|
|
ContentTypeUtils $contentTypeUtils |
36
|
|
|
) { |
37
|
|
|
$this->translator = $translator; |
38
|
|
|
$this->contentTypeUtils = $contentTypeUtils; |
39
|
|
|
$this->informationCollection = $informationCollection; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function export(ExportCriteria $criteria): Export |
46
|
|
|
{ |
47
|
|
|
$fields = $this->contentTypeUtils |
48
|
|
|
->getInfoCollectorFields($criteria->getContent()->id); |
49
|
|
|
|
50
|
|
|
$fields['created'] = $this->translator->trans('netgen_information_collection_admin_export_created', [], 'netgen_information_collection_admin'); |
51
|
|
|
|
52
|
|
|
$collections = $this->informationCollection->getCollections($criteria->getContent()->id); |
53
|
|
|
|
54
|
|
|
$rows = []; |
55
|
|
|
|
56
|
|
|
foreach ($collections->getCollections() as $collection) { |
57
|
|
|
$row = []; |
58
|
|
|
|
59
|
|
|
foreach ($fields as $fieldId => $fieldName) { |
60
|
|
|
if ($fieldId === 'created') { |
61
|
|
|
$row[] = $collection->getCreated()->format('d-m-Y'); |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$row[] = $this->getAttributeValue($fieldId, $collection->getAttributes()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$rows[] = $row; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$header = array_values($fields); |
72
|
|
|
|
73
|
|
|
return new Export($header, $rows); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get attribute value string. |
78
|
|
|
* |
79
|
|
|
* @param int $fieldId |
80
|
|
|
* @param array $attributes |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function getAttributeValue($fieldId, $attributes) |
85
|
|
|
{ |
86
|
|
|
/** @var Attribute $attribute */ |
87
|
|
|
foreach ($attributes as $attribute) { |
88
|
|
|
if ($fieldId === $attribute->getFieldDefinition()->id) { |
89
|
|
|
$value = $attribute->getValue(); |
90
|
|
|
$value = str_replace('"', '""', $value); |
91
|
|
|
$value = str_replace(';', ', ', $value); |
92
|
|
|
$value = strip_tags($value); |
93
|
|
|
|
94
|
|
|
return preg_replace(['/\r|\n/'], [' '], $value); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return ''; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|