GlobalSearch.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Backend\Controller\Adminhtml\Index;
  8. use Magento\Backend\Controller\Adminhtml\Index as IndexAction;
  9. use Magento\Framework\App\Action\HttpGetActionInterface;
  10. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  11. /**
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class GlobalSearch extends IndexAction implements HttpGetActionInterface, HttpPostActionInterface
  16. {
  17. /**
  18. * @var \Magento\Framework\Controller\Result\JsonFactory
  19. */
  20. protected $resultJsonFactory;
  21. /**
  22. * Search modules list
  23. *
  24. * @var array
  25. */
  26. protected $_searchModules;
  27. /**
  28. * @param \Magento\Backend\App\Action\Context $context
  29. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  30. * @param array $searchModules
  31. */
  32. public function __construct(
  33. \Magento\Backend\App\Action\Context $context,
  34. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
  35. array $searchModules = []
  36. ) {
  37. $this->_searchModules = $searchModules;
  38. parent::__construct($context);
  39. $this->resultJsonFactory = $resultJsonFactory;
  40. }
  41. /**
  42. * Global Search Action
  43. *
  44. * @return \Magento\Framework\Controller\Result\Json
  45. */
  46. public function execute()
  47. {
  48. $items = [];
  49. if (!$this->_authorization->isAllowed('Magento_Backend::global_search')) {
  50. $items[] = [
  51. 'id' => 'error',
  52. 'type' => __('Error'),
  53. 'name' => __('Access Denied.'),
  54. 'description' => __('You need more permissions to do this.'),
  55. ];
  56. } else {
  57. if (empty($this->_searchModules)) {
  58. $items[] = [
  59. 'id' => 'error',
  60. 'type' => __('Error'),
  61. 'name' => __('No search modules were registered'),
  62. 'description' => __(
  63. 'Please make sure that all global admin search modules are installed and activated.'
  64. ),
  65. ];
  66. } else {
  67. $start = $this->getRequest()->getParam('start', 1);
  68. $limit = $this->getRequest()->getParam('limit', 10);
  69. $query = $this->getRequest()->getParam('query', '');
  70. foreach ($this->_searchModules as $searchConfig) {
  71. if ($searchConfig['acl'] && !$this->_authorization->isAllowed($searchConfig['acl'])) {
  72. continue;
  73. }
  74. $className = $searchConfig['class'];
  75. if (empty($className)) {
  76. continue;
  77. }
  78. $searchInstance = $this->_objectManager->create($className);
  79. $results = $searchInstance->setStart(
  80. $start
  81. )->setLimit(
  82. $limit
  83. )->setQuery(
  84. $query
  85. )->load()->getResults();
  86. $items = array_merge_recursive($items, $results);
  87. }
  88. }
  89. }
  90. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  91. $resultJson = $this->resultJsonFactory->create();
  92. return $resultJson->setData($items);
  93. }
  94. }