PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

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