| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * File containing the Relation converter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (C) eZ Systems AS. All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license For full copyright and license information view LICENSE file distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use DOMDocument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use eZ\Publish\SPI\Persistence\Content\FieldValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class RelationConverter implements Converter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Factory for current class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Note: Class should instead be configured as service if it gains dependencies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @deprecated since 6.8, will be removed in 7.x, use default constructor instead. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\RelationConverter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public static function create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return new self(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Converts data from $value to $storageFieldValue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 40 |  | View Code Duplication |     public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $storageFieldValue->dataInt = !empty($value->data['destinationContentId']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ? $value->data['destinationContentId'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $storageFieldValue->sortKeyInt = (int)$value->sortKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Converts data from $value to $fieldValue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 54 |  | View Code Duplication |     public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $fieldValue->data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             'destinationContentId' => $value->dataInt ?: null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $fieldValue->sortKey = (int)$value->sortKeyInt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Converts field definition data in $fieldDef into $storageFieldDef. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef) | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         $doc = new DOMDocument('1.0', 'utf-8'); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         $root = $doc->createElement('related-objects'); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $doc->appendChild($root); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $constraints = $doc->createElement('constraints'); | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 76 |  | View Code Duplication |         if (!empty($fieldSettings['selectionContentTypes'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             foreach ($fieldSettings['selectionContentTypes'] as $typeIdentifier) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 $allowedClass = $doc->createElement('allowed-class'); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 $allowedClass->setAttribute('contentclass-identifier', $typeIdentifier); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 $constraints->appendChild($allowedClass); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 unset($allowedClass); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $root->appendChild($constraints); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $selectionType = $doc->createElement('selection_type'); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         $selectionMethod = isset($fieldSettings['selectionMethod']) ? (int)$fieldSettings['selectionMethod'] : 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         $selectionType->setAttribute('value', $selectionMethod); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         $root->appendChild($selectionType); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $defaultLocation = $doc->createElement('contentobject-placement'); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         if (!empty($fieldSettings['selectionRoot'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             $defaultLocation->setAttribute('node-id', (int)$fieldSettings['selectionRoot']); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $root->appendChild($defaultLocation); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         $doc->appendChild($root); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         $storageDef->dataText5 = $doc->saveXML(); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         // BC: For Backwards Compatibility for legacy and in case of downgrades or data sharing | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         // Selection method, 0 = browse, 1 = dropdown | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         $storageDef->dataInt1 = $selectionMethod; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         // Selection root, location ID, or 0 if empty | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         $storageDef->dataInt2 = (int)$fieldSettings['selectionRoot']; | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * Converts field definition data in $storageDef into $fieldDef. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * <code> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      *   <?xml version="1.0" encoding="utf-8"?> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      *   <related-objects> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      *     <constraints> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      *       <allowed-class contentclass-identifier="blog_post"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      *     </constraints> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      *     <selection_type value="1"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      *     <contentobject-placement node-id="67"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      *   </related-objects> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      *   <?xml version="1.0" encoding="utf-8"?> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      *   <related-objects> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      *     <constraints/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      *     <selection_type value="0"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      *     <contentobject-placement/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      *   </related-objects> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * </code> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         // default settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         // use dataInt1 and dataInt2 fields as default for backward compatibility | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $fieldDef->fieldTypeConstraints->fieldSettings = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             'selectionMethod' => $storageDef->dataInt1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             'selectionRoot' => $storageDef->dataInt2 === 0 ? '' : $storageDef->dataInt2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             'selectionContentTypes' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         if ($storageDef->dataText5 === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         // read settings from storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $fieldSettings = &$fieldDef->fieldTypeConstraints->fieldSettings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $dom = new DOMDocument('1.0', 'utf-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         if (empty($storageDef->dataText5) || $dom->loadXML($storageDef->dataText5) !== true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 153 |  | View Code Duplication |         if ( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             ($selectionType = $dom->getElementsByTagName('selection_type')) && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             $selectionType->item(0)->hasAttribute('value') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $fieldSettings['selectionMethod'] = (int)$selectionType->item(0)->getAttribute('value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 160 |  | View Code Duplication |         if ( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             ($defaultLocation = $dom->getElementsByTagName('contentobject-placement')) && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             $defaultLocation->item(0)->hasAttribute('node-id') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             $fieldSettings['selectionRoot'] = (int)$defaultLocation->item(0)->getAttribute('node-id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         if (!($constraints = $dom->getElementsByTagName('constraints'))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         foreach ($constraints->item(0)->getElementsByTagName('allowed-class') as $allowedClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             $fieldSettings['selectionContentTypes'][] = $allowedClass->getAttribute('contentclass-identifier'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * Returns the name of the index column in the attribute table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * Returns the name of the index column the datatype uses, which is either | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * "sort_key_int" or "sort_key_string". This column is then used for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * filtering and sorting for this type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     public function getIndexColumn() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         return 'sort_key_int'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 189 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 190 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.