Customdatafields.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Dotdigitalgroup\Email\Block\Adminhtml\Config\Automation;
  3. class Customdatafields extends
  4. \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
  5. {
  6. /**
  7. * @var \Dotdigitalgroup\Email\Block\Adminhtml\Config\Select
  8. */
  9. public $statusRenderer;
  10. /**
  11. * @var \Dotdigitalgroup\Email\Block\Adminhtml\Config\Select
  12. */
  13. public $automationRenderer;
  14. /**
  15. * @var \Dotdigitalgroup\Email\Model\Config\Source\Automation\ProgramFactory
  16. */
  17. public $programFactory;
  18. /**
  19. * @var \Magento\Framework\Data\Form\Element\Factory
  20. */
  21. public $elementFactory;
  22. /**
  23. * Customdatafields constructor.
  24. *
  25. * @param \Magento\Backend\Block\Template\Context $context
  26. * @param \Magento\Framework\Data\Form\Element\Factory $elementFactory
  27. * @param \Dotdigitalgroup\Email\Model\Config\Source\Automation\ProgramFactory $programFactory
  28. * @param array $data
  29. */
  30. public function __construct(
  31. \Magento\Backend\Block\Template\Context $context,
  32. \Magento\Framework\Data\Form\Element\Factory $elementFactory,
  33. \Dotdigitalgroup\Email\Model\Config\Source\Automation\ProgramFactory $programFactory,
  34. array $data = []
  35. ) {
  36. $this->elementFactory = $elementFactory;
  37. $this->programFactory = $programFactory->create();
  38. parent::__construct($context, $data);
  39. }
  40. /**
  41. * @return null
  42. */
  43. public function _prepareToRender()
  44. {
  45. $this->addColumn(
  46. 'status',
  47. [
  48. 'label' => __('Order Status')
  49. ]
  50. );
  51. $this->addColumn(
  52. 'automation',
  53. [
  54. 'label' => __('Automation Program')
  55. ]
  56. );
  57. $this->_addAfter = false;
  58. $this->_addButtonLabel = __('Add New Enrolment');
  59. }
  60. /**
  61. * @param string $columnName
  62. *
  63. * @return string
  64. *
  65. * @throws \Exception
  66. */
  67. public function renderCellTemplate($columnName)
  68. {
  69. if ($columnName == 'status' && isset($this->_columns[$columnName])) {
  70. $options = $this->getElement()->getValues();
  71. $element = $this->elementFactory->create('select');
  72. $element->setForm(
  73. $this->getForm()
  74. )->setName(
  75. $this->_getCellInputElementName($columnName)
  76. )->setHtmlId(
  77. $this->_getCellInputElementId('<%- _id %>', $columnName)
  78. )->setValues(
  79. $options
  80. );
  81. return str_replace("\n", '', $element->getElementHtml());
  82. }
  83. if ($columnName == 'automation'
  84. && isset($this->_columns[$columnName])
  85. ) {
  86. $options = $this->programFactory->toOptionArray();
  87. $element = $this->elementFactory->create('select');
  88. $element->setForm(
  89. $this->getForm()
  90. )->setName(
  91. $this->_getCellInputElementName($columnName)
  92. )->setHtmlId(
  93. $this->_getCellInputElementId('<%- _id %>', $columnName)
  94. )->setValues(
  95. $options
  96. );
  97. return str_replace("\n", '', $element->getElementHtml());
  98. }
  99. return parent::renderCellTemplate($columnName);
  100. }
  101. /**
  102. * @param \Magento\Framework\DataObject $row
  103. *
  104. * @return void
  105. */
  106. public function _prepareArrayRow(\Magento\Framework\DataObject $row)
  107. {
  108. $optionExtraAttr = [];
  109. $optionExtraAttr['option_' . $this->getStatusRenderer()
  110. ->calcOptionHash($row->getData('status'))]
  111. = 'selected="selected"';
  112. $optionExtraAttr['option_' . $this->getAutomationRenderer()
  113. ->calcOptionHash($row->getData('automation'))]
  114. = 'selected="selected"';
  115. $row->setData(
  116. 'option_extra_attrs',
  117. $optionExtraAttr
  118. );
  119. }
  120. /**
  121. * @return \Magento\Framework\View\Element\BlockInterface
  122. *
  123. * @throws \Magento\Framework\Exception\LocalizedException
  124. */
  125. public function getStatusRenderer()
  126. {
  127. $this->statusRenderer = $this->getLayout()->createBlock(
  128. \Dotdigitalgroup\Email\Block\Adminhtml\Config\Select::class,
  129. '',
  130. ['data' => ['is_render_to_js_template' => true]]
  131. );
  132. return $this->statusRenderer;
  133. }
  134. /**
  135. * @return \Magento\Framework\View\Element\BlockInterface
  136. *
  137. * @throws \Magento\Framework\Exception\LocalizedException
  138. */
  139. public function getAutomationRenderer()
  140. {
  141. $this->automationRenderer = $this->getLayout()->createBlock(
  142. \Dotdigitalgroup\Email\Block\Adminhtml\Config\Select::class,
  143. '',
  144. ['data' => ['is_render_to_js_template' => true]]
  145. );
  146. return $this->automationRenderer;
  147. }
  148. /**
  149. * @return string
  150. *
  151. * @throws \Exception
  152. */
  153. public function _toHtml()
  154. {
  155. return '<input type="hidden" id="' . $this->getElement()->getHtmlId()
  156. . '"/>' . parent::_toHtml();
  157. }
  158. }