123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Backend\Controller\Adminhtml\Index;
- use Magento\Backend\Controller\Adminhtml\Index as IndexAction;
- use Magento\Framework\App\Action\HttpGetActionInterface;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- /**
- * @api
- * @since 100.0.2
- */
- class GlobalSearch extends IndexAction implements HttpGetActionInterface, HttpPostActionInterface
- {
- /**
- * @var \Magento\Framework\Controller\Result\JsonFactory
- */
- protected $resultJsonFactory;
- /**
- * Search modules list
- *
- * @var array
- */
- protected $_searchModules;
- /**
- * @param \Magento\Backend\App\Action\Context $context
- * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
- * @param array $searchModules
- */
- public function __construct(
- \Magento\Backend\App\Action\Context $context,
- \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
- array $searchModules = []
- ) {
- $this->_searchModules = $searchModules;
- parent::__construct($context);
- $this->resultJsonFactory = $resultJsonFactory;
- }
- /**
- * Global Search Action
- *
- * @return \Magento\Framework\Controller\Result\Json
- */
- public function execute()
- {
- $items = [];
- if (!$this->_authorization->isAllowed('Magento_Backend::global_search')) {
- $items[] = [
- 'id' => 'error',
- 'type' => __('Error'),
- 'name' => __('Access Denied.'),
- 'description' => __('You need more permissions to do this.'),
- ];
- } else {
- if (empty($this->_searchModules)) {
- $items[] = [
- 'id' => 'error',
- 'type' => __('Error'),
- 'name' => __('No search modules were registered'),
- 'description' => __(
- 'Please make sure that all global admin search modules are installed and activated.'
- ),
- ];
- } else {
- $start = $this->getRequest()->getParam('start', 1);
- $limit = $this->getRequest()->getParam('limit', 10);
- $query = $this->getRequest()->getParam('query', '');
- foreach ($this->_searchModules as $searchConfig) {
- if ($searchConfig['acl'] && !$this->_authorization->isAllowed($searchConfig['acl'])) {
- continue;
- }
- $className = $searchConfig['class'];
- if (empty($className)) {
- continue;
- }
- $searchInstance = $this->_objectManager->create($className);
- $results = $searchInstance->setStart(
- $start
- )->setLimit(
- $limit
- )->setQuery(
- $query
- )->load()->getResults();
- $items = array_merge_recursive($items, $results);
- }
- }
- }
- /** @var \Magento\Framework\Controller\Result\Json $resultJson */
- $resultJson = $this->resultJsonFactory->create();
- return $resultJson->setData($items);
- }
- }
|