PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/vincent.perdereau/picandparts
PHP | 478 lines | 237 code | 44 blank | 197 comment | 38 complexity | 7fd21ab1980277fe851d3dcdee2ee9f7 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.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('catalog/session')->getLastViewedProductId()) {
  153. $product = Mage::getModel('catalog/product')->load($productId);
  154. /* @var $product Mage_Catalog_Model_Product */
  155. if (Mage::helper('catalog/product')->canShow($product, 'catalog')) {
  156. return $product->getProductUrl();
  157. }
  158. }
  159. if ($categoryId = Mage::getSingleton('catalog/session')->getLastViewedCategoryId()) {
  160. $category = Mage::getModel('catalog/category')->load($categoryId);
  161. /* @var $category Mage_Catalog_Model_Category */
  162. if (!Mage::helper('catalog/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('core/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. * Initialize mapping for old and new field names
  265. *
  266. * @return array
  267. */
  268. public function getOldFieldMap()
  269. {
  270. $node = Mage::getConfig()->getNode('global/catalog_product/old_fields_map');
  271. if ($node === false) {
  272. return array();
  273. }
  274. return (array) $node;
  275. }
  276. /**
  277. * Check if Minimum Advertised Price is enabled
  278. *
  279. * @return bool
  280. */
  281. public function isMsrpEnabled()
  282. {
  283. return (bool)Mage::getStoreConfig(self::XML_PATH_MSRP_ENABLED, $this->_storeId);
  284. }
  285. /**
  286. * Return MAP display actual type
  287. *
  288. * @return null|string
  289. */
  290. public function getMsrpDisplayActualPriceType()
  291. {
  292. return Mage::getStoreConfig(self::XML_PATH_MSRP_DISPLAY_ACTUAL_PRICE_TYPE, $this->_storeId);
  293. }
  294. /**
  295. * Check if MAP apply to all products
  296. *
  297. * @return bool
  298. */
  299. public function isMsrpApplyToAll()
  300. {
  301. return (bool)Mage::getStoreConfig(self::XML_PATH_MSRP_APPLY_TO_ALL, $this->_storeId);
  302. }
  303. /**
  304. * Return MAP explanation message
  305. *
  306. * @return string
  307. */
  308. public function getMsrpExplanationMessage()
  309. {
  310. return $this->escapeHtml(
  311. Mage::getStoreConfig(self::XML_PATH_MSRP_EXPLANATION_MESSAGE, $this->_storeId),
  312. array('b','br','strong','i','u', 'p', 'span')
  313. );
  314. }
  315. /**
  316. * Return MAP explanation message for "Whats This" window
  317. *
  318. * @return string
  319. */
  320. public function getMsrpExplanationMessageWhatsThis()
  321. {
  322. return $this->escapeHtml(
  323. Mage::getStoreConfig(self::XML_PATH_MSRP_EXPLANATION_MESSAGE_WHATS_THIS, $this->_storeId),
  324. array('b','br','strong','i','u', 'p', 'span')
  325. );
  326. }
  327. /**
  328. * Check if can apply Minimum Advertise price to product
  329. * in specific visibility
  330. *
  331. * @param int|Mage_Catalog_Model_Product $product
  332. * @param int $visibility Check displaying price in concrete place (by default generally)
  333. * @param bool $checkAssociatedItems
  334. * @return bool
  335. */
  336. public function canApplyMsrp($product, $visibility = null, $checkAssociatedItems = true)
  337. {
  338. if (!$this->isMsrpEnabled()) {
  339. return false;
  340. }
  341. if (is_numeric($product)) {
  342. $product = Mage::getModel('catalog/product')
  343. ->setStoreId(Mage::app()->getStore()->getId())
  344. ->load($product);
  345. }
  346. if (!$this->canApplyMsrpToProductType($product)) {
  347. return false;
  348. }
  349. $result = $product->getMsrpEnabled();
  350. if ($result == Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type_Enabled::MSRP_ENABLE_USE_CONFIG) {
  351. $result = $this->isMsrpApplyToAll();
  352. }
  353. if (!$product->hasMsrpEnabled() && $this->isMsrpApplyToAll()) {
  354. $result = true;
  355. }
  356. if ($result && $visibility !== null) {
  357. $productVisibility = $product->getMsrpDisplayActualPriceType();
  358. if ($productVisibility == Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type_Price::TYPE_USE_CONFIG) {
  359. $productVisibility = $this->getMsrpDisplayActualPriceType();
  360. }
  361. $result = ($productVisibility == $visibility);
  362. }
  363. if ($product->getTypeInstance(true)->isComposite($product)
  364. && $checkAssociatedItems
  365. && (!$result || $visibility !== null)
  366. ) {
  367. $resultInOptions = $product->getTypeInstance(true)->isMapEnabledInOptions($product, $visibility);
  368. if ($resultInOptions !== null) {
  369. $result = $resultInOptions;
  370. }
  371. }
  372. return $result;
  373. }
  374. /**
  375. * Check whether MAP applied to product Product Type
  376. *
  377. * @param Mage_Catalog_Model_Product $product
  378. * @return bool
  379. */
  380. public function canApplyMsrpToProductType($product)
  381. {
  382. if($this->_mapApplyToProductType === null) {
  383. /** @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
  384. $attribute = Mage::getModel('catalog/resource_eav_attribute')
  385. ->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'msrp_enabled');
  386. $this->_mapApplyToProductType = $attribute->getApplyTo();
  387. }
  388. return empty($this->_mapApplyToProductType) || in_array($product->getTypeId(), $this->_mapApplyToProductType);
  389. }
  390. /**
  391. * Get MAP message for price
  392. *
  393. * @param Mage_Catalog_Model_Product $product
  394. * @return string
  395. */
  396. public function getMsrpPriceMessage($product)
  397. {
  398. $message = "";
  399. if ($this->canApplyMsrp(
  400. $product,
  401. Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_IN_CART
  402. )) {
  403. $message = $this->__('To see product price, add this item to your cart. You can always remove it later.');
  404. } elseif ($this->canApplyMsrp(
  405. $product,
  406. Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_BEFORE_ORDER_CONFIRM
  407. )) {
  408. $message = $this->__('See price before order confirmation.');
  409. }
  410. return $message;
  411. }
  412. /**
  413. * Check is product need gesture to show price
  414. *
  415. * @param Mage_Catalog_Model_Product $product
  416. * @return bool
  417. */
  418. public function isShowPriceOnGesture($product)
  419. {
  420. return $this->canApplyMsrp(
  421. $product,
  422. Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type::TYPE_ON_GESTURE
  423. );
  424. }
  425. /**
  426. * Whether to display items count for each filter option
  427. * @param int $storeId Store view ID
  428. * @return bool
  429. */
  430. public function shouldDisplayProductCountOnLayer($storeId = null)
  431. {
  432. return Mage::getStoreConfigFlag(self::XML_PATH_DISPLAY_PRODUCT_COUNT, $storeId);
  433. }
  434. }