AttributeTypeController   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 299
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 2
dl 0
loc 299
rs 9.84
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A viewAllAction() 0 8 2
B viewAction() 0 53 6
A createAction() 0 27 3
A updateAction() 0 33 3
A deleteAction() 0 36 4
B copyAction() 0 50 5
B hydrateAttributeTypeByForm() 0 39 8
A viewAttribute() 0 6 1
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\Controller;
10
11
use AttributeType\Model\AttributeTypeI18n;
12
use AttributeType\Model\AttributeTypeQuery;
13
use AttributeType\Event\AttributeTypeEvent;
14
use AttributeType\Event\AttributeTypeEvents;
15
use AttributeType\Model\AttributeType;
16
use AttributeType\AttributeType as AttributeTypeCore;
17
use Symfony\Component\Form\Form;
18
use Thelia\Controller\Admin\BaseAdminController;
19
use Thelia\Core\Security\AccessManager;
20
use Thelia\Core\Security\Resource\AdminResources;
21
use Thelia\Core\Translation\Translator;
22
use Thelia\Model\LangQuery;
23
use Thelia\Core\HttpFoundation\Response;
24
25
/**
26
 * Class AttributeTypeController
27
 * @package AttributeType\Controller
28
 * @author Gilles Bourgeat <[email protected]>
29
 */
30
class AttributeTypeController extends BaseAdminController
31
{
32
    protected $objectName = 'Attribute type';
33
34
    /**
35
     * @param array $params
36
     * @return Response
37
     */
38
    public function viewAllAction($params = array())
39
    {
40
        if (null !== $response = $this->checkAuth(array(), 'AttributeType', AccessManager::VIEW)) {
41
            return $response;
42
        }
43
44
        return $this->render("attribute-type/configuration", $params);
45
    }
46
47
    /**
48
     * @param int $id
49
     * @return Response
50
     * @throws \Exception
51
     */
52
    public function viewAction($id)
53
    {
54
        if (null !== $response = $this->checkAuth(array(), 'AttributeType', AccessManager::VIEW)) {
55
            return $response;
56
        }
57
58
        if (null === $attributeType = AttributeTypeQuery::create()->findPk($id)) {
59
            throw new \Exception(Translator::getInstance()->trans(
60
                "Attribute type not found",
61
                array(),
62
                AttributeTypeCore::MODULE_DOMAIN
63
            ));
64
        }
65
66
        $title = array();
67
        $description = array();
68
69
        /** @var AttributeTypeI18n $i18n */
70
        foreach ($attributeType->getAttributeTypeI18ns() as $i18n) {
71
            if (null !== $lang = LangQuery::create()->findOneByLocale($i18n->getLocale())) {
72
                $title[$lang->getId()] = $i18n->getTitle();
73
                $description[$lang->getId()] = $i18n->getDescription();
74
            }
75
        }
76
77
        $form = $this->createForm('attribute_type.update', 'form', array(
78
            'id' => $attributeType->getId(),
79
            'slug' => $attributeType->getSlug(),
80
            'pattern' => $attributeType->getPattern(),
81
            'css_class' => $attributeType->getCssClass(),
82
            'has_attribute_av_value' => $attributeType->getHasAttributeAvValue(),
83
            'is_multilingual_attribute_av_value' => $attributeType->getIsMultilingualAttributeAvValue(),
84
            'input_type' => $attributeType->getInputType(),
85
            'min' => $attributeType->getMin(),
86
            'max' => $attributeType->getMax(),
87
            'step' => $attributeType->getStep(),
88
            'image_max_width' => $attributeType->getImageMaxWidth(),
89
            'image_max_height' => $attributeType->getImageMaxHeight(),
90
            'image_ratio' => $attributeType->getImageRatio(),
91
            'title' => $title,
92
            'description' => $description
93
        ));
94
95
        $this->getParserContext()->addForm($form);
96
97
        if ($this->getRequest()->isXmlHttpRequest()) {
98
            return $this->render("attribute-type/include/form-update");
99
        } else {
100
            return $this->viewAllAction(array(
101
                'attribute_type_id' => $id
102
            ));
103
        }
104
    }
105
106
    /**
107
     * @return Response
108
     */
109
    public function createAction()
110
    {
111
        if (null !== $response = $this->checkAuth(array(), 'AttributeType', AccessManager::CREATE)) {
112
            return $response;
113
        }
114
115
        $form = $this->createForm('attribute_type.create');
116
117
        try {
118
            $this->dispatch(
119
                AttributeTypeEvents::ATTRIBUTE_TYPE_CREATE,
120
                new AttributeTypeEvent($this->hydrateAttributeTypeByForm(
121
                    $this->validateForm($form, 'POST')
122
                ))
123
            );
124
125
            return $this->generateSuccessRedirect($form);
126
        } catch (\Exception $e) {
127
            $this->setupFormErrorContext(
128
                $this->getTranslator()->trans("%obj modification", array('%obj' => $this->objectName)),
129
                $e->getMessage(),
130
                $form
131
            );
132
133
            return $this->viewAllAction();
134
        }
135
    }
136
137
    /**
138
     * @param int $id
139
     * @return Response
140
     */
141
    public function updateAction($id)
142
    {
143
        if (null !== $response = $this->checkAuth(array(), 'AttributeType', AccessManager::UPDATE)) {
144
            return $response;
145
        }
146
147
        $form = $this->createForm('attribute_type.update');
148
149
        try {
150
            $this->dispatch(
151
                AttributeTypeEvents::ATTRIBUTE_TYPE_UPDATE,
152
                new AttributeTypeEvent(
153
                    $this->hydrateAttributeTypeByForm(
154
                        $this->validateForm($form, 'POST'),
155
                        $id
156
                    )
157
                )
158
            );
159
160
            return $this->generateSuccessRedirect($form);
161
162
        } catch (\Exception $e) {
163
            $this->setupFormErrorContext(
164
                $this->getTranslator()->trans("%obj modification", array('%obj' => $this->objectName)),
165
                $e->getMessage(),
166
                $form
167
            );
168
169
            return $this->viewAllAction(array(
170
                'attribute_type_id' => $id
171
            ));
172
        }
173
    }
174
175
    /**
176
     * @param int $id
177
     * @return Response
178
     */
179
    public function deleteAction($id)
180
    {
181
        if (null !== $response = $this->checkAuth(array(), 'AttributeType', AccessManager::DELETE)) {
182
            return $response;
183
        }
184
185
        $form = $this->createForm('attribute_type.delete');
186
187
        try {
188
            $this->validateForm($form, 'POST');
189
190
            if (null === $attributeType = AttributeTypeQuery::create()->findPk($id)) {
191
                throw new \Exception(Translator::getInstance()->trans(
192
                    "Attribute type not found",
193
                    array(),
194
                    AttributeTypeCore::MODULE_DOMAIN
195
                ));
196
            }
197
198
            $this->dispatch(
199
                AttributeTypeEvents::ATTRIBUTE_TYPE_DELETE,
200
                new AttributeTypeEvent($attributeType)
201
            );
202
203
            return $this->generateSuccessRedirect($form);
204
205
        } catch (\Exception $e) {
206
            $this->setupFormErrorContext(
207
                $this->getTranslator()->trans("%obj modification", array('%obj' => $this->objectName)),
208
                $e->getMessage(),
209
                $form
210
            );
211
212
            return $this->viewAllAction();
213
        }
214
    }
215
216
    /**
217
     * @param int $id
218
     * @return Response
219
     * @throws \Exception
220
     */
221
    public function copyAction($id)
222
    {
223
        if (null !== $response = $this->checkAuth(array(), 'AttributeType', AccessManager::CREATE)) {
224
            return $response;
225
        }
226
227
        if (null === $attributeType = AttributeTypeQuery::create()->findPk($id)) {
228
            throw new \Exception(Translator::getInstance()->trans(
229
                "Attribute type not found",
230
                array(),
231
                AttributeTypeCore::MODULE_DOMAIN
232
            ));
233
        }
234
235
        $title = array();
236
        $description = array();
237
238
        /** @var AttributeTypeI18n $i18n */
239
        foreach ($attributeType->getAttributeTypeI18ns() as $i18n) {
240
            if (null !== $lang = LangQuery::create()->findOneByLocale($i18n->getLocale())) {
241
                $title[$lang->getId()] = $i18n->getTitle();
242
                $description[$lang->getId()] = $i18n->getDescription();
243
            }
244
        }
245
246
        $form = $this->createForm('attribute_type.create', 'form', array(
247
            'slug' => $attributeType->getSlug() . '_' . Translator::getInstance()->trans(
248
                'copy',
249
                array(),
250
                AttributeTypeCore::MODULE_DOMAIN
251
            ),
252
            'pattern' => $attributeType->getPattern(),
253
            'css_class' => $attributeType->getCssClass(),
254
            'has_attribute_av_value' => $attributeType->getHasAttributeAvValue(),
255
            'is_multilingual_attribute_av_value' => $attributeType->getIsMultilingualAttributeAvValue(),
256
            'input_type' => $attributeType->getInputType(),
257
            'min' => $attributeType->getMin(),
258
            'max' => $attributeType->getMax(),
259
            'step' => $attributeType->getStep(),
260
            'image_max_width' => $attributeType->getImageMaxWidth(),
261
            'image_max_height' => $attributeType->getImageMaxHeight(),
262
            'image_ratio' => $attributeType->getImageRatio(),
263
            'title' => $title,
264
            'description' => $description
265
        ));
266
267
        $this->getParserContext()->addForm($form);
268
269
        return $this->render("attribute-type/include/form-create");
270
    }
271
272
    /**
273
     * @param Form $form
274
     * @param int|null $id
275
     * @return AttributeType
276
     * @throws \Exception
277
     */
278
    protected function hydrateAttributeTypeByForm($form, $id = null)
279
    {
280
        $data = $form->getData();
281
282
        if ($id !== null) {
283
            if (null === $attributeType = AttributeTypeQuery::create()->findPk($id)) {
284
                throw new \Exception(Translator::getInstance()->trans(
285
                    "Attribute type not found",
286
                    array(),
287
                    AttributeTypeCore::MODULE_DOMAIN
288
                ));
289
            }
290
        } else {
291
            $attributeType = new AttributeType();
292
        }
293
294
        $attributeType
295
            ->setSlug($data['slug'])
296
            ->setPattern($data['pattern'])
297
            ->setCssClass($data['css_class'])
298
            ->setHasAttributeAvValue(isset($data['has_attribute_av_value']) && (int) $data['has_attribute_av_value'] ? 1 : 0)
299
            ->setIsMultilingualAttributeAvValue(isset($data['is_multilingual_attribute_av_value']) && (int) $data['is_multilingual_attribute_av_value'] ? 1 : 0)
300
            ->setInputType($data['input_type'])
301
            ->setMin($data['min'])
302
            ->setMax($data['max'])
303
            ->setStep($data['step'])
304
            ->setImageMaxWidth($data['image_max_width'])
305
            ->setImageMaxHeight($data['image_max_height'])
306
            ->setImageRatio($data['image_ratio']);
307
308
        foreach ($data['title'] as $langId => $title) {
309
            $attributeType
310
                ->setLocale(LangQuery::create()->findPk($langId)->getLocale())
311
                ->setTitle($title)
312
                ->setDescription($data['description'][$langId]);
313
        }
314
315
        return $attributeType;
316
    }
317
318
    /**
319
     * @param int $id
320
     * @return Response
321
     */
322
    protected function viewAttribute($id)
323
    {
324
        return $this->render("attribute-edit", array(
325
            'attribute_id' => $id
326
        ));
327
    }
328
}
329