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

PageConverter.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\FieldDefinition;
13 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
14 use eZ\Publish\Core\FieldType\FieldSettings;
15 use eZ\Publish\Core\FieldType\Page\Parts;
16 use DOMDocument;
17 use DOMElement;
18
19 class PageConverter implements Converter
20 {
27 public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
28 {
29 $storageFieldValue->dataText = $value->data === null
30 ? null
31 : $this->generateXmlString($value->data);
32 }
33
40 public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
41 {
42 $fieldValue->data = $value->dataText === null
43 ? null
44 : $this->restoreValueFromXmlString($value->dataText);
45 }
46
53 public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
54 {
55 $storageDef->dataText1 = (isset($fieldDef->fieldTypeConstraints->fieldSettings['defaultLayout'])
56 ? $fieldDef->fieldTypeConstraints->fieldSettings['defaultLayout']
57 : '');
58 }
59
66 public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
67 {
68 $fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings(
69 [
70 'defaultLayout' => $storageDef->dataText1,
71 ]
72 );
73 }
74
84 public function getIndexColumn()
85 {
86 return false;
87 }
88
96 public function generateXmlString(Parts\Page $page)
97 {
98 $dom = new DOMDocument('1.0', 'utf-8');
99 $dom->formatOutput = true;
100 $dom->loadXML('<page />');
101
102 $pageNode = $dom->documentElement;
103
104 foreach ($page->getState() as $attrName => $attrValue) {
105 switch ($attrName) {
106 case 'id':
107 $pageNode->setAttribute('id', $attrValue);
108 break;
109 case 'zones':
110 foreach ($page->{$attrName} as $zone) {
111 $pageNode->appendChild($this->generateZoneXmlString($zone, $dom));
112 }
113 break;
114 case 'layout':
115 $node = $dom->createElement('zone_layout', $attrValue);
116 $pageNode->appendChild($node);
117 break;
118 case 'attributes':
119 foreach ($attrValue as $arrayItemKey => $arrayItemValue) {
120 $this->addNewXmlElement($dom, $pageNode, $arrayItemKey, $arrayItemValue);
121 }
122 break;
123 case 'zonesById':
124 // Do not store
125 break;
126 default:
127 $this->addNewNotEmptyXmlElement($dom, $pageNode, $attrName, $attrValue);
128 break;
129 }
130 }
131
132 return $dom->saveXML();
133 }
134
143 protected function generateZoneXmlString(Parts\Zone $zone, DOMDocument $dom)
144 {
145 $zoneNode = $dom->createElement('zone');
146 foreach ($zone->getState() as $attrName => $attrValue) {
147 switch ($attrName) {
148 case 'id':
149 $zoneNode->setAttribute('id', 'id_' . $attrValue);
150 break;
151 case 'action':
152 if ($attrValue !== null) {
153 $zoneNode->setAttribute('action', $attrValue);
154 }
155 break;
156 case 'identifier':
157 $this->addNewXmlElement($dom, $zoneNode, 'zone_identifier', $attrValue);
158 break;
159 case 'blocks':
160 foreach ($zone->{$attrName} as $block) {
161 $zoneNode->appendChild($this->generateBlockXmlString($block, $dom));
162 }
163 break;
164 case 'attributes':
165 foreach ($attrValue as $arrayItemKey => $arrayItemValue) {
166 $this->addNewXmlElement($dom, $zoneNode, $arrayItemKey, $arrayItemValue);
167 }
168 break;
169 case 'blocksById':
170 // Do not store
171 break;
172 default:
173 $this->addNewNotEmptyXmlElement($dom, $zoneNode, $attrName, $attrValue);
174 break;
175 }
176 }
177
178 return $zoneNode;
179 }
180
189 protected function generateBlockXmlString(Parts\Block $block, DOMDocument $dom)
190 {
191 $blockNode = $dom->createElement('block');
192
193 foreach ($block->getState() as $attrName => $attrValue) {
194 switch ($attrName) {
195 case 'id':
196 $blockNode->setAttribute('id', 'id_' . $attrValue);
197 break;
198 case 'zoneId':
199 $blockNode->appendChild($dom->createElement('zone_id', $attrValue));
200 break;
201 case 'action':
202 if ($attrValue !== null) {
203 $blockNode->setAttribute('action', $attrValue);
204 }
205 break;
206 case 'items':
207 foreach ($block->items as $item) {
208 $itemNode = $this->generateItemXmlString($item, $dom);
209 if ($itemNode) {
210 $blockNode->appendChild($itemNode);
211 }
212 }
213 break;
214 case 'overflowId':
215 $this->addNewXmlElement($dom, $blockNode, 'overflow_id', $attrValue);
216 break;
217 case 'rotation':
218 if ($attrValue === null) {
219 continue 2;
220 }
221
222 $node = $dom->createElement($attrName);
223 $blockNode->appendChild($node);
224
225 foreach ($attrValue as $arrayItemKey => $arrayItemValue) {
226 $this->addNewXmlElement($dom, $node, $arrayItemKey, $arrayItemValue);
227 }
228 break;
229 case 'customAttributes':
230 if ($attrValue === null) {
231 continue 2;
232 }
233
234 $node = $dom->createElement('custom_attributes');
235 $blockNode->appendChild($node);
236
237 foreach ($attrValue as $arrayItemKey => $arrayItemValue) {
238 $this->addNewXmlElement($dom, $node, $arrayItemKey, $arrayItemValue);
239 }
240 break;
241 case 'attributes':
242 foreach ($attrValue as $arrayItemKey => $arrayItemValue) {
243 $this->addNewXmlElement($dom, $blockNode, $arrayItemKey, $arrayItemValue);
244 }
245 break;
246 default:
247 $this->addNewNotEmptyXmlElement($dom, $blockNode, $attrName, $attrValue);
248 break;
249 }
250 }
251
252 return $blockNode;
253 }
254
263 protected function generateItemXmlString(Parts\Item $item, DOMDocument $dom)
264 {
265 $itemNode = $dom->createElement('item');
266
267 foreach ($item->getState() as $attrName => $attrValue) {
268 switch ($attrName) {
269 case 'action':
270 if ($attrValue !== null) {
271 $itemNode->setAttribute('action', $attrValue);
272 }
273 break;
274 case 'contentId':
275 $this->addNewNotEmptyXmlElement($dom, $itemNode, 'object_id', $attrValue);
276 break;
277 case 'locationId':
278 $this->addNewNotEmptyXmlElement($dom, $itemNode, 'node_id', $attrValue);
279 break;
280 case 'priority':
281 $this->addNewNotEmptyXmlElement($dom, $itemNode, 'priority', $attrValue);
282 break;
283 case 'publicationDate':
284 $this->addNewNotEmptyXmlElement($dom, $itemNode, 'ts_publication', $attrValue);
285 break;
286 case 'visibilityDate':
287 $this->addNewNotEmptyXmlElement($dom, $itemNode, 'ts_visible', $attrValue);
288 break;
289 case 'hiddenDate':
290 $this->addNewNotEmptyXmlElement($dom, $itemNode, 'ts_hidden', $attrValue);
291 break;
292 case 'rotationUntilDate':
293 $this->addNewNotEmptyXmlElement($dom, $itemNode, 'rotation_until', $attrValue);
294 break;
295 case 'movedTo':
296 $this->addNewNotEmptyXmlElement($dom, $itemNode, 'moved_to', $attrValue);
297 break;
298 case 'attributes':
299 foreach ($attrValue as $arrayItemKey => $arrayItemValue) {
300 $this->addNewNotEmptyXmlElement($dom, $itemNode, $arrayItemKey, $arrayItemValue);
301 }
302 break;
303 }
304 }
305
306 return $itemNode;
307 }
308
317 private function addNewNotEmptyXmlElement(DOMDocument $dom, DOMElement $node, $name, $value)
318 {
319 if (!empty($value)) {
320 $this->addNewXmlElement($dom, $node, $name, $value);
321 }
322 }
323
332 private function addNewXmlElement(DOMDocument $dom, DOMElement $node, $name, $value)
333 {
334 $new = $dom->createElement($name);
335 $new->appendChild($dom->createTextNode($value));
336 $node->appendChild($new);
337 }
338
346 public function restoreValueFromXmlString($xmlString)
347 {
348 $zones = [];
349 $attributes = [];
350 $layout = null;
351
352 if ($xmlString) {
353 $dom = new DOMDocument('1.0', 'utf-8');
354 $dom->loadXML($xmlString);
355 $root = $dom->documentElement;
356
357 foreach ($root->childNodes as $node) {
358 if ($node->nodeType !== XML_ELEMENT_NODE) {
359 continue;
360 }
361
362 switch ($node->nodeName) {
363 case 'zone':
364 $zone = $this->restoreZoneFromXml($node);
365 $zones[] = $zone;
366 break;
367 case 'zone_layout':
368 $layout = $node->nodeValue;
369 break;
370 default:
371 $attributes[$node->nodeName] = $node->nodeValue;
372 break;
373 }
374 }
375
376 if ($root->hasAttributes()) {
377 foreach ($root->attributes as $attr) {
378 $attributes[$attr->name] = $attr->value;
379 }
380 }
381 }
382
383 return new Parts\Page(
384 [
385 'zones' => $zones,
386 'layout' => $layout,
387 'attributes' => $attributes,
388 ]
389 );
390 }
391
399 protected function restoreZoneFromXml(DOMElement $node)
400 {
401 $zoneId = null;
402 $zoneIdentifier = null;
403 $action = null;
404 $blocks = [];
405 $attributes = [];
406
407 if ($node->hasAttributes()) {
408 foreach ($node->attributes as $attr) {
409 switch ($attr->name) {
410 case 'id':
411 // Stored Id has following format : id_<zoneId>, so extract <zoneId>
412 $zoneId = substr(
413 $attr->value,
414 strpos($attr->value, '_') + 1
415 );
416 break;
417 case 'action':
418 $action = $attr->value;
419 break;
420 default:
421 $attributes[$attr->name] = $attr->value;
422 }
423 }
424 }
425
426 foreach ($node->childNodes as $node) {
427 if ($node->nodeType !== XML_ELEMENT_NODE) {
428 continue;
429 }
430
431 switch ($node->nodeName) {
432 case 'block':
433 $block = $this->restoreBlockFromXml($node);
434 $blocks[] = $block;
435 break;
436 case 'zone_identifier':
437 $zoneIdentifier = $node->nodeValue;
438 break;
439 default:
440 $attributes[$node->nodeName] = $node->nodeValue;
441 }
442 }
443
444 return new Parts\Zone(
445 [
446 'id' => $zoneId,
447 'identifier' => $zoneIdentifier,
448 'attributes' => $attributes,
449 'action' => $action,
450 'blocks' => $blocks,
451 ]
452 );
453 }
454
462 protected function restoreBlockFromXml(DOMElement $node)
463 {
464 $blockId = null;
465 $items = [];
466 $rotation = null;
467 $customAttributes = null;
468 $attributes = [];
469 $name = null;
470 $type = null;
471 $view = null;
472 $overflowId = null;
473 $action = null;
474 $zoneId = null;
475
476 if ($node->hasAttributes()) {
477 foreach ($node->attributes as $attr) {
478 switch ($attr->name) {
479 case 'id':
480 // Stored Id has following format : id_<blockId>, so extract <blockId>
481 $blockId = substr(
482 $attr->value,
483 strpos($attr->value, '_') + 1
484 );
485 break;
486 case 'action':
487 $action = $attr->value;
488 break;
489 default:
490 $attributes[$attr->name] = $attr->value;
491 }
492 }
493 }
494
495 foreach ($node->childNodes as $node) {
496 if ($node->nodeType !== XML_ELEMENT_NODE) {
497 continue;
498 }
499
500 switch ($node->nodeName) {
501 case 'item':
502 $items[] = $this->restoreItemFromXml($node);
503 break;
504 case 'rotation':
505 if ($rotation === null) {
506 $rotation = [];
507 }
508
509 foreach ($node->childNodes as $subNode) {
510 if ($subNode->nodeType !== XML_ELEMENT_NODE) {
511 continue;
512 }
513
514 $rotation[$subNode->nodeName] = $subNode->nodeValue;
515 }
516 break;
517 case 'custom_attributes':
518 if ($customAttributes === null) {
519 $customAttributes = [];
520 }
521
522 foreach ($node->childNodes as $subNode) {
523 if ($subNode->nodeType !== XML_ELEMENT_NODE) {
524 continue;
525 }
526
527 $customAttributes[$subNode->nodeName] = $subNode->nodeValue;
528 }
529 break;
530 case 'name':
531 case 'type':
532 case 'view':
533 ${$node->nodeName} = $node->nodeValue;
534 break;
535 case 'overflow_id':
536 $overflowId = $node->nodeValue;
537 break;
538 case 'zone_id':
539 $zoneId = $node->nodeValue;
540 break;
541 default:
542 $attributes[$node->nodeName] = $node->nodeValue;
543 }
544 }
545
546 return new Parts\Block(
547 [
548 'id' => $blockId,
549 'action' => $action,
550 'items' => $items,
551 'rotation' => $rotation,
552 'customAttributes' => $customAttributes,
553 'attributes' => $attributes,
554 'name' => $name,
555 'type' => $type,
556 'view' => $view,
557 'overflowId' => $overflowId,
558 'zoneId' => $zoneId,
559 ]
560 );
561 }
562
570 protected function restoreItemFromXml(DOMElement $node)
571 {
572 $item = ['attributes' => []];
573
574 if ($node->hasAttributes()) {
575 foreach ($node->attributes as $attr) {
576 switch ($attr->name) {
577 case 'action':
578 $item['action'] = $attr->value;
579 break;
580 default:
581 $item['attributes'][$attr->name] = $attr->value;
582 }
583 }
584 }
585
586 foreach ($node->childNodes as $node) {
587 if ($node->nodeType !== XML_ELEMENT_NODE) {
588 continue;
589 }
590
591 switch ($node->nodeName) {
592 case 'object_id':
593 $item['contentId'] = $node->nodeValue;
594 break;
595 case 'node_id':
596 $item['locationId'] = $node->nodeValue;
597 break;
598 case 'priority':
599 $item[$node->nodeName] = $node->nodeValue;
600 break;
601 case 'ts_publication':
602 $item['publicationDate'] = $node->nodeValue;
603 break;
604 case 'ts_visible':
605 $item['visibilityDate'] = $node->nodeValue;
606 break;
607 case 'ts_hidden':
608 $item['hiddenDate'] = $node->nodeValue;
609 break;
610 case 'rotation_until':
611 $item['rotationUntilDate'] = $node->nodeValue;
612 break;
613 case 'moved_to':
614 $item['movedTo'] = $node->nodeValue;
615 break;
616 }
617 }
618
619 return new Parts\Item($item);
620 }
621 }
eZ\Publish\Core\FieldType\FieldSettings
Container for field type specific properties.
Definition: FieldSettings.php:19
eZ\Publish\Core\FieldType\Page\Parts\Block
Definition: FieldType/Page/Parts/Block.php:22
eZ\Publish\Core\FieldType\Page\Parts\Item
Definition: Item.php:24
eZ\Publish\Core\FieldType\Page\Parts\Page
Definition: Publish/Core/FieldType/Page/Parts/Page.php:15
eZ\Publish\Core\FieldType\Page\Parts\Zone
Definition: FieldType/Page/Parts/Zone.php:17
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter
Definition: PageConverter.php:20
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\generateXmlString
generateXmlString(Parts\Page $page)
Generates XML string from $page object to be stored in storage engine.
Definition: PageConverter.php:96
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\toStorageFieldDefinition
toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
Converts field definition data in $fieldDef into $storageFieldDef.
Definition: PageConverter.php:53
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\restoreValueFromXmlString
restoreValueFromXmlString($xmlString)
Restores value from XML string.
Definition: PageConverter.php:346
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\restoreItemFromXml
restoreItemFromXml(DOMElement $node)
Restores value for a given Item $node.
Definition: PageConverter.php:570
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\toFieldValue
toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
Converts data from $value to $fieldValue.
Definition: PageConverter.php:40
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\toFieldDefinition
toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
Converts field definition data in $storageDef into $fieldDef.
Definition: PageConverter.php:66
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\restoreZoneFromXml
restoreZoneFromXml(DOMElement $node)
Restores value for a given Zone $node.
Definition: PageConverter.php:399
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\addNewXmlElement
addNewXmlElement(DOMDocument $dom, DOMElement $node, $name, $value)
Utility method to add new elements to an xml node.
Definition: PageConverter.php:332
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\addNewNotEmptyXmlElement
addNewNotEmptyXmlElement(DOMDocument $dom, DOMElement $node, $name, $value)
Utility method to add new elements to an xml node if their value is not empty.
Definition: PageConverter.php:317
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\restoreBlockFromXml
restoreBlockFromXml(DOMElement $node)
Restores value for a given Block $node.
Definition: PageConverter.php:462
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\generateZoneXmlString
generateZoneXmlString(Parts\Zone $zone, DOMDocument $dom)
Generates XML string for a given $zone object.
Definition: PageConverter.php:143
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\getIndexColumn
getIndexColumn()
Returns the name of the index column in the attribute table.
Definition: PageConverter.php:84
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\generateItemXmlString
generateItemXmlString(Parts\Item $item, DOMDocument $dom)
Generates XML string for a given $item object.
Definition: PageConverter.php:263
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\generateBlockXmlString
generateBlockXmlString(Parts\Block $block, DOMDocument $dom)
Generates XML string for a given $block object.
Definition: PageConverter.php:189
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter\toStorageValue
toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
Converts data from $value to $storageFieldValue.
Definition: PageConverter.php:27
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\Type\FieldDefinition
Definition: SPI/Persistence/Content/Type/FieldDefinition.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\FieldType\Page\Parts
Definition: FieldType/Page/Parts/Base.php:7
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter
Definition: AuthorConverter.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.