Value.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Dotdigitalgroup\Email\Controller\Adminhtml\Rules;
  3. class Value extends \Magento\Backend\App\AbstractAction
  4. {
  5. /**
  6. * Authorization level of a basic admin session
  7. *
  8. * @see _isAllowed()
  9. */
  10. const ADMIN_RESOURCE = 'Dotdigitalgroup_Email::exclusion_rules';
  11. /**
  12. * @var \Magento\Framework\App\Response\Http
  13. */
  14. private $http;
  15. /**
  16. * @var \Dotdigitalgroup\Email\Model\Adminhtml\Source\Rules\Value
  17. */
  18. private $ruleValue;
  19. /**
  20. * @var \Magento\Framework\Json\Helper\Data
  21. */
  22. private $jsonEncoder;
  23. /**
  24. * @var \Magento\Framework\Escaper
  25. */
  26. private $escaper;
  27. /**
  28. * Value constructor.
  29. *
  30. * @param \Dotdigitalgroup\Email\Model\Adminhtml\Source\Rules\Value $ruleValue
  31. * @param \Magento\Framework\Json\Helper\Data $jsonEncoder
  32. * @param \Magento\Backend\App\Action\Context $context
  33. * @param \Magento\Framework\App\Response\Http $http
  34. * @param \Magento\Framework\Escaper $escaper
  35. */
  36. public function __construct(
  37. \Dotdigitalgroup\Email\Model\Adminhtml\Source\Rules\Value $ruleValue,
  38. \Magento\Framework\Json\Helper\Data $jsonEncoder,
  39. \Magento\Backend\App\Action\Context $context,
  40. \Magento\Framework\App\Response\Http $http,
  41. \Magento\Framework\Escaper $escaper
  42. ) {
  43. $this->jsonEncoder = $jsonEncoder;
  44. $this->ruleValue = $ruleValue;
  45. $this->http = $http;
  46. $this->escaper = $escaper;
  47. parent::__construct($context);
  48. }
  49. /**
  50. * Execute method.
  51. *
  52. * @return null
  53. */
  54. public function execute()
  55. {
  56. $response = [];
  57. $valueName = $this->getRequest()->getParam('value');
  58. $conditionValue = $this->getRequest()->getParam('condValue');
  59. $attributeValue = $this->getRequest()->getParam('attributeValue');
  60. if ($valueName && $attributeValue && $conditionValue) {
  61. if ($conditionValue == 'null') {
  62. $valueOptions = $this->ruleValue->getValueSelectOptions($attributeValue, true);
  63. $response['cvalue'] = $this->getOptionHtml('cvalue', $valueName, $valueOptions);
  64. } else {
  65. $elmType = $this->ruleValue->getValueElementType($attributeValue);
  66. if ($elmType == 'select') {
  67. $valueOptions = $this->ruleValue->getValueSelectOptions($attributeValue);
  68. $response['cvalue'] = $this->getOptionHtml('cvalue', $valueName, $valueOptions);
  69. } elseif ($elmType == 'text') {
  70. $html = "<input style='width:160px' title='cvalue' class='' id='' name=$valueName />";
  71. $response['cvalue'] = $html;
  72. }
  73. }
  74. $this->http->getHeaders()->clearHeaders();
  75. $this->http->setHeader('Content-Type', 'application/json')->setBody(
  76. $this->jsonEncoder->jsonEncode($response)
  77. );
  78. }
  79. }
  80. /**
  81. * @param string $title
  82. * @param string $name
  83. * @param array $options
  84. *
  85. * @return string
  86. */
  87. private function getOptionHtml($title, $name, $options)
  88. {
  89. $block = $this->_view->getLayout()->createBlock(\Magento\Framework\View\Element\Html\Select::class);
  90. $block->setOptions($options)
  91. ->setId('')
  92. ->setClass('')
  93. ->setTitle($title)
  94. ->setName($name)
  95. ->setExtraParams('style="width:160px"');
  96. return $block->toHtml();
  97. }
  98. }