AttributeEditHook   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 136
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onAttributeEditBottom() 0 28 2
A onAttributeEditJs() 0 16 2
A getAttributeTypeAvMetas() 0 21 1
B hydrateForm() 0 40 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\Hook;
10
11
use AttributeType\Form\AttributeTypeAvMetaUpdateForm;
12
use AttributeType\Model\AttributeAttributeType;
13
use AttributeType\Model\AttributeAttributeTypeQuery;
14
use AttributeType\Model\AttributeTypeAvMeta;
15
use AttributeType\Model\AttributeTypeAvMetaQuery;
16
use AttributeType\Model\Map\AttributeAttributeTypeTableMap;
17
use AttributeType\Model\Map\AttributeTypeAvMetaTableMap;
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\AttributeAv;
26
use Thelia\Model\AttributeAvQuery;
27
use Thelia\Model\Lang;
28
use Thelia\Model\LangQuery;
29
30
/**
31
 * Class AttributeEditHook
32
 * @package AttributeType\Hook
33
 * @author Gilles Bourgeat <[email protected]>
34
 */
35
class AttributeEditHook 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 onAttributeEditBottom(HookRenderEvent $event)
52
    {
53
        $data = $this->hydrateForm($event->getArgument('attribute_id'));
54
55
        /** @var ParserContext $parserContext */
56
        $parserContext = $this->container->get('thelia.parser.context');
57
        $form = $parserContext->getForm('attribute_type_av_meta.update', AttributeTypeAvMetaUpdateForm::class, 'form');
58
59
        if (!$form) {
60
            $form = new AttributeTypeAvMetaUpdateForm(
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
            'attribute-type/hook/attribute-edit-bottom.html',
73
            array(
74
                'attribute_id' => $event->getArgument('attribute_id'),
75
                'form_meta_data' => $data
76
            )
77
        ));
78
    }
79
80
    /**
81
     * @param HookRenderEvent $event
82
     */
83
    public function onAttributeEditJs(HookRenderEvent $event)
84
    {
85
        // Fix for Thelia 2.1, because the hook "attribute-edit.bottom" does not exist
86
        if (version_compare(Thelia::THELIA_VERSION, '2.2', '<')) {
87
            $event->add('<script type="text/template" id="attribute-type-fix-t21">');
88
            $this->onAttributeEditBottom($event);
89
            $event->add('</script>');
90
        }
91
92
        $event->add($this->render(
93
            'attribute-type/hook/attribute-edit-js.html',
94
            array(
95
                'attribute_id' => $event->getArgument('attribute_id')
96
            )
97
        ));
98
    }
99
100
    /**
101
     * @param AttributeAv $attributeAv
102
     * @return array|mixed|\Propel\Runtime\Collection\ObjectCollection
103
     */
104
    protected function getAttributeTypeAvMetas(AttributeAv $attributeAv)
105
    {
106
        $join = new Join();
107
108
        $join->addExplicitCondition(
109
            AttributeTypeAvMetaTableMap::TABLE_NAME,
110
            'ATTRIBUTE_ATTRIBUTE_TYPE_ID',
111
            null,
112
            AttributeAttributeTypeTableMap::TABLE_NAME,
113
            'ID',
114
            null
115
        );
116
117
        $join->setJoinType(Criteria::INNER_JOIN);
118
119
        return AttributeTypeAvMetaQuery::create()
120
            ->filterByAttributeAvId($attributeAv->getId())
121
            ->addJoinObject($join)
122
            ->withColumn('`attribute_attribute_type`.`attribute_type_id`', 'ATTRIBUTE_TYPE_ID')
123
            ->find();
124
    }
125
126
    /**
127
     * @param int $attributeId
128
     * @return array
129
     */
130
    protected function hydrateForm($attributeId)
131
    {
132
        $data = array('attribute_av' => array());
133
134
        $attributeAvs = AttributeAvQuery::create()->findByAttributeId($attributeId);
135
136
        $attributeTypes = AttributeAttributeTypeQuery::create()->findByAttributeId($attributeId);
137
138
        $langs = LangQuery::create()->find();
139
140
        /** @var AttributeAv $attributeAv */
141
        foreach ($attributeAvs as $attributeAv) {
142
            $attributeAvMetas = $this->getAttributeTypeAvMetas($attributeAv);
143
144
            $data['attribute_av'][$attributeAv->getId()] = array(
145
                'lang' => array()
146
            );
147
148
            /** @var Lang $lang */
149
            foreach ($langs as $lang) {
150
                $data['attribute_av'][$attributeAv->getId()]['lang'][$lang->getId()] = array(
151
                    'attribute_type' => array()
152
                );
153
154
                /** @var AttributeTypeAvMeta $attributeAvMeta */
155
                foreach ($attributeAvMetas as $attributeAvMeta) {
156
                    /** @var AttributeAttributeType $attributeType */
157
                    foreach ($attributeTypes as $attributeType) {
158
                        if ($attributeAvMeta->getLocale() === $lang->getLocale()
159
                            && intval($attributeAvMeta->getVirtualColumn("ATTRIBUTE_TYPE_ID")) === $attributeType->getAttributeTypeId()
160
                        ) {
161
                            $data['attribute_av'][$attributeAv->getId()]['lang'][$lang->getId()]['attribute_type'][$attributeType->getAttributeTypeId()] = $attributeAvMeta->getValue();
162
                        }
163
                    }
164
                }
165
            }
166
        }
167
168
        return $data;
169
    }
170
}
171