Public Member Functions | |
getStores ($storeId=null) | |
getResource () | |
getCategoryModel () | |
getProductModel () | |
refreshRewrites ($storeId=null) | |
refreshCategoryRewrite ($categoryId, $storeId=null, $refreshProducts=true) | |
refreshProductRewrite ($productId, $storeId=null) | |
refreshProductRewrites ($storeId) | |
getUnusedPath ($storeId, $requestPath, $idPath) | |
getProductUrlSuffix ($storeId) | |
getCategoryUrlSuffix ($storeId) | |
generatePath ($type= 'target', $product=null, $category=null, $parentPath=null) | |
Public Attributes | |
const | MAX_REQUEST_PATH_LENGTH = 240 |
const | ALLOWED_REQUEST_PATH_OVERFLOW = 10 |
Protected Member Functions | |
_refreshCategoryRewrites (Varien_Object $category, $parentPath=null, $refreshProducts=true) | |
_refreshProductRewrite (Varien_Object $product, Varien_Object $category) | |
_refreshCategoryProductRewrites (Varien_Object $category) | |
Protected Attributes | |
$_resourceModel | |
$_categories = array() | |
$_rewrites = array() | |
$_rewrite | |
$_productUrlSuffix = array() | |
$_categoryUrlSuffix = array() |
Definition at line 34 of file Url.php.
_refreshCategoryProductRewrites | ( | Varien_Object $ | category | ) | [protected] |
Refresh products for catwgory
Varien_Object | $category |
Definition at line 278 of file Url.php.
00279 { 00280 $originalRewrites = $this->_rewrites; 00281 $process = true; 00282 $lastEntityId = 0; 00283 while ($process == true) { 00284 $products = $this->getResource()->getProductsByCategory($category, $lastEntityId); 00285 if (!$products) { 00286 $process = false; 00287 break; 00288 } 00289 00290 $this->_rewrites = $this->getResource()->prepareRewrites($category->getStoreId(), $category->getId(), array_keys($products)); 00291 00292 foreach ($products as $product) { 00293 $this->_refreshProductRewrite($product, $category); 00294 } 00295 unset($products); 00296 } 00297 $this->_rewrites = $originalRewrites; 00298 return $this; 00299 }
_refreshCategoryRewrites | ( | Varien_Object $ | category, | |
$ | parentPath = null , |
|||
$ | refreshProducts = true | |||
) | [protected] |
Refresh category rewrite
Varien_Object | $category | |
string | $parentPath |
Definition at line 166 of file Url.php.
00167 { 00168 if ($category->getId() != $this->getStores($category->getStoreId())->getRootCategoryId()) { 00169 if ($category->getUrlKey() == '') { 00170 $urlKey = $this->getCategoryModel()->formatUrlKey($category->getName()); 00171 } 00172 else { 00173 $urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey()); 00174 } 00175 00176 $idPath = $this->generatePath('id', null, $category); 00177 $targetPath = $this->generatePath('target', null, $category); 00178 $requestPath = $this->generatePath('request', null, $category, $parentPath); 00179 00180 $rewriteData = array( 00181 'store_id' => $category->getStoreId(), 00182 'category_id' => $category->getId(), 00183 'product_id' => null, 00184 'id_path' => $idPath, 00185 'request_path' => $requestPath, 00186 'target_path' => $targetPath, 00187 'is_system' => 1 00188 ); 00189 00190 $this->getResource()->saveRewrite($rewriteData, $this->_rewrite); 00191 00192 if ($category->getUrlKey() != $urlKey) { 00193 $category->setUrlKey($urlKey); 00194 $this->getResource()->saveCategoryAttribute($category, 'url_key'); 00195 } 00196 if ($category->getUrlPath() != $requestPath) { 00197 $category->setUrlPath($requestPath); 00198 $this->getResource()->saveCategoryAttribute($category, 'url_path'); 00199 } 00200 } 00201 else { 00202 if ($category->getUrlPath() != '') { 00203 $category->setUrlPath(''); 00204 $this->getResource()->saveCategoryAttribute($category, 'url_path'); 00205 } 00206 } 00207 00208 if ($refreshProducts) { 00209 $this->_refreshCategoryProductRewrites($category); 00210 } 00211 00212 foreach ($category->getChilds() as $child) { 00213 $this->_refreshCategoryRewrites($child, $category->getUrlPath() . '/', $refreshProducts); 00214 } 00215 00216 return $this; 00217 }
_refreshProductRewrite | ( | Varien_Object $ | product, | |
Varien_Object $ | category | |||
) | [protected] |
Refresh product rewrite
Varien_Object | $product | |
Varien_Object | $category |
Definition at line 226 of file Url.php.
00227 { 00228 if ($category->getId() == $category->getPath()) { 00229 return $this; 00230 } 00231 if ($product->getUrlKey() == '') { 00232 $urlKey = $this->getProductModel()->formatUrlKey($product->getName()); 00233 } 00234 else { 00235 $urlKey = $this->getProductModel()->formatUrlKey($product->getUrlKey()); 00236 } 00237 00238 $idPath = $this->generatePath('id', $product, $category); 00239 $targetPath = $this->generatePath('target', $product, $category); 00240 $requestPath = $this->generatePath('request', $product, $category); 00241 $categoryId = null; 00242 $updateKeys = true; 00243 if ($category->getUrlPath()) { 00244 $categoryId = $category->getId(); 00245 $updateKeys = false; 00246 } 00247 00248 $rewriteData = array( 00249 'store_id' => $category->getStoreId(), 00250 'category_id' => $categoryId, 00251 'product_id' => $product->getId(), 00252 'id_path' => $idPath, 00253 'request_path' => $requestPath, 00254 'target_path' => $targetPath, 00255 'is_system' => 1 00256 ); 00257 00258 $this->getResource()->saveRewrite($rewriteData, $this->_rewrite); 00259 00260 if ($updateKeys && $product->getUrlKey() != $urlKey) { 00261 $product->setUrlKey($urlKey); 00262 $this->getResource()->saveProductAttribute($product, 'url_key'); 00263 } 00264 if ($updateKeys && $product->getUrlPath() != $requestPath) { 00265 $product->setUrlPath($requestPath); 00266 $this->getResource()->saveProductAttribute($product, 'url_path'); 00267 } 00268 00269 return $this; 00270 }
generatePath | ( | $ | type = 'target' , |
|
$ | product = null , |
|||
$ | category = null , |
|||
$ | parentPath = null | |||
) |
Generate either id path, request path or target path for product and/or category
For generating id or system path, either product or category is required For generating request path - category is required $parentPath used only for generating category path
string | $type | |
Varien_Object | $product | |
Varien_Object | $category | |
string | $parentPath |
Mage_Core_Exception |
Definition at line 515 of file Url.php.
00516 { 00517 if (!$product && !$category) { 00518 Mage::throwException(Mage::helper('core')->__('Specify either category or product, or both.')); 00519 } 00520 00521 // generate id_path 00522 if ('id' === $type) { 00523 if (!$product) { 00524 return 'category/' . $category->getId(); 00525 } 00526 if ($category && $category->getUrlPath()) { 00527 return 'product/' . $product->getId() . '/' . $category->getId(); 00528 } 00529 return 'product/' . $product->getId(); 00530 } 00531 00532 // generate request_path 00533 if ('request' === $type) { 00534 // for category 00535 if (!$product) { 00536 if ($category->getUrlKey() == '') { 00537 $urlKey = $this->getCategoryModel()->formatUrlKey($category->getName()); 00538 } 00539 else { 00540 $urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey()); 00541 } 00542 00543 $categoryUrlSuffix = $this->getCategoryUrlSuffix($category->getStoreId()); 00544 if (null === $parentPath) { 00545 $parentPath = $this->getResource()->getCategoryParentPath($category); 00546 } 00547 elseif ($parentPath == '/') { 00548 $parentPath = ''; 00549 } 00550 $parentPath = Mage::helper('catalog/category')->getCategoryUrlPath($parentPath, true, $category->getStoreId()); 00551 00552 return $this->getUnusedPath($category->getStoreId(), $parentPath . $urlKey . $categoryUrlSuffix, 00553 $this->generatePath('id', null, $category) 00554 ); 00555 } 00556 00557 // for product & category 00558 if (!$category) { 00559 Mage::throwException(Mage::helper('core')->__('Category object is required for determining product request path')); // why? 00560 } 00561 00562 if ($product->getUrlKey() == '') { 00563 $urlKey = $this->getProductModel()->formatUrlKey($product->getName()); 00564 } 00565 else { 00566 $urlKey = $this->getProductModel()->formatUrlKey($product->getUrlKey()); 00567 } 00568 $productUrlSuffix = $this->getProductUrlSuffix($category->getStoreId()); 00569 if ($category->getUrlPath()) { 00570 $categoryUrl = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(), false, $category->getStoreId()); 00571 return $this->getUnusedPath($category->getStoreId(), $categoryUrl . '/' . $urlKey . $productUrlSuffix, 00572 $this->generatePath('id', $product, $category) 00573 ); 00574 } 00575 00576 // for product only 00577 return $this->getUnusedPath($category->getStoreId(), $urlKey . $productUrlSuffix, 00578 $this->generatePath('id', $product) 00579 ); 00580 } 00581 00582 // generate target_path 00583 if (!$product) { 00584 return 'catalog/category/view/id/' . $category->getId(); 00585 } 00586 if ($category && $category->getUrlPath()) { 00587 return 'catalog/product/view/id/' . $product->getId() . '/category/' . $category->getId(); 00588 } 00589 return 'catalog/product/view/id/' . $product->getId(); 00590 }
getCategoryModel | ( | ) |
Retrieve Category model singleton
Definition at line 122 of file Url.php.
00123 { 00124 return $this->getResource()->getCategoryModel(); 00125 }
getCategoryUrlSuffix | ( | $ | storeId | ) |
Retrieve category rewrite sufix for store
int | $storeId |
Definition at line 496 of file Url.php.
00497 { 00498 return Mage::helper('catalog/category')->getCategoryUrlSuffix($storeId); 00499 }
getProductModel | ( | ) |
Retrieve product model singleton
Definition at line 132 of file Url.php.
00133 { 00134 return $this->getResource()->getProductModel(); 00135 }
getProductUrlSuffix | ( | $ | storeId | ) |
Retrieve product rewrite sufix for store
int | $storeId |
Definition at line 485 of file Url.php.
00486 { 00487 return Mage::helper('catalog/product')->getProductUrlSuffix($storeId); 00488 }
getResource | ( | ) |
Retrieve resource model
Definition at line 109 of file Url.php.
00110 { 00111 if (is_null($this->_resourceModel)) { 00112 $this->_resourceModel = Mage::getResourceModel('catalog/url'); 00113 } 00114 return $this->_resourceModel; 00115 }
getStores | ( | $ | storeId = null |
) |
Retrieve stores array or store model
int | $storeId |
Definition at line 99 of file Url.php.
00100 { 00101 return $this->getResource()->getStores($storeId); 00102 }
getUnusedPath | ( | $ | storeId, | |
$ | requestPath, | |||
$ | idPath | |||
) |
Get requestPath that was not used yet.
Will try to get unique path by adding -1 -2 etc. between url_key and optional url_suffix
int | $storeId | |
string | $requestPath | |
string | $idPath |
Definition at line 435 of file Url.php.
00436 { 00437 if (empty($requestPath)) { 00438 $requestPath = '-'; 00439 } 00440 elseif ($requestPath == $this->getProductUrlSuffix($storeId)) { 00441 $requestPath = '-' . $this->getProductUrlSuffix($storeId); 00442 } 00443 00444 if (strlen($requestPath) > self::MAX_REQUEST_PATH_LENGTH + self::ALLOWED_REQUEST_PATH_OVERFLOW) { 00445 $requestPath = substr($requestPath, 0, self::MAX_REQUEST_PATH_LENGTH); 00446 } 00447 00448 if (isset($this->_rewrites[$idPath])) { 00449 $this->_rewrite = $this->_rewrites[$idPath]; 00450 if ($this->_rewrites[$idPath]->getRequestPath() == $requestPath) { 00451 return $requestPath; 00452 } 00453 } 00454 else { 00455 $this->_rewrite = null; 00456 } 00457 00458 $rewrite = $this->getResource()->getRewriteByRequestPath($requestPath, $storeId); 00459 if ($rewrite && $rewrite->getId()) { 00460 if ($rewrite->getIdPath() == $idPath) { 00461 $this->_rewrite = $rewrite; 00462 return $requestPath; 00463 } 00464 // retrieve url_suffix for product urls 00465 $productUrlSuffix = $this->getProductUrlSuffix($storeId); 00466 // match request_url abcdef1234(-12)(.html) pattern 00467 $match = array(); 00468 if (!preg_match('#^([0-9a-z/-]+?)(-([0-9]+))?('.preg_quote($productUrlSuffix).')?$#i', $requestPath, $match)) { 00469 return $this->getUnusedPath($storeId, '-', $idPath); 00470 } 00471 $requestPath = $match[1].(isset($match[3])?'-'.($match[3]+1):'-1').(isset($match[4])?$match[4]:''); 00472 return $this->getUnusedPath($storeId, $requestPath, $idPath); 00473 } 00474 else { 00475 return $requestPath; 00476 } 00477 }
refreshCategoryRewrite | ( | $ | categoryId, | |
$ | storeId = null , |
|||
$ | refreshProducts = true | |||
) |
Refresh category and childs rewrites
int | $categoryId | |
int | $storeId | |
bool | $refreshProducts |
Definition at line 309 of file Url.php.
00310 { 00311 if (is_null($storeId)) { 00312 foreach ($this->getStores() as $store) { 00313 $this->refreshCategoryRewrite($categoryId, $store->getId(), $refreshProducts); 00314 } 00315 return $this; 00316 } 00317 00318 $category = $this->getResource()->getCategory($categoryId, $storeId); 00319 if (!$category) { 00320 return $this; 00321 } 00322 $category = $this->getResource()->loadCategoryChilds($category); 00323 $categoryIds = array($category->getId()); 00324 if ($category->getAllChilds()) { 00325 $categoryIds = array_merge($categoryIds, array_keys($category->getAllChilds())); 00326 } 00327 $this->_rewrites = $this->getResource()->prepareRewrites($storeId, $categoryIds); 00328 $this->_refreshCategoryRewrites($category, null, $refreshProducts); 00329 00330 unset($category); 00331 $this->_rewrites = array(); 00332 00333 return $this; 00334 }
refreshProductRewrite | ( | $ | productId, | |
$ | storeId = null | |||
) |
Refresh product and categories urls
int | $productId | |
int | $storeId |
Definition at line 343 of file Url.php.
00344 { 00345 if (is_null($storeId)) { 00346 foreach ($this->getStores() as $store) { 00347 $this->refreshProductRewrite($productId, $store->getId()); 00348 } 00349 return $this; 00350 } 00351 00352 if ($product = $this->getResource()->getProduct($productId, $storeId)) { 00353 $storeRootCategoryId = $this->getStores($storeId)->getRootCategoryId(); 00354 $categories = $this->getResource()->getCategories($product->getCategoryIds(), $storeId); 00355 $this->_rewrites = $this->getResource()->prepareRewrites($storeId, '', $productId); 00356 00357 if (!isset($categories[$storeRootCategoryId])) { 00358 $categories[$storeRootCategoryId] = $this->getResource()->getCategory($storeRootCategoryId, $storeId); 00359 } 00360 00361 foreach ($categories as $category) { 00362 $this->_refreshProductRewrite($product, $category); 00363 } 00364 00365 unset($categories); 00366 unset($product); 00367 00368 // $this->getResource()->clearCategoryProduct($storeId); 00369 } 00370 00371 return $this; 00372 }
refreshProductRewrites | ( | $ | storeId | ) |
Definition at line 374 of file Url.php.
00375 { 00376 $this->_categories = array(); 00377 $storeRootCategoryId = $this->getStores($storeId)->getRootCategoryId(); 00378 $this->_categories[$storeRootCategoryId] = $this->getResource()->getCategory($storeRootCategoryId, $storeId); 00379 00380 $lastEntityId = 0; 00381 $process = true; 00382 00383 while ($process == true) { 00384 $products = $this->getResource()->getProductsByStore($storeId, $lastEntityId); 00385 if (!$products) { 00386 $process = false; 00387 break; 00388 } 00389 00390 $this->_rewrites = array(); 00391 $this->_rewrites = $this->getResource()->prepareRewrites($storeId, false, array_keys($products)); 00392 00393 $loadCategories = array(); 00394 foreach ($products as $product) { 00395 foreach ($product->getCategoryIds() as $categoryId) { 00396 if (!isset($this->_categories[$categoryId])) { 00397 $loadCategories[$categoryId] = $categoryId; 00398 } 00399 } 00400 } 00401 00402 if ($loadCategories) { 00403 foreach ($this->getResource()->getCategories($loadCategories, $storeId) as $category) { 00404 $this->_categories[$category->getId()] = $category; 00405 } 00406 } 00407 00408 foreach ($products as $product) { 00409 $this->_refreshProductRewrite($product, $this->_categories[$storeRootCategoryId]); 00410 foreach ($product->getCategoryIds() as $categoryId) { 00411 if ($categoryId != $storeRootCategoryId && isset($this->_categories[$categoryId])) { 00412 $this->_refreshProductRewrite($product, $this->_categories[$categoryId]); 00413 } 00414 } 00415 } 00416 00417 unset($products); 00418 $this->_rewrites = array(); 00419 } 00420 00421 $this->_categories = array(); 00422 return $this; 00423 }
refreshRewrites | ( | $ | storeId = null |
) |
Refresh rewrite urls
int | $storeId |
Definition at line 143 of file Url.php.
00144 { 00145 if (is_null($storeId)) { 00146 foreach ($this->getStores() as $store) { 00147 $this->refreshRewrites($store->getId()); 00148 } 00149 return $this; 00150 } 00151 00152 $this->refreshCategoryRewrite($this->getStores($storeId)->getRootCategoryId(), $storeId, false); 00153 $this->refreshProductRewrites($storeId); 00154 $this->getResource()->clearCategoryProduct($storeId); 00155 00156 return $this; 00157 }
const ALLOWED_REQUEST_PATH_OVERFLOW = 10 |
const MAX_REQUEST_PATH_LENGTH = 240 |