Customdatafields.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace Dotdigitalgroup\Email\Block\Adminhtml\Config\Rules;
  3. class Customdatafields extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
  4. {
  5. /**
  6. * @var \Magento\Framework\View\Element\BlockInterface
  7. */
  8. protected $getAttributeRenderer;
  9. /**
  10. * @var \Magento\Framework\View\Element\BlockInterface
  11. */
  12. protected $getConditionsRenderer;
  13. /**
  14. * @var \Magento\Framework\View\Element\BlockInterface
  15. */
  16. private $getValueRenderer;
  17. /**
  18. * @var \Dotdigitalgroup\Email\Model\Adminhtml\Source\Rules\Condition
  19. */
  20. private $condition;
  21. /**
  22. * @var \Dotdigitalgroup\Email\Model\Adminhtml\Source\Rules\Value
  23. */
  24. private $value;
  25. /**
  26. * @var string
  27. */
  28. private $className;
  29. /**
  30. * Customdatafields constructor.
  31. *
  32. * @param \Magento\Backend\Block\Template\Context $context
  33. * @param \Dotdigitalgroup\Email\Model\Adminhtml\Source\Rules\Condition $condition
  34. * @param \Dotdigitalgroup\Email\Model\Adminhtml\Source\Rules\Value $value
  35. * @param array $data
  36. */
  37. public function __construct(
  38. \Magento\Backend\Block\Template\Context $context,
  39. \Dotdigitalgroup\Email\Model\Adminhtml\Source\Rules\Condition $condition,
  40. \Dotdigitalgroup\Email\Model\Adminhtml\Source\Rules\Value $value,
  41. $data = []
  42. ) {
  43. $this->condition = $condition;
  44. $this->value = $value;
  45. $this->_addAfter = false;
  46. $this->className = 'ddg-rules-conditions';
  47. $this->_addButtonLabel = __('Add New Condition');
  48. parent::__construct($context, $data);
  49. }
  50. /**
  51. * @return void
  52. */
  53. protected function _prepareToRender()
  54. {
  55. $this->getConditionsRenderer = null;
  56. $this->getAttributeRenderer = null;
  57. $this->getValueRenderer = null;
  58. $this->addColumn(
  59. 'attribute',
  60. [
  61. 'label' => __('Attribute'),
  62. 'class' => $this->className,
  63. ]
  64. );
  65. $this->addColumn(
  66. 'conditions',
  67. [
  68. 'label' => __('Condition'),
  69. 'class' => $this->className,
  70. ]
  71. );
  72. $this->addColumn(
  73. 'cvalue',
  74. [
  75. 'label' => __('Value'),
  76. 'class' => $this->className,
  77. ]
  78. );
  79. }
  80. /**
  81. * render cell template.
  82. *
  83. * @param string $columnName
  84. *
  85. * @return string
  86. */
  87. public function renderCellTemplate($columnName)
  88. {
  89. if ($columnName == 'attribute') {
  90. return $this->_getAttributeRenderer()
  91. ->setName($this->_getCellInputElementName($columnName))
  92. ->setTitle($columnName)
  93. ->setClass($this->className)
  94. ->setOptions(
  95. $this->getElement()->getValues()
  96. )
  97. ->toHtml();
  98. } elseif ($columnName == 'conditions') {
  99. return $this->_getConditionsRenderer()
  100. ->setName($this->_getCellInputElementName($columnName))
  101. ->setTitle($columnName)
  102. ->setClass($this->className)
  103. ->setOptions(
  104. $this->condition->toOptionArray()
  105. )
  106. ->toHtml();
  107. } elseif ($columnName == 'cvalue') {
  108. return $this->_getValueRenderer()
  109. ->setName($this->_getCellInputElementName($columnName))
  110. ->setTitle($columnName)
  111. ->setClass($this->className)
  112. ->setOptions(
  113. $this->value->toOptionArray()
  114. )
  115. ->toHtml();
  116. }
  117. return parent::renderCellTemplate($columnName);
  118. }
  119. /**
  120. * @param \Magento\Framework\DataObject $row
  121. *
  122. * @return null
  123. */
  124. protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
  125. {
  126. $options = [];
  127. $options['option_' . $this->_getAttributeRenderer()->calcOptionHash(
  128. $row->getData('attribute')
  129. )]
  130. = 'selected="selected"';
  131. $options['option_' . $this->_getConditionsRenderer()->calcOptionHash(
  132. $row->getData('conditions')
  133. )]
  134. = 'selected="selected"';
  135. $options['option_' . $this->_getValueRenderer()->calcOptionHash(
  136. $row->getData('cvalue')
  137. )]
  138. = 'selected="selected"';
  139. $row->setData('option_extra_attrs', $options);
  140. }
  141. /**
  142. * Get rendered for attribute field.
  143. *
  144. * @return \Magento\Framework\View\Element\BlockInterface
  145. */
  146. private function _getAttributeRenderer()
  147. {
  148. if (!$this->getAttributeRenderer) {
  149. $this->getAttributeRenderer = $this->getLayout()
  150. ->createBlock(
  151. \Dotdigitalgroup\Email\Block\Adminhtml\Config\Select::class,
  152. '',
  153. ['data' => ['is_render_to_js_template' => true]]
  154. );
  155. }
  156. return $this->getAttributeRenderer;
  157. }
  158. /**
  159. * Get renderer for conditions field.
  160. *
  161. * @return \Magento\Framework\View\Element\BlockInterface
  162. */
  163. private function _getConditionsRenderer()
  164. {
  165. if (!$this->getConditionsRenderer) {
  166. $this->getConditionsRenderer = $this->getLayout()
  167. ->createBlock(
  168. \Dotdigitalgroup\Email\Block\Adminhtml\Config\Select::class,
  169. '',
  170. ['data' => ['is_render_to_js_template' => true]]
  171. );
  172. }
  173. return $this->getConditionsRenderer;
  174. }
  175. /**
  176. * Get renderer for value field.
  177. *
  178. * @return \Magento\Framework\View\Element\BlockInterface
  179. */
  180. private function _getValueRenderer()
  181. {
  182. if (!$this->getValueRenderer) {
  183. $this->getValueRenderer = $this->getLayout()
  184. ->createBlock(
  185. \Dotdigitalgroup\Email\Block\Adminhtml\Config\Select::class,
  186. '',
  187. ['data' => ['is_render_to_js_template' => true]]
  188. );
  189. }
  190. return $this->getValueRenderer;
  191. }
  192. /**
  193. * @return string
  194. *
  195. * @throws \Exception
  196. */
  197. protected function _toHtml()
  198. {
  199. return '<input type="hidden" id="' . $this->getElement()->getHtmlId()
  200. . '"/>' . parent::_toHtml();
  201. }
  202. }