Last.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Block\Adminhtml\Dashboard;
  7. /**
  8. * Dashboard last search keywords block
  9. * @api
  10. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  11. * @since 100.0.2
  12. */
  13. class Last extends \Magento\Backend\Block\Dashboard\Grid
  14. {
  15. /**
  16. * @var \Magento\Search\Model\ResourceModel\Query\Collection
  17. */
  18. protected $_collection;
  19. /**
  20. * @var \Magento\Search\Model\ResourceModel\Query\CollectionFactory
  21. */
  22. protected $_queriesFactory;
  23. /**
  24. * @var \Magento\Framework\Module\Manager
  25. */
  26. protected $_moduleManager;
  27. /**
  28. * @var string
  29. */
  30. protected $_template = 'Magento_Backend::dashboard/grid.phtml';
  31. /**
  32. * @param \Magento\Backend\Block\Template\Context $context
  33. * @param \Magento\Backend\Helper\Data $backendHelper
  34. * @param \Magento\Framework\Module\Manager $moduleManager
  35. * @param \Magento\Search\Model\ResourceModel\Query\CollectionFactory $queriesFactory
  36. * @param array $data
  37. */
  38. public function __construct(
  39. \Magento\Backend\Block\Template\Context $context,
  40. \Magento\Backend\Helper\Data $backendHelper,
  41. \Magento\Framework\Module\Manager $moduleManager,
  42. \Magento\Search\Model\ResourceModel\Query\CollectionFactory $queriesFactory,
  43. array $data = []
  44. ) {
  45. $this->_moduleManager = $moduleManager;
  46. $this->_queriesFactory = $queriesFactory;
  47. parent::__construct($context, $backendHelper, $data);
  48. }
  49. /**
  50. * @return void
  51. */
  52. protected function _construct()
  53. {
  54. parent::_construct();
  55. $this->setId('lastSearchGrid');
  56. }
  57. /**
  58. * @return $this
  59. * @throws \Magento\Framework\Exception\LocalizedException
  60. */
  61. protected function _prepareCollection()
  62. {
  63. $this->_collection = $this->_queriesFactory->create();
  64. $this->_collection->setRecentQueryFilter();
  65. if ($this->getRequest()->getParam('store')) {
  66. $this->_collection->addFieldToFilter('store_id', $this->getRequest()->getParam('store'));
  67. } elseif ($this->getRequest()->getParam('website')) {
  68. $storeIds = $this->_storeManager->getWebsite($this->getRequest()->getParam('website'))->getStoreIds();
  69. $this->_collection->addFieldToFilter('store_id', ['in' => $storeIds]);
  70. } elseif ($this->getRequest()->getParam('group')) {
  71. $storeIds = $this->_storeManager->getGroup($this->getRequest()->getParam('group'))->getStoreIds();
  72. $this->_collection->addFieldToFilter('store_id', ['in' => $storeIds]);
  73. }
  74. $this->setCollection($this->_collection);
  75. return parent::_prepareCollection();
  76. }
  77. /**
  78. * @return $this
  79. * @throws \Exception
  80. */
  81. protected function _prepareColumns()
  82. {
  83. $this->addColumn(
  84. 'search_query',
  85. [
  86. 'header' => __('Search Term'),
  87. 'sortable' => false,
  88. 'index' => 'query_text',
  89. 'renderer' => \Magento\Backend\Block\Dashboard\Searches\Renderer\Searchquery::class,
  90. 'header_css_class' => 'col-search-query',
  91. 'column_css_class' => 'col-search-query'
  92. ]
  93. );
  94. $this->addColumn(
  95. 'num_results',
  96. ['header' => __('Results'), 'sortable' => false, 'index' => 'num_results', 'type' => 'number']
  97. );
  98. $this->addColumn(
  99. 'popularity',
  100. ['header' => __('Uses'), 'sortable' => false, 'index' => 'popularity', 'type' => 'number']
  101. );
  102. $this->setFilterVisibility(false);
  103. $this->setPagerVisibility(false);
  104. return parent::_prepareColumns();
  105. }
  106. /**
  107. * @inheritdoc
  108. */
  109. public function getRowUrl($row)
  110. {
  111. return $this->getUrl('search/term/edit', ['id' => $row->getId()]);
  112. }
  113. }