|
1
|
|
|
<?php |
|
2
|
|
|
/*************************************************************************************/ |
|
3
|
|
|
/* This file is part of the module AttributeType */ |
|
4
|
|
|
/* */ |
|
5
|
|
|
/* For the full copyright and license information, please view the LICENSE.txt */ |
|
6
|
|
|
/* file that was distributed with this source code. */ |
|
7
|
|
|
/*************************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace AttributeType\Model; |
|
10
|
|
|
|
|
11
|
|
|
use AttributeType\Model\Base\AttributeType as BaseAttributeType; |
|
12
|
|
|
use AttributeType\Model\Map\AttributeAttributeTypeTableMap; |
|
13
|
|
|
use AttributeType\Model\Map\AttributeTypeAvMetaTableMap; |
|
14
|
|
|
use AttributeType\Model\Map\AttributeTypeTableMap; |
|
15
|
|
|
use Propel\Runtime\ActiveQuery\Criteria; |
|
16
|
|
|
use Propel\Runtime\ActiveQuery\Join; |
|
17
|
|
|
use Thelia\Model\AttributeAvQuery; |
|
18
|
|
|
use Thelia\Model\Map\AttributeAvTableMap; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class AttributeType |
|
22
|
|
|
* @package AttributeType\Model |
|
23
|
|
|
* @author Gilles Bourgeat <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class AttributeType extends BaseAttributeType |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Returns a value based on the slug, attribute_av_id and locale |
|
29
|
|
|
* |
|
30
|
|
|
* <code> |
|
31
|
|
|
* $value = AttributeType::getValue('color', 2); |
|
32
|
|
|
* </code> |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $slug |
|
35
|
|
|
* @param int $attributeAvId |
|
36
|
|
|
* @param string $locale |
|
37
|
|
|
* @return string |
|
38
|
|
|
* @throws \Propel\Runtime\Exception\PropelException |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function getValue($slug, $attributeAvId, $locale = 'en_US') |
|
41
|
|
|
{ |
|
42
|
|
|
return self::getValues([$slug], [$attributeAvId], $locale)[$slug][$attributeAvId]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns a set of values |
|
47
|
|
|
* If the value does not exist, it is replaced by null |
|
48
|
|
|
* |
|
49
|
|
|
* <code> |
|
50
|
|
|
* $values = AttributeType::getValue(['color','texture'], [4,7]); |
|
51
|
|
|
* </code> |
|
52
|
|
|
* |
|
53
|
|
|
* <sample> |
|
54
|
|
|
* array( |
|
55
|
|
|
* 'color' => [4 => '#00000', 7 => '#FFF000'], |
|
56
|
|
|
* 'texture' => [4 => null, 7 => 'lines.jpg'] |
|
57
|
|
|
* ) |
|
58
|
|
|
* </sample> |
|
59
|
|
|
* |
|
60
|
|
|
* @param string[] $slugs |
|
61
|
|
|
* @param int[] $attributeAvIds |
|
62
|
|
|
* @param string $locale |
|
63
|
|
|
* @return string |
|
64
|
|
|
* @throws \Propel\Runtime\Exception\PropelException |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function getValues(array $slugs, array $attributeAvIds, $locale = 'en_US') |
|
67
|
|
|
{ |
|
68
|
|
|
$return = array(); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($slugs as $slug) { |
|
71
|
|
|
$return[$slug] = array(); |
|
72
|
|
|
foreach ($attributeAvIds as $attributeAvId) { |
|
73
|
|
|
$return[$slug][$attributeAvId] = null; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$query = AttributeTypeAvMetaQuery::create() |
|
78
|
|
|
->filterByLocale($locale) |
|
79
|
|
|
->filterByAttributeAvId($attributeAvIds, Criteria::IN); |
|
80
|
|
|
|
|
81
|
|
|
self::addJoinAttributeAttributeType($query); |
|
82
|
|
|
self::addJoinAttributeType($query); |
|
83
|
|
|
self::addJoinAttributeAv($query); |
|
84
|
|
|
|
|
85
|
|
|
$query |
|
86
|
|
|
->addJoinCondition('attribute_type', "`attribute_type`.`SLUG` IN (" . self::formatStringsForIn($slugs) . ")") |
|
87
|
|
|
->addJoinCondition('attribute_av', "`attribute_av`.`ID` = `attribute_type_av_meta`.`ATTRIBUTE_AV_ID`") |
|
88
|
|
|
->withColumn('`attribute_type`.`SLUG`', 'SLUG') |
|
89
|
|
|
->withColumn('`attribute_av`.`ID`', 'ATTRIBUTE_AV_ID'); |
|
90
|
|
|
|
|
91
|
|
|
$results = $query->find(); |
|
92
|
|
|
|
|
93
|
|
|
foreach ($results as $result) { |
|
94
|
|
|
$return[$result->getVirtualColumn('SLUG')][$result->getVirtualColumn('ATTRIBUTE_AV_ID')] |
|
95
|
|
|
= $result->getValue(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns a set of first values |
|
103
|
|
|
* If the value does not exist, it is replaced by null |
|
104
|
|
|
* |
|
105
|
|
|
* <code> |
|
106
|
|
|
* $values = AttributeType::getFirstValues(['color','texture','other'], [4,7]); |
|
107
|
|
|
* </code> |
|
108
|
|
|
* |
|
109
|
|
|
* <sample> |
|
110
|
|
|
* array( |
|
111
|
|
|
* 'color' => '#00000', |
|
112
|
|
|
* 'texture' => 'lines.jpg', |
|
113
|
|
|
* 'other' => null |
|
114
|
|
|
* ) |
|
115
|
|
|
* </sample> |
|
116
|
|
|
* |
|
117
|
|
|
* @param string[] $slugs |
|
118
|
|
|
* @param int[] $attributeAvIds |
|
119
|
|
|
* @param string $locale |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function getFirstValues(array $slugs, array $attributeAvIds, $locale = 'en_US') |
|
123
|
|
|
{ |
|
124
|
|
|
$results = self::getValues($slugs, $attributeAvIds, $locale); |
|
125
|
|
|
|
|
126
|
|
|
$return = array(); |
|
127
|
|
|
|
|
128
|
|
|
foreach ($slugs as $slug) { |
|
129
|
|
|
if (!isset($return[$slug])) { |
|
130
|
|
|
$return[$slug] = null; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
foreach ($results[$slug] as $value) { |
|
134
|
|
|
if ($return[$slug] === null) { |
|
135
|
|
|
$return[$slug] = $value; |
|
136
|
|
|
continue; |
|
137
|
|
|
} |
|
138
|
|
|
break; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $return; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Find AttributeAv by slugs, attributeIds, values, locales |
|
147
|
|
|
* |
|
148
|
|
|
* <code> |
|
149
|
|
|
* $attributeAv = AttributeType::getAttributeAv('color', '1', '#00000'); |
|
150
|
|
|
* </code> |
|
151
|
|
|
* |
|
152
|
|
|
* @param null|string|array $slugs |
|
153
|
|
|
* @param null|string|array $attributeIds |
|
154
|
|
|
* @param null|string|array $values meta values |
|
155
|
|
|
* @param null|string|array $locale |
|
156
|
|
|
* |
|
157
|
|
|
* @return \Thelia\Model\AttributeAv |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function getAttributeAv($slugs = null, $attributeIds = null, $values = null, $locale = 'en_US') |
|
160
|
|
|
{ |
|
161
|
|
|
return self::queryAttributeAvs($slugs, $attributeIds, $values, $locale)->findOne(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Find AttributeAvs by slug, attributeId, value, locale |
|
166
|
|
|
* |
|
167
|
|
|
* <code> |
|
168
|
|
|
* $attributeAvs = AttributeType::getAttributeAvs('color', '1', '#00000'); |
|
169
|
|
|
* </code> |
|
170
|
|
|
* |
|
171
|
|
|
* @param null|string|array $slugs |
|
172
|
|
|
* @param null|string|array $attributeIds |
|
173
|
|
|
* @param null|string|array $values meta values |
|
174
|
|
|
* @param null|string|array $locale |
|
175
|
|
|
* |
|
176
|
|
|
* @return \Thelia\Model\AttributeAv |
|
177
|
|
|
*/ |
|
178
|
|
|
public static function getAttributeAvs($slugs = null, $attributeIds = null, $values = null, $locale = 'en_US') |
|
179
|
|
|
{ |
|
180
|
|
|
return self::queryAttributeAvs($slugs, $attributeIds, $values, $locale)->find(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param null|string|array $slugs |
|
185
|
|
|
* @param null|string|array $attributeIds |
|
186
|
|
|
* @param null|string|array $values meta values |
|
187
|
|
|
* @param null|string|array $locales |
|
188
|
|
|
* |
|
189
|
|
|
* @return AttributeAvQuery |
|
190
|
|
|
*/ |
|
191
|
|
|
protected static function queryAttributeAvs($slugs = null, $attributeIds = null, $values = null, $locales = null) |
|
192
|
|
|
{ |
|
193
|
|
|
if (!is_array($slugs) && $slugs !== null) { |
|
194
|
|
|
$slugs = array($slugs); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
if (!is_array($attributeIds) && $attributeIds !== null) { |
|
198
|
|
|
$attributeIds = array($attributeIds); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
if (!is_array($values) && $values !== null) { |
|
202
|
|
|
$values = array($values); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
if (!is_array($locales) && $locales !== null) { |
|
206
|
|
|
$locales = array($locales); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$query = AttributeAvQuery::create(); |
|
210
|
|
|
|
|
211
|
|
|
if ($attributeIds !== null) { |
|
212
|
|
|
$query->filterByAttributeId($attributeIds, Criteria::IN); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
self::addJoinAttributeTypeAvMeta($query); |
|
216
|
|
|
self::addJoinAttributeAttributeType($query); |
|
217
|
|
|
self::addJoinAttributeType($query); |
|
218
|
|
|
|
|
219
|
|
|
if ($locales !== null) { |
|
220
|
|
|
$query->addJoinCondition( |
|
221
|
|
|
'attribute_type_av_meta', |
|
222
|
|
|
"`attribute_type_av_meta`.`LOCALE` IN (" . self::formatStringsForIn($locales) . ")" |
|
223
|
|
|
); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if ($values !== null) { |
|
227
|
|
|
$query->addJoinCondition( |
|
228
|
|
|
'attribute_type_av_meta', |
|
229
|
|
|
"`attribute_type_av_meta`.`VALUE` IN (" . self::formatStringsForIn($values) . ")" |
|
230
|
|
|
); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
if ($slugs !== null) { |
|
234
|
|
|
$query->addJoinCondition( |
|
235
|
|
|
'attribute_type', |
|
236
|
|
|
"`attribute_type`.`SLUG` IN (" . self::formatStringsForIn($slugs) . ")" |
|
237
|
|
|
); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $query; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param array $strings |
|
245
|
|
|
* @return string |
|
246
|
|
|
*/ |
|
247
|
|
|
protected static function formatStringsForIn(array $strings) |
|
248
|
|
|
{ |
|
249
|
|
|
return implode( |
|
250
|
|
|
',', |
|
251
|
|
|
array_map( |
|
252
|
|
|
function($v) { |
|
253
|
|
|
return "'" . $v . "'"; |
|
254
|
|
|
}, |
|
255
|
|
|
$strings |
|
256
|
|
|
) |
|
257
|
|
|
); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @param Criteria $query |
|
262
|
|
|
*/ |
|
263
|
|
|
protected static function addJoinAttributeTypeAvMeta(Criteria & $query) |
|
264
|
|
|
{ |
|
265
|
|
|
$join = new Join(); |
|
266
|
|
|
|
|
267
|
|
|
$join->addExplicitCondition( |
|
268
|
|
|
AttributeAvTableMap::TABLE_NAME, |
|
269
|
|
|
'ID', |
|
270
|
|
|
null, |
|
271
|
|
|
AttributeTypeAvMetaTableMap::TABLE_NAME, |
|
272
|
|
|
'ATTRIBUTE_AV_ID', |
|
273
|
|
|
null |
|
274
|
|
|
); |
|
275
|
|
|
|
|
276
|
|
|
$join->setJoinType(Criteria::INNER_JOIN); |
|
277
|
|
|
|
|
278
|
|
|
$query->addJoinObject($join, 'attribute_type_av_meta'); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @param Criteria $query |
|
283
|
|
|
*/ |
|
284
|
|
|
protected static function addJoinAttributeAttributeType(Criteria & $query) |
|
285
|
|
|
{ |
|
286
|
|
|
$join = new Join(); |
|
287
|
|
|
|
|
288
|
|
|
$join->addExplicitCondition( |
|
289
|
|
|
AttributeTypeAvMetaTableMap::TABLE_NAME, |
|
290
|
|
|
'ATTRIBUTE_ATTRIBUTE_TYPE_ID', |
|
291
|
|
|
null, |
|
292
|
|
|
AttributeAttributeTypeTableMap::TABLE_NAME, |
|
293
|
|
|
'ID', |
|
294
|
|
|
null |
|
295
|
|
|
); |
|
296
|
|
|
|
|
297
|
|
|
$join->setJoinType(Criteria::INNER_JOIN); |
|
298
|
|
|
|
|
299
|
|
|
$query->addJoinObject($join, 'attribute_attribute_type'); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @param Criteria $query |
|
304
|
|
|
*/ |
|
305
|
|
|
protected static function addJoinAttributeType(Criteria & $query) |
|
306
|
|
|
{ |
|
307
|
|
|
$join = new Join(); |
|
308
|
|
|
|
|
309
|
|
|
$join->addExplicitCondition( |
|
310
|
|
|
AttributeAttributeTypeTableMap::TABLE_NAME, |
|
311
|
|
|
'ATTRIBUTE_TYPE_ID', |
|
312
|
|
|
null, |
|
313
|
|
|
AttributeTypeTableMap::TABLE_NAME, |
|
314
|
|
|
'ID', |
|
315
|
|
|
null |
|
316
|
|
|
); |
|
317
|
|
|
|
|
318
|
|
|
$join->setJoinType(Criteria::INNER_JOIN); |
|
319
|
|
|
|
|
320
|
|
|
$query->addJoinObject($join, 'attribute_type'); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @param Criteria $query |
|
325
|
|
|
* @return $this |
|
326
|
|
|
*/ |
|
327
|
|
|
protected static function addJoinAttributeAv(Criteria & $query) |
|
328
|
|
|
{ |
|
329
|
|
|
$join = new Join(); |
|
330
|
|
|
|
|
331
|
|
|
$join->addExplicitCondition( |
|
332
|
|
|
AttributeAttributeTypeTableMap::TABLE_NAME, |
|
333
|
|
|
'ATTRIBUTE_ID', |
|
334
|
|
|
null, |
|
335
|
|
|
AttributeAvTableMap::TABLE_NAME, |
|
336
|
|
|
'ATTRIBUTE_ID', |
|
337
|
|
|
null |
|
338
|
|
|
); |
|
339
|
|
|
|
|
340
|
|
|
$join->setJoinType(Criteria::INNER_JOIN); |
|
341
|
|
|
|
|
342
|
|
|
$query->addJoinObject($join, 'attribute_av'); |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
|