1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Model_Product_Url extends Varien_Object
36: {
37: const CACHE_TAG = 'url_rewrite';
38:
39: 40: 41: 42: 43:
44: protected static $_url;
45:
46: 47: 48: 49: 50:
51: protected static $_urlRewrite;
52:
53: 54: 55: 56: 57:
58: public function getUrlInstance()
59: {
60: if (!self::$_url) {
61: self::$_url = Mage::getModel('core/url');
62: }
63: return self::$_url;
64: }
65:
66: 67: 68: 69: 70:
71: public function getUrlRewrite()
72: {
73: if (!self::$_urlRewrite) {
74: self::$_urlRewrite = Mage::getModel('core/url_rewrite');
75: }
76: return self::$_urlRewrite;
77: }
78:
79: 80: 81: 82: 83: 84:
85: protected function _validImage($image)
86: {
87: if($image == 'no_selection') {
88: $image = null;
89: }
90: return $image;
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: public function getUrlInStore(Mage_Catalog_Model_Product $product, $params = array())
101: {
102: $params['_store_to_url'] = true;
103: return $this->getUrl($product, $params);
104: }
105:
106: 107: 108: 109: 110: 111: 112:
113: public function getProductUrl($product, $useSid = null)
114: {
115: if ($useSid === null) {
116: $useSid = Mage::app()->getUseSessionInUrl();
117: }
118:
119: $params = array();
120: if (!$useSid) {
121: $params['_nosid'] = true;
122: }
123:
124: return $this->getUrl($product, $params);
125: }
126:
127: 128: 129: 130: 131: 132:
133: public function formatUrlKey($str)
134: {
135: $urlKey = preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format($str));
136: $urlKey = strtolower($urlKey);
137: $urlKey = trim($urlKey, '-');
138:
139: return $urlKey;
140: }
141:
142: 143: 144: 145: 146: 147: 148: 149:
150: public function getUrlPath($product, $category=null)
151: {
152: $path = $product->getData('url_path');
153:
154: if (is_null($category)) {
155:
156: return $path;
157: } elseif (!$category instanceof Mage_Catalog_Model_Category) {
158: Mage::throwException('Invalid category object supplied');
159: }
160:
161: return Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath())
162: . '/' . $path;
163: }
164:
165: 166: 167: 168: 169: 170: 171:
172: public function getUrl(Mage_Catalog_Model_Product $product, $params = array())
173: {
174: $routePath = '';
175: $routeParams = $params;
176:
177: $storeId = $product->getStoreId();
178: if (isset($params['_ignore_category'])) {
179: unset($params['_ignore_category']);
180: $categoryId = null;
181: } else {
182: $categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId()
183: ? $product->getCategoryId() : null;
184: }
185:
186: if ($product->hasUrlDataObject()) {
187: $requestPath = $product->getUrlDataObject()->getUrlRewrite();
188: $routeParams['_store'] = $product->getUrlDataObject()->getStoreId();
189: } else {
190: $requestPath = $product->getRequestPath();
191: if (empty($requestPath) && $requestPath !== false) {
192: $idPath = sprintf('product/%d', $product->getEntityId());
193: if ($categoryId) {
194: $idPath = sprintf('%s/%d', $idPath, $categoryId);
195: }
196: $rewrite = $this->getUrlRewrite();
197: $rewrite->setStoreId($storeId)
198: ->loadByIdPath($idPath);
199: if ($rewrite->getId()) {
200: $requestPath = $rewrite->getRequestPath();
201: $product->setRequestPath($requestPath);
202: } else {
203: $product->setRequestPath(false);
204: }
205: }
206: }
207:
208: if (isset($routeParams['_store'])) {
209: $storeId = Mage::app()->getStore($routeParams['_store'])->getId();
210: }
211:
212: if ($storeId != Mage::app()->getStore()->getId()) {
213: $routeParams['_store_to_url'] = true;
214: }
215:
216: if (!empty($requestPath)) {
217: $routeParams['_direct'] = $requestPath;
218: } else {
219: $routePath = 'catalog/product/view';
220: $routeParams['id'] = $product->getId();
221: $routeParams['s'] = $product->getUrlKey();
222: if ($categoryId) {
223: $routeParams['category'] = $categoryId;
224: }
225: }
226:
227:
228: if (!isset($routeParams['_query'])) {
229: $routeParams['_query'] = array();
230: }
231:
232: return $this->getUrlInstance()->setStore($storeId)
233: ->getUrl($routePath, $routeParams);
234: }
235: }
236: