|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the Selection 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 eZ\Publish\API\Repository\LanguageService; |
|
12
|
|
|
use eZ\Publish\Core\FieldType\FieldSettings; |
|
13
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter; |
|
14
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue; |
|
15
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition; |
|
16
|
|
|
use eZ\Publish\SPI\Persistence\Content\FieldValue; |
|
17
|
|
|
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition; |
|
18
|
|
|
use DOMDocument; |
|
19
|
|
|
|
|
20
|
|
|
class SelectionConverter implements Converter |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var \eZ\Publish\API\Repository\LanguageService |
|
24
|
|
|
*/ |
|
25
|
|
|
private $languageService; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param \eZ\Publish\API\Repository\LanguageService $languageService |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct( |
|
31
|
|
|
LanguageService $languageService |
|
32
|
|
|
) { |
|
33
|
|
|
$this->languageService = $languageService; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Factory for current class. |
|
38
|
|
|
* |
|
39
|
|
|
* Note: Class should instead be configured as service if it gains dependencies. |
|
40
|
|
|
* |
|
41
|
|
|
* @deprecated since 6.8, will be removed in 7.x, use default constructor instead. |
|
42
|
|
|
* |
|
43
|
|
|
* @return \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function create() |
|
46
|
|
|
{ |
|
47
|
|
|
return new self(); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Converts data from $value to $storageFieldValue. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value |
|
54
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue |
|
55
|
|
|
*/ |
|
56
|
|
|
public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue) |
|
57
|
|
|
{ |
|
58
|
|
|
$storageFieldValue->sortKeyString = $storageFieldValue->dataText = $value->sortKey; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Converts data from $value to $fieldValue. |
|
63
|
|
|
* |
|
64
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
|
65
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue |
|
66
|
|
|
*/ |
|
67
|
|
|
public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue) |
|
68
|
|
|
{ |
|
69
|
|
|
if ($value->dataText !== '') { |
|
70
|
|
|
$fieldValue->data = array_map( |
|
71
|
|
|
'intval', |
|
72
|
|
|
explode('-', $value->dataText) |
|
73
|
|
|
); |
|
74
|
|
|
} else { |
|
75
|
|
|
$fieldValue->data = array(); |
|
76
|
|
|
} |
|
77
|
|
|
$fieldValue->sortKey = $value->sortKeyString; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Converts field definition data in $fieldDef into $storageFieldDef. |
|
82
|
|
|
* |
|
83
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef |
|
84
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef |
|
85
|
|
|
*/ |
|
86
|
|
|
public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef) |
|
87
|
|
|
{ |
|
88
|
|
|
$fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings; |
|
89
|
|
|
|
|
90
|
|
|
if (isset($fieldSettings['isMultiple'])) { |
|
91
|
|
|
$storageDef->dataInt1 = (int)$fieldSettings['isMultiple']; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (!empty($fieldSettings['options'])) { |
|
95
|
|
|
$xml = $this->buildOptionsXml($fieldSettings['options']); |
|
96
|
|
|
$storageDef->dataText5 = $xml->saveXML(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (!isset($fieldSettings['multilingualOptions'])) { |
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
foreach ($fieldSettings['multilingualOptions'] as $languageCode => $option) { |
|
104
|
|
|
$xml = $this->buildOptionsXml($option); |
|
105
|
|
|
|
|
106
|
|
|
$storageDef->multilingualData[$languageCode]->dataText = $xml->saveXML(); |
|
107
|
|
|
|
|
108
|
|
|
if ($fieldDef->mainLanguageCode === $languageCode) { |
|
109
|
|
|
$storageDef->dataText5 = $xml->saveXML(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Converts field definition data in $storageDef into $fieldDef. |
|
116
|
|
|
* |
|
117
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef |
|
118
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef |
|
119
|
|
|
*/ |
|
120
|
|
|
public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef) |
|
121
|
|
|
{ |
|
122
|
|
|
$options = []; |
|
123
|
|
|
$multiLingualOptions = [$fieldDef->mainLanguageCode => []]; |
|
124
|
|
|
|
|
125
|
|
|
if (isset($storageDef->dataText5)) { |
|
126
|
|
|
$optionsXml = simplexml_load_string($storageDef->dataText5); |
|
127
|
|
|
if ($optionsXml !== false) { |
|
128
|
|
|
foreach ($optionsXml->options->option as $option) { |
|
129
|
|
|
$options[(int)$option['id']] = (string)$option['name']; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (isset($fieldDef->mainLanguageCode) && !empty($options)) { |
|
135
|
|
|
$multiLingualOptions[$fieldDef->mainLanguageCode] = $options; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
foreach ($storageDef->multilingualData as $languageCode => $mlData) { |
|
139
|
|
|
$xml = simplexml_load_string($mlData->dataText); |
|
140
|
|
|
if ($xml !== false) { |
|
141
|
|
|
foreach ($xml->options->option as $option) { |
|
142
|
|
|
$multiLingualOptions[$languageCode][(int)$option['id']] = (string)$option['name']; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings([ |
|
148
|
|
|
'isMultiple' => !empty($storageDef->dataInt1) ? (bool)$storageDef->dataInt1 : false, |
|
149
|
|
|
'options' => $options, |
|
150
|
|
|
'multilingualOptions' => $multiLingualOptions, |
|
151
|
|
|
] |
|
152
|
|
|
); |
|
153
|
|
|
|
|
154
|
|
|
// @todo: Can Selection store a default value in the DB? |
|
155
|
|
|
$fieldDef->defaultValue = new FieldValue(); |
|
156
|
|
|
$fieldDef->defaultValue->data = array(); |
|
157
|
|
|
$fieldDef->defaultValue->sortKey = ''; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Returns the name of the index column in the attribute table. |
|
162
|
|
|
* |
|
163
|
|
|
* Returns the name of the index column the datatype uses, which is either |
|
164
|
|
|
* "sort_key_int" or "sort_key_string". This column is then used for |
|
165
|
|
|
* filtering and sorting for this type. |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getIndexColumn() |
|
170
|
|
|
{ |
|
171
|
|
|
return 'sort_key_string'; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param string[] $selectionOptions |
|
176
|
|
|
* |
|
177
|
|
|
* @return \DOMDocument |
|
178
|
|
|
*/ |
|
179
|
|
|
private function buildOptionsXml(array $selectionOptions): DOMDocument |
|
180
|
|
|
{ |
|
181
|
|
|
$xml = new DOMDocument('1.0', 'utf-8'); |
|
182
|
|
|
$xml->appendChild( |
|
183
|
|
|
$selection = $xml->createElement('ezselection') |
|
184
|
|
|
); |
|
185
|
|
|
$selection->appendChild( |
|
186
|
|
|
$options = $xml->createElement('options') |
|
187
|
|
|
); |
|
188
|
|
|
foreach ($selectionOptions as $id => $name) { |
|
189
|
|
|
$options->appendChild( |
|
190
|
|
|
$option = $xml->createElement('option') |
|
191
|
|
|
); |
|
192
|
|
|
$option->setAttribute('id', $id); |
|
193
|
|
|
$option->setAttribute('name', $name); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $xml; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
This check looks for function calls that miss required arguments.