|
1
|
|
|
<?php |
|
2
|
|
|
namespace EWW\Dpf\Services\Api; |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of the TYPO3 CMS project. |
|
6
|
|
|
* |
|
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
9
|
|
|
* of the License, or any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please read the |
|
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
13
|
|
|
* |
|
14
|
|
|
* The TYPO3 project - inspiring people to share! |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
use EWW\Dpf\Domain\Model\Document; |
|
18
|
|
|
use EWW\Dpf\Services\ProcessNumber\ProcessNumberGenerator; |
|
19
|
|
|
use JsonPath\JsonObject; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class JsonToDocumentMapper |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* objectManager |
|
25
|
|
|
* |
|
26
|
|
|
* @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
|
27
|
|
|
* @inject |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $objectManager; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* documentTypeRepository |
|
33
|
|
|
* |
|
34
|
|
|
* @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository |
|
35
|
|
|
* @inject |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $documentTypeRepository = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* documentRepository |
|
41
|
|
|
* |
|
42
|
|
|
* @var \EWW\Dpf\Domain\Repository\DocumentRepository |
|
43
|
|
|
* @inject |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $documentRepository = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Replaces the data from the document with the data from the json |
|
49
|
|
|
* @param Document $document |
|
50
|
|
|
* @param $jsonData |
|
51
|
|
|
* @return Document |
|
52
|
|
|
*/ |
|
53
|
|
|
public function editDocument(Document $document, $jsonData) { |
|
54
|
|
|
|
|
55
|
|
|
$metaData = $this->getMetadataFromJson($jsonData, $document->getDocumentType()); |
|
56
|
|
|
$xmlData = $document->getXmlData(); |
|
57
|
|
|
|
|
58
|
|
|
$domDocument = new \DOMDocument(); |
|
59
|
|
|
$domDocument->loadXML($xmlData); |
|
60
|
|
|
|
|
61
|
|
|
$xpath = \EWW\Dpf\Helper\XPath::create($domDocument); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($metaData as $groupKey => $group) { |
|
64
|
|
|
$groupMapping = $group['mapping']; |
|
65
|
|
|
if ($group['values']) { |
|
66
|
|
|
foreach ($group['values'] as $fieldKey => $field) { |
|
67
|
|
|
$nodes = $xpath->query($groupMapping .'/'. $field['mapping']); |
|
68
|
|
|
$nodes->item(0)->nodeValue = $field['value']; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$xmlData = $domDocument->saveXML(); |
|
74
|
|
|
$document->setXmlData($xmlData); |
|
75
|
|
|
|
|
76
|
|
|
return $document; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Creates a document from the given json data |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $jsonData |
|
83
|
|
|
* @return Document $document |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getDocument($jsonData) |
|
86
|
|
|
{ |
|
87
|
|
|
$jsonObject = new JsonObject($jsonData); |
|
88
|
|
|
$publicationType = $jsonObject->get('$.publicationType'); |
|
89
|
|
|
|
|
90
|
|
|
if ($publicationType && is_array($publicationType)) { |
|
91
|
|
|
$publicationType = $publicationType[0]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$documentType = $this->documentTypeRepository->findOneByName($publicationType); |
|
|
|
|
|
|
95
|
|
|
if (!$documentType) { |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** @var Document $document */ |
|
100
|
|
|
$document = $this->objectManager->get(Document::class); |
|
101
|
|
|
|
|
102
|
|
|
$document->setDocumentType($documentType); |
|
103
|
|
|
|
|
104
|
|
|
$processNumberGenerator = $this->objectManager->get(ProcessNumberGenerator::class); |
|
105
|
|
|
$processNumber = $processNumberGenerator->getProcessNumber(); |
|
106
|
|
|
$document->setProcessNumber($processNumber); |
|
107
|
|
|
|
|
108
|
|
|
$metaData = $this->getMetadataFromJson($jsonData); |
|
109
|
|
|
|
|
110
|
|
|
$exporter = new \EWW\Dpf\Services\ParserGenerator(); |
|
111
|
|
|
|
|
112
|
|
|
$documentData['documentUid'] = 0; |
|
|
|
|
|
|
113
|
|
|
$documentData['metadata'] = $metaData; |
|
114
|
|
|
$documentData['files'] = array(); |
|
115
|
|
|
|
|
116
|
|
|
$exporter->buildXmlFromForm($documentData); |
|
117
|
|
|
|
|
118
|
|
|
$internalXml = $exporter->getXMLData(); |
|
119
|
|
|
$document->setXmlData($internalXml); |
|
120
|
|
|
|
|
121
|
|
|
$internalFormat = new \EWW\Dpf\Helper\InternalFormat($internalXml); |
|
122
|
|
|
|
|
123
|
|
|
$document->setTitle($internalFormat->getTitle()); |
|
124
|
|
|
$document->setAuthors($internalFormat->getAuthors()); |
|
125
|
|
|
$document->setDateIssued($internalFormat->getDateIssued()); |
|
126
|
|
|
//$document->setEmbargoDate($formMetaData['embargo']); |
|
127
|
|
|
|
|
128
|
|
|
$internalFormat->setDocumentType($documentType->getName()); |
|
|
|
|
|
|
129
|
|
|
$internalFormat->setProcessNumber($document->getProcessNumber()); |
|
130
|
|
|
|
|
131
|
|
|
$document->setXmlData($internalFormat->getXml()); |
|
132
|
|
|
|
|
133
|
|
|
$document->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_REGISTERED_NONE); |
|
134
|
|
|
|
|
135
|
|
|
return $document; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
public function getMetadataFromJson($jsonData, $documentType = null) |
|
140
|
|
|
{ |
|
141
|
|
|
$jsonObject = new JsonObject($jsonData); |
|
142
|
|
|
|
|
143
|
|
|
if ($documentType) { |
|
144
|
|
|
$publicationType = $documentType; |
|
|
|
|
|
|
145
|
|
|
} else { |
|
146
|
|
|
$publicationType = $jsonObject->get('$.publicationType'); |
|
147
|
|
|
if ($publicationType && is_array($publicationType)) { |
|
148
|
|
|
$publicationType = $publicationType[0]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** @var \EWW\Dpf\Domain\Model\DocumentType $documentType */ |
|
152
|
|
|
$documentType = $this->documentTypeRepository->findOneByName($publicationType); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$resultData = []; |
|
156
|
|
|
|
|
157
|
|
|
if (empty($documentType)) { |
|
158
|
|
|
// default type |
|
159
|
|
|
$documentType = $this->documentTypeRepository->findOneByName('article'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
foreach ($documentType->getMetadataPage() as $metadataPage) { |
|
163
|
|
|
|
|
164
|
|
|
foreach ($metadataPage->getMetadataGroup() as $metadataGroup) { |
|
165
|
|
|
|
|
166
|
|
|
// Group mapping |
|
167
|
|
|
$jsonDataObject = new JsonObject($jsonData); |
|
168
|
|
|
$jsonGroupMapping = $metadataGroup->getJsonMapping(); |
|
169
|
|
|
$groupItems = []; |
|
170
|
|
|
if ($jsonGroupMapping) { |
|
171
|
|
|
$groupItems = $jsonDataObject->get($jsonGroupMapping); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
foreach ($groupItems as $groupItem) { |
|
175
|
|
|
|
|
176
|
|
|
$resultGroup = [ |
|
177
|
|
|
'attributes' => [], |
|
178
|
|
|
'values' => [] |
|
179
|
|
|
]; |
|
180
|
|
|
$resultGroup['mapping'] = $metadataGroup->getRelativeMapping(); |
|
181
|
|
|
$resultGroup['modsExtensionMapping'] = $metadataGroup->getRelativeModsExtensionMapping(); |
|
182
|
|
|
$resultGroup['modsExtensionReference'] = trim($metadataGroup->getModsExtensionReference(), " /"); |
|
183
|
|
|
$resultGroup['groupUid'] = $metadataGroup->getUid(); |
|
184
|
|
|
|
|
185
|
|
|
foreach ($metadataGroup->getMetadataObject() as $metadataObject) { |
|
186
|
|
|
|
|
187
|
|
|
$json = json_encode($groupItem); |
|
188
|
|
|
|
|
189
|
|
|
$jsonObject = new JsonObject($json); |
|
190
|
|
|
$fieldItems = []; |
|
191
|
|
|
$jsonFieldMapping = $metadataObject->getJsonMapping(); |
|
192
|
|
|
|
|
193
|
|
|
if ($jsonFieldMapping) { |
|
194
|
|
|
$fieldItems = $jsonObject->get($jsonFieldMapping); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
foreach ($fieldItems as $fieldItem) { |
|
198
|
|
|
$resultField = []; |
|
199
|
|
|
|
|
200
|
|
|
if (!is_array($fieldItem)) { |
|
201
|
|
|
$value = $fieldItem; |
|
202
|
|
|
} else { |
|
203
|
|
|
$value = implode("; ", $fieldItem); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
if ($metadataObject->getDataType() == \EWW\Dpf\Domain\Model\MetadataObject::INPUT_DATA_TYPE_DATE) { |
|
207
|
|
|
$date = date_create_from_format('d.m.Y', trim($value)); |
|
208
|
|
|
if ($date) { |
|
209
|
|
|
$value = date_format($date, 'Y-m-d'); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
if ($value) { |
|
214
|
|
|
$value = str_replace('"', "'", $value); |
|
215
|
|
|
$fieldMapping = $metadataObject->getRelativeMapping(); |
|
216
|
|
|
$resultField['modsExtension'] = $metadataObject->getModsExtension(); |
|
217
|
|
|
$resultField['mapping'] = $fieldMapping; |
|
218
|
|
|
$resultField['value'] = $value; |
|
219
|
|
|
|
|
220
|
|
|
if (strpos($fieldMapping, "@") === 0) { |
|
221
|
|
|
$resultGroup['attributes'][] = $resultField; |
|
222
|
|
|
} else { |
|
223
|
|
|
$resultGroup['values'][] = $resultField; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
$resultData[] = $resultGroup;; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return $resultData; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths