Grid.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CheckoutAgreements\Block\Adminhtml\Agreement;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\CheckoutAgreements\Model\ResourceModel\Agreement\Grid\CollectionFactory as GridCollectionFactory;
  9. class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
  10. {
  11. /**
  12. * @var \Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory
  13. * @deprecated 100.2.2
  14. */
  15. protected $_collectionFactory;
  16. /**
  17. * @param GridCollectionFactory
  18. */
  19. private $gridCollectionFactory;
  20. /**
  21. * @param \Magento\Backend\Block\Template\Context $context
  22. * @param \Magento\Backend\Helper\Data $backendHelper
  23. * @param \Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory $collectionFactory
  24. * @param array $data
  25. * @param GridCollectionFactory $gridColFactory
  26. * @codeCoverageIgnore
  27. */
  28. public function __construct(
  29. \Magento\Backend\Block\Template\Context $context,
  30. \Magento\Backend\Helper\Data $backendHelper,
  31. \Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory $collectionFactory,
  32. array $data = [],
  33. GridCollectionFactory $gridColFactory = null
  34. ) {
  35. $this->_collectionFactory = $collectionFactory;
  36. $this->gridCollectionFactory = $gridColFactory
  37. ? : ObjectManager::getInstance()->get(GridCollectionFactory::class);
  38. parent::__construct($context, $backendHelper, $data);
  39. }
  40. /**
  41. * @return void
  42. */
  43. protected function _construct()
  44. {
  45. parent::_construct();
  46. $this->setDefaultSort('agreement_id');
  47. $this->setId('agreementGrid');
  48. $this->setDefaultDir('asc');
  49. $this->setSaveParametersInSession(true);
  50. }
  51. /**
  52. * @return $this
  53. * @codeCoverageIgnore
  54. */
  55. protected function _prepareCollection()
  56. {
  57. $this->setCollection($this->gridCollectionFactory->create());
  58. return parent::_prepareCollection();
  59. }
  60. /**
  61. * @return $this
  62. */
  63. protected function _prepareColumns()
  64. {
  65. $this->addColumn(
  66. 'agreement_id',
  67. [
  68. 'header' => __('ID'),
  69. 'index' => 'agreement_id',
  70. 'header_css_class' => 'col-id',
  71. 'column_css_class' => 'col-id'
  72. ]
  73. );
  74. $this->addColumn(
  75. 'name',
  76. [
  77. 'header' => __('Condition'),
  78. 'index' => 'name',
  79. 'header_css_class' => 'col-name',
  80. 'column_css_class' => 'col-name'
  81. ]
  82. );
  83. if (!$this->_storeManager->isSingleStoreMode()) {
  84. $this->addColumn(
  85. 'stores',
  86. [
  87. 'header' => __('Store View'),
  88. 'index' => 'stores',
  89. 'type' => 'store',
  90. 'store_all' => true,
  91. 'store_view' => true,
  92. 'sortable' => false,
  93. 'filter_condition_callback' => [$this, '_filterStoreCondition'],
  94. 'header_css_class' => 'col-store-view',
  95. 'column_css_class' => 'col-store-view'
  96. ]
  97. );
  98. }
  99. $this->addColumn(
  100. 'is_active',
  101. [
  102. 'header' => __('Status'),
  103. 'index' => 'is_active',
  104. 'type' => 'options',
  105. 'options' => [0 => __('Disabled'), 1 => __('Enabled')],
  106. 'header_css_class' => 'col-status',
  107. 'column_css_class' => 'col-status'
  108. ]
  109. );
  110. return parent::_prepareColumns();
  111. }
  112. /**
  113. * @return void
  114. */
  115. protected function _afterLoadCollection()
  116. {
  117. $this->getCollection()->walk('afterLoad');
  118. parent::_afterLoadCollection();
  119. }
  120. /**
  121. * @param \Magento\Framework\Data\Collection $collection
  122. * @param \Magento\Backend\Block\Widget\Grid\Column $column
  123. * @return void
  124. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  125. */
  126. protected function _filterStoreCondition($collection, $column)
  127. {
  128. if (!($value = $column->getFilter()->getValue())) {
  129. return;
  130. }
  131. $this->getCollection()->addStoreFilter($value);
  132. }
  133. /**
  134. * @param \Magento\Framework\DataObject $row
  135. * @return string
  136. * @codeCoverageIgnore
  137. */
  138. public function getRowUrl($row)
  139. {
  140. return $this->getUrl('checkout/*/edit', ['id' => $row->getId()]);
  141. }
  142. }