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_Helper_Data extends Mage_Core_Helper_Abstract
36: {
37: const PRICE_SCOPE_GLOBAL = 0;
38: const PRICE_SCOPE_WEBSITE = 1;
39: const XML_PATH_PRICE_SCOPE = 'catalog/price/scope';
40: const XML_PATH_SEO_SAVE_HISTORY = 'catalog/seo/save_rewrites_history';
41: const CONFIG_USE_STATIC_URLS = 'cms/wysiwyg/use_static_urls_in_catalog';
42: const CONFIG_PARSE_URL_DIRECTIVES = 'catalog/frontend/parse_url_directives';
43: const XML_PATH_CONTENT_TEMPLATE_FILTER = 'global/catalog/content/tempate_filter';
44: const XML_PATH_DISPLAY_PRODUCT_COUNT = 'catalog/layered_navigation/display_product_count';
45:
46: 47: 48:
49: const XML_PATH_MSRP_ENABLED = 'sales/msrp/enabled';
50: const XML_PATH_MSRP_DISPLAY_ACTUAL_PRICE_TYPE = 'sales/msrp/display_price_type';
51: const XML_PATH_MSRP_APPLY_TO_ALL = 'sales/msrp/apply_for_all';
52: const XML_PATH_MSRP_EXPLANATION_MESSAGE = 'sales/msrp/explanation_message';
53: const XML_PATH_MSRP_EXPLANATION_MESSAGE_WHATS_THIS = 'sales/msrp/explanation_message_whats_this';
54:
55:
56: 57: 58: 59: 60:
61: protected $_categoryPath;
62:
63: 64: 65: 66: 67:
68: protected $_mapApplyToProductType = null;
69:
70: 71: 72: 73: 74:
75: protected $_storeId = null;
76:
77: 78: 79: 80: 81: 82:
83: public function setStoreId($store)
84: {
85: $this->_storeId = $store;
86: return $this;
87: }
88:
89: 90: 91: 92: 93: 94:
95: public function getBreadcrumbPath()
96: {
97: if (!$this->_categoryPath) {
98:
99: $path = array();
100: if ($category = $this->getCategory()) {
101: $pathInStore = $category->getPathInStore();
102: $pathIds = array_reverse(explode(',', $pathInStore));
103:
104: $categories = $category->getParentCategories();
105:
106:
107: foreach ($pathIds as $categoryId) {
108: if (isset($categories[$categoryId]) && $categories[$categoryId]->getName()) {
109: $path['category'.$categoryId] = array(
110: 'label' => $categories[$categoryId]->getName(),
111: 'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getUrl() : ''
112: );
113: }
114: }
115: }
116:
117: if ($this->getProduct()) {
118: $path['product'] = array('label'=>$this->getProduct()->getName());
119: }
120:
121: $this->_categoryPath = $path;
122: }
123: return $this->_categoryPath;
124: }
125:
126: 127: 128: 129: 130: 131:
132: protected function _isCategoryLink($categoryId)
133: {
134: if ($this->getProduct()) {
135: return true;
136: }
137: if ($categoryId != $this->getCategory()->getId()) {
138: return true;
139: }
140: return false;
141: }
142:
143: 144: 145: 146: 147:
148: public function getCategory()
149: {
150: return Mage::registry('current_category');
151: }
152:
153: 154: 155: 156: 157:
158: public function getProduct()
159: {
160: return Mage::registry('current_product');
161: }
162:
163: 164: 165: 166: 167:
168: public function getLastViewedUrl()
169: {
170: if ($productId = Mage::getSingleton('catalog/session')->getLastViewedProductId()) {
171: $product = Mage::getModel('catalog/product')->load($productId);
172:
173: if (Mage::helper('catalog/product')->canShow($product, 'catalog')) {
174: return $product->getProductUrl();
175: }
176: }
177: if ($categoryId = Mage::getSingleton('catalog/session')->getLastViewedCategoryId()) {
178: $category = Mage::getModel('catalog/category')->load($categoryId);
179:
180: if (!Mage::helper('catalog/category')->canShow($category)) {
181: return '';
182: }
183: return $category->getCategoryUrl();
184: }
185: return '';
186: }
187:
188: 189: 190: 191: 192: 193: 194: 195:
196: public function splitSku($sku, $length = 30)
197: {
198: return Mage::helper('core/string')->str_split($sku, $length, true, false, '[\-\s]');
199: }
200:
201: 202: 203: 204: 205:
206: public function getAttributeHiddenFields()
207: {
208: if (Mage::registry('attribute_type_hidden_fields')) {
209: return Mage::registry('attribute_type_hidden_fields');
210: } else {
211: return array();
212: }
213: }
214:
215: 216: 217: 218: 219:
220: public function getAttributeDisabledTypes()
221: {
222: if (Mage::registry('attribute_type_disabled_types')) {
223: return Mage::registry('attribute_type_disabled_types');
224: } else {
225: return array();
226: }
227: }
228:
229: 230: 231: 232: 233:
234: public function getPriceScope()
235: {
236: return Mage::getStoreConfig(self::XML_PATH_PRICE_SCOPE);
237: }
238:
239: 240: 241: 242: 243:
244: public function isPriceGlobal()
245: {
246: return $this->getPriceScope() == self::PRICE_SCOPE_GLOBAL;
247: }
248:
249: 250: 251: 252: 253: 254:
255: public function shouldSaveUrlRewritesHistory($storeId = null)
256: {
257: return Mage::getStoreConfigFlag(self::XML_PATH_SEO_SAVE_HISTORY, $storeId);
258: }
259:
260: 261: 262: 263: 264:
265: public function isUsingStaticUrlsAllowed()
266: {
267: return Mage::getStoreConfigFlag(self::CONFIG_USE_STATIC_URLS, $this->_storeId);
268: }
269:
270: 271: 272: 273: 274:
275: public function isUrlDirectivesParsingAllowed()
276: {
277: return Mage::getStoreConfigFlag(self::CONFIG_PARSE_URL_DIRECTIVES, $this->_storeId);
278: }
279:
280: 281: 282: 283: 284:
285: public function getPageTemplateProcessor()
286: {
287: $model = (string)Mage::getConfig()->getNode(self::XML_PATH_CONTENT_TEMPLATE_FILTER);
288: return Mage::getModel($model);
289: }
290:
291: 292: 293: 294: 295:
296: public function getOldFieldMap()
297: {
298: $node = Mage::getConfig()->getNode('global/catalog_product/old_fields_map');
299: if ($node === false) {
300: return array();
301: }
302: return (array) $node;
303: }
304: 305: 306: 307: 308:
309: public function isMsrpEnabled()
310: {
311: return (bool)Mage::getStoreConfig(self::XML_PATH_MSRP_ENABLED, $this->_storeId);
312: }
313:
314: 315: 316: 317: 318:
319: public function getMsrpDisplayActualPriceType()
320: {
321: return Mage::getStoreConfig(self::XML_PATH_MSRP_DISPLAY_ACTUAL_PRICE_TYPE, $this->_storeId);
322: }
323:
324: 325: 326: 327: 328:
329: public function isMsrpApplyToAll()
330: {
331: return (bool)Mage::getStoreConfig(self::XML_PATH_MSRP_APPLY_TO_ALL, $this->_storeId);
332: }
333:
334: 335: 336: 337: 338:
339: public function getMsrpExplanationMessage()
340: {
341: return $this->escapeHtml(
342: Mage::getStoreConfig(self::XML_PATH_MSRP_EXPLANATION_MESSAGE, $this->_storeId),
343: array('b','br','strong','i','u', 'p', 'span')
344: );
345: }
346:
347: 348: 349: 350: 351:
352: public function getMsrpExplanationMessageWhatsThis()
353: {
354: return $this->escapeHtml(
355: Mage::getStoreConfig(self::XML_PATH_MSRP_EXPLANATION_MESSAGE_WHATS_THIS, $this->_storeId),
356: array('b','br','strong','i','u', 'p', 'span')
357: );
358: }
359:
360: 361: 362: 363: 364: 365: 366: 367: 368:
369: public function canApplyMsrp($product, $visibility = null, $checkAssociatedItems = true)
370: {
371: if (!$this->isMsrpEnabled()) {
372: return false;
373: }
374:
375: if (is_numeric($product)) {
376: $product = Mage::getModel('catalog/product')
377: ->setStoreId(Mage::app()->getStore()->getId())
378: ->load($product);
379: }
380:
381: if (!$this->canApplyMsrpToProductType($product)) {
382: return false;
383: }
384:
385: $result = $product->getMsrpEnabled();
386: if ($result == Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type_Enabled::MSRP_ENABLE_USE_CONFIG) {
387: $result = $this->isMsrpApplyToAll();
388: }
389:
390: if (!$product->hasMsrpEnabled() && $this->isMsrpApplyToAll()) {
391: $result = true;
392: }
393:
394: if ($result && $visibility !== null) {
395: $productVisibility = $product->getMsrpDisplayActualPriceType();
396: if ($productVisibility == Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type_Price::TYPE_USE_CONFIG) {
397: $productVisibility = $this->getMsrpDisplayActualPriceType();
398: }
399: $result = ($productVisibility == $visibility);
400: }
401:
402: if ($product->getTypeInstance(true)->isComposite($product)
403: && $checkAssociatedItems
404: && (!$result || $visibility !== null)
405: ) {
406: $resultInOptions = $product->getTypeInstance(true)->isMapEnabledInOptions($product, $visibility);
407: if ($resultInOptions !== null) {
408: $result = $resultInOptions;
409: }
410: }
411:
412: return $result;
413: }
414:
415: 416: 417: 418: 419: 420:
421: public function canApplyMsrpToProductType($product)
422: {
423: if($this->_mapApplyToProductType === null) {
424:
425: $attribute = Mage::getModel('catalog/resource_eav_attribute')
426: ->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'msrp_enabled');
427: $this->_mapApplyToProductType = $attribute->getApplyTo();
428: }
429: return empty($this->_mapApplyToProductType) || in_array($product->getTypeId(), $this->_mapApplyToProductType);
430: }
431:
432: 433: 434: 435: 436: 437:
438: public function getMsrpPriceMessage($product)
439: {
440: $message = "";
441: if ($this->canApplyMsrp(
442: $product,
443: Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_IN_CART
444: )) {
445: $message = $this->__('To see product price, add this item to your cart. You can always remove it later.');
446: } elseif ($this->canApplyMsrp(
447: $product,
448: Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_BEFORE_ORDER_CONFIRM
449: )) {
450: $message = $this->__('See price before order confirmation.');
451: }
452: return $message;
453: }
454:
455: 456: 457: 458: 459: 460:
461: public function isShowPriceOnGesture($product)
462: {
463: return $this->canApplyMsrp(
464: $product,
465: Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_ON_GESTURE
466: );
467: }
468:
469: 470: 471: 472: 473:
474: public function shouldDisplayProductCountOnLayer($storeId = null)
475: {
476: return Mage::getStoreConfigFlag(self::XML_PATH_DISPLAY_PRODUCT_COUNT, $storeId);
477: }
478: }
479: