AttributeExtendLoop   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 getAttributesType() 0 30 2
A formatSlug() 0 4 1
B parseResults() 0 53 7
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\Loop;
10
11
use AttributeType\Model\AttributeAttributeType;
12
use AttributeType\Model\AttributeAttributeTypeQuery;
13
use AttributeType\Model\AttributeType;
14
use AttributeType\Model\Map\AttributeAttributeTypeTableMap;
15
use AttributeType\Model\Map\AttributeTypeTableMap;
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\Attribute;
23
use Thelia\Model\AttributeAv as AttributeModel;
24
use Thelia\Model\Map\AttributeTableMap;
25
26
/**
27
 * Class AttributeExtendLoop
28
 * @package AttributeType\Loop
29
 * @author Gilles Bourgeat <[email protected]>
30
 *
31
 * @method string getAttributeTypeId()
32
 * @method int[] getAttributeTypeSlug()
33
 */
34
class AttributeExtendLoop extends Attribute implements PropelSearchLoopInterface
35
{
36
    /**
37
     * @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection
38
     */
39
    protected function getArgDefinitions()
40
    {
41
        return parent::getArgDefinitions()->addArguments(array(
42
            Argument::createIntListTypeArgument("attribute_type_id"),
43
            Argument::createAnyTypeArgument("attribute_type_slug")
44
        ));
45
    }
46
47
    /**
48
     * this method returns a Propel ModelCriteria
49
     *
50
     * @return \Propel\Runtime\ActiveQuery\ModelCriteria
51
     */
52
    public function buildModelCriteria()
53
    {
54
        $query = parent::buildModelCriteria();
55
56
        if (null !== $attributeTypeSlug = $this->getAttributeTypeSlug()) {
57
            $attributeTypeSlug = array_map(function($value) {
58
                return "'" . addslashes($value) . "'";
59
            }, explode(',', $attributeTypeSlug));
60
61
            $join = new Join();
62
63
            $join->addExplicitCondition(
64
                AttributeTableMap::TABLE_NAME,
65
                'ID',
66
                null,
67
                AttributeAttributeTypeTableMap::TABLE_NAME,
68
                'ATTRIBUTE_ID',
69
                null
70
            );
71
72
            $join2 = new Join();
73
74
            $join2->addExplicitCondition(
75
                AttributeAttributeTypeTableMap::TABLE_NAME,
76
                'ATTRIBUTE_TYPE_ID',
77
                null,
78
                AttributeTypeTableMap::TABLE_NAME,
79
                'ID',
80
                null
81
            );
82
83
            $join->setJoinType(Criteria::JOIN);
84
            $join2->setJoinType(Criteria::JOIN);
85
86
            $query
87
                ->addJoinObject($join, 'attribute_attribute_type_join')
88
                ->addJoinObject($join2, 'attribute_type_join')
89
                ->addJoinCondition(
90
                    'attribute_type_join',
91
                    '`attribute_type`.`slug` IN ('.implode(',', $attributeTypeSlug).')'
92
                );
93
        }
94
95
        if (null !== $attributeTypeId = $this->getAttributeTypeId()) {
96
            $join = new Join();
97
98
            $join->addExplicitCondition(
99
                AttributeTableMap::TABLE_NAME,
100
                'ID',
101
                null,
102
                AttributeAttributeTypeTableMap::TABLE_NAME,
103
                'ATTRIBUTE_ID',
104
                null
105
            );
106
107
            $join->setJoinType(Criteria::JOIN);
108
109
            $query
110
                ->addJoinObject($join, 'attribute_type_join')
111
                ->addJoinCondition(
112
                    'attribute_type_join',
113
                    '`attribute_attribute_type`.`attribute_type_id` IN (?)',
114
                    implode(',', $attributeTypeId),
115
                    null,
116
                    \PDO::PARAM_INT
117
                );
118
        }
119
120
        return $query;
121
    }
122
123
    /**
124
     * @param LoopResult $loopResult
125
     * @return array|mixed|\Propel\Runtime\Collection\ObjectCollection
126
     */
127
    protected function getAttributesType(LoopResult $loopResult)
128
    {
129
        $attributeIds = array();
130
131
        /** @var AttributeModel $attribute */
132
        foreach ($loopResult->getResultDataCollection() as $attribute) {
133
            $attributeIds[] = $attribute->getId();
134
        }
135
136
        $join = new Join();
137
138
        $join->addExplicitCondition(
139
            AttributeAttributeTypeTableMap::TABLE_NAME,
140
            'ATTRIBUTE_TYPE_ID',
141
            null,
142
            AttributeTypeTableMap::TABLE_NAME,
143
            'ID',
144
            null
145
        );
146
147
        $join->setJoinType(Criteria::INNER_JOIN);
148
149
        $query = AttributeAttributeTypeQuery::create()
150
            ->filterByAttributeId($attributeIds, Criteria::IN)
151
            ->addJoinObject($join);
152
153
        return $query
154
            ->withColumn('`attribute_type`.`SLUG`', 'SLUG')
155
            ->find();
156
    }
157
158
    /**
159
     * @param string $slug
160
     * @return string
161
     */
162
    protected function formatSlug($slug)
163
    {
164
        return strtoupper(str_replace('-', '_', $slug));
165
    }
166
167
    /**
168
     * @param LoopResult $loopResult
169
     * @return LoopResult
170
     * @throws \Propel\Runtime\Exception\PropelException
171
     */
172
    public function parseResults(LoopResult $loopResult)
173
    {
174
        $attributeTypes = $this->getAttributesType($loopResult);
175
176
        $slugs = array();
177
178
        /** @var AttributeType $attributeType */
179
        foreach ($attributeTypes as $attributeType) {
180
            $slugs[$attributeType->getVirtualColumn('SLUG')] = true;
181
        }
182
183
        /** @var AttributeModel $attribute */
184
        foreach ($loopResult->getResultDataCollection() as $attribute) {
185
            $loopResultRow = new LoopResultRow($attribute);
186
            $loopResultRow->set("ID", $attribute->getId())
187
                ->set("IS_TRANSLATED", $attribute->getVirtualColumn('IS_TRANSLATED'))
188
                ->set("LOCALE", $this->locale)
189
                ->set("TITLE", $attribute->getVirtualColumn('i18n_TITLE'))
190
                ->set("CHAPO", $attribute->getVirtualColumn('i18n_CHAPO'))
191
                ->set("DESCRIPTION", $attribute->getVirtualColumn('i18n_DESCRIPTION'))
192
                ->set("POSTSCRIPTUM", $attribute->getVirtualColumn('i18n_POSTSCRIPTUM'))
193
                ->set("POSITION", $this->useAttributePosistion ? $attribute->getPosition() : $attribute->getVirtualColumn('position'))
194
            ;
195
196
            // init slug variable
197
            foreach ($slugs as $slug => $bool) {
198
                $loopResultRow->set(
199
                    $this->formatSlug(
200
                        $slug
201
                    ),
202
                    null
203
                );
204
            }
205
206
            /** @var AttributeAttributeType $attributeType */
207
            foreach ($attributeTypes as $attributeType) {
208
                if ($attributeType->getAttributeId() === $attribute->getId()) {
209
                    $loopResultRow->set(
210
                        $this->formatSlug(
211
                            $attributeType->getVirtualColumn('SLUG')
212
                        ),
213
                        true
214
                    );
215
                }
216
            }
217
218
            $this->addOutputFields($loopResultRow, $attribute);
219
220
            $loopResult->addRow($loopResultRow);
221
        }
222
223
        return $loopResult;
224
    }
225
}
226