FeatureExtendLoop   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 0
dl 0
loc 192
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getArgDefinitions() 0 7 1
B buildModelCriteria() 0 70 3
A getFeaturesType() 0 30 2
A formatSlug() 0 4 1
B parseResults() 0 53 7
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\FeatureFeatureType;
12
use FeatureType\Model\FeatureFeatureTypeQuery;
13
use FeatureType\Model\FeatureType;
14
use FeatureType\Model\Map\FeatureFeatureTypeTableMap;
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\Feature;
23
use Thelia\Model\FeatureAv as FeatureModel;
24
use Thelia\Model\Map\FeatureTableMap;
25
26
/**
27
 * Class FeatureExtendLoop
28
 * @package FeatureType\Loop
29
 * @author Gilles Bourgeat <[email protected]>
30
 */
31
class FeatureExtendLoop extends Feature 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
                FeatureTableMap::TABLE_NAME,
62
                '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
                FeatureTableMap::TABLE_NAME,
97
                '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 getFeaturesType(LoopResult $loopResult)
125
    {
126
        $featureIds = array();
127
128
        /** @var FeatureModel $feature */
129
        foreach ($loopResult->getResultDataCollection() as $feature) {
130
            $featureIds[] = $feature->getId();
131
        }
132
133
        $join = new Join();
134
135
        $join->addExplicitCondition(
136
            FeatureFeatureTypeTableMap::TABLE_NAME,
137
            'FEATURE_TYPE_ID',
138
            null,
139
            FeatureTypeTableMap::TABLE_NAME,
140
            'ID',
141
            null
142
        );
143
144
        $join->setJoinType(Criteria::INNER_JOIN);
145
146
        $query = FeatureFeatureTypeQuery::create()
147
            ->filterByFeatureId($featureIds, Criteria::IN)
148
            ->addJoinObject($join);
149
150
        return $query
151
            ->withColumn('`feature_type`.`SLUG`', 'SLUG')
152
            ->find();
153
    }
154
155
    /**
156
     * @param string $slug
157
     * @return string
158
     */
159
    protected function formatSlug($slug)
160
    {
161
        return strtoupper(str_replace('-', '_', $slug));
162
    }
163
164
    /**
165
     * @param LoopResult $loopResult
166
     * @return LoopResult
167
     * @throws \Propel\Runtime\Exception\PropelException
168
     */
169
    public function parseResults(LoopResult $loopResult)
170
    {
171
        $featureTypes = self::getFeaturesType($loopResult);
172
173
        $slugs = array();
174
175
        /** @var FeatureType $featureType */
176
        foreach ($featureTypes as $featureType) {
177
            $slugs[$featureType->getVirtualColumn('SLUG')] = true;
178
        }
179
180
        /** @var FeatureModel $feature */
181
        foreach ($loopResult->getResultDataCollection() as $feature) {
182
            $loopResultRow = new LoopResultRow($feature);
183
            $loopResultRow->set("ID", $feature->getId())
184
                ->set("IS_TRANSLATED", $feature->getVirtualColumn('IS_TRANSLATED'))
185
                ->set("LOCALE", $this->locale)
186
                ->set("TITLE", $feature->getVirtualColumn('i18n_TITLE'))
187
                ->set("CHAPO", $feature->getVirtualColumn('i18n_CHAPO'))
188
                ->set("DESCRIPTION", $feature->getVirtualColumn('i18n_DESCRIPTION'))
189
                ->set("POSTSCRIPTUM", $feature->getVirtualColumn('i18n_POSTSCRIPTUM'))
190
                ->set("POSITION", $this->useFeaturePosition ? $feature->getPosition() : $feature->getVirtualColumn('position'))
191
            ;
192
193
            // init slug variable
194
            foreach ($slugs as $slug => $bool) {
195
                $loopResultRow->set(
196
                    self::formatSlug(
197
                        $slug
198
                    ),
199
                    null
200
                );
201
            }
202
203
            /** @var FeatureFeatureType $featureType */
204
            foreach ($featureTypes as $featureType) {
205
                if ($featureType->getFeatureId() === $feature->getId()) {
206
                    $loopResultRow->set(
207
                        self::formatSlug(
208
                            $featureType->getVirtualColumn('SLUG')
209
                        ),
210
                        true
211
                    );
212
                }
213
            }
214
215
            $this->addOutputFields($loopResultRow, $feature);
216
217
            $loopResult->addRow($loopResultRow);
218
        }
219
220
        return $loopResult;
221
    }
222
}
223