Grid.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Adminhtml reviews grid
  8. *
  9. * @method int getProductId() getProductId()
  10. * @method \Magento\Review\Block\Adminhtml\Grid setProductId() setProductId(int $productId)
  11. * @method int getCustomerId() getCustomerId()
  12. * @method \Magento\Review\Block\Adminhtml\Grid setCustomerId() setCustomerId(int $customerId)
  13. * @method \Magento\Review\Block\Adminhtml\Grid setMassactionIdFieldOnlyIndexValue()
  14. * setMassactionIdFieldOnlyIndexValue(bool $onlyIndex)
  15. *
  16. * @author Magento Core Team <core@magentocommerce.com>
  17. */
  18. namespace Magento\Review\Block\Adminhtml;
  19. class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
  20. {
  21. /**
  22. * Review action pager
  23. *
  24. * @var \Magento\Review\Helper\Action\Pager
  25. */
  26. protected $_reviewActionPager = null;
  27. /**
  28. * Review data
  29. *
  30. * @var \Magento\Review\Helper\Data
  31. */
  32. protected $_reviewData = null;
  33. /**
  34. * Core registry
  35. *
  36. * @var \Magento\Framework\Registry
  37. */
  38. protected $_coreRegistry = null;
  39. /**
  40. * Review collection model factory
  41. *
  42. * @var \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory
  43. */
  44. protected $_productsFactory;
  45. /**
  46. * Review model factory
  47. *
  48. * @var \Magento\Review\Model\ReviewFactory
  49. */
  50. protected $_reviewFactory;
  51. /**
  52. * @param \Magento\Backend\Block\Template\Context $context
  53. * @param \Magento\Backend\Helper\Data $backendHelper
  54. * @param \Magento\Review\Model\ReviewFactory $reviewFactory
  55. * @param \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $productsFactory
  56. * @param \Magento\Review\Helper\Data $reviewData
  57. * @param \Magento\Review\Helper\Action\Pager $reviewActionPager
  58. * @param \Magento\Framework\Registry $coreRegistry
  59. * @param array $data
  60. */
  61. public function __construct(
  62. \Magento\Backend\Block\Template\Context $context,
  63. \Magento\Backend\Helper\Data $backendHelper,
  64. \Magento\Review\Model\ReviewFactory $reviewFactory,
  65. \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $productsFactory,
  66. \Magento\Review\Helper\Data $reviewData,
  67. \Magento\Review\Helper\Action\Pager $reviewActionPager,
  68. \Magento\Framework\Registry $coreRegistry,
  69. array $data = []
  70. ) {
  71. $this->_productsFactory = $productsFactory;
  72. $this->_coreRegistry = $coreRegistry;
  73. $this->_reviewData = $reviewData;
  74. $this->_reviewActionPager = $reviewActionPager;
  75. $this->_reviewFactory = $reviewFactory;
  76. parent::__construct($context, $backendHelper, $data);
  77. }
  78. /**
  79. * Initialize grid
  80. *
  81. * @return void
  82. */
  83. protected function _construct()
  84. {
  85. parent::_construct();
  86. $this->setId('reviwGrid');
  87. $this->setDefaultSort('created_at');
  88. }
  89. /**
  90. * Save search results
  91. *
  92. * @return \Magento\Backend\Block\Widget\Grid
  93. */
  94. protected function _afterLoadCollection()
  95. {
  96. /** @var $actionPager \Magento\Review\Helper\Action\Pager */
  97. $actionPager = $this->_reviewActionPager;
  98. $actionPager->setStorageId('reviews');
  99. $actionPager->setItems($this->getCollection()->getResultingIds());
  100. return parent::_afterLoadCollection();
  101. }
  102. /**
  103. * Prepare collection
  104. *
  105. * @return \Magento\Review\Block\Adminhtml\Grid
  106. */
  107. protected function _prepareCollection()
  108. {
  109. /** @var $model \Magento\Review\Model\Review */
  110. $model = $this->_reviewFactory->create();
  111. /** @var $collection \Magento\Review\Model\ResourceModel\Review\Product\Collection */
  112. $collection = $this->_productsFactory->create();
  113. if ($this->getProductId() || $this->getRequest()->getParam('productId', false)) {
  114. $productId = $this->getProductId();
  115. if (!$productId) {
  116. $productId = $this->getRequest()->getParam('productId');
  117. }
  118. $this->setProductId($productId);
  119. $collection->addEntityFilter($this->getProductId());
  120. }
  121. if ($this->getCustomerId() || $this->getRequest()->getParam('customerId', false)) {
  122. $customerId = $this->getCustomerId();
  123. if (!$customerId) {
  124. $customerId = $this->getRequest()->getParam('customerId');
  125. }
  126. $this->setCustomerId($customerId);
  127. $collection->addCustomerFilter($this->getCustomerId());
  128. }
  129. if ($this->_coreRegistry->registry('usePendingFilter') === true) {
  130. $collection->addStatusFilter($model->getPendingStatus());
  131. }
  132. $collection->addStoreData();
  133. $this->setCollection($collection);
  134. return parent::_prepareCollection();
  135. }
  136. /**
  137. * Prepare grid columns
  138. *
  139. * @return \Magento\Backend\Block\Widget\Grid
  140. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  141. */
  142. protected function _prepareColumns()
  143. {
  144. $this->addColumn(
  145. 'review_id',
  146. [
  147. 'header' => __('ID'),
  148. 'filter_index' => 'rt.review_id',
  149. 'index' => 'review_id',
  150. 'header_css_class' => 'col-id',
  151. 'column_css_class' => 'col-id'
  152. ]
  153. );
  154. $this->addColumn(
  155. 'created_at',
  156. [
  157. 'header' => __('Created'),
  158. 'type' => 'datetime',
  159. 'filter_index' => 'rt.created_at',
  160. 'index' => 'review_created_at',
  161. 'header_css_class' => 'col-date',
  162. 'column_css_class' => 'col-date'
  163. ]
  164. );
  165. if (!$this->_coreRegistry->registry('usePendingFilter')) {
  166. $this->addColumn(
  167. 'status',
  168. [
  169. 'header' => __('Status'),
  170. 'type' => 'options',
  171. 'options' => $this->_reviewData->getReviewStatuses(),
  172. 'filter_index' => 'rt.status_id',
  173. 'index' => 'status_id'
  174. ]
  175. );
  176. }
  177. $this->addColumn(
  178. 'title',
  179. [
  180. 'header' => __('Title'),
  181. 'filter_index' => 'rdt.title',
  182. 'index' => 'title',
  183. 'type' => 'text',
  184. 'truncate' => 50,
  185. 'escape' => true
  186. ]
  187. );
  188. $this->addColumn(
  189. 'nickname',
  190. [
  191. 'header' => __('Nickname'),
  192. 'filter_index' => 'rdt.nickname',
  193. 'index' => 'nickname',
  194. 'type' => 'text',
  195. 'truncate' => 50,
  196. 'escape' => true,
  197. 'header_css_class' => 'col-name',
  198. 'column_css_class' => 'col-name'
  199. ]
  200. );
  201. $this->addColumn(
  202. 'detail',
  203. [
  204. 'header' => __('Review'),
  205. 'index' => 'detail',
  206. 'filter_index' => 'rdt.detail',
  207. 'type' => 'text',
  208. 'truncate' => 50,
  209. 'nl2br' => true,
  210. 'escape' => true
  211. ]
  212. );
  213. /**
  214. * Check is single store mode
  215. */
  216. if (!$this->_storeManager->isSingleStoreMode()) {
  217. $this->addColumn(
  218. 'visible_in',
  219. ['header' => __('Visibility'), 'index' => 'stores', 'type' => 'store', 'store_view' => true]
  220. );
  221. }
  222. $this->addColumn(
  223. 'type',
  224. [
  225. 'header' => __('Type'),
  226. 'type' => 'select',
  227. 'index' => 'type',
  228. 'filter' => \Magento\Review\Block\Adminhtml\Grid\Filter\Type::class,
  229. 'renderer' => \Magento\Review\Block\Adminhtml\Grid\Renderer\Type::class
  230. ]
  231. );
  232. $this->addColumn(
  233. 'name',
  234. ['header' => __('Product'), 'type' => 'text', 'index' => 'name', 'escape' => true]
  235. );
  236. $this->addColumn(
  237. 'sku',
  238. [
  239. 'header' => __('SKU'),
  240. 'type' => 'text',
  241. 'index' => 'sku',
  242. 'escape' => true
  243. ]
  244. );
  245. $this->addColumn(
  246. 'action',
  247. [
  248. 'header' => __('Action'),
  249. 'type' => 'action',
  250. 'getter' => 'getReviewId',
  251. 'actions' => [
  252. [
  253. 'caption' => __('Edit'),
  254. 'url' => [
  255. 'base' => 'review/product/edit',
  256. 'params' => [
  257. 'productId' => $this->getProductId(),
  258. 'customerId' => $this->getCustomerId(),
  259. 'ret' => $this->_coreRegistry->registry('usePendingFilter') ? 'pending' : null,
  260. ],
  261. ],
  262. 'field' => 'id',
  263. ],
  264. ],
  265. 'filter' => false,
  266. 'sortable' => false
  267. ]
  268. );
  269. $block = $this->getLayout()->getBlock('grid.bottom.links');
  270. if ($block) {
  271. $this->setChild('grid.bottom.links', $block);
  272. }
  273. return parent::_prepareColumns();
  274. }
  275. /**
  276. * Prepare grid mass actions
  277. *
  278. * @return void
  279. */
  280. protected function _prepareMassaction()
  281. {
  282. $this->setMassactionIdField('review_id');
  283. $this->setMassactionIdFilter('rt.review_id');
  284. $this->setMassactionIdFieldOnlyIndexValue(true);
  285. $this->getMassactionBlock()->setFormFieldName('reviews');
  286. $this->getMassactionBlock()->addItem(
  287. 'delete',
  288. [
  289. 'label' => __('Delete'),
  290. 'url' => $this->getUrl(
  291. '*/*/massDelete',
  292. ['ret' => $this->_coreRegistry->registry('usePendingFilter') ? 'pending' : 'index']
  293. ),
  294. 'confirm' => __('Are you sure?')
  295. ]
  296. );
  297. $statuses = $this->_reviewData->getReviewStatusesOptionArray();
  298. array_unshift($statuses, ['label' => '', 'value' => '']);
  299. $this->getMassactionBlock()->addItem(
  300. 'update_status',
  301. [
  302. 'label' => __('Update Status'),
  303. 'url' => $this->getUrl(
  304. '*/*/massUpdateStatus',
  305. ['ret' => $this->_coreRegistry->registry('usePendingFilter') ? 'pending' : 'index']
  306. ),
  307. 'additional' => [
  308. 'status' => [
  309. 'name' => 'status',
  310. 'type' => 'select',
  311. 'class' => 'required-entry',
  312. 'label' => __('Status'),
  313. 'values' => $statuses,
  314. ],
  315. ]
  316. ]
  317. );
  318. }
  319. /**
  320. * Get row url
  321. *
  322. * @param \Magento\Review\Model\Review|\Magento\Framework\DataObject $row
  323. * @return string
  324. */
  325. public function getRowUrl($row)
  326. {
  327. return $this->getUrl(
  328. 'review/product/edit',
  329. [
  330. 'id' => $row->getReviewId(),
  331. 'productId' => $this->getProductId(),
  332. 'customerId' => $this->getCustomerId(),
  333. 'ret' => $this->_coreRegistry->registry('usePendingFilter') ? 'pending' : null
  334. ]
  335. );
  336. }
  337. /**
  338. * Get grid url
  339. *
  340. * @return string
  341. */
  342. public function getGridUrl()
  343. {
  344. if ($this->getProductId() || $this->getCustomerId()) {
  345. return $this->getUrl(
  346. 'review/product' . ($this->_coreRegistry->registry('usePendingFilter') ? 'pending' : ''),
  347. ['productId' => $this->getProductId(), 'customerId' => $this->getCustomerId()]
  348. );
  349. } else {
  350. return $this->getCurrentUrl();
  351. }
  352. }
  353. }