00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class Mage_Catalog_Model_Product_Url extends Varien_Object
00034 {
00035 protected static $_url;
00036 protected static $_urlRewrite;
00037
00038 const CACHE_TAG = 'url_rewrite';
00039
00040
00041
00042
00043 public function getUrlInstance()
00044 {
00045 if (!self::$_url) {
00046 self::$_url = Mage::getModel('core/url');
00047 }
00048 return self::$_url;
00049 }
00050
00051
00052
00053
00054 public function getUrlRewrite()
00055 {
00056 if (!self::$_urlRewrite) {
00057 self::$_urlRewrite = Mage::getModel('core/url_rewrite');
00058 }
00059 return self::$_urlRewrite;
00060 }
00061
00062
00063
00064
00065
00066
00067 protected function _validImage($image)
00068 {
00069 if($image == 'no_selection') {
00070 $image = null;
00071 }
00072 return $image;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082 public function getProductUrl($product, $useSid = null)
00083 {
00084 if ($useSid === null) {
00085 $useSid = Mage::app()->getUseSessionInUrl();
00086 }
00087
00088 $categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId() ? $product->getCategoryId() : 0;
00089
00090 $cacheUrlKey = sprintf('url_%d_%d', $categoryId, $useSid);
00091 $url = $product->getData($cacheUrlKey);
00092
00093 if (is_null($url)) {
00094 if ($product->getStoreId()) {
00095 $this->getUrlInstance()->setStore($product->getStoreId());
00096 }
00097
00098
00099 $originalSid = $this->getUrlInstance()->getUseSession();
00100 if ($originalSid != $useSid) {
00101 $this->getUrlInstance()->setUseSession($useSid);
00102 }
00103
00104 if ($product->hasData('request_path') && $product->getRequestPath() != '') {
00105 $this->setData($cacheUrlKey, $this->getUrlInstance()->getDirectUrl($product->getRequestPath()));
00106 $this->getUrlInstance()->setUseSession($originalSid);
00107 return $this->getData($cacheUrlKey);
00108 }
00109
00110 Varien_Profiler::start('REWRITE: '.__METHOD__);
00111
00112 $rewrite = $this->getUrlRewrite();
00113 if ($product->getStoreId()) {
00114 $rewrite->setStoreId($product->getStoreId());
00115 }
00116 else {
00117 $rewrite->setStoreId(Mage::app()->getStore()->getId());
00118 }
00119
00120 $idPath = 'product/'.$product->getId();
00121 if ($product->getCategoryId() && !$product->getDoNotUseCategoryId() && Mage::getStoreConfig('catalog/seo/product_use_categories')) {
00122 $idPath .= '/'.$product->getCategoryId();
00123 }
00124
00125 $rewrite->loadByIdPath($idPath);
00126
00127 if ($rewrite->getId()) {
00128 $this->setData($cacheUrlKey, $this->getUrlInstance()->getDirectUrl($rewrite->getRequestPath()));
00129 Varien_Profiler::stop('REWRITE: '.__METHOD__);
00130 $this->getUrlInstance()->setUseSession($originalSid);
00131 return $this->getData($cacheUrlKey);
00132 }
00133
00134 Varien_Profiler::stop('REWRITE: '.__METHOD__);
00135 Varien_Profiler::start('REGULAR: '.__METHOD__);
00136
00137 $url = $this->getUrlInstance()->getUrl('catalog/product/view', array(
00138 'id' => $product->getId(),
00139 's' => $product->getUrlKey(),
00140 'category' => $product->getCategoryId()
00141 ));
00142
00143 $this->getUrlInstance()->setUseSession($originalSid);
00144
00145 Varien_Profiler::stop('REGULAR: '.__METHOD__);
00146 }
00147 return $url;
00148 }
00149
00150 public function formatUrlKey($str)
00151 {
00152 $urlKey = preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format($str));
00153 $urlKey = strtolower($urlKey);
00154 $urlKey = trim($urlKey, '-');
00155
00156 return $urlKey;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 public function getUrlPath($product, $category=null)
00168 {
00169 $path = $product->getData('url_path');
00170
00171 if (is_null($category)) {
00172
00173 return $path;
00174 } elseif (!$category instanceof Mage_Catalog_Model_Category) {
00175 Mage::throwException('Invalid category object supplied');
00176 }
00177
00178 return Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath())
00179 . '/' . $path;
00180 }
00181 }