Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

RelationListConverter.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
8
9 use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
10 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
11 use eZ\Publish\SPI\Persistence\Content\FieldValue;
12 use eZ\Publish\SPI\Persistence\Content\Type as ContentType;
13 use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
14 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
15 use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
16 use DOMDocument;
17 use PDO;
18
19 class RelationListConverter implements Converter
20 {
22 private $db;
23
29 public function __construct(DatabaseHandler $db)
30 {
31 $this->db = $db;
32 }
33
40 public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
41 {
42 $doc = new DOMDocument('1.0', 'utf-8');
43 $root = $doc->createElement('related-objects');
44 $doc->appendChild($root);
45
46 $relationList = $doc->createElement('relation-list');
47 $data = $this->getRelationXmlHashFromDB($value->data['destinationContentIds']);
48 $priority = 0;
49
50 foreach ($value->data['destinationContentIds'] as $id) {
51 if (!isset($data[$id][0])) {
52 // Ignore deleted content items (we can't throw as it would block ContentService->createContentDraft())
53 continue;
54 }
55 $row = $data[$id][0];
56 $row['ezcontentobject_id'] = $id;
57 $row['priority'] = (++$priority);
58
59 $relationItem = $doc->createElement('relation-item');
60 foreach (self::dbAttributeMap() as $domAttrKey => $propertyKey) {
61 if (!isset($row[$propertyKey])) {
62 // left join data missing, ignore the given attribute (content in trash missing location)
63 continue;
64 }
65
66 $relationItem->setAttribute($domAttrKey, $row[$propertyKey]);
67 }
68 $relationList->appendChild($relationItem);
69 unset($relationItem);
70 }
71
72 $root->appendChild($relationList);
73 $doc->appendChild($root);
74
75 $storageFieldValue->dataText = $doc->saveXML();
76 $storageFieldValue->sortKeyString = $value->sortKey;
77 }
78
85 public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
86 {
87 $fieldValue->data = ['destinationContentIds' => []];
88 if ($value->dataText === null) {
89 return;
90 }
91
92 $priorityByContentId = [];
93
94 $dom = new DOMDocument('1.0', 'utf-8');
95 if ($dom->loadXML($value->dataText) === true) {
96 foreach ($dom->getElementsByTagName('relation-item') as $relationItem) {
97 /* @var \DOMElement $relationItem */
98 $priorityByContentId[$relationItem->getAttribute('contentobject-id')] =
99 $relationItem->getAttribute('priority');
100 }
101 }
102
103 asort($priorityByContentId, SORT_NUMERIC);
104
105 $fieldValue->data['destinationContentIds'] = array_keys($priorityByContentId);
106 $fieldValue->sortKey = $value->sortKeyString;
107 }
108
115 public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
116 {
117 $fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings;
118 $validators = $fieldDef->fieldTypeConstraints->validators;
119 $doc = new DOMDocument('1.0', 'utf-8');
120 $root = $doc->createElement('related-objects');
121 $doc->appendChild($root);
122
123 $constraints = $doc->createElement('constraints');
124 if (!empty($fieldSettings['selectionContentTypes'])) {
125 foreach ($fieldSettings['selectionContentTypes'] as $typeIdentifier) {
126 $allowedClass = $doc->createElement('allowed-class');
127 $allowedClass->setAttribute('contentclass-identifier', $typeIdentifier);
128 $constraints->appendChild($allowedClass);
129 unset($allowedClass);
130 }
131 }
132 $root->appendChild($constraints);
133
134 $type = $doc->createElement('type');
135 $type->setAttribute('value', 2); //Deprecated advance object relation list type, set since 4.x does
136 $root->appendChild($type);
137
138 $objectClass = $doc->createElement('object_class');
139 $objectClass->setAttribute('value', ''); //Deprecated advance object relation class type, set since 4.x does
140 $root->appendChild($objectClass);
141
142 $selectionType = $doc->createElement('selection_type');
143 if (isset($fieldSettings['selectionMethod'])) {
144 $selectionType->setAttribute('value', (int)$fieldSettings['selectionMethod']);
145 } else {
146 $selectionType->setAttribute('value', 0);
147 }
148 $root->appendChild($selectionType);
149
150 $defaultLocation = $doc->createElement('contentobject-placement');
151 if (!empty($fieldSettings['selectionDefaultLocation'])) {
152 $defaultLocation->setAttribute('node-id', (int)$fieldSettings['selectionDefaultLocation']);
153 }
154 $root->appendChild($defaultLocation);
155
156 $selectionLimit = $doc->createElement('selection_limit');
157 if (isset($validators['RelationListValueValidator']['selectionLimit'])) {
158 $selectionLimit->setAttribute('value', (int)$validators['RelationListValueValidator']['selectionLimit']);
159 } else {
160 $selectionLimit->setAttribute('value', 0);
161 }
162 $root->appendChild($selectionLimit);
163
164 $doc->appendChild($root);
165 $storageDef->dataText5 = $doc->saveXML();
166 }
167
197 public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
198 {
199 // default settings
200 $fieldDef->fieldTypeConstraints->fieldSettings = [
201 'selectionMethod' => 0,
202 'selectionDefaultLocation' => null,
203 'selectionContentTypes' => [],
204 ];
205
206 $fieldDef->fieldTypeConstraints->validators = [
207 'RelationListValueValidator' => [
208 'selectionLimit' => 0,
209 ],
210 ];
211
212 // default value
213 $fieldDef->defaultValue = new FieldValue();
214 $fieldDef->defaultValue->data = ['destinationContentIds' => []];
215
216 if ($storageDef->dataText5 === null) {
217 return;
218 }
219
220 $dom = new DOMDocument('1.0', 'utf-8');
221 if (empty($storageDef->dataText5) || $dom->loadXML($storageDef->dataText5) !== true) {
222 return;
223 }
224
225 // read settings from storage
226 $fieldSettings = &$fieldDef->fieldTypeConstraints->fieldSettings;
227 if (
228 ($selectionType = $dom->getElementsByTagName('selection_type')->item(0)) &&
229 $selectionType->hasAttribute('value')
230 ) {
231 $fieldSettings['selectionMethod'] = (int)$selectionType->getAttribute('value');
232 }
233
234 if (
235 ($defaultLocation = $dom->getElementsByTagName('contentobject-placement')->item(0)) &&
236 $defaultLocation->hasAttribute('node-id')
237 ) {
238 $fieldSettings['selectionDefaultLocation'] = (int)$defaultLocation->getAttribute('node-id');
239 }
240
241 if (!($constraints = $dom->getElementsByTagName('constraints'))) {
242 return;
243 }
244
245 foreach ($constraints->item(0)->getElementsByTagName('allowed-class') as $allowedClass) {
246 $fieldSettings['selectionContentTypes'][] = $allowedClass->getAttribute('contentclass-identifier');
247 }
248
249 // read validators configuration from storage
250 $validators = &$fieldDef->fieldTypeConstraints->validators;
251 if (
252 ($selectionLimit = $dom->getElementsByTagName('selection_limit')->item(0)) &&
253 $selectionLimit->hasAttribute('value')
254 ) {
255 $validators['RelationListValueValidator']['selectionLimit'] = (int)$selectionLimit->getAttribute('value');
256 }
257 }
258
268 public function getIndexColumn()
269 {
270 return 'sort_key_string';
271 }
272
280 protected function getRelationXmlHashFromDB(array $destinationContentIds)
281 {
282 if (empty($destinationContentIds)) {
283 return [];
284 }
285
286 $q = $this->db->createSelectQuery();
287 $q
288 ->select(
289 $this->db->aliasedColumn($q, 'id', 'ezcontentobject'),
290 $this->db->aliasedColumn($q, 'remote_id', 'ezcontentobject'),
291 $this->db->aliasedColumn($q, 'current_version', 'ezcontentobject'),
292 $this->db->aliasedColumn($q, 'contentclass_id', 'ezcontentobject'),
293 $this->db->aliasedColumn($q, 'node_id', 'ezcontentobject_tree'),
294 $this->db->aliasedColumn($q, 'parent_node_id', 'ezcontentobject_tree'),
295 $this->db->aliasedColumn($q, 'identifier', 'ezcontentclass')
296 )
297 ->from($this->db->quoteTable('ezcontentobject'))
298 ->leftJoin(
299 $this->db->quoteTable('ezcontentobject_tree'),
300 $q->expr->lAnd(
301 $q->expr->eq(
302 $this->db->quoteColumn('contentobject_id', 'ezcontentobject_tree'),
303 $this->db->quoteColumn('id', 'ezcontentobject')
304 ),
305 $q->expr->eq(
306 $this->db->quoteColumn('node_id', 'ezcontentobject_tree'),
307 $this->db->quoteColumn('main_node_id', 'ezcontentobject_tree')
308 )
309 )
310 )
311 ->leftJoin(
312 $this->db->quoteTable('ezcontentclass'),
313 $q->expr->lAnd(
314 $q->expr->eq(
315 $this->db->quoteColumn('id', 'ezcontentclass'),
316 $this->db->quoteColumn('contentclass_id', 'ezcontentobject')
317 ),
318 $q->expr->eq(
319 $this->db->quoteColumn('version', 'ezcontentclass'),
320 $q->bindValue(ContentType::STATUS_DEFINED, null, PDO::PARAM_INT)
321 )
322 )
323 )
324 ->where(
325 $q->expr->in(
326 $this->db->quoteColumn('id', 'ezcontentobject'),
327 $destinationContentIds
328 )
329 );
330 $stmt = $q->prepare();
331 $stmt->execute();
332
333 return $stmt->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP);
334 }
335
339 private static function dbAttributeMap()
340 {
341 return [
342 // 'identifier' => 'identifier',// not used
343 'priority' => 'priority',
344 // 'in-trash' => 'in_trash',// false by default and implies
345 'contentobject-id' => 'ezcontentobject_id',
346 'contentobject-version' => 'ezcontentobject_current_version',
347 'node-id' => 'ezcontentobject_tree_node_id',
348 'parent-node-id' => 'ezcontentobject_tree_parent_node_id',
349 'contentclass-id' => 'ezcontentobject_contentclass_id',
350 'contentclass-identifier' => 'ezcontentclass_identifier',
351 // 'is-modified' => 'is_modified',// deprecated and not used
352 'contentobject-remote-id' => 'ezcontentobject_remote_id',
353 ];
354 }
355 }
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter
Definition: RelationListConverter.php:20
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter\toFieldDefinition
toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
Converts field definition data in $storageDef into $fieldDef.
Definition: RelationListConverter.php:197
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter\getRelationXmlHashFromDB
getRelationXmlHashFromDB(array $destinationContentIds)
Definition: RelationListConverter.php:280
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter\toFieldValue
toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
Converts data from $value to $fieldValue.
Definition: RelationListConverter.php:85
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter\toStorageFieldDefinition
toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
Converts field definition data in $fieldDef into $storageFieldDef.
Definition: RelationListConverter.php:115
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter\__construct
__construct(DatabaseHandler $db)
Create instance of RelationList converter.
Definition: RelationListConverter.php:29
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter\$db
$db
Definition: RelationListConverter.php:22
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter\getIndexColumn
getIndexColumn()
Returns the name of the index column in the attribute table.
Definition: RelationListConverter.php:268
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter\dbAttributeMap
static dbAttributeMap()
Definition: RelationListConverter.php:339
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationListConverter\toStorageValue
toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
Converts data from $value to $storageFieldValue.
Definition: RelationListConverter.php:40
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition
Definition: StorageFieldDefinition.php:12
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue
Definition: StorageFieldValue.php:12
eZ\Publish\SPI\Persistence\Content\FieldValue
Definition: FieldValue.php:12
eZ\Publish\SPI\Persistence\Content\FieldValue\$data
$data
Definition: FieldValue.php:23
eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
Definition: SPI/Persistence/Content/Type/FieldDefinition.php:18
eZ\Publish\Core\Persistence\Database\DatabaseHandler
Interface for relational database abstractions supported by eZ.
Definition: DatabaseHandler.php:18
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter
Converter for field values in legacy storage.
Definition: Persistence/Legacy/Content/FieldValue/Converter.php:18
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter
Definition: AuthorConverter.php:7
eZ\Publish\SPI\Persistence\Content\Type
Definition: SPI/Persistence/Content/Type/CreateStruct.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.