FeatureEditHook::__construct()   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\Hook;
10
11
use FeatureType\Form\FeatureTypeAvMetaUpdateForm;
12
use FeatureType\Model\FeatureFeatureType;
13
use FeatureType\Model\FeatureFeatureTypeQuery;
14
use FeatureType\Model\FeatureTypeAvMeta;
15
use FeatureType\Model\FeatureTypeAvMetaQuery;
16
use FeatureType\Model\Map\FeatureFeatureTypeTableMap;
17
use FeatureType\Model\Map\FeatureTypeAvMetaTableMap;
18
use Propel\Runtime\ActiveQuery\Criteria;
19
use Propel\Runtime\ActiveQuery\Join;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
use Thelia\Core\Event\Hook\HookRenderEvent;
22
use Thelia\Core\Hook\BaseHook;
23
use Thelia\Core\Template\ParserContext;
24
use Thelia\Core\Thelia;
25
use Thelia\Model\FeatureAv;
26
use Thelia\Model\FeatureAvQuery;
27
use Thelia\Model\Lang;
28
use Thelia\Model\LangQuery;
29
30
/**
31
 * Class FeatureEditHook
32
 * @package FeatureType\Hook
33
 * @author Gilles Bourgeat <[email protected]>
34
 */
35
class FeatureEditHook extends BaseHook
36
{
37
    /** @var ContainerInterface */
38
    protected $container = null;
39
40
    /**
41
     * @param ContainerInterface $container
42
     */
43
    public function __construct(ContainerInterface $container)
44
    {
45
        $this->container = $container;
46
    }
47
48
    /**
49
     * @param HookRenderEvent $event
50
     */
51
    public function onFeatureEditBottom(HookRenderEvent $event)
52
    {
53
        $data = self::hydrateForm($event->getArgument('feature_id'));
54
55
        /** @var ParserContext $parserContext */
56
        $parserContext = $this->container->get('thelia.parser.context');
57
        $form = $parserContext->getForm('feature_type_av_meta.update', FeatureTypeAvMetaUpdateForm::class, 'form');
58
59
        if (!$form) {
60
            $form = new FeatureTypeAvMetaUpdateForm(
61
                $this->getRequest(),
62
                'form',
63
                $data,
64
                array(),
65
                $this->container
66
            );
67
        }
68
69
        $this->container->get('thelia.parser.context')->addForm($form);
70
71
        $event->add($this->render(
72
            'feature-type/hook/feature-edit-bottom.html',
73
            array(
74
                'feature_id' => $event->getArgument('feature_id'),
75
                'form_meta_data' => $data
76
            )
77
        ));
78
    }
79
80
    /**
81
     * @param HookRenderEvent $event
82
     */
83
    public function onFeatureEditJs(HookRenderEvent $event)
84
    {
85
        // Fix for Thelia 2.1, because the hook "feature-edit.bottom" does not exist
86
        if (version_compare(Thelia::THELIA_VERSION, '2.2', '<')) {
87
            $event->add('<script type="text/template" id="feature-type-fix-t21">');
88
            self::onFeatureEditBottom($event);
89
            $event->add('</script>');
90
        }
91
92
        $event->add($this->render(
93
            'feature-type/hook/feature-edit-js.html',
94
            array(
95
                'feature_id' => $event->getArgument('feature_id')
96
            )
97
        ));
98
    }
99
100
    /**
101
     * @param FeatureAv $featureAv
102
     * @return array|mixed|\Propel\Runtime\Collection\ObjectCollection
103
     */
104
    protected function getFeatureTypeAvMetas(FeatureAv $featureAv)
105
    {
106
        $join = new Join();
107
108
        $join->addExplicitCondition(
109
            FeatureTypeAvMetaTableMap::TABLE_NAME,
110
            'FEATURE_FEATURE_TYPE_ID',
111
            null,
112
            FeatureFeatureTypeTableMap::TABLE_NAME,
113
            'ID',
114
            null
115
        );
116
117
        $join->setJoinType(Criteria::INNER_JOIN);
118
119
        return FeatureTypeAvMetaQuery::create()
120
            ->filterByFeatureAvId($featureAv->getId())
121
            ->addJoinObject($join)
122
            ->withColumn('`feature_feature_type`.`feature_type_id`', 'FEATURE_TYPE_ID')
123
            ->find();
124
    }
125
126
    /**
127
     * @param int $featureId
128
     * @return array
129
     */
130
    protected function hydrateForm($featureId)
131
    {
132
        $data = array('feature_av' => array());
133
134
        $featureAvs = FeatureAvQuery::create()->findByFeatureId($featureId);
135
136
        $featureTypes = FeatureFeatureTypeQuery::create()->findByFeatureId($featureId);
137
138
        $langs = LangQuery::create()->find();
139
140
        /** @var FeatureAv $featureAv */
141
        foreach ($featureAvs as $featureAv) {
142
            $featureAvMetas = self::getFeatureTypeAvMetas($featureAv);
143
144
            $data['feature_av'][$featureAv->getId()] = array(
145
                'lang' => array()
146
            );
147
148
            /** @var Lang $lang */
149
            foreach ($langs as $lang) {
150
                $data['feature_av'][$featureAv->getId()]['lang'][$lang->getId()] = array(
151
                    'feature_type' => array()
152
                );
153
154
                /** @var FeatureTypeAvMeta $featureAvMeta */
155
                foreach ($featureAvMetas as $featureAvMeta) {
156
                    /** @var FeatureFeatureType $featureType */
157
                    foreach ($featureTypes as $featureType) {
158
                        if ($featureAvMeta->getLocale() === $lang->getLocale()
159
                            && intval($featureAvMeta->getVirtualColumn("FEATURE_TYPE_ID")) === $featureType->getFeatureTypeId()
160
                        ) {
161
                            $data['feature_av'][$featureAv->getId()]['lang'][$lang->getId()]['feature_type'][$featureType->getFeatureTypeId()] = $featureAvMeta->getValue();
162
                        }
163
                    }
164
                }
165
            }
166
        }
167
168
        return $data;
169
    }
170
}
171