FeatureValueExtendLoop::getFeaturesMeta()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 9.2
c 0
b 0
f 0
cc 2
nc 2
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\FeatureValue;
22
use Thelia\Model\FeatureProduct;
23
use Thelia\Model\Map\FeatureAvTableMap;
24
25
/**
26
 * Class FeatureValueExtendLoop
27
 * @package FeatureType\Loop
28
 * @author Gilles Bourgeat <[email protected]>
29
 */
30
class FeatureValueExtendLoop extends FeatureValue implements PropelSearchLoopInterface
31
{
32
    /**
33
     * @param LoopResult $loopResult
34
     * @return array|mixed|\Propel\Runtime\Collection\ObjectCollection
35
     */
36
    protected function getFeaturesMeta(LoopResult $loopResult)
37
    {
38
        $featureAvIds = array();
39
40
        /** @var FeatureProduct $featureValue */
41
        foreach ($loopResult->getResultDataCollection() as $featureValue) {
42
            $featureAvIds[] = $featureValue->getFeatureAvId();
43
        }
44
45
        $joinFeatureFeatureType = new Join();
46
47
        $joinFeatureFeatureType->addExplicitCondition(
48
            FeatureTypeAvMetaTableMap::TABLE_NAME,
49
            'FEATURE_FEATURE_TYPE_ID',
50
            null,
51
            FeatureFeatureTypeTableMap::TABLE_NAME,
52
            'ID',
53
            null
54
        );
55
56
        $joinFeatureFeatureType->setJoinType(Criteria::INNER_JOIN);
57
58
        $joinFeatureType = new Join();
59
60
        $joinFeatureType->addExplicitCondition(
61
            FeatureFeatureTypeTableMap::TABLE_NAME,
62
            'FEATURE_TYPE_ID',
63
            null,
64
            FeatureTypeTableMap::TABLE_NAME,
65
            'ID',
66
            null
67
        );
68
69
        $joinFeatureType->setJoinType(Criteria::INNER_JOIN);
70
71
        $query = FeatureTypeAvMetaQuery::create()
72
            ->filterByLocale($this->locale)
73
            ->filterByFeatureAvId($featureAvIds, Criteria::IN)
74
            ->addJoinObject($joinFeatureFeatureType)
75
            ->addJoinObject($joinFeatureType);
76
77
        $query->withColumn('`feature_type`.`SLUG`', 'SLUG');
78
79
        return $query->find();
80
    }
81
82
    /**
83
     * @param string $slug
84
     * @return string
85
     */
86
    protected function formatSlug($slug)
87
    {
88
        return strtoupper(str_replace('-', '_', $slug));
89
    }
90
91
    /**
92
     * @param LoopResult $loopResult
93
     * @return LoopResult
94
     * @throws \Propel\Runtime\Exception\PropelException
95
     */
96
    public function parseResults(LoopResult $loopResult)
97
    {
98
        $featuresMeta = self::getFeaturesMeta($loopResult);
99
100
        $slugs = array();
101
102
        /** @var FeatureTypeAvMeta $featureMeta */
103
        foreach ($featuresMeta as $featureMeta) {
104
            $slugs[$featureMeta->getVirtualColumn('SLUG')] = true;
105
        }
106
107
        /** @var FeatureProduct $featureValue */
108
        foreach ($loopResult->getResultDataCollection() as $featureValue) {
109
            $loopResultRow = new LoopResultRow($featureValue);
110
111
            $loopResultRow
112
                ->set("ID", $featureValue->getId())
113
                ->set("PRODUCT", $featureValue->getProductId())
114
                ->set("FEATURE_AV_ID", $featureValue->getFeatureAvId())
115
                ->set("FREE_TEXT_VALUE", $featureValue->getFreeTextValue())
116
                ->set("IS_FREE_TEXT", is_null($featureValue->getFeatureAvId()) ? 1 : 0)
117
                ->set("IS_FEATURE_AV", is_null($featureValue->getFeatureAvId()) ? 0 : 1)
118
                ->set("LOCALE", $this->locale)
119
                ->set("TITLE", $featureValue->getVirtualColumn(FeatureAvTableMap::TABLE_NAME . '_i18n_TITLE'))
120
                ->set("CHAPO", $featureValue->getVirtualColumn(FeatureAvTableMap::TABLE_NAME . '_i18n_CHAPO'))
121
                ->set("DESCRIPTION", $featureValue->getVirtualColumn(FeatureAvTableMap::TABLE_NAME . '_i18n_DESCRIPTION'))
122
                ->set("POSTSCRIPTUM", $featureValue->getVirtualColumn(FeatureAvTableMap::TABLE_NAME . '_i18n_POSTSCRIPTUM'))
123
                ->set("POSITION", $featureValue->getPosition())
124
            ;
125
126
            // init slug variable
127
            foreach ($slugs as $slug => $bool) {
128
                $loopResultRow->set(
129
                    self::formatSlug(
130
                        $slug
131
                    ),
132
                    null
133
                );
134
            }
135
136
            /** @var FeatureTypeAvMeta $featureMeta */
137
            foreach ($featuresMeta as $featureMeta) {
138
                if ($featureMeta->getFeatureAvId() === $featureValue->getFeatureAvId()) {
139
                    $loopResultRow->set(
140
                        self::formatSlug(
141
                            $featureMeta->getVirtualColumn('SLUG')
142
                        ),
143
                        $featureMeta->getValue()
144
                    );
145
                }
146
            }
147
148
            $this->addOutputFields($loopResultRow, $featureValue);
149
150
            $loopResult->addRow($loopResultRow);
151
        }
152
153
        return $loopResult;
154
    }
155
}
156