Completed
Pull Request — master (#2363)
by Franck
07:07
created

Template::duplicate()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 5
nop 3
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace Thelia\Action;
14
15
use Propel\Runtime\Propel;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Thelia\Core\Event\Template\TemplateAddAttributeEvent;
19
use Thelia\Core\Event\Template\TemplateAddFeatureEvent;
20
use Thelia\Core\Event\Template\TemplateCreateEvent;
21
use Thelia\Core\Event\Template\TemplateDeleteAttributeEvent;
22
use Thelia\Core\Event\Template\TemplateDeleteEvent;
23
use Thelia\Core\Event\Template\TemplateDeleteFeatureEvent;
24
use Thelia\Core\Event\Template\TemplateDuplicateEvent;
25
use Thelia\Core\Event\Template\TemplateUpdateEvent;
26
use Thelia\Core\Event\TheliaEvents;
27
use Thelia\Core\Event\UpdatePositionEvent;
28
use Thelia\Core\Translation\Translator;
29
use Thelia\Model\AttributeTemplate;
30
use Thelia\Model\AttributeTemplateQuery;
31
use Thelia\Model\CategoryQuery;
32
use Thelia\Model\FeatureTemplate;
33
use Thelia\Model\FeatureTemplateQuery;
34
use Thelia\Model\Map\TemplateTableMap;
35
use Thelia\Model\ProductQuery;
36
use Thelia\Model\Template as TemplateModel;
37
use Thelia\Model\TemplateQuery;
38
39
class Template extends BaseAction implements EventSubscriberInterface
40
{
41
    /**
42
     * Create a new template entry
43
     *
44
     * @param \Thelia\Core\Event\Template\TemplateCreateEvent $event
45
     * @param $eventName
46
     * @param EventDispatcherInterface $dispatcher
47
     */
48
    public function create(TemplateCreateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
49
    {
50
        $template = new TemplateModel();
51
        
52
        $template
53
            ->setDispatcher($dispatcher)
54
            
55
            ->setLocale($event->getLocale())
56
            ->setName($event->getTemplateName())
57
            
58
            ->save()
59
        ;
60
        
61
        $event->setTemplate($template);
62
    }
63
    
64
    /**
65
     * Dupliucate an existing template entry
66
     *
67
     * @param \Thelia\Core\Event\Template\TemplateCreateEvent $event
68
     * @param $eventName
69
     * @param EventDispatcherInterface $dispatcher
70
     */
71
    public function duplicate(TemplateDuplicateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
72
    {
73
        if (null !== $source = TemplateQuery::create()->findPk($event->getSourceTemplateId())) {
74
            $source->setLocale($event->getLocale());
75
            
76
            $createEvent = new TemplateCreateEvent();
77
            $createEvent
78
                ->setLocale($event->getLocale())
79
                ->setTemplateName(
80
                    Translator::getInstance()->trans("Copy of %tpl", ["%tpl" => $source->getName() ])
81
                );
82
            
83
            $dispatcher->dispatch(TheliaEvents::TEMPLATE_CREATE, $createEvent);
84
            
85
            $clone = $createEvent->getTemplate();
86
            
87
            $attrList = AttributeTemplateQuery::create()->findByTemplateId($source->getId());
88
            
89
            /** @var  $feat AttributeTemplate */
90
            foreach ($attrList as $feat) {
91
                $dispatcher->dispatch(
92
                    TheliaEvents::TEMPLATE_ADD_ATTRIBUTE,
93
                    new TemplateAddAttributeEvent($clone, $feat->getAttributeId())
94
                );
95
            }
96
            
97
            $featList = FeatureTemplateQuery::create()->findByTemplateId($source->getId());
98
            
99
            /** @var  $feat FeatureTemplate */
100
            foreach ($featList as $feat) {
101
                $dispatcher->dispatch(
102
                    TheliaEvents::TEMPLATE_ADD_FEATURE,
103
                    new TemplateAddFeatureEvent($clone, $feat->getFeatureId())
104
                );
105
            }
106
            
107
            $event->setTemplate($clone);
108
        }
109
    }
110
    
111
    /**
112
     * Change a product template
113
     *
114
     * @param \Thelia\Core\Event\Template\TemplateUpdateEvent $event
115
     * @param $eventName
116
     * @param EventDispatcherInterface $dispatcher
117
     */
118
    public function update(TemplateUpdateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
119
    {
120
        if (null !== $template = TemplateQuery::create()->findPk($event->getTemplateId())) {
121
            $template
122
                ->setDispatcher($dispatcher)
123
                
124
                ->setLocale($event->getLocale())
125
                ->setName($event->getTemplateName())
126
                ->save();
127
            
128
            $event->setTemplate($template);
129
        }
130
    }
131
    
132
    /**
133
     * Delete a product template entry
134
     *
135
     * @param \Thelia\Core\Event\Template\TemplateDeleteEvent $event
136
     * @param $eventName
137
     * @param EventDispatcherInterface $dispatcher
138
     * @throws \Exception
139
     */
140
    public function delete(TemplateDeleteEvent $event, $eventName, EventDispatcherInterface $dispatcher)
141
    {
142
        if (null !== ($template = TemplateQuery::create()->findPk($event->getTemplateId()))) {
143
            // Check if template is used by a product
144
            $productCount = ProductQuery::create()->findByTemplateId($template->getId())->count();
145
            
146
            if ($productCount <= 0) {
147
                $con = Propel::getWriteConnection(TemplateTableMap::DATABASE_NAME);
148
                $con->beginTransaction();
149
                
150
                try {
151
                    $template
152
                        ->setDispatcher($dispatcher)
153
                        ->delete($con);
154
                    
155
                    // We have to also delete any reference of this template in category tables
156
                    // We can't use a FK here, as the DefaultTemplateId column may be NULL
157
                    // so let's take care of this.
158
                    CategoryQuery::create()
159
                        ->filterByDefaultTemplateId($event->getTemplateId())
160
                        ->update([ 'DefaultTemplateId' => null], $con);
161
                    
162
                    $con->commit();
163
                } catch (\Exception $ex) {
164
                    $con->rollback();
165
                    
166
                    throw $ex;
167
                }
168
            }
169
            
170
            $event->setTemplate($template);
171
            
172
            $event->setProductCount($productCount);
173
        }
174
    }
175
    
176
    public function addAttribute(TemplateAddAttributeEvent $event)
177
    {
178
        if (null === AttributeTemplateQuery::create()
179
                ->filterByAttributeId($event->getAttributeId())
180
                ->filterByTemplate($event->getTemplate())
181
                ->findOne()) {
182
            $attributeTemplate = new AttributeTemplate();
183
            
184
            $attributeTemplate
185
                ->setAttributeId($event->getAttributeId())
186
                ->setTemplate($event->getTemplate())
187
                ->save()
188
            ;
189
        }
190
    }
191
    
192
    /**
193
     * Changes position, selecting absolute ou relative change.
194
     *
195
     * @param UpdatePositionEvent $event
196
     * @param $eventName
197
     * @param EventDispatcherInterface $dispatcher
198
     */
199
    public function updateAttributePosition(UpdatePositionEvent $event, $eventName, EventDispatcherInterface $dispatcher)
200
    {
201
        $this->genericUpdatePosition(AttributeTemplateQuery::create(), $event, $dispatcher);
202
    }
203
    
204
    /**
205
     * Changes position, selecting absolute ou relative change.
206
     *
207
     * @param UpdatePositionEvent $event
208
     * @param $eventName
209
     * @param EventDispatcherInterface $dispatcher
210
     */
211
    public function updateFeaturePosition(UpdatePositionEvent $event, $eventName, EventDispatcherInterface $dispatcher)
212
    {
213
        $this->genericUpdatePosition(FeatureTemplateQuery::create(), $event, $dispatcher);
214
    }
215
    
216
    public function deleteAttribute(TemplateDeleteAttributeEvent $event, $eventName, EventDispatcherInterface $dispatcher)
217
    {
218
        $attributeTemplate = AttributeTemplateQuery::create()
219
            ->filterByAttributeId($event->getAttributeId())
220
            ->filterByTemplate($event->getTemplate())->findOne()
221
        ;
222
        
223
        if ($attributeTemplate !== null) {
224
            $attributeTemplate
225
                ->setDispatcher($dispatcher)
226
                ->delete();
227
        } else {
228
            // Prevent event propagation
229
            $event->stopPropagation();
230
        }
231
    }
232
    
233
    public function addFeature(TemplateAddFeatureEvent $event)
234
    {
235
        if (null === FeatureTemplateQuery::create()
236
                ->filterByFeatureId($event->getFeatureId())
237
                ->filterByTemplate($event->getTemplate())
238
                ->findOne()
239
        ) {
240
            $featureTemplate = new FeatureTemplate();
241
            
242
            $featureTemplate
243
                ->setFeatureId($event->getFeatureId())
244
                ->setTemplate($event->getTemplate())
245
                ->save()
246
            ;
247
        }
248
    }
249
    
250
    public function deleteFeature(TemplateDeleteFeatureEvent $event, $eventName, EventDispatcherInterface $dispatcher)
251
    {
252
        $featureTemplate = FeatureTemplateQuery::create()
253
            ->filterByFeatureId($event->getFeatureId())
254
            ->filterByTemplate($event->getTemplate())->findOne()
255
        ;
256
        
257
        if ($featureTemplate !== null) {
258
            $featureTemplate
259
                ->setDispatcher($dispatcher)
260
                ->delete();
261
        } else {
262
            // Prevent event propagation
263
            $event->stopPropagation();
264
        }
265
    }
266
    
267
    /**
268
     * {@inheritDoc}
269
     */
270
    public static function getSubscribedEvents()
271
    {
272
        return array(
273
            TheliaEvents::TEMPLATE_CREATE          => array("create", 128),
274
            TheliaEvents::TEMPLATE_UPDATE          => array("update", 128),
275
            TheliaEvents::TEMPLATE_DELETE          => array("delete", 128),
276
            TheliaEvents::TEMPLATE_DUPLICATE       => array("duplicate", 128),
277
            
278
            TheliaEvents::TEMPLATE_ADD_ATTRIBUTE    => array("addAttribute", 128),
279
            TheliaEvents::TEMPLATE_DELETE_ATTRIBUTE => array("deleteAttribute", 128),
280
            
281
            TheliaEvents::TEMPLATE_ADD_FEATURE    => array("addFeature", 128),
282
            TheliaEvents::TEMPLATE_DELETE_FEATURE => array("deleteFeature", 128),
283
            
284
            TheliaEvents::TEMPLATE_CHANGE_ATTRIBUTE_POSITION => array('updateAttributePosition', 128),
285
            TheliaEvents::TEMPLATE_CHANGE_FEATURE_POSITION   => array('updateFeaturePosition', 128),
286
        );
287
    }
288
}
289