|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Category\Observers\UrlRewriteUpdateObserver |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import-category |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Category\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Category\Utils\MemberNames; |
|
24
|
|
|
use TechDivision\Import\Category\Utils\CoreConfigDataKeys; |
|
25
|
|
|
use TechDivision\Import\Category\Utils\ColumnKeys; |
|
26
|
|
|
use TechDivision\Import\Category\Utils\ConfigurationKeys; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Observer that creates/updates the category's URL rewrites. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Tim Wagner <[email protected]> |
|
32
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
34
|
|
|
* @link https://github.com/techdivision/import-category |
|
35
|
|
|
* @link http://www.techdivision.com |
|
36
|
|
|
*/ |
|
37
|
|
|
class UrlRewriteUpdateObserver extends UrlRewriteObserver |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Array with the existing URL rewrites of the actual category. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $existingUrlRewrites = array(); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Return's the URL rewrite for the passed request path. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $requestPath The request path to return the URL rewrite for |
|
51
|
|
|
* |
|
52
|
|
|
* @return array|null The URL rewrite |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function getExistingUrlRewrite($requestPath) |
|
55
|
|
|
{ |
|
56
|
|
|
if (isset($this->existingUrlRewrites[$requestPath])) { |
|
57
|
|
|
return $this->existingUrlRewrites[$requestPath]; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Remove's the passed URL rewrite from the existing one's. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $urlRewrite The URL rewrite to remove |
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function removeExistingUrlRewrite(array $urlRewrite) |
|
69
|
|
|
{ |
|
70
|
|
|
|
|
71
|
|
|
// load store ID and request path |
|
72
|
|
|
$requestPath = $urlRewrite[MemberNames::REQUEST_PATH]; |
|
73
|
|
|
|
|
74
|
|
|
// query whether or not the URL rewrite exists and remove it, if available |
|
75
|
|
|
if (isset($this->existingUrlRewrites[$requestPath])) { |
|
76
|
|
|
unset($this->existingUrlRewrites[$requestPath]); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Process the observer's business logic. |
|
82
|
|
|
* |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function process() |
|
86
|
|
|
{ |
|
87
|
|
|
|
|
88
|
|
|
// process the new URL rewrites first |
|
89
|
|
|
parent::process(); |
|
90
|
|
|
|
|
91
|
|
|
// create redirect URL rewrites for the existing URL rewrites |
|
92
|
|
|
foreach ($this->existingUrlRewrites as $existingUrlRewrite) { |
|
93
|
|
|
// query whether or not 301 redirects have to be created, so don't |
|
94
|
|
|
// create redirects if the the rewrite history has been deactivated |
|
95
|
|
|
if ($this->getSubject()->getCoreConfigData(CoreConfigDataKeys::CATALOG_SEO_SAVE_REWRITES_HISTORY, true)) { |
|
96
|
|
|
|
|
97
|
|
|
// load target path |
|
98
|
|
|
$targetPath = $this->prepareRequestPath(); |
|
99
|
|
|
|
|
100
|
|
|
// skip update of URL rewrite, if nothing to change |
|
101
|
|
|
if ($targetPath === $existingUrlRewrite[MemberNames::TARGET_PATH] && |
|
102
|
|
|
301 === (int)$existingUrlRewrite[MemberNames::REDIRECT_TYPE]) { |
|
103
|
|
|
// stop processing the URL rewrite |
|
104
|
|
|
continue; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// override data with the 301 configuration |
|
108
|
|
|
$attr = array( |
|
109
|
|
|
MemberNames::REDIRECT_TYPE => 301, |
|
110
|
|
|
MemberNames::TARGET_PATH => $targetPath, |
|
111
|
|
|
MemberNames::IS_AUTOGENERATED => 0 |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
// merge and return the prepared URL rewrite |
|
115
|
|
|
$existingUrlRewrite = $this->mergeEntity($existingUrlRewrite, $attr); |
|
116
|
|
|
|
|
117
|
|
|
// create the URL rewrite |
|
118
|
|
|
$this->persistUrlRewrite($existingUrlRewrite); |
|
119
|
|
|
} else { |
|
120
|
|
|
// query whether or not the URL rewrite has to be removed |
|
121
|
|
|
if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_URL_REWRITES) && |
|
122
|
|
|
$this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_URL_REWRITES) |
|
123
|
|
|
) { |
|
124
|
|
|
// delete the existing URL rewrite |
|
125
|
|
|
$this->deleteUrlRewrite($existingUrlRewrite); |
|
126
|
|
|
|
|
127
|
|
|
// log a message, that old URL rewrites have been cleaned-up |
|
128
|
|
|
$this->getSubject() |
|
129
|
|
|
->getSystemLogger() |
|
130
|
|
|
->warning( |
|
131
|
|
|
sprintf( |
|
132
|
|
|
'Cleaned-up %d URL rewrite "%s" for category with path "%s"', |
|
133
|
|
|
$existingUrlRewrite[MemberNames::REQUEST_PATH], |
|
134
|
|
|
$this->getValue(ColumnKeys::PATH) |
|
135
|
|
|
) |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Prepare's the URL rewrites that has to be created/updated. |
|
144
|
|
|
* |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function prepareUrlRewrites() |
|
148
|
|
|
{ |
|
149
|
|
|
|
|
150
|
|
|
// call the parent method |
|
151
|
|
|
parent::prepareUrlRewrites(); |
|
152
|
|
|
|
|
153
|
|
|
// (re-)initialize the array for the existing URL rewrites |
|
154
|
|
|
$this->existingUrlRewrites = array(); |
|
155
|
|
|
|
|
156
|
|
|
// load primary key and entity type |
|
157
|
|
|
$pk = $this->getPrimaryKey(); |
|
158
|
|
|
$entityType = UrlRewriteObserver::ENTITY_TYPE; |
|
159
|
|
|
|
|
160
|
|
|
// load the store ID to use |
|
161
|
|
|
$storeId = $this->getSubject()->getRowStoreId(); |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
// load the existing URL rewrites of the actual entity |
|
164
|
|
|
$existingUrlRewrites = $this->getUrlRewritesByEntityTypeAndEntityIdAndStoreId($entityType, $pk, $storeId); |
|
165
|
|
|
|
|
166
|
|
|
// prepare the existing URL rewrites to improve searching them by store ID/request path |
|
167
|
|
|
foreach ($existingUrlRewrites as $existingUrlRewrite) { |
|
168
|
|
|
// load the request path from the existing URL rewrite |
|
169
|
|
|
$requestPath = $existingUrlRewrite[MemberNames::REQUEST_PATH]; |
|
170
|
|
|
|
|
171
|
|
|
// append the URL rewrite with its store ID/request path |
|
172
|
|
|
$this->existingUrlRewrites[$requestPath] = $existingUrlRewrite; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Initialize the URL rewrite with the passed attributes and returns an instance. |
|
178
|
|
|
* |
|
179
|
|
|
* @param array $attr The URL rewrite attributes |
|
180
|
|
|
* |
|
181
|
|
|
* @return array The initialized URL rewrite |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function initializeUrlRewrite(array $attr) |
|
184
|
|
|
{ |
|
185
|
|
|
|
|
186
|
|
|
// load store ID and request path |
|
187
|
|
|
$categoryId = $attr[MemberNames::ENTITY_ID]; |
|
188
|
|
|
$requestPath = $attr[MemberNames::REQUEST_PATH]; |
|
189
|
|
|
|
|
190
|
|
|
// iterate over the available URL rewrites to find the one that matches the category ID |
|
191
|
|
|
foreach ($this->existingUrlRewrites as $urlRewrite) { |
|
192
|
|
|
// compare the category IDs AND the request path |
|
193
|
|
|
if ($categoryId === $urlRewrite[MemberNames::ENTITY_ID] && |
|
194
|
|
|
$requestPath === $urlRewrite[MemberNames::REQUEST_PATH] |
|
195
|
|
|
) { |
|
196
|
|
|
// if a URL rewrite has been found, do NOT create a redirect |
|
197
|
|
|
$this->removeExistingUrlRewrite($urlRewrite); |
|
198
|
|
|
|
|
199
|
|
|
// if the found URL rewrite has been autogenerated, then update it |
|
200
|
|
|
return $this->mergeEntity($urlRewrite, $attr); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
// simple return the attributes |
|
205
|
|
|
return $attr; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Return's the URL rewrites for the passed URL entity type and ID. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $entityType The entity type to load the URL rewrites for |
|
212
|
|
|
* @param integer $entityId The entity ID to load the URL rewrites for |
|
213
|
|
|
* @param integer $storeId The store ID to load the URL rewrites for |
|
214
|
|
|
* |
|
215
|
|
|
* @return array The URL rewrites |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function getUrlRewritesByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId) |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->getCategoryBunchProcessor()->getUrlRewritesByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Delete's the URL rewrite with the passed attributes. |
|
224
|
|
|
* |
|
225
|
|
|
* @param array $row The attributes of the entity to delete |
|
226
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
|
227
|
|
|
* |
|
228
|
|
|
* @return void |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function deleteUrlRewrite($row, $name = null) |
|
231
|
|
|
{ |
|
232
|
|
|
$this->getCategoryBunchProcessor()->deleteUrlRewrite($row, $name); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.