1
|
|
|
<?php |
2
|
|
|
/*************************************************************************************/ |
3
|
|
|
/* This file is part of the RewriteUrl module for Thelia. */ |
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 RewriteUrl\EventListeners; |
14
|
|
|
|
15
|
|
|
use RewriteUrl\Event\RewriteUrlEvent; |
16
|
|
|
use RewriteUrl\Event\RewriteUrlEvents; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
19
|
|
|
use Thelia\Model\RewritingUrlQuery; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class RewriteUrlListener |
23
|
|
|
* @package RewriteUrl\EventListeners |
24
|
|
|
* @author Vincent Lopes <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class RewriteUrlListener implements EventSubscriberInterface |
27
|
|
|
{ |
28
|
|
|
protected $dispatcher; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* RewriteUrlListener constructor. |
32
|
|
|
* @param EventDispatcherInterface $dispatcher |
33
|
|
|
*/ |
34
|
|
|
public function __construct(EventDispatcherInterface $dispatcher) |
35
|
|
|
{ |
36
|
|
|
$this->dispatcher = $dispatcher; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public static function getSubscribedEvents() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
RewriteUrlEvents::REWRITEURL_DELETE =>['deleteRewrite'], |
46
|
|
|
RewriteUrlEvents::REWRITEURL_UPDATE =>['updateRewrite'], |
47
|
|
|
RewriteUrlEvents::REWRITEURL_ADD =>['addRewrite'], |
48
|
|
|
RewriteUrlEvents::REWRITEURL_SET_DEFAULT =>['setDefaultRewrite'] |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param RewriteUrlEvent $event |
54
|
|
|
*/ |
55
|
|
|
public function deleteRewrite(RewriteUrlEvent $event) |
56
|
|
|
{ |
57
|
|
|
$rewritingUrl = $event->getRewritingUrl(); |
58
|
|
|
|
59
|
|
|
$newDefault = null; |
60
|
|
|
|
61
|
|
|
// test if default url |
62
|
|
|
if ($event->getRewritingUrl()->getRedirected() === null) { |
63
|
|
|
// add new default url |
64
|
|
|
if (null !== $newDefault = RewritingUrlQuery::create()->findOneByRedirected($rewritingUrl->getId())) { |
65
|
|
|
$this->dispatcher->dispatch( |
66
|
|
|
RewriteUrlEvents::REWRITEURL_UPDATE, |
67
|
|
|
new RewriteUrlEvent( |
68
|
|
|
$newDefault->setRedirected(null) |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$isRedirection = RewritingUrlQuery::create()->findByRedirected($rewritingUrl->getId()); |
75
|
|
|
|
76
|
|
|
//Update urls who redirected to deleted URL |
77
|
|
|
/** @var \Thelia\Model\RewritingUrl $redirected */ |
78
|
|
|
foreach ($isRedirection as $redirected) { |
79
|
|
|
$this->dispatcher->dispatch( |
80
|
|
|
RewriteUrlEvents::REWRITEURL_UPDATE, |
81
|
|
|
new RewriteUrlEvent( |
82
|
|
|
$redirected->setRedirected( |
83
|
|
|
($newDefault !== null) ? $newDefault->getId() : $rewritingUrl->getRedirected() |
84
|
|
|
) |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$rewritingUrl->delete(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param RewriteUrlEvent $event |
94
|
|
|
* @throws \Exception |
95
|
|
|
* @throws \Propel\Runtime\Exception\PropelException |
96
|
|
|
*/ |
97
|
|
|
public function addRewrite(RewriteUrlEvent $event) |
98
|
|
|
{ |
99
|
|
|
$rewritingUrl = $event->getRewritingUrl(); |
100
|
|
|
$rewritingUrl->save(); |
101
|
|
|
|
102
|
|
|
if ($rewritingUrl->getRedirected() === null) { |
103
|
|
|
//check if the new redirect is set to default if yes redirect all to the new one |
104
|
|
|
RewritingUrlQuery::create() |
105
|
|
|
->filterByView($rewritingUrl->getView()) |
106
|
|
|
->filterByViewId($rewritingUrl->getViewId()) |
107
|
|
|
->filterByViewLocale($rewritingUrl->getViewLocale()) |
108
|
|
|
->update(array( |
109
|
|
|
"Redirected" => $rewritingUrl->getId() |
110
|
|
|
)); |
111
|
|
|
|
112
|
|
|
//Re set new url to default |
113
|
|
|
$rewritingDefault = RewritingUrlQuery::create()->findOneById($rewritingUrl->getId()); |
114
|
|
|
$rewritingDefault->setRedirected(null); |
115
|
|
|
$rewritingDefault->save(); |
116
|
|
|
} else { |
117
|
|
|
$redirectType = $event->getRedirectType(); |
118
|
|
|
if ($redirectType !== null) { |
119
|
|
|
$redirectType->setId($rewritingUrl->getId()); |
120
|
|
|
$redirectType->save(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param RewriteUrlEvent $event |
127
|
|
|
* @throws \Exception |
128
|
|
|
* @throws \Propel\Runtime\Exception\PropelException |
129
|
|
|
*/ |
130
|
|
|
public function setDefaultRewrite(RewriteUrlEvent $event) |
131
|
|
|
{ |
132
|
|
|
$rewritingUrl = $event->getRewritingUrl(); |
133
|
|
|
|
134
|
|
|
//redirect all url to the new one |
135
|
|
|
RewritingUrlQuery::create() |
136
|
|
|
->filterByView($rewritingUrl->getView()) |
137
|
|
|
->filterByViewId($rewritingUrl->getViewId()) |
138
|
|
|
->filterByViewLocale($rewritingUrl->getViewLocale()) |
139
|
|
|
->update(array( |
140
|
|
|
"Redirected" => $rewritingUrl->getId() |
141
|
|
|
)); |
142
|
|
|
|
143
|
|
|
//Re set new url to default |
144
|
|
|
$rewritingUrl->setRedirected(null); |
145
|
|
|
$rewritingUrl->save(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param RewriteUrlEvent $event |
150
|
|
|
* @throws \Exception |
151
|
|
|
* @throws \Propel\Runtime\Exception\PropelException |
152
|
|
|
*/ |
153
|
|
|
public function updateRewrite(RewriteUrlEvent $event) |
154
|
|
|
{ |
155
|
|
|
$event->getRewritingUrl()->save(); |
156
|
|
|
$redirectType = $event->getRedirectType(); |
157
|
|
|
if ($redirectType !== null) { |
158
|
|
|
$redirectType->save(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|