PageRenderTime 61ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Helper/Data.php

https://bitbucket.org/acidel/buykoala
PHP | 467 lines | 232 code | 43 blank | 192 comment | 38 complexity | 6f1f73a5bbfb69b879c30a2e7aa0b71b MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog data helper
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Helper_Data extends Mage_Core_Helper_Abstract
  34. {
  35. const PRICE_SCOPE_GLOBAL = 0;
  36. const PRICE_SCOPE_WEBSITE = 1;
  37. const XML_PATH_PRICE_SCOPE = 'catalog/price/scope';
  38. const XML_PATH_SEO_SAVE_HISTORY = 'catalog/seo/save_rewrites_history';
  39. const CONFIG_USE_STATIC_URLS = 'cms/wysiwyg/use_static_urls_in_catalog';
  40. const CONFIG_PARSE_URL_DIRECTIVES = 'catalog/frontend/parse_url_directives';
  41. const XML_PATH_CONTENT_TEMPLATE_FILTER = 'global/catalog/content/tempate_filter';
  42. /**
  43. * Minimum advertise price constants
  44. */
  45. const XML_PATH_MSRP_ENABLED = 'sales/msrp/enabled';
  46. const XML_PATH_MSRP_DISPLAY_ACTUAL_PRICE_TYPE = 'sales/msrp/display_price_type';
  47. const XML_PATH_MSRP_APPLY_TO_ALL = 'sales/msrp/apply_for_all';
  48. const XML_PATH_MSRP_EXPLANATION_MESSAGE = 'sales/msrp/explanation_message';
  49. const XML_PATH_MSRP_EXPLANATION_MESSAGE_WHATS_THIS = 'sales/msrp/explanation_message_whats_this';
  50. /**
  51. * Breadcrumb Path cache
  52. *
  53. * @var string
  54. */
  55. protected $_categoryPath;
  56. /**
  57. * Array of product types that MAP enabled
  58. *
  59. * @var array
  60. */
  61. protected $_mapApplyToProductType = null;
  62. /**
  63. * Currenty selected store ID if applicable
  64. *
  65. * @var int
  66. */
  67. protected $_storeId = null;
  68. /**
  69. * Set a specified store ID value
  70. *
  71. * @param int $store
  72. * @return Mage_Catalog_Helper_Data
  73. */
  74. public function setStoreId($store)
  75. {
  76. $this->_storeId = $store;
  77. return $this;
  78. }
  79. /**
  80. * Return current category path or get it from current category
  81. * and creating array of categories|product paths for breadcrumbs
  82. *
  83. * @return string
  84. */
  85. public function getBreadcrumbPath()
  86. {
  87. if (!$this->_categoryPath) {
  88. $path = array();
  89. if ($category = $this->getCategory()) {
  90. $pathInStore = $category->getPathInStore();
  91. $pathIds = array_reverse(explode(',', $pathInStore));
  92. $categories = $category->getParentCategories();
  93. // add category path breadcrumb
  94. foreach ($pathIds as $categoryId) {
  95. if (isset($categories[$categoryId]) && $categories[$categoryId]->getName()) {
  96. $path['category'.$categoryId] = array(
  97. 'label' => $categories[$categoryId]->getName(),
  98. 'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getUrl() : ''
  99. );
  100. }
  101. }
  102. }
  103. if ($this->getProduct()) {
  104. $path['product'] = array('label'=>$this->getProduct()->getName());
  105. }
  106. $this->_categoryPath = $path;
  107. }
  108. return $this->_categoryPath;
  109. }
  110. /**
  111. * Check is category link
  112. *
  113. * @param int $categoryId
  114. * @return bool
  115. */
  116. protected function _isCategoryLink($categoryId)
  117. {
  118. if ($this->getProduct()) {
  119. return true;
  120. }
  121. if ($categoryId != $this->getCategory()->getId()) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. /**
  127. * Return current category object
  128. *
  129. * @return Mage_Catalog_Model_Category|null
  130. */
  131. public function getCategory()
  132. {
  133. return Mage::registry('current_category');
  134. }
  135. /**
  136. * Retrieve current Product object
  137. *
  138. * @return Mage_Catalog_Model_Product|null
  139. */
  140. public function getProduct()
  141. {
  142. return Mage::registry('current_product');
  143. }
  144. /**
  145. * Retrieve Visitor/Customer Last Viewed URL
  146. *
  147. * @return string
  148. */
  149. public function getLastViewedUrl()
  150. {
  151. if ($productId = Mage::getSingleton('catalog/session')->getLastViewedProductId()) {
  152. $product = Mage::getModel('catalog/product')->load($productId);
  153. /* @var $product Mage_Catalog_Model_Product */
  154. if (Mage::helper('catalog/product')->canShow($product, 'catalog')) {
  155. return $product->getProductUrl();
  156. }
  157. }
  158. if ($categoryId = Mage::getSingleton('catalog/session')->getLastViewedCategoryId()) {
  159. $category = Mage::getModel('catalog/category')->load($categoryId);
  160. /* @var $category Mage_Catalog_Model_Category */
  161. if (!Mage::helper('catalog/category')->canShow($category)) {
  162. return '';
  163. }
  164. return $category->getCategoryUrl();
  165. }
  166. return '';
  167. }
  168. /**
  169. * Split SKU of an item by dashes and spaces
  170. * Words will not be broken, unless thir length is greater than $length
  171. *
  172. * @param string $sku
  173. * @param int $length
  174. * @return array
  175. */
  176. public function splitSku($sku, $length = 30)
  177. {
  178. return Mage::helper('core/string')->str_split($sku, $length, true, false, '[\-\s]');
  179. }
  180. /**
  181. * Retrieve attribute hidden fields
  182. *
  183. * @return array
  184. */
  185. public function getAttributeHiddenFields()
  186. {
  187. if (Mage::registry('attribute_type_hidden_fields')) {
  188. return Mage::registry('attribute_type_hidden_fields');
  189. } else {
  190. return array();
  191. }
  192. }
  193. /**
  194. * Retrieve attribute disabled types
  195. *
  196. * @return array
  197. */
  198. public function getAttributeDisabledTypes()
  199. {
  200. if (Mage::registry('attribute_type_disabled_types')) {
  201. return Mage::registry('attribute_type_disabled_types');
  202. } else {
  203. return array();
  204. }
  205. }
  206. /**
  207. * Retrieve Catalog Price Scope
  208. *
  209. * @return int
  210. */
  211. public function getPriceScope()
  212. {
  213. return Mage::getStoreConfig(self::XML_PATH_PRICE_SCOPE);
  214. }
  215. /**
  216. * Is Global Price
  217. *
  218. * @return bool
  219. */
  220. public function isPriceGlobal()
  221. {
  222. return $this->getPriceScope() == self::PRICE_SCOPE_GLOBAL;
  223. }
  224. /**
  225. * Indicate whether to save URL Rewrite History or not (create redirects to old URLs)
  226. *
  227. * @param int $storeId Store View
  228. * @return bool
  229. */
  230. public function shouldSaveUrlRewritesHistory($storeId = null)
  231. {
  232. return Mage::getStoreConfigFlag(self::XML_PATH_SEO_SAVE_HISTORY, $storeId);
  233. }
  234. /**
  235. * Check if the store is configured to use static URLs for media
  236. *
  237. * @return bool
  238. */
  239. public function isUsingStaticUrlsAllowed()
  240. {
  241. return Mage::getStoreConfigFlag(self::CONFIG_USE_STATIC_URLS, $this->_storeId);
  242. }
  243. /**
  244. * Check if the parsing of URL directives is allowed for the catalog
  245. *
  246. * @return bool
  247. */
  248. public function isUrlDirectivesParsingAllowed()
  249. {
  250. return Mage::getStoreConfigFlag(self::CONFIG_PARSE_URL_DIRECTIVES, $this->_storeId);
  251. }
  252. /**
  253. * Retrieve template processor for catalog content
  254. *
  255. * @return Varien_Filter_Template
  256. */
  257. public function getPageTemplateProcessor()
  258. {
  259. $model = (string)Mage::getConfig()->getNode(self::XML_PATH_CONTENT_TEMPLATE_FILTER);
  260. return Mage::getModel($model);
  261. }
  262. /**
  263. * Initialize mapping for old and new field names
  264. *
  265. * @return array
  266. */
  267. public function getOldFieldMap()
  268. {
  269. $node = Mage::getConfig()->getNode('global/catalog_product/old_fields_map');
  270. if ($node === false) {
  271. return array();
  272. }
  273. return (array) $node;
  274. }
  275. /**
  276. * Check if Minimum Advertised Price is enabled
  277. *
  278. * @return bool
  279. */
  280. public function isMsrpEnabled()
  281. {
  282. return (bool)Mage::getStoreConfig(self::XML_PATH_MSRP_ENABLED, $this->_storeId);
  283. }
  284. /**
  285. * Return MAP display actual type
  286. *
  287. * @return null|string
  288. */
  289. public function getMsrpDisplayActualPriceType()
  290. {
  291. return Mage::getStoreConfig(self::XML_PATH_MSRP_DISPLAY_ACTUAL_PRICE_TYPE, $this->_storeId);
  292. }
  293. /**
  294. * Check if MAP apply to all products
  295. *
  296. * @return bool
  297. */
  298. public function isMsrpApplyToAll()
  299. {
  300. return (bool)Mage::getStoreConfig(self::XML_PATH_MSRP_APPLY_TO_ALL, $this->_storeId);
  301. }
  302. /**
  303. * Return MAP explanation message
  304. *
  305. * @return string
  306. */
  307. public function getMsrpExplanationMessage()
  308. {
  309. return $this->escapeHtml(
  310. Mage::getStoreConfig(self::XML_PATH_MSRP_EXPLANATION_MESSAGE, $this->_storeId),
  311. array('b','br','strong','i','u', 'p', 'span')
  312. );
  313. }
  314. /**
  315. * Return MAP explanation message for "Whats This" window
  316. *
  317. * @return string
  318. */
  319. public function getMsrpExplanationMessageWhatsThis()
  320. {
  321. return $this->escapeHtml(
  322. Mage::getStoreConfig(self::XML_PATH_MSRP_EXPLANATION_MESSAGE_WHATS_THIS, $this->_storeId),
  323. array('b','br','strong','i','u', 'p', 'span')
  324. );
  325. }
  326. /**
  327. * Check if can apply Minimum Advertise price to product
  328. * in specific visibility
  329. *
  330. * @param int|Mage_Catalog_Model_Product $product
  331. * @param int $visibility Check displaying price in concrete place (by default generally)
  332. * @param bool $checkAssociatedItems
  333. * @return bool
  334. */
  335. public function canApplyMsrp($product, $visibility = null, $checkAssociatedItems = true)
  336. {
  337. if (!$this->isMsrpEnabled()) {
  338. return false;
  339. }
  340. if (is_numeric($product)) {
  341. $product = Mage::getModel('catalog/product')
  342. ->setStoreId(Mage::app()->getStore()->getId())
  343. ->load($product);
  344. }
  345. if (!$this->canApplyMsrpToProductType($product)) {
  346. return false;
  347. }
  348. $result = $product->getMsrpEnabled();
  349. if ($result == Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type_Enabled::MSRP_ENABLE_USE_CONFIG) {
  350. $result = $this->isMsrpApplyToAll();
  351. }
  352. if (!$product->hasMsrpEnabled() && $this->isMsrpApplyToAll()) {
  353. $result = true;
  354. }
  355. if ($result && $visibility !== null) {
  356. $productVisibility = $product->getMsrpDisplayActualPriceType();
  357. if ($productVisibility == Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type_Price::TYPE_USE_CONFIG) {
  358. $productVisibility = $this->getMsrpDisplayActualPriceType();
  359. }
  360. $result = ($productVisibility == $visibility);
  361. }
  362. if ($product->getTypeInstance(true)->isComposite($product)
  363. && $checkAssociatedItems
  364. && (!$result || $visibility !== null)
  365. ) {
  366. $resultInOptions = $product->getTypeInstance(true)->isMapEnabledInOptions($product, $visibility);
  367. if ($resultInOptions !== null) {
  368. $result = $resultInOptions;
  369. }
  370. }
  371. return $result;
  372. }
  373. /**
  374. * Check whether MAP applied to product Product Type
  375. *
  376. * @param Mage_Catalog_Model_Product $product
  377. * @return bool
  378. */
  379. public function canApplyMsrpToProductType($product)
  380. {
  381. if($this->_mapApplyToProductType === null) {
  382. /** @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
  383. $attribute = Mage::getModel('catalog/resource_eav_attribute')
  384. ->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'msrp_enabled');
  385. $this->_mapApplyToProductType = $attribute->getApplyTo();
  386. }
  387. return empty($this->_mapApplyToProductType) || in_array($product->getTypeId(), $this->_mapApplyToProductType);
  388. }
  389. /**
  390. * Get MAP message for price
  391. *
  392. * @param Mage_Catalog_Model_Product $product
  393. * @return string
  394. */
  395. public function getMsrpPriceMessage($product)
  396. {
  397. $message = "";
  398. if ($this->canApplyMsrp(
  399. $product,
  400. Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_IN_CART
  401. )) {
  402. $message = $this->__('To see product price, add this item to your cart. You can always remove it later.');
  403. } elseif ($this->canApplyMsrp(
  404. $product,
  405. Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_BEFORE_ORDER_CONFIRM
  406. )) {
  407. $message = $this->__('See price before order confirmation.');
  408. }
  409. return $message;
  410. }
  411. /**
  412. * Check is product need gesture to show price
  413. *
  414. * @param Mage_Catalog_Model_Product $product
  415. * @return bool
  416. */
  417. public function isShowPriceOnGesture($product)
  418. {
  419. return $this->canApplyMsrp(
  420. $product,
  421. Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_ON_GESTURE
  422. );
  423. }
  424. }