Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GlobalSearch.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
12 
17 class GlobalSearch extends IndexAction implements HttpGetActionInterface, HttpPostActionInterface
18 {
22  protected $resultJsonFactory;
23 
29  protected $_searchModules;
30 
36  public function __construct(
37  \Magento\Backend\App\Action\Context $context,
38  \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
39  array $searchModules = []
40  ) {
41  $this->_searchModules = $searchModules;
42  parent::__construct($context);
43  $this->resultJsonFactory = $resultJsonFactory;
44  }
45 
51  public function execute()
52  {
53  $items = [];
54 
55  if (!$this->_authorization->isAllowed('Magento_Backend::global_search')) {
56  $items[] = [
57  'id' => 'error',
58  'type' => __('Error'),
59  'name' => __('Access Denied.'),
60  'description' => __('You need more permissions to do this.'),
61  ];
62  } else {
63  if (empty($this->_searchModules)) {
64  $items[] = [
65  'id' => 'error',
66  'type' => __('Error'),
67  'name' => __('No search modules were registered'),
68  'description' => __(
69  'Please make sure that all global admin search modules are installed and activated.'
70  ),
71  ];
72  } else {
73  $start = $this->getRequest()->getParam('start', 1);
74  $limit = $this->getRequest()->getParam('limit', 10);
75  $query = $this->getRequest()->getParam('query', '');
76  foreach ($this->_searchModules as $searchConfig) {
77  if ($searchConfig['acl'] && !$this->_authorization->isAllowed($searchConfig['acl'])) {
78  continue;
79  }
80 
81  $className = $searchConfig['class'];
82  if (empty($className)) {
83  continue;
84  }
85  $searchInstance = $this->_objectManager->create($className);
86  $results = $searchInstance->setStart(
87  $start
88  )->setLimit(
89  $limit
90  )->setQuery(
91  $query
92  )->load()->getResults();
93  $items = array_merge_recursive($items, $results);
94  }
95  }
96  }
97 
99  $resultJson = $this->resultJsonFactory->create();
100  return $resultJson->setData($items);
101  }
102 }
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, array $searchModules=[])
$results
Definition: popup.phtml:13
__()
Definition: __.php:13
$start
Definition: listing.phtml:18
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
$items