Result.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Block\Advanced;
  7. use Magento\Catalog\Model\Layer\Resolver as LayerResolver;
  8. use Magento\CatalogSearch\Model\Advanced;
  9. use Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection;
  10. use Magento\Framework\UrlFactory;
  11. use Magento\Framework\View\Element\AbstractBlock;
  12. use Magento\Framework\View\Element\Template;
  13. use Magento\Framework\View\Element\Template\Context;
  14. /**
  15. * Advanced search result
  16. *
  17. * @api
  18. * @since 100.0.2
  19. */
  20. class Result extends Template
  21. {
  22. /**
  23. * Url factory
  24. *
  25. * @var UrlFactory
  26. */
  27. protected $_urlFactory;
  28. /**
  29. * Catalog layer
  30. *
  31. * @var \Magento\Catalog\Model\Layer
  32. */
  33. protected $_catalogLayer;
  34. /**
  35. * Catalog search advanced
  36. *
  37. * @var Advanced
  38. */
  39. protected $_catalogSearchAdvanced;
  40. /**
  41. * @param Context $context
  42. * @param Advanced $catalogSearchAdvanced
  43. * @param LayerResolver $layerResolver
  44. * @param UrlFactory $urlFactory
  45. * @param array $data
  46. */
  47. public function __construct(
  48. Context $context,
  49. Advanced $catalogSearchAdvanced,
  50. LayerResolver $layerResolver,
  51. UrlFactory $urlFactory,
  52. array $data = []
  53. ) {
  54. $this->_catalogSearchAdvanced = $catalogSearchAdvanced;
  55. $this->_catalogLayer = $layerResolver->get();
  56. $this->_urlFactory = $urlFactory;
  57. parent::__construct($context, $data);
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. protected function _prepareLayout()
  63. {
  64. $this->pageConfig->getTitle()->set($this->getPageTitle());
  65. $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
  66. if ($breadcrumbs) {
  67. $breadcrumbs->addCrumb(
  68. 'home',
  69. [
  70. 'label' => __('Home'),
  71. 'title' => __('Go to Home Page'),
  72. 'link' => $this->_storeManager->getStore()->getBaseUrl()
  73. ]
  74. )->addCrumb(
  75. 'search',
  76. ['label' => __('Catalog Advanced Search'), 'link' => $this->getUrl('*/*/')]
  77. )->addCrumb(
  78. 'search_result',
  79. ['label' => __('Results')]
  80. );
  81. }
  82. return parent::_prepareLayout();
  83. }
  84. /**
  85. * Get page title
  86. *
  87. * @return \Magento\Framework\Phrase
  88. */
  89. private function getPageTitle()
  90. {
  91. return __('Advanced Search Results');
  92. }
  93. /**
  94. * Set order options
  95. *
  96. * @return void
  97. */
  98. public function setListOrders()
  99. {
  100. /* @var $category \Magento\Catalog\Model\Category */
  101. $category = $this->_catalogLayer->getCurrentCategory();
  102. $availableOrders = $category->getAvailableSortByOptions();
  103. unset($availableOrders['position']);
  104. $this->getChildBlock('search_result_list')->setAvailableOrders($availableOrders);
  105. }
  106. /**
  107. * Set view mode options
  108. *
  109. * @return void
  110. */
  111. public function setListModes()
  112. {
  113. $this->getChildBlock('search_result_list')->setModes(['grid' => __('Grid'), 'list' => __('List')]);
  114. }
  115. /**
  116. * Initialize list collection.
  117. *
  118. * @return void
  119. */
  120. public function setListCollection()
  121. {
  122. $this->getChildBlock('search_result_list')->setCollection($this->_getProductCollection());
  123. }
  124. /**
  125. * Get product collection.
  126. *
  127. * @return Collection
  128. */
  129. protected function _getProductCollection()
  130. {
  131. return $this->getSearchModel()->getProductCollection();
  132. }
  133. /**
  134. * Set search model.
  135. *
  136. * @return Advanced
  137. */
  138. public function getSearchModel()
  139. {
  140. return $this->_catalogSearchAdvanced;
  141. }
  142. /**
  143. * Get result count.
  144. *
  145. * @return mixed
  146. */
  147. public function getResultCount()
  148. {
  149. if (!$this->getData('result_count')) {
  150. $size = $this->getSearchModel()->getProductCollection()->getSize();
  151. $this->setResultCount($size);
  152. }
  153. return $this->getData('result_count');
  154. }
  155. /**
  156. * Get product list HTML.
  157. *
  158. * @return string
  159. */
  160. public function getProductListHtml()
  161. {
  162. return $this->getChildHtml('search_result_list');
  163. }
  164. /**
  165. * Get form URL.
  166. *
  167. * @return string
  168. */
  169. public function getFormUrl()
  170. {
  171. return $this->_urlFactory->create()->addQueryParams(
  172. $this->getRequest()->getQueryValue()
  173. )->getUrl(
  174. '*/*/',
  175. ['_escape' => true]
  176. );
  177. }
  178. /**
  179. * Get search criteria.
  180. *
  181. * @return array
  182. */
  183. public function getSearchCriterias()
  184. {
  185. $searchCriterias = $this->getSearchModel()->getSearchCriterias();
  186. $middle = ceil(count($searchCriterias) / 2);
  187. $left = array_slice($searchCriterias, 0, $middle);
  188. $right = array_slice($searchCriterias, $middle);
  189. return ['left' => $left, 'right' => $right];
  190. }
  191. }