Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Grid.php
Go to the documentation of this file.
1 <?php
20 
21 class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
22 {
28  protected $_reviewActionPager = null;
29 
35  protected $_reviewData = null;
36 
42  protected $_coreRegistry = null;
43 
49  protected $_productsFactory;
50 
56  protected $_reviewFactory;
57 
68  public function __construct(
69  \Magento\Backend\Block\Template\Context $context,
70  \Magento\Backend\Helper\Data $backendHelper,
71  \Magento\Review\Model\ReviewFactory $reviewFactory,
72  \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $productsFactory,
73  \Magento\Review\Helper\Data $reviewData,
74  \Magento\Review\Helper\Action\Pager $reviewActionPager,
75  \Magento\Framework\Registry $coreRegistry,
76  array $data = []
77  ) {
78  $this->_productsFactory = $productsFactory;
79  $this->_coreRegistry = $coreRegistry;
80  $this->_reviewData = $reviewData;
81  $this->_reviewActionPager = $reviewActionPager;
82  $this->_reviewFactory = $reviewFactory;
83  parent::__construct($context, $backendHelper, $data);
84  }
85 
91  protected function _construct()
92  {
93  parent::_construct();
94  $this->setId('reviwGrid');
95  $this->setDefaultSort('created_at');
96  }
97 
103  protected function _afterLoadCollection()
104  {
106  $actionPager = $this->_reviewActionPager;
107  $actionPager->setStorageId('reviews');
108  $actionPager->setItems($this->getCollection()->getResultingIds());
109 
110  return parent::_afterLoadCollection();
111  }
112 
118  protected function _prepareCollection()
119  {
121  $model = $this->_reviewFactory->create();
123  $collection = $this->_productsFactory->create();
124 
125  if ($this->getProductId() || $this->getRequest()->getParam('productId', false)) {
126  $productId = $this->getProductId();
127  if (!$productId) {
128  $productId = $this->getRequest()->getParam('productId');
129  }
130  $this->setProductId($productId);
131  $collection->addEntityFilter($this->getProductId());
132  }
133 
134  if ($this->getCustomerId() || $this->getRequest()->getParam('customerId', false)) {
135  $customerId = $this->getCustomerId();
136  if (!$customerId) {
137  $customerId = $this->getRequest()->getParam('customerId');
138  }
139  $this->setCustomerId($customerId);
140  $collection->addCustomerFilter($this->getCustomerId());
141  }
142 
143  if ($this->_coreRegistry->registry('usePendingFilter') === true) {
144  $collection->addStatusFilter($model->getPendingStatus());
145  }
146 
147  $collection->addStoreData();
148 
149  $this->setCollection($collection);
150  return parent::_prepareCollection();
151  }
152 
159  protected function _prepareColumns()
160  {
161  $this->addColumn(
162  'review_id',
163  [
164  'header' => __('ID'),
165  'filter_index' => 'rt.review_id',
166  'index' => 'review_id',
167  'header_css_class' => 'col-id',
168  'column_css_class' => 'col-id'
169  ]
170  );
171 
172  $this->addColumn(
173  'created_at',
174  [
175  'header' => __('Created'),
176  'type' => 'datetime',
177  'filter_index' => 'rt.created_at',
178  'index' => 'review_created_at',
179  'header_css_class' => 'col-date',
180  'column_css_class' => 'col-date'
181  ]
182  );
183 
184  if (!$this->_coreRegistry->registry('usePendingFilter')) {
185  $this->addColumn(
186  'status',
187  [
188  'header' => __('Status'),
189  'type' => 'options',
190  'options' => $this->_reviewData->getReviewStatuses(),
191  'filter_index' => 'rt.status_id',
192  'index' => 'status_id'
193  ]
194  );
195  }
196 
197  $this->addColumn(
198  'title',
199  [
200  'header' => __('Title'),
201  'filter_index' => 'rdt.title',
202  'index' => 'title',
203  'type' => 'text',
204  'truncate' => 50,
205  'escape' => true
206  ]
207  );
208 
209  $this->addColumn(
210  'nickname',
211  [
212  'header' => __('Nickname'),
213  'filter_index' => 'rdt.nickname',
214  'index' => 'nickname',
215  'type' => 'text',
216  'truncate' => 50,
217  'escape' => true,
218  'header_css_class' => 'col-name',
219  'column_css_class' => 'col-name'
220  ]
221  );
222 
223  $this->addColumn(
224  'detail',
225  [
226  'header' => __('Review'),
227  'index' => 'detail',
228  'filter_index' => 'rdt.detail',
229  'type' => 'text',
230  'truncate' => 50,
231  'nl2br' => true,
232  'escape' => true
233  ]
234  );
235 
239  if (!$this->_storeManager->isSingleStoreMode()) {
240  $this->addColumn(
241  'visible_in',
242  ['header' => __('Visibility'), 'index' => 'stores', 'type' => 'store', 'store_view' => true]
243  );
244  }
245 
246  $this->addColumn(
247  'type',
248  [
249  'header' => __('Type'),
250  'type' => 'select',
251  'index' => 'type',
252  'filter' => \Magento\Review\Block\Adminhtml\Grid\Filter\Type::class,
253  'renderer' => \Magento\Review\Block\Adminhtml\Grid\Renderer\Type::class
254  ]
255  );
256 
257  $this->addColumn(
258  'name',
259  ['header' => __('Product'), 'type' => 'text', 'index' => 'name', 'escape' => true]
260  );
261 
262  $this->addColumn(
263  'sku',
264  [
265  'header' => __('SKU'),
266  'type' => 'text',
267  'index' => 'sku',
268  'escape' => true
269  ]
270  );
271 
272  $this->addColumn(
273  'action',
274  [
275  'header' => __('Action'),
276  'type' => 'action',
277  'getter' => 'getReviewId',
278  'actions' => [
279  [
280  'caption' => __('Edit'),
281  'url' => [
282  'base' => 'review/product/edit',
283  'params' => [
284  'productId' => $this->getProductId(),
285  'customerId' => $this->getCustomerId(),
286  'ret' => $this->_coreRegistry->registry('usePendingFilter') ? 'pending' : null,
287  ],
288  ],
289  'field' => 'id',
290  ],
291  ],
292  'filter' => false,
293  'sortable' => false
294  ]
295  );
296 
297  $block = $this->getLayout()->getBlock('grid.bottom.links');
298  if ($block) {
299  $this->setChild('grid.bottom.links', $block);
300  }
301 
302  return parent::_prepareColumns();
303  }
304 
310  protected function _prepareMassaction()
311  {
312  $this->setMassactionIdField('review_id');
313  $this->setMassactionIdFilter('rt.review_id');
314  $this->setMassactionIdFieldOnlyIndexValue(true);
315  $this->getMassactionBlock()->setFormFieldName('reviews');
316 
317  $this->getMassactionBlock()->addItem(
318  'delete',
319  [
320  'label' => __('Delete'),
321  'url' => $this->getUrl(
322  '*/*/massDelete',
323  ['ret' => $this->_coreRegistry->registry('usePendingFilter') ? 'pending' : 'index']
324  ),
325  'confirm' => __('Are you sure?')
326  ]
327  );
328 
329  $statuses = $this->_reviewData->getReviewStatusesOptionArray();
330  array_unshift($statuses, ['label' => '', 'value' => '']);
331  $this->getMassactionBlock()->addItem(
332  'update_status',
333  [
334  'label' => __('Update Status'),
335  'url' => $this->getUrl(
336  '*/*/massUpdateStatus',
337  ['ret' => $this->_coreRegistry->registry('usePendingFilter') ? 'pending' : 'index']
338  ),
339  'additional' => [
340  'status' => [
341  'name' => 'status',
342  'type' => 'select',
343  'class' => 'required-entry',
344  'label' => __('Status'),
345  'values' => $statuses,
346  ],
347  ]
348  ]
349  );
350  }
351 
358  public function getRowUrl($row)
359  {
360  return $this->getUrl(
361  'review/product/edit',
362  [
363  'id' => $row->getReviewId(),
364  'productId' => $this->getProductId(),
365  'customerId' => $this->getCustomerId(),
366  'ret' => $this->_coreRegistry->registry('usePendingFilter') ? 'pending' : null
367  ]
368  );
369  }
370 
376  public function getGridUrl()
377  {
378  if ($this->getProductId() || $this->getCustomerId()) {
379  return $this->getUrl(
380  'review/product' . ($this->_coreRegistry->registry('usePendingFilter') ? 'pending' : ''),
381  ['productId' => $this->getProductId(), 'customerId' => $this->getCustomerId()]
382  );
383  } else {
384  return $this->getCurrentUrl();
385  }
386  }
387 }
getCurrentUrl($params=[])
Definition: Widget.php:53
return false
Definition: gallery.phtml:36
__()
Definition: __.php:13
$block
Definition: block.php:8
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Backend\Helper\Data $backendHelper, \Magento\Review\Model\ReviewFactory $reviewFactory, \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $productsFactory, \Magento\Review\Helper\Data $reviewData, \Magento\Review\Helper\Action\Pager $reviewActionPager, \Magento\Framework\Registry $coreRegistry, array $data=[])
Definition: Grid.php:68
getParam($paramName, $default=null)
Definition: Grid.php:729