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\API\Value\Filter\ContentId; |
13
|
|
|
use Netgen\InformationCollection\Core\Persistence\ContentTypeUtils; |
14
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
15
|
|
|
|
16
|
|
|
class ExporterService implements Exporter |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Symfony\Component\Translation\TranslatorInterface |
20
|
|
|
*/ |
21
|
|
|
protected $translator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Netgen\InformationCollection\Core\Persistence\ContentTypeUtils |
25
|
|
|
*/ |
26
|
|
|
protected $contentTypeUtils; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Netgen\InformationCollection\API\Service\InformationCollection |
30
|
|
|
*/ |
31
|
|
|
protected $informationCollection; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
InformationCollection $informationCollection, |
35
|
|
|
TranslatorInterface $translator, |
36
|
|
|
ContentTypeUtils $contentTypeUtils |
37
|
|
|
) { |
38
|
|
|
$this->translator = $translator; |
|
|
|
|
39
|
|
|
$this->contentTypeUtils = $contentTypeUtils; |
40
|
|
|
$this->informationCollection = $informationCollection; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function export(ExportCriteria $criteria): Export |
47
|
|
|
{ |
48
|
|
|
$fields = $this->contentTypeUtils |
49
|
|
|
->getInfoCollectorFields($criteria->getContent()->id); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$fields['created'] = $this->translator->trans('netgen_information_collection_admin_export_created', [], 'netgen_information_collection_admin'); |
52
|
|
|
|
53
|
|
|
$collections = $this->informationCollection->getCollections( |
54
|
|
|
new ContentId($criteria->getContent()->id, $criteria->getOffset(), $criteria->getLimit()) |
|
|
|
|
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$rows = []; |
58
|
|
|
|
59
|
|
|
foreach ($collections->getCollections() as $collection) { |
60
|
|
|
$row = []; |
61
|
|
|
|
62
|
|
|
foreach ($fields as $fieldId => $fieldName) { |
63
|
|
|
if ($fieldId === 'created') { |
64
|
|
|
$row[] = $collection->getCreated()->format('d-m-Y'); |
65
|
|
|
|
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$row[] = $this->getAttributeValue((int)$fieldId, $collection->getAttributes()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$rows[] = $row; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$header = array_values($fields); |
76
|
|
|
|
77
|
|
|
return new Export($header, $rows); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get attribute value string. |
82
|
|
|
* |
83
|
|
|
* @param int $fieldId |
84
|
|
|
* @param array $attributes |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
protected function getAttributeValue(int $fieldId, array $attributes) |
89
|
|
|
{ |
90
|
|
|
/** @var Attribute $attribute */ |
91
|
|
|
foreach ($attributes as $attribute) { |
92
|
|
|
if ($fieldId === $attribute->getFieldDefinition()->id) { |
93
|
|
|
$value = $attribute->getValue(); |
94
|
|
|
$value = str_replace('"', '""', (string)$value); |
95
|
|
|
$value = str_replace(';', ', ', (string)$value); |
96
|
|
|
$value = strip_tags($value); |
97
|
|
|
|
98
|
|
|
$res = preg_replace(['/\r|\n/'], [' '], $value); |
99
|
|
|
|
100
|
|
|
return $res ?? ''; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return ''; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..