FeatureType::addJoinFeatureType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the module FeatureType                                  */
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 FeatureType\Model;
10
11
use FeatureType\Model\Base\FeatureType as BaseFeatureType;
12
use FeatureType\Model\Map\FeatureFeatureTypeTableMap;
13
use FeatureType\Model\Map\FeatureTypeAvMetaTableMap;
14
use FeatureType\Model\Map\FeatureTypeTableMap;
15
use Propel\Runtime\ActiveQuery\Criteria;
16
use Propel\Runtime\ActiveQuery\Join;
17
use Thelia\Model\FeatureAvQuery;
18
use Thelia\Model\Map\FeatureAvTableMap;
19
20
/**
21
 * Class FeatureType
22
 * @package FeatureType\Model
23
 * @author Gilles Bourgeat <[email protected]>
24
 */
25
class FeatureType extends BaseFeatureType
26
{
27
    /**
28
     * Returns a value based on the slug, feature_av_id and locale
29
     *
30
     * <code>
31
     * $value  = FeatureType::getValue('color', 2);
32
     * </code>
33
     *
34
     * @param string $slug
35
     * @param int $featureId
36
     * @param string $locale
37
     * @return string
38
     * @throws \Propel\Runtime\Exception\PropelException
39
     */
40
    public static function getValue($slug, $featureId, $locale = 'en_US')
41
    {
42
        return self::getValues([$slug], [$featureId], $locale)[$slug][$featureId];
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 = FeatureType::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[] $featureIds
62
     * @param string $locale
63
     * @return string
64
     * @throws \Propel\Runtime\Exception\PropelException
65
     */
66
    public static function getValues(array $slugs, array $featureIds, $locale = 'en_US')
67
    {
68
        $return = array();
69
70
        foreach ($slugs as $slug) {
71
            $return[$slug] = array();
72
            foreach ($featureIds as $featureId) {
73
                $return[$slug][$featureId] = null;
74
            }
75
        }
76
77
        $query = FeatureTypeAvMetaQuery::create()
78
            ->filterByLocale($locale)
79
            ->filterByFeatureAvId($featureIds, Criteria::IN);
80
81
        self::addJoinFeatureFeatureType($query);
82
        self::addJoinFeatureType($query);
83
        self::addJoinFeatureAv($query);
84
85
        $query
86
            ->addJoinCondition('feature_type', "`feature_type`.`SLUG` IN (" . self::formatStringsForIn($slugs) . ")")
87
            ->addJoinCondition('feature_av', "`feature_av`.`ID` = `feature_type_av_meta`.`FEATURE_AV_ID`")
88
            ->withColumn('`feature_type`.`SLUG`', 'SLUG')
89
            ->withColumn('`feature_av`.`ID`', 'FEATURE_AV_ID');
90
91
        $results = $query->find();
92
93
        foreach ($results as $result) {
94
            $return[$result->getVirtualColumn('SLUG')][$result->getVirtualColumn('FEATURE_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 = FeatureType::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[] $featureIds
119
     * @param string $locale
120
     * @return array
121
     */
122
    public static function getFirstValues(array $slugs, array $featureIds, $locale = 'en_US')
123
    {
124
        $results = self::getValues($slugs, $featureIds, $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 FeatureAv by slugs, featureIds, values, locales
147
     *
148
     * <code>
149
     * $featureAv = FeatureType::getFeatureAv('color', '1', '#00000');
150
     * </code>
151
     *
152
     * @param null|string|array $slugs
153
     * @param null|string|array $featureIds
154
     * @param null|string|array $values meta values
155
     * @param null|string|array $locale
156
     *
157
     * @return \Thelia\Model\FeatureAv
158
     */
159
    public static function getFeatureAv($slugs = null, $featureIds = null, $values = null, $locale = 'en_US')
160
    {
161
        return self::queryFeatureAvs($slugs, $featureIds, $values, $locale)->findOne();
162
    }
163
164
    /**
165
     * Find FeatureAv by slugs, featureIds, values, locales
166
     *
167
     * <code>
168
     * $featureAv = FeatureType::getFeatureAvs('color', '1', '#00000');
169
     * </code>
170
     *
171
     * @param null|string|array $slugs
172
     * @param null|string|array $featureIds
173
     * @param null|string|array $values meta values
174
     * @param null|string|array $locale
175
     *
176
     * @return \Thelia\Model\FeatureAv
177
     */
178
    public static function getFeatureAvs($slugs = null, $featureIds = null, $values = null, $locale = 'en_US')
179
    {
180
        return self::queryFeatureAvs($slugs, $featureIds, $values, $locale)->find();
181
    }
182
183
    /**
184
     * @param null|string|array $slugs
185
     * @param null|string|array $featureIds
186
     * @param null|string|array $values meta values
187
     * @param null|string|array $locales
188
     *
189
     * @return FeatureAvQuery
190
     */
191
    protected static function queryFeatureAvs($slugs = null, $featureIds = null, $values = null, $locales = null)
192
    {
193
        if (!is_array($slugs) && $slugs !== null) {
194
            $slugs = array($slugs);
195
        }
196
197
        if (!is_array($featureIds) && $featureIds !== null) {
198
            $featureIds = array($featureIds);
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 = FeatureAvQuery::create();
210
211
        if ($featureIds !== null) {
212
            $query->filterByFeatureId($featureIds, Criteria::IN);
213
        }
214
215
        self::addJoinFeatureTypeAvMeta($query);
216
        self::addJoinFeatureFeatureType($query);
217
        self::addJoinFeatureType($query);
218
219
        if ($locales !== null) {
220
            $query->addJoinCondition(
221
                'feature_type_av_meta',
222
                "`feature_type_av_meta`.`LOCALE` IN (" . self::formatStringsForIn($locales) . ")"
223
            );
224
        }
225
226
        if ($values !== null) {
227
            $query->addJoinCondition(
228
                'feature_type_av_meta',
229
                "`feature_type_av_meta`.`VALUE` IN (" . self::formatStringsForIn($values) . ")"
230
            );
231
        }
232
233
        if ($slugs !== null) {
234
            $query->addJoinCondition(
235
                'feature_type',
236
                "`feature_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 addJoinFeatureTypeAvMeta(Criteria & $query)
264
    {
265
        $join = new Join();
266
267
        $join->addExplicitCondition(
268
            FeatureAvTableMap::TABLE_NAME,
269
            'ID',
270
            null,
271
            FeatureTypeAvMetaTableMap::TABLE_NAME,
272
            'FEATURE_AV_ID',
273
            null
274
        );
275
276
        $join->setJoinType(Criteria::INNER_JOIN);
277
278
        $query->addJoinObject($join, 'feature_type_av_meta');
279
    }
280
281
    /**
282
     * @param Criteria $query
283
     */
284
    protected static function addJoinFeatureFeatureType(Criteria & $query)
285
    {
286
        $join = new Join();
287
288
        $join->addExplicitCondition(
289
            FeatureTypeAvMetaTableMap::TABLE_NAME,
290
            'FEATURE_FEATURE_TYPE_ID',
291
            null,
292
            FeatureFeatureTypeTableMap::TABLE_NAME,
293
            'ID',
294
            null
295
        );
296
297
        $join->setJoinType(Criteria::INNER_JOIN);
298
299
        $query->addJoinObject($join, 'feature_feature_type');
300
    }
301
302
    /**
303
     * @param Criteria $query
304
     */
305
    protected static function addJoinFeatureType(Criteria & $query)
306
    {
307
        $join = new Join();
308
309
        $join->addExplicitCondition(
310
            FeatureFeatureTypeTableMap::TABLE_NAME,
311
            'FEATURE_TYPE_ID',
312
            null,
313
            FeatureTypeTableMap::TABLE_NAME,
314
            'ID',
315
            null
316
        );
317
318
        $join->setJoinType(Criteria::INNER_JOIN);
319
320
        $query->addJoinObject($join, 'feature_type');
321
    }
322
323
    /**
324
     * @param Criteria $query
325
     * @return $this
326
     */
327
    protected static function addJoinFeatureAv(Criteria & $query)
328
    {
329
        $join = new Join();
330
331
        $join->addExplicitCondition(
332
            FeatureFeatureTypeTableMap::TABLE_NAME,
333
            'FEATURE_ID',
334
            null,
335
            FeatureAvTableMap::TABLE_NAME,
336
            'FEATURE_ID',
337
            null
338
        );
339
340
        $join->setJoinType(Criteria::INNER_JOIN);
341
342
        $query->addJoinObject($join, 'feature_av');
343
    }
344
}
345