Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Chooser.php
Go to the documentation of this file.
1 <?php
8 
12 class Chooser extends \Magento\Backend\Block\Widget\Grid\Extended
13 {
17  protected $ruleFactory;
18 
25  public function __construct(
26  \Magento\Backend\Block\Template\Context $context,
27  \Magento\Backend\Helper\Data $backendHelper,
28  \Magento\SalesRule\Model\RuleFactory $ruleFactory,
29  array $data = []
30  ) {
31  $this->ruleFactory = $ruleFactory;
32  parent::__construct($context, $backendHelper, $data);
33  }
34 
40  protected function _construct()
41  {
42  parent::_construct();
43  $this->setDefaultSort('rule_id');
44  $this->setDefaultDir('ASC');
45  $this->setUseAjax(true);
46  }
47 
53  protected function _prepareCollection()
54  {
55  $collection = $this->ruleFactory->create()->getResourceCollection();
56  $this->setCollection($collection);
57 
58  $this->_eventManager->dispatch(
59  'adminhtml_block_promo_widget_chooser_prepare_collection',
60  ['collection' => $collection]
61  );
62 
63  return parent::_prepareCollection();
64  }
65 
72  public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
73  {
74  $uniqId = $this->mathRandom->getUniqueHash($element->getId());
75  $sourceUrl = $this->getUrl('sales_rule/promo_quote/chooser', ['uniq_id' => $uniqId]);
76 
77  $chooser = $this->getLayout()->createBlock(
78  \Magento\Widget\Block\Adminhtml\Widget\Chooser::class
79  )->setElement(
80  $element
81  )->setConfig(
82  $this->getConfig()
83  )->setFieldsetId(
84  $this->getFieldsetId()
85  )->setSourceUrl(
86  $sourceUrl
87  )->setUniqId(
88  $uniqId
89  );
90 
91  if ($element->getValue()) {
92  $rule = $this->ruleFactory->create()->load((int)$element->getValue());
93  if ($rule->getId()) {
94  $chooser->setLabel($this->escapeHtml($rule->getName()));
95  }
96  }
97 
98  $element->setData('after_element_html', $chooser->toHtml());
99  return $element;
100  }
101 
107  public function getRowClickCallback()
108  {
109  $chooserJsObject = $this->getId();
110  $js = '
111  function (grid, event) {
112  var trElement = Event.findElement(event, "tr");
113  var ruleName = trElement.down("td").next().innerHTML;
114  var ruleId = trElement.down("td").innerHTML.replace(/^\s+|\s+$/g,"");
115  ' .
116  $chooserJsObject .
117  '.setElementValue(ruleId);
118  ' .
119  $chooserJsObject .
120  '.setElementLabel(ruleName);
121  ' .
122  $chooserJsObject .
123  '.close();
124  }
125  ';
126  return $js;
127  }
128 
134  protected function _prepareColumns()
135  {
136  $this->addColumn(
137  'rule_id',
138  ['header' => __('ID'), 'align' => 'right', 'width' => '50px', 'index' => 'rule_id']
139  );
140 
141  $this->addColumn('name', ['header' => __('Rule'), 'align' => 'left', 'index' => 'name']);
142 
143  $this->addColumn(
144  'coupon_code',
145  ['header' => __('Coupon Code'), 'align' => 'left', 'width' => '150px', 'index' => 'code']
146  );
147 
148  $this->addColumn(
149  'from_date',
150  [
151  'header' => __('Start'),
152  'align' => 'left',
153  'width' => '120px',
154  'type' => 'date',
155  'index' => 'from_date'
156  ]
157  );
158 
159  $this->addColumn(
160  'to_date',
161  [
162  'header' => __('End'),
163  'align' => 'left',
164  'width' => '120px',
165  'type' => 'date',
166  'default' => '--',
167  'index' => 'to_date'
168  ]
169  );
170 
171  $this->addColumn(
172  'is_active',
173  [
174  'header' => __('Status'),
175  'align' => 'left',
176  'width' => '80px',
177  'index' => 'is_active',
178  'type' => 'options',
179  'options' => [1 => 'Active', 0 => 'Inactive']
180  ]
181  );
182 
183  return parent::_prepareColumns();
184  }
185 
191  public function getGridUrl()
192  {
193  return $this->getUrl('sales_rule/promo_quote/chooser', ['_current' => true]);
194  }
195 }
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Backend\Helper\Data $backendHelper, \Magento\SalesRule\Model\RuleFactory $ruleFactory, array $data=[])
Definition: Chooser.php:25
__()
Definition: __.php:13
prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
Definition: Chooser.php:72
$element
Definition: element.phtml:12