1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alchemy\Phraseanet\Helper; |
4
|
|
|
|
5
|
|
|
use Alchemy\Phraseanet\Mapping\FieldMap; |
6
|
|
|
use PhraseanetSDK\Entity\Record; |
7
|
|
|
use PhraseanetSDK\Entity\Story; |
8
|
|
|
|
9
|
|
|
class MetadataHelper |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $defaultLocale; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var FieldMap |
19
|
|
|
*/ |
20
|
|
|
private $fieldsMap; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $fallbackLocale; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param FieldMap $fieldsMap |
29
|
|
|
* @param string $defaultLocale |
30
|
|
|
* @param string $fallbackLocale |
31
|
|
|
*/ |
32
|
11 |
|
public function __construct(FieldMap $fieldsMap, $defaultLocale, $fallbackLocale) |
33
|
|
|
{ |
34
|
11 |
|
$this->fieldsMap = $fieldsMap; |
35
|
11 |
|
$this->defaultLocale = $defaultLocale; |
36
|
11 |
|
$this->fallbackLocale = $fallbackLocale; |
37
|
11 |
|
} |
38
|
|
|
|
39
|
|
|
public function getFieldName($alias, $locale = null) |
40
|
|
|
{ |
41
|
|
|
if ($locale === null) { |
42
|
|
|
$locale = $this->defaultLocale; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
return $this->fieldsMap->getFieldName($alias, $locale); |
47
|
|
|
} catch (\OutOfBoundsException $exception) { |
48
|
|
|
if ($locale !== $this->defaultLocale) { |
49
|
|
|
return $this->getFieldName($alias, $this->defaultLocale); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $fieldName |
56
|
|
|
* @param null $locale |
57
|
|
|
* @param bool $fallback Whether the lookup should fallback to the default locale |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getFieldAlias($fieldName, $locale = null, $fallback = true) |
61
|
|
|
{ |
62
|
|
|
if ($locale == null) { |
63
|
|
|
$locale = $this->defaultLocale; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->fieldsMap->isFieldMapped($fieldName, $locale)) { |
67
|
|
|
return $this->fieldsMap->getAliasFromFieldName($fieldName, $locale); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($locale !== $this->defaultLocale && $fallback) { |
71
|
|
|
return $this->getFieldAlias($fieldName, $this->defaultLocale); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new \RuntimeException("No alias is available for field '$fieldName' with locale '$locale'."); |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
public function getStoryField(Story $story, $field, $locale = null) |
78
|
|
|
{ |
79
|
|
|
// @todo Clean up code |
80
|
3 |
|
if ($locale == null) { |
81
|
|
|
$locale = $this->defaultLocale; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
if (! $this->fieldsMap->hasAlias($field, $locale)) { |
85
|
1 |
|
if ($locale !== $this->defaultLocale) { |
86
|
|
|
return $this->getStoryField($story, $field, $this->defaultLocale); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
$key = $this->fieldsMap->getFieldName($field, $locale); |
93
|
|
|
|
94
|
2 |
|
foreach ($story->getCaption() as $captionField) { |
95
|
1 |
|
if ($key === $captionField->getName()) { |
96
|
1 |
|
return $captionField->getValue(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
if ($locale !== $this->defaultLocale) { |
101
|
|
|
return $this->getStoryField($story, $field, $this->defaultLocale); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return ''; |
105
|
|
|
} |
106
|
|
|
|
107
|
3 |
|
public function getRecordFields(Record $record, array $fields = null, $locale = null) |
108
|
|
|
{ |
109
|
|
|
// @todo Clean up code |
110
|
3 |
|
if ($locale == null) { |
111
|
|
|
$locale = $this->defaultLocale; |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
$map = []; |
115
|
|
|
|
116
|
3 |
|
foreach ($record->getMetadata() as $metadata) { |
117
|
3 |
|
if (!$this->fieldsMap->isFieldMapped($metadata->getName(), $locale)) { |
118
|
3 |
|
continue; |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
$alias = $this->fieldsMap->getAliasFromFieldName($metadata->getName(), $locale); |
122
|
|
|
|
123
|
3 |
|
if ($fields !== null && !in_array($alias, $fields)) { |
124
|
2 |
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
3 |
|
$map = $this->appendValueToMap($map, $alias, $metadata->getValue()); |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
return $map; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array $map |
135
|
|
|
* @param string $alias |
136
|
|
|
* @param string $value |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
3 |
|
private function appendValueToMap($map, $alias, $value) |
140
|
|
|
{ |
141
|
3 |
|
if (isset($map[$alias])) { |
142
|
1 |
|
if (!is_array($map[$alias])) { |
143
|
1 |
|
$map[$alias] = [$map[$alias]]; |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
$map[$alias][] = $value; |
147
|
|
|
} else { |
148
|
3 |
|
$map[$alias] = $value; |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
return $map; |
152
|
|
|
} |
153
|
|
|
|
154
|
2 |
|
public function getRecordField(Record $record, $field, $locale = null) |
155
|
|
|
{ |
156
|
2 |
|
if ($locale == null) { |
157
|
|
|
$locale = $this->defaultLocale; |
158
|
|
|
} |
159
|
|
|
|
160
|
2 |
|
if (!$this->fieldsMap->hasAlias($field, $locale)) { |
161
|
1 |
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
2 |
|
$key = $this->fieldsMap->getFieldName($field, $locale); |
165
|
|
|
|
166
|
2 |
|
foreach ($record->getMetadata() as $metadata) { |
167
|
|
|
// Try to find the corresponding RecordCaption |
168
|
2 |
|
if ($key === $metadata->getName()) { |
169
|
2 |
|
return $metadata->getValue(); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
public function getRecordMultiField(Record $record, $field, $locale = null) |
177
|
|
|
{ |
178
|
2 |
|
if (!$this->fieldsMap->hasAlias($field, $locale)) { |
179
|
1 |
|
return []; |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
$key = $this->fieldsMap->getFieldName($field, $locale); |
183
|
1 |
|
$values = array(); |
184
|
|
|
|
185
|
1 |
|
foreach ($record->getMetadata() as $metadata) { |
186
|
|
|
// Try to find the corresponding RecordCaption |
187
|
1 |
|
if ($key === $metadata->getName()) { |
188
|
1 |
|
$values[] = $metadata->getValue(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
return $values; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|