PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-catalog/Model/Layer/Filter/Price.php

https://gitlab.com/daigiangaitu91/magento
PHP | 292 lines | 138 code | 35 blank | 119 comment | 16 complexity | 924fb21a4285c3b689e6c5d69b25ec2b MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2015 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Magento\Catalog\Model\Layer\Filter;
  8. /**
  9. * Layer price filter
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class Price extends \Magento\Catalog\Model\Layer\Filter\AbstractFilter
  15. {
  16. /**
  17. * Resource instance
  18. *
  19. * @var \Magento\Catalog\Model\ResourceModel\Layer\Filter\Price
  20. */
  21. protected $_resource;
  22. /**
  23. * Core registry
  24. *
  25. * @var \Magento\Framework\Registry
  26. */
  27. protected $_coreRegistry = null;
  28. /**
  29. * Catalog layer filter price algorithm
  30. *
  31. * @var \Magento\Framework\Search\Dynamic\Algorithm
  32. */
  33. protected $_priceAlgorithm;
  34. /**
  35. * Customer session
  36. *
  37. * @var \Magento\Customer\Model\Session
  38. */
  39. protected $_customerSession;
  40. /**
  41. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  42. */
  43. protected $_scopeConfig;
  44. /**
  45. * @var \Magento\Framework\Pricing\PriceCurrencyInterface
  46. */
  47. protected $priceCurrency;
  48. /**
  49. * @var Dynamic\AlgorithmFactory
  50. */
  51. private $algorithmFactory;
  52. /** @var DataProvider\Price */
  53. private $dataProvider;
  54. /**
  55. * @param ItemFactory $filterItemFactory
  56. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  57. * @param \Magento\Catalog\Model\Layer $layer
  58. * @param \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder
  59. * @param \Magento\Catalog\Model\ResourceModel\Layer\Filter\Price $resource
  60. * @param \Magento\Customer\Model\Session $customerSession
  61. * @param \Magento\Framework\Search\Dynamic\Algorithm $priceAlgorithm
  62. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  63. * @param Dynamic\AlgorithmFactory $algorithmFactory
  64. * @param DataProvider\PriceFactory $dataProviderFactory
  65. * @param array $data
  66. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  67. */
  68. public function __construct(
  69. \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory,
  70. \Magento\Store\Model\StoreManagerInterface $storeManager,
  71. \Magento\Catalog\Model\Layer $layer,
  72. \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
  73. \Magento\Catalog\Model\ResourceModel\Layer\Filter\Price $resource,
  74. \Magento\Customer\Model\Session $customerSession,
  75. \Magento\Framework\Search\Dynamic\Algorithm $priceAlgorithm,
  76. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
  77. \Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory $algorithmFactory,
  78. \Magento\Catalog\Model\Layer\Filter\DataProvider\PriceFactory $dataProviderFactory,
  79. array $data = []
  80. ) {
  81. $this->priceCurrency = $priceCurrency;
  82. $this->_resource = $resource;
  83. $this->_customerSession = $customerSession;
  84. $this->_priceAlgorithm = $priceAlgorithm;
  85. parent::__construct($filterItemFactory, $storeManager, $layer, $itemDataBuilder, $data);
  86. $this->_requestVar = 'price';
  87. $this->algorithmFactory = $algorithmFactory;
  88. $this->dataProvider = $dataProviderFactory->create(['layer' => $this->getLayer()]);
  89. }
  90. /**
  91. * Apply price range filter
  92. *
  93. * @param \Magento\Framework\App\RequestInterface $request
  94. * @return $this
  95. */
  96. public function apply(\Magento\Framework\App\RequestInterface $request)
  97. {
  98. /**
  99. * Filter must be string: $fromPrice-$toPrice
  100. */
  101. $filter = $request->getParam($this->getRequestVar());
  102. if (!$filter || is_array($filter)) {
  103. return $this;
  104. }
  105. //validate filter
  106. $filterParams = explode(',', $filter);
  107. $filter = $this->dataProvider->validateFilter($filterParams[0]);
  108. if (!$filter) {
  109. return $this;
  110. }
  111. list($from, $to) = $filter;
  112. $this->dataProvider->setInterval([$from, $to]);
  113. $priorFilters = $this->dataProvider->getPriorFilters($filterParams);
  114. if ($priorFilters) {
  115. $this->dataProvider->setPriorIntervals($priorFilters);
  116. }
  117. $this->_applyPriceRange();
  118. $this->getLayer()
  119. ->getState()
  120. ->addFilter(
  121. $this->_createItem($this->_renderRangeLabel(empty($from) ? 0 : $from, $to), $filter)
  122. );
  123. return $this;
  124. }
  125. /**
  126. * Retrieve active customer group id
  127. *
  128. * @return int
  129. */
  130. public function getCustomerGroupId()
  131. {
  132. $customerGroupId = $this->_getData('customer_group_id');
  133. if (is_null($customerGroupId)) {
  134. $customerGroupId = $this->_customerSession->getCustomerGroupId();
  135. }
  136. return $customerGroupId;
  137. }
  138. /**
  139. * Set active customer group id for filter
  140. *
  141. * @param int $customerGroupId
  142. * @return $this
  143. */
  144. public function setCustomerGroupId($customerGroupId)
  145. {
  146. return $this->setData('customer_group_id', $customerGroupId);
  147. }
  148. /**
  149. * Retrieve active currency rate for filter
  150. *
  151. * @return float
  152. */
  153. public function getCurrencyRate()
  154. {
  155. $rate = $this->_getData('currency_rate');
  156. if (is_null($rate)) {
  157. $rate = $this->_storeManager->getStore($this->getStoreId())
  158. ->getCurrentCurrencyRate();
  159. }
  160. if (!$rate) {
  161. $rate = 1;
  162. }
  163. return $rate;
  164. }
  165. /**
  166. * Set active currency rate for filter
  167. *
  168. * @param float $rate
  169. * @return $this
  170. */
  171. public function setCurrencyRate($rate)
  172. {
  173. return $this->setData('currency_rate', $rate);
  174. }
  175. /**
  176. * Get filter value for reset current filter state
  177. *
  178. * @return null|string
  179. */
  180. public function getResetValue()
  181. {
  182. return $this->dataProvider->getResetValue();
  183. }
  184. /**
  185. * Get 'clear price' link text
  186. *
  187. * @return \Magento\Framework\Phrase|bool
  188. */
  189. public function getClearLinkText()
  190. {
  191. if ($this->dataProvider->getPriorIntervals()
  192. ) {
  193. return __('Clear Price');
  194. }
  195. return parent::getClearLinkText();
  196. }
  197. /**
  198. * Prepare text of range label
  199. *
  200. * @param float|string $fromPrice
  201. * @param float|string $toPrice
  202. * @return float|\Magento\Framework\Phrase
  203. */
  204. protected function _renderRangeLabel($fromPrice, $toPrice)
  205. {
  206. $formattedFromPrice = $this->priceCurrency->format($fromPrice);
  207. if ($toPrice === '') {
  208. return __('%1 and above', $formattedFromPrice);
  209. } elseif ($fromPrice == $toPrice && $this->dataProvider->getOnePriceIntervalValue()
  210. ) {
  211. return $formattedFromPrice;
  212. } else {
  213. if ($fromPrice != $toPrice) {
  214. $toPrice -= .01;
  215. }
  216. return __('%1 - %2', $formattedFromPrice, $this->priceCurrency->format($toPrice));
  217. }
  218. }
  219. /**
  220. * Get additional request param data
  221. *
  222. * @return string
  223. */
  224. protected function _getAdditionalRequestData()
  225. {
  226. $result = '';
  227. $appliedInterval = $this->dataProvider->getInterval();
  228. if ($appliedInterval) {
  229. $result = ',' . $appliedInterval[0] . '-' . $appliedInterval[1];
  230. $priorIntervals = $this->getResetValue();
  231. if ($priorIntervals) {
  232. $result .= ',' . $priorIntervals;
  233. }
  234. }
  235. return $result;
  236. }
  237. /**
  238. * Get data for build price filter items
  239. * @return array
  240. * @throws \Magento\Framework\Exception\LocalizedException
  241. */
  242. protected function _getItemsData()
  243. {
  244. $algorithm = $this->algorithmFactory->create();
  245. return $algorithm->getItemsData((array)$this->dataProvider->getInterval(), $this->dataProvider->getAdditionalRequestData());
  246. }
  247. /**
  248. * Apply price range filter to collection
  249. *
  250. * @return $this
  251. */
  252. protected function _applyPriceRange()
  253. {
  254. $this->dataProvider->getResource()->applyPriceRange($this, $this->dataProvider->getInterval());
  255. return $this;
  256. }
  257. }