FeatureAvailabilityExtendLoop::formatSlug()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
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\Loop;
10
11
use FeatureType\Model\FeatureTypeAvMeta;
12
use FeatureType\Model\FeatureTypeAvMetaQuery;
13
use FeatureType\Model\Map\FeatureFeatureTypeTableMap;
14
use FeatureType\Model\Map\FeatureTypeAvMetaTableMap;
15
use FeatureType\Model\Map\FeatureTypeTableMap;
16
use Propel\Runtime\ActiveQuery\Criteria;
17
use Propel\Runtime\ActiveQuery\Join;
18
use Thelia\Core\Template\Element\LoopResult;
19
use Thelia\Core\Template\Element\LoopResultRow;
20
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
21
use Thelia\Core\Template\Loop\Argument\Argument;
22
use Thelia\Core\Template\Loop\FeatureAvailability;
23
use Thelia\Model\FeatureAv;
24
use Thelia\Model\Map\FeatureAvTableMap;
25
26
/**
27
 * Class FeatureAvailabilityExtendLoop
28
 * @package FeatureType\Loop
29
 * @author Gilles Bourgeat <[email protected]>
30
 */
31
class FeatureAvailabilityExtendLoop extends FeatureAvailability implements PropelSearchLoopInterface
32
{
33
    /**
34
     * @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection
35
     */
36
    protected function getArgDefinitions()
37
    {
38
        return parent::getArgDefinitions()->addArguments(array(
39
            Argument::createIntListTypeArgument("feature_type_id"),
40
            Argument::createAnyTypeArgument("feature_type_slug")
41
        ));
42
    }
43
44
    /**
45
     * this method returns a Propel ModelCriteria
46
     *
47
     * @return \Propel\Runtime\ActiveQuery\ModelCriteria
48
     */
49
    public function buildModelCriteria()
50
    {
51
        $query = parent::buildModelCriteria();
52
53
        if (null !== $featureTypeSlug = $this->getFeatureTypeSlug()) {
54
            $featureTypeSlug = array_map(function($value) {
55
                return "'" . addslashes($value) . "'";
56
            }, explode(',', $featureTypeSlug));
57
58
            $join = new Join();
59
60
            $join->addExplicitCondition(
61
                FeatureAvTableMap::TABLE_NAME,
62
                'FEATURE_ID',
63
                null,
64
                FeatureFeatureTypeTableMap::TABLE_NAME,
65
                'FEATURE_ID',
66
                null
67
            );
68
69
            $join2 = new Join();
70
71
            $join2->addExplicitCondition(
72
                FeatureFeatureTypeTableMap::TABLE_NAME,
73
                'FEATURE_TYPE_ID',
74
                null,
75
                FeatureTypeTableMap::TABLE_NAME,
76
                'ID',
77
                null
78
            );
79
80
            $join->setJoinType(Criteria::JOIN);
81
            $join2->setJoinType(Criteria::JOIN);
82
83
            $query
84
                ->addJoinObject($join, 'feature_feature_type_join')
85
                ->addJoinObject($join2, 'feature_type_join')
86
                ->addJoinCondition(
87
                    'feature_type_join',
88
                    '`feature_type`.`slug` IN ('.implode(',', $featureTypeSlug).')'
89
                );
90
        }
91
92
        if (null !== $featureTypeId = $this->getFeatureTypeId()) {
93
            $join = new Join();
94
95
            $join->addExplicitCondition(
96
                FeatureAvTableMap::TABLE_NAME,
97
                'FEATURE_ID',
98
                null,
99
                FeatureFeatureTypeTableMap::TABLE_NAME,
100
                'FEATURE_ID',
101
                null
102
            );
103
104
            $join->setJoinType(Criteria::JOIN);
105
106
            $query
107
                ->addJoinObject($join, 'feature_type_join')
108
                ->addJoinCondition(
109
                    'feature_type_join',
110
                    '`feature_feature_type`.`feature_type_id` IN (?)',
111
                    implode(',', $featureTypeId),
112
                    null,
113
                    \PDO::PARAM_INT
114
                );
115
        }
116
117
        return $query;
118
    }
119
120
    /**
121
     * @param LoopResult $loopResult
122
     * @return array|mixed|\Propel\Runtime\Collection\ObjectCollection
123
     */
124
    protected function getFeaturesMeta(LoopResult $loopResult)
125
    {
126
        $featureAvIds = array();
127
128
        /** @var FeatureAV $featureAv */
129
        foreach ($loopResult->getResultDataCollection() as $featureAv) {
130
            $featureAvIds[] = $featureAv->getId();
131
        }
132
133
        $joinFeatureFeatureType = new Join();
134
135
        $joinFeatureFeatureType->addExplicitCondition(
136
            FeatureTypeAvMetaTableMap::TABLE_NAME,
137
            'FEATURE_FEATURE_TYPE_ID',
138
            null,
139
            FeatureFeatureTypeTableMap::TABLE_NAME,
140
            'ID',
141
            null
142
        );
143
144
        $joinFeatureFeatureType->setJoinType(Criteria::INNER_JOIN);
145
146
        $joinFeatureType = new Join();
147
148
        $joinFeatureType->addExplicitCondition(
149
            FeatureFeatureTypeTableMap::TABLE_NAME,
150
            'FEATURE_TYPE_ID',
151
            null,
152
            FeatureTypeTableMap::TABLE_NAME,
153
            'ID',
154
            null
155
        );
156
157
        $joinFeatureType->setJoinType(Criteria::INNER_JOIN);
158
159
        $query = FeatureTypeAvMetaQuery::create()
160
            ->filterByLocale($this->locale)
161
            ->filterByFeatureAvId($featureAvIds, Criteria::IN)
162
            ->addJoinObject($joinFeatureFeatureType)
163
            ->addJoinObject($joinFeatureType);
164
165
        $query->withColumn('`feature_type`.`SLUG`', 'SLUG');
166
167
        return $query->find();
168
    }
169
170
    /**
171
     * @param string $slug
172
     * @return string
173
     */
174
    protected function formatSlug($slug)
175
    {
176
        return strtoupper(str_replace('-', '_', $slug));
177
    }
178
179
    /**
180
     * @param LoopResult $loopResult
181
     * @return LoopResult
182
     * @throws \Propel\Runtime\Exception\PropelException
183
     */
184
    public function parseResults(LoopResult $loopResult)
185
    {
186
        $featuresMeta = self::getFeaturesMeta($loopResult);
187
188
        $slugs = array();
189
190
        /** @var FeatureTypeAvMeta $featureMeta */
191
        foreach ($featuresMeta as $featureMeta) {
192
            $slugs[$featureMeta->getVirtualColumn('SLUG')] = true;
193
        }
194
195
        /** @var FeatureAv $featureAv */
196
        foreach ($loopResult->getResultDataCollection() as $featureAv) {
197
            $loopResultRow = new LoopResultRow($featureAv);
198
            $loopResultRow
199
                ->set("ID", $featureAv->getId())
200
                ->set("FEATURE_ID", $featureAv->getFeatureId())
201
                ->set("IS_TRANSLATED", $featureAv->getVirtualColumn('IS_TRANSLATED'))
202
                ->set("LOCALE", $this->locale)
203
                ->set("TITLE", $featureAv->getVirtualColumn('i18n_TITLE'))
204
                ->set("CHAPO", $featureAv->getVirtualColumn('i18n_CHAPO'))
205
                ->set("DESCRIPTION", $featureAv->getVirtualColumn('i18n_DESCRIPTION'))
206
                ->set("POSTSCRIPTUM", $featureAv->getVirtualColumn('i18n_POSTSCRIPTUM'))
207
                ->set("POSITION", $featureAv->getPosition())
208
            ;
209
210
            // init slug variable
211
            foreach ($slugs as $slug => $bool) {
212
                $loopResultRow->set(
213
                    self::formatSlug(
214
                        $slug
215
                    ),
216
                    null
217
                );
218
            }
219
220
            /** @var FeatureTypeAvMeta $featureMeta */
221
            foreach ($featuresMeta as $featureMeta) {
222
                if ($featureMeta->getFeatureAvId() === $featureAv->getId()) {
223
                    $loopResultRow->set(
224
                        self::formatSlug(
225
                            $featureMeta->getVirtualColumn('SLUG')
226
                        ),
227
                        $featureMeta->getValue()
228
                    );
229
                }
230
            }
231
232
            $this->addOutputFields($loopResultRow, $featureAv);
233
234
            $loopResult->addRow($loopResultRow);
235
        }
236
237
        return $loopResult;
238
    }
239
}
240