Result.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\CatalogSearch\Controller\Advanced;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. use Magento\CatalogSearch\Model\Advanced as ModelAdvanced;
  10. use Magento\Framework\App\Action\Context;
  11. use Magento\Framework\App\Action\HttpPostActionInterface;
  12. use Magento\Framework\UrlFactory;
  13. /**
  14. * Advanced search result.
  15. */
  16. class Result extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface, HttpPostActionInterface
  17. {
  18. /**
  19. * No results default handle.
  20. */
  21. const DEFAULT_NO_RESULT_HANDLE = 'catalogsearch_advanced_result_noresults';
  22. /**
  23. * Url factory
  24. *
  25. * @var UrlFactory
  26. */
  27. protected $_urlFactory;
  28. /**
  29. * Catalog search advanced
  30. *
  31. * @var ModelAdvanced
  32. */
  33. protected $_catalogSearchAdvanced;
  34. /**
  35. * Construct
  36. *
  37. * @param Context $context
  38. * @param ModelAdvanced $catalogSearchAdvanced
  39. * @param UrlFactory $urlFactory
  40. */
  41. public function __construct(
  42. Context $context,
  43. ModelAdvanced $catalogSearchAdvanced,
  44. UrlFactory $urlFactory
  45. ) {
  46. parent::__construct($context);
  47. $this->_catalogSearchAdvanced = $catalogSearchAdvanced;
  48. $this->_urlFactory = $urlFactory;
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function execute()
  54. {
  55. try {
  56. $this->_catalogSearchAdvanced->addFilters($this->getRequest()->getQueryValue());
  57. $size = $this->_catalogSearchAdvanced->getProductCollection()->getSize();
  58. $handles = null;
  59. if ($size == 0) {
  60. $this->_view->getPage()->initLayout();
  61. $handles = $this->_view->getLayout()->getUpdate()->getHandles();
  62. $handles[] = static::DEFAULT_NO_RESULT_HANDLE;
  63. }
  64. $this->_view->loadLayout($handles);
  65. $this->_view->renderLayout();
  66. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  67. $this->messageManager->addError($e->getMessage());
  68. $defaultUrl = $this->_urlFactory->create()
  69. ->addQueryParams($this->getRequest()->getQueryValue())
  70. ->getUrl('*/*/');
  71. $resultRedirect = $this->resultRedirectFactory->create();
  72. $resultRedirect->setUrl($this->_redirect->error($defaultUrl));
  73. return $resultRedirect;
  74. }
  75. }
  76. }