1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\EnhancedSelectionBundle\Core\Persistence\Legacy\Content\FieldValue\Converter; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use eZ\Publish\Core\FieldType\FieldSettings; |
7
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter; |
8
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition; |
9
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue; |
10
|
|
|
use eZ\Publish\SPI\Persistence\Content\FieldValue; |
11
|
|
|
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition; |
12
|
|
|
|
13
|
|
|
class EnhancedSelectionConverter implements Converter |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Converts data from $value to $storageFieldValue. |
17
|
|
|
* |
18
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value |
19
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue |
20
|
|
|
*/ |
21
|
|
|
public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue) |
22
|
|
|
{ |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Converts data from $value to $fieldValue. |
27
|
|
|
* |
28
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
29
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue |
30
|
|
|
*/ |
31
|
|
|
public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue) |
32
|
|
|
{ |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Converts field definition data in $fieldDef into $storageFieldDef. |
37
|
|
|
* |
38
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef |
39
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef |
40
|
|
|
*/ |
41
|
|
|
public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef) |
42
|
|
|
{ |
43
|
|
|
$fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings; |
44
|
|
|
|
45
|
|
|
$xml = new DOMDocument('1.0', 'utf-8'); |
46
|
|
|
$xml->appendChild( |
47
|
|
|
$selection = $xml->createElement('content') |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
// Options |
51
|
|
|
if (!empty($fieldSettings['options'])) { |
52
|
|
|
$selection->appendChild( |
53
|
|
|
$options = $xml->createElement('options') |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
foreach ($fieldSettings['options'] as $key => $option) { |
57
|
|
|
$options->appendChild( |
58
|
|
|
$optionNode = $xml->createElement('option') |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$optionNode->setAttribute('id', (string) ($key + 1)); |
62
|
|
|
$optionNode->setAttribute('name', (string) $option['name']); |
63
|
|
|
$optionNode->setAttribute('identifier', (string) $option['identifier']); |
64
|
|
|
$optionNode->setAttribute('priority', (string) $option['priority']); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Multiselect |
69
|
|
|
$multiSelectNode = $xml->createElement( |
70
|
|
|
'multiselect', |
71
|
|
|
!empty($fieldSettings['isMultiple']) ? '1' : '0' |
72
|
|
|
); |
73
|
|
|
$selection->appendChild($multiSelectNode); |
74
|
|
|
|
75
|
|
|
// Delimiter |
76
|
|
View Code Duplication |
if (!empty($fieldSettings['delimiter'])) { |
|
|
|
|
77
|
|
|
$delimiterElement = $xml->createElement('delimiter'); |
78
|
|
|
$delimiterElement->appendChild($xml->createCDATASection($fieldSettings['delimiter'])); |
79
|
|
|
$selection->appendChild($delimiterElement); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// DB query |
83
|
|
View Code Duplication |
if (!empty($fieldSettings['query'])) { |
|
|
|
|
84
|
|
|
$queryElement = $xml->createElement('query'); |
85
|
|
|
$queryElement->appendChild($xml->createCDATASection($fieldSettings['query'])); |
86
|
|
|
$selection->appendChild($queryElement); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$storageDef->dataText5 = $xml->saveXML(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Converts field definition data in $storageDef into $fieldDef. |
94
|
|
|
* |
95
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef |
96
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef |
97
|
|
|
*/ |
98
|
|
|
public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef) |
99
|
|
|
{ |
100
|
|
|
$simpleXml = simplexml_load_string($storageDef->dataText5); |
101
|
|
|
$options = array(); |
102
|
|
|
$isMultiple = false; |
103
|
|
|
$delimiter = ''; |
104
|
|
|
$query = ''; |
105
|
|
|
|
106
|
|
|
if ($simpleXml !== false) { |
107
|
|
|
if (!empty($simpleXml->options)) { |
108
|
|
|
foreach ($simpleXml->options->option as $option) { |
109
|
|
|
$options[] = array( |
110
|
|
|
'id' => (int) $option['id'], |
111
|
|
|
'name' => (string) $option['name'], |
112
|
|
|
'identifier' => (string) $option['identifier'], |
113
|
|
|
'priority' => (int) $option['priority'], |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!empty($simpleXml->multiselect)) { |
119
|
|
|
$isMultiple = true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!empty($simpleXml->delimiter)) { |
123
|
|
|
$delimiter = (string) $simpleXml->delimiter; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (!empty($simpleXml->query)) { |
127
|
|
|
$query = (string) $simpleXml->query; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings( |
132
|
|
|
array( |
133
|
|
|
'isMultiple' => $isMultiple, |
134
|
|
|
'delimiter' => $delimiter, |
135
|
|
|
'options' => $options, |
136
|
|
|
'query' => $query, |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the name of the index column in the attribute table. |
143
|
|
|
* |
144
|
|
|
* Returns the name of the index column the datatype uses, which is either |
145
|
|
|
* "sort_key_int" or "sort_key_string". This column is then used for |
146
|
|
|
* filtering and sorting for this type. |
147
|
|
|
* |
148
|
|
|
* If the indexing is not supported, this method must return false. |
149
|
|
|
* |
150
|
|
|
* @return string|false |
151
|
|
|
*/ |
152
|
|
|
public function getIndexColumn() |
153
|
|
|
{ |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.