Chooser.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Block\Adminhtml\Promo\Widget;
  7. /**
  8. * Widget that allows to select a sales rule.
  9. */
  10. class Chooser extends \Magento\Backend\Block\Widget\Grid\Extended
  11. {
  12. /**
  13. * @var \Magento\SalesRule\Model\RuleFactory
  14. */
  15. protected $ruleFactory;
  16. /**
  17. * @param \Magento\Backend\Block\Template\Context $context
  18. * @param \Magento\Backend\Helper\Data $backendHelper
  19. * @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
  20. * @param array $data
  21. */
  22. public function __construct(
  23. \Magento\Backend\Block\Template\Context $context,
  24. \Magento\Backend\Helper\Data $backendHelper,
  25. \Magento\SalesRule\Model\RuleFactory $ruleFactory,
  26. array $data = []
  27. ) {
  28. $this->ruleFactory = $ruleFactory;
  29. parent::__construct($context, $backendHelper, $data);
  30. }
  31. /**
  32. * Block constructor, prepare grid params
  33. *
  34. * @return void
  35. */
  36. protected function _construct()
  37. {
  38. parent::_construct();
  39. $this->setDefaultSort('rule_id');
  40. $this->setDefaultDir('ASC');
  41. $this->setUseAjax(true);
  42. }
  43. /**
  44. * Prepare rules collection
  45. *
  46. * @return $this
  47. */
  48. protected function _prepareCollection()
  49. {
  50. $collection = $this->ruleFactory->create()->getResourceCollection();
  51. $this->setCollection($collection);
  52. $this->_eventManager->dispatch(
  53. 'adminhtml_block_promo_widget_chooser_prepare_collection',
  54. ['collection' => $collection]
  55. );
  56. return parent::_prepareCollection();
  57. }
  58. /**
  59. * Prepare chooser element HTML
  60. *
  61. * @param \Magento\Framework\Data\Form\Element\AbstractElement $element Form Element
  62. * @return \Magento\Framework\Data\Form\Element\AbstractElement
  63. */
  64. public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
  65. {
  66. $uniqId = $this->mathRandom->getUniqueHash($element->getId());
  67. $sourceUrl = $this->getUrl('sales_rule/promo_quote/chooser', ['uniq_id' => $uniqId]);
  68. $chooser = $this->getLayout()->createBlock(
  69. \Magento\Widget\Block\Adminhtml\Widget\Chooser::class
  70. )->setElement(
  71. $element
  72. )->setConfig(
  73. $this->getConfig()
  74. )->setFieldsetId(
  75. $this->getFieldsetId()
  76. )->setSourceUrl(
  77. $sourceUrl
  78. )->setUniqId(
  79. $uniqId
  80. );
  81. if ($element->getValue()) {
  82. $rule = $this->ruleFactory->create()->load((int)$element->getValue());
  83. if ($rule->getId()) {
  84. $chooser->setLabel($this->escapeHtml($rule->getName()));
  85. }
  86. }
  87. $element->setData('after_element_html', $chooser->toHtml());
  88. return $element;
  89. }
  90. /**
  91. * Grid Row JS Callback
  92. *
  93. * @return string
  94. */
  95. public function getRowClickCallback()
  96. {
  97. $chooserJsObject = $this->getId();
  98. $js = '
  99. function (grid, event) {
  100. var trElement = Event.findElement(event, "tr");
  101. var ruleName = trElement.down("td").next().innerHTML;
  102. var ruleId = trElement.down("td").innerHTML.replace(/^\s+|\s+$/g,"");
  103. ' .
  104. $chooserJsObject .
  105. '.setElementValue(ruleId);
  106. ' .
  107. $chooserJsObject .
  108. '.setElementLabel(ruleName);
  109. ' .
  110. $chooserJsObject .
  111. '.close();
  112. }
  113. ';
  114. return $js;
  115. }
  116. /**
  117. * Prepare columns for rules grid
  118. *
  119. * @return $this
  120. */
  121. protected function _prepareColumns()
  122. {
  123. $this->addColumn(
  124. 'rule_id',
  125. ['header' => __('ID'), 'align' => 'right', 'width' => '50px', 'index' => 'rule_id']
  126. );
  127. $this->addColumn('name', ['header' => __('Rule'), 'align' => 'left', 'index' => 'name']);
  128. $this->addColumn(
  129. 'coupon_code',
  130. ['header' => __('Coupon Code'), 'align' => 'left', 'width' => '150px', 'index' => 'code']
  131. );
  132. $this->addColumn(
  133. 'from_date',
  134. [
  135. 'header' => __('Start'),
  136. 'align' => 'left',
  137. 'width' => '120px',
  138. 'type' => 'date',
  139. 'index' => 'from_date'
  140. ]
  141. );
  142. $this->addColumn(
  143. 'to_date',
  144. [
  145. 'header' => __('End'),
  146. 'align' => 'left',
  147. 'width' => '120px',
  148. 'type' => 'date',
  149. 'default' => '--',
  150. 'index' => 'to_date'
  151. ]
  152. );
  153. $this->addColumn(
  154. 'is_active',
  155. [
  156. 'header' => __('Status'),
  157. 'align' => 'left',
  158. 'width' => '80px',
  159. 'index' => 'is_active',
  160. 'type' => 'options',
  161. 'options' => [1 => 'Active', 0 => 'Inactive']
  162. ]
  163. );
  164. return parent::_prepareColumns();
  165. }
  166. /**
  167. * Prepare grid URL
  168. *
  169. * @return string
  170. */
  171. public function getGridUrl()
  172. {
  173. return $this->getUrl('sales_rule/promo_quote/chooser', ['_current' => true]);
  174. }
  175. }