1 | <?php |
||
39 | class UrlRewriteUpdateObserver extends UrlRewriteObserver |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * Array with the existing URL rewrites of the actual product. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $existingUrlRewrites = array(); |
||
48 | |||
49 | /** |
||
50 | * Process the observer's business logic. |
||
51 | * |
||
52 | * @return void |
||
53 | * @see \TechDivision\Import\Product\UrlRewrite\Observers\UrlRewriteObserver::process() |
||
54 | */ |
||
55 | protected function process() |
||
56 | { |
||
57 | |||
58 | // process the new URL rewrites first |
||
59 | parent::process(); |
||
60 | |||
61 | // load the root category |
||
62 | $rootCategory = $this->getRootCategory(); |
||
63 | |||
64 | // create redirect URL rewrites for the existing URL rewrites |
||
65 | foreach ($this->existingUrlRewrites as $existingUrlRewrite) { |
||
66 | // if the URL rewrite has been created manually |
||
67 | if ((integer) $existingUrlRewrite[MemberNames::IS_AUTOGENERATED] === 0) { |
||
68 | // do NOTHING, because someone really WANTED to create THIS redirect |
||
69 | continue; |
||
70 | } |
||
71 | |||
72 | // query whether or not 301 redirects have to be created, so don't create redirects |
||
73 | // if the product is NOT visible or the rewrite history has been deactivated |
||
74 | if ($this->isVisible() && $this->getSubject()->getCoreConfigData(CoreConfigDataKeys::CATALOG_SEO_SAVE_REWRITES_HISTORY, true)) { |
||
75 | // if the URL rewrite already IS a redirect |
||
76 | if ((integer) $existingUrlRewrite[MemberNames::REDIRECT_TYPE] !== 0) { |
||
77 | // do NOT create another redirect or update the actual one |
||
78 | continue; |
||
79 | } |
||
80 | |||
81 | // initialize the data with the URL rewrites new 301 configuration |
||
82 | $attr = array(MemberNames::REDIRECT_TYPE => 301); |
||
83 | |||
84 | // initialize the category with the root category |
||
85 | $category = $rootCategory; |
||
86 | |||
87 | // load the metadata from the existing URL rewrite |
||
88 | $metadata = $this->getMetadata($existingUrlRewrite); |
||
89 | |||
90 | // query whether or not the URL key of the existing URL rewrite has changed |
||
91 | if (is_array($metadata) && isset($metadata[UrlRewriteObserver::CATEGORY_ID])) { |
||
92 | if (isset($this->urlRewrites[$metadata[UrlRewriteObserver::CATEGORY_ID]])) { |
||
93 | try { |
||
94 | // if yes, try to load the original category and OVERRIDE the default category |
||
95 | $category = $this->getCategory($metadata[UrlRewriteObserver::CATEGORY_ID], $this->getValue(ColumnKeys::STORE_VIEW_CODE)); |
||
96 | } catch (\Exception $e) { |
||
97 | // if the old category can NOT be loaded, remove the |
||
98 | // category ID from the URL rewrites metadata |
||
99 | $attr[MemberNames::METADATA] = null; |
||
100 | |||
101 | // finally log a warning that the old category is not available ony more |
||
102 | $this->getSubject() |
||
103 | ->getSystemLogger() |
||
104 | ->warning( |
||
105 | sprintf( |
||
106 | 'Category with ID "%d" is not longer available for URL rewrite with ID "%d"', |
||
107 | $metadata[UrlRewriteObserver::CATEGORY_ID], |
||
108 | $existingUrlRewrite[MemberNames::URL_REWRITE_ID] |
||
109 | ) |
||
110 | ); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | // load target path/metadata for the actual category |
||
116 | $targetPath = $this->prepareRequestPath($category); |
||
117 | |||
118 | // skip update of URL rewrite, if resulting new target path EQUALS old request path |
||
119 | if ($targetPath === $existingUrlRewrite[MemberNames::REQUEST_PATH]) { |
||
120 | // finally log a warning that the old category is not available ony more |
||
121 | $this->getSubject() |
||
122 | ->getSystemLogger() |
||
123 | ->warning( |
||
124 | sprintf( |
||
125 | 'New target path "%s" eqals request path for URL rewrite with ID "%d"', |
||
126 | $existingUrlRewrite[MemberNames::REQUEST_PATH], |
||
127 | $existingUrlRewrite[MemberNames::URL_REWRITE_ID] |
||
128 | ) |
||
129 | ); |
||
130 | |||
131 | // stop processing the URL rewrite |
||
132 | continue; |
||
133 | } |
||
134 | |||
135 | // set the target path |
||
136 | $attr[MemberNames::TARGET_PATH] = $targetPath; |
||
137 | |||
138 | // merge and return the prepared URL rewrite |
||
139 | $existingUrlRewrite = $this->mergeEntity($existingUrlRewrite, $attr); |
||
140 | |||
141 | // create the URL rewrite |
||
142 | $this->persistUrlRewrite($existingUrlRewrite); |
||
143 | } else { |
||
144 | // query whether or not the URL rewrite has to be removed |
||
145 | if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_URL_REWRITES) && |
||
146 | $this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_URL_REWRITES) |
||
147 | ) { |
||
148 | // delete the existing URL rewrite |
||
149 | $this->deleteUrlRewrite( |
||
150 | array(MemberNames::URL_REWRITE_ID => $existingUrlRewrite[MemberNames::URL_REWRITE_ID]), |
||
151 | SqlStatementKeys::DELETE_URL_REWRITE |
||
152 | ); |
||
153 | |||
154 | // log a message, that old URL rewrites have been cleaned-up |
||
155 | $this->getSubject() |
||
156 | ->getSystemLogger() |
||
157 | ->warning( |
||
158 | sprintf( |
||
159 | 'Cleaned-up URL rewrite "%s" for product with SKU "%s"', |
||
160 | $existingUrlRewrite[MemberNames::REQUEST_PATH], |
||
161 | $this->getValue(ColumnKeys::SKU) |
||
162 | ) |
||
163 | ); |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Remove's the passed URL rewrite from the existing one's. |
||
171 | * |
||
172 | * @param array $urlRewrite The URL rewrite to remove |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | protected function removeExistingUrlRewrite(array $urlRewrite) |
||
187 | |||
188 | /** |
||
189 | * Prepare's the URL rewrites that has to be created/updated. |
||
190 | * |
||
191 | * @return void |
||
192 | * @see \TechDivision\Import\Product\UrlRewrite\Observers\UrlRewriteObserver::prepareUrlRewrites() |
||
193 | */ |
||
194 | protected function prepareUrlRewrites() |
||
195 | { |
||
196 | |||
197 | // (re-)initialize the array for the existing URL rewrites |
||
198 | $this->existingUrlRewrites = array(); |
||
199 | |||
200 | // prepare the new URL rewrites first |
||
201 | parent::prepareUrlRewrites(); |
||
202 | |||
203 | // load the store ID to use |
||
204 | $storeId = $this->getSubject()->getRowStoreId(); |
||
|
|||
205 | |||
206 | // load the existing URL rewrites of the actual entity |
||
207 | $existingUrlRewrites = $this->getUrlRewritesByEntityTypeAndEntityIdAndStoreId( |
||
208 | UrlRewriteObserver::ENTITY_TYPE, |
||
209 | $this->entityId, |
||
210 | $storeId |
||
211 | ); |
||
212 | |||
213 | // prepare the existing URL rewrites to improve searching them by request path |
||
214 | foreach ($existingUrlRewrites as $existingUrlRewrite) { |
||
215 | $this->existingUrlRewrites[$existingUrlRewrite[MemberNames::REQUEST_PATH]] = $existingUrlRewrite; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Initialize the category product with the passed attributes and returns an instance. |
||
221 | * |
||
222 | * @param array $attr The category product attributes |
||
223 | * |
||
224 | * @return array The initialized category product |
||
225 | */ |
||
226 | protected function initializeUrlRewrite(array $attr) |
||
227 | { |
||
228 | |||
229 | // load the category ID of the passed URL rewrite entity |
||
230 | $categoryId = $this->getCategoryIdFromMetadata($attr); |
||
231 | |||
232 | // iterate over the available URL rewrites to find the one that matches the category ID |
||
233 | foreach ($this->existingUrlRewrites as $urlRewrite) { |
||
234 | // compare the category IDs AND the request path |
||
235 | if ($categoryId === $this->getCategoryIdFromMetadata($urlRewrite) && |
||
236 | $attr[MemberNames::REQUEST_PATH] === $urlRewrite[MemberNames::REQUEST_PATH] |
||
237 | ) { |
||
238 | // if a URL rewrite has been found, do NOT create OR keep an existing redirect |
||
239 | $this->removeExistingUrlRewrite($urlRewrite); |
||
240 | |||
241 | // if the found URL rewrite has been created manually |
||
242 | if ((integer) $urlRewrite[MemberNames::IS_AUTOGENERATED] === 0) { |
||
243 | // do NOT update it nor create another redirect |
||
244 | return false; |
||
245 | } |
||
246 | |||
247 | // if the found URL rewrite has been autogenerated, then update it |
||
248 | return $this->mergeEntity($urlRewrite, $attr); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | // simple return the attributes |
||
253 | return $attr; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Extracts the category ID of the passed URL rewrite entity, if available, and return's it. |
||
258 | * |
||
259 | * @param array $attr The URL rewrite entity to extract and return the category ID for |
||
260 | * |
||
261 | * @return integer|null The category ID if available, else NULL |
||
262 | */ |
||
263 | protected function getCategoryIdFromMetadata(array $attr) |
||
264 | { |
||
265 | |||
266 | // load the metadata of the passed URL rewrite entity |
||
267 | $metadata = $this->getMetadata($attr); |
||
268 | |||
269 | // return the category ID from the metadata |
||
270 | return (integer) $metadata[UrlRewriteObserver::CATEGORY_ID]; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Initialize the URL rewrite product => category relation with the passed attributes |
||
275 | * and returns an instance. |
||
276 | * |
||
277 | * @param array $attr The URL rewrite product => category relation attributes |
||
278 | * |
||
279 | * @return array|null The initialized URL rewrite product => category relation |
||
280 | */ |
||
281 | protected function initializeUrlRewriteProductCategory($attr) |
||
282 | { |
||
283 | |||
284 | // try to load the URL rewrite product category relation |
||
285 | if ($urlRewriteProductCategory = $this->loadUrlRewriteProductCategory($attr[MemberNames::URL_REWRITE_ID])) { |
||
286 | return $this->mergeEntity($urlRewriteProductCategory, $attr); |
||
287 | } |
||
288 | |||
289 | // simple return the URL rewrite product category |
||
290 | return $attr; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Return's the unserialized metadata of the passed URL rewrite. If the |
||
295 | * metadata doesn't contain a category ID, the category ID of the root |
||
296 | * category will be added. |
||
297 | * |
||
298 | * @param array $urlRewrite The URL rewrite to return the metadata for |
||
299 | * |
||
300 | * @return array The metadata of the passed URL rewrite |
||
301 | */ |
||
302 | protected function getMetadata($urlRewrite) |
||
303 | { |
||
304 | |||
305 | // initialize the array with the metaddata |
||
306 | $metadata = array(); |
||
307 | |||
308 | // try to unserialize the metadata from the passed URL rewrite |
||
309 | if (isset($urlRewrite[MemberNames::METADATA])) { |
||
310 | $metadata = json_decode($urlRewrite[MemberNames::METADATA], true); |
||
311 | } |
||
312 | |||
313 | // query whether or not a category ID has been found |
||
314 | if (isset($metadata[UrlRewriteObserver::CATEGORY_ID])) { |
||
315 | // if yes, return the metadata |
||
316 | return $metadata; |
||
317 | } |
||
318 | |||
319 | // if not, append the ID of the root category |
||
320 | $rootCategory = $this->getRootCategory(); |
||
321 | $metadata[UrlRewriteObserver::CATEGORY_ID] = (integer) $rootCategory[MemberNames::ENTITY_ID]; |
||
322 | |||
323 | // and return the metadata |
||
324 | return $metadata; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Return's the category with the passed ID. |
||
329 | * |
||
330 | * @param integer $categoryId The ID of the category to return |
||
331 | * @param string $storeViewCode The store view code of the category to return, defaults to "admin" |
||
332 | * |
||
333 | * @return array The category data |
||
334 | */ |
||
335 | protected function getCategory($categoryId, $storeViewCode = StoreViewCodes::ADMIN) |
||
336 | { |
||
337 | return $this->getSubject()->getCategory($categoryId, $storeViewCode); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
342 | * |
||
343 | * @param string $entityType The entity type to load the URL rewrites for |
||
344 | * @param integer $entityId The entity ID to load the URL rewrites for |
||
345 | * @param integer $storeId The store ID to load the URL rewrites for |
||
346 | * |
||
347 | * @return array The URL rewrites |
||
348 | */ |
||
349 | protected function getUrlRewritesByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId) |
||
350 | { |
||
351 | return $this->getProductUrlRewriteProcessor()->getUrlRewritesByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Return's the URL rewrite product category relation for the passed |
||
356 | * URL rewrite ID. |
||
357 | * |
||
358 | * @param integer $urlRewriteId The URL rewrite ID to load the URL rewrite product category relation for |
||
359 | * |
||
360 | * @return array|false The URL rewrite product category relations |
||
361 | */ |
||
362 | protected function loadUrlRewriteProductCategory($urlRewriteId) |
||
363 | { |
||
364 | return $this->getProductUrlRewriteProcessor()->loadUrlRewriteProductCategory($urlRewriteId); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Delete's the URL rewrite with the passed attributes. |
||
369 | * |
||
370 | * @param array $row The attributes of the entity to delete |
||
371 | * @param string|null $name The name of the prepared statement that has to be executed |
||
372 | * |
||
373 | * @return void |
||
374 | */ |
||
375 | protected function deleteUrlRewrite($row, $name = null) |
||
379 | } |
||
380 |
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.