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