Config.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Variable\Model\Variable;
  7. use Magento\Framework\App\ObjectManager;
  8. /**
  9. * Variable Wysiwyg Plugin Config
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Config
  15. {
  16. /**
  17. * @var \Magento\Framework\View\Asset\Repository
  18. */
  19. protected $_assetRepo;
  20. /**
  21. * @var \Magento\Backend\Model\UrlInterface
  22. */
  23. protected $_url;
  24. /**
  25. * @var \Magento\Variable\Model\ResourceModel\Variable\CollectionFactory
  26. */
  27. private $collectionFactory;
  28. /**
  29. * @var \Magento\Variable\Model\Source\Variables
  30. */
  31. private $storesVariables;
  32. /**
  33. * @var \Magento\Framework\Serialize\Serializer\Json
  34. */
  35. private $encoder;
  36. /**
  37. * Config constructor.
  38. * @param \Magento\Framework\View\Asset\Repository $assetRepo
  39. * @param \Magento\Backend\Model\UrlInterface $url
  40. * @param \Magento\Variable\Model\ResourceModel\Variable\CollectionFactory|null $collectionFactory
  41. * @param \Magento\Variable\Model\Source\Variables|null $storesVariables
  42. * @param \Magento\Framework\Serialize\Serializer\Json|null $encoder
  43. */
  44. public function __construct(
  45. \Magento\Framework\View\Asset\Repository $assetRepo,
  46. \Magento\Backend\Model\UrlInterface $url,
  47. \Magento\Variable\Model\ResourceModel\Variable\CollectionFactory $collectionFactory = null,
  48. \Magento\Variable\Model\Source\Variables $storesVariables = null,
  49. \Magento\Framework\Serialize\Serializer\Json $encoder = null
  50. ) {
  51. $this->collectionFactory = $collectionFactory ?: ObjectManager::getInstance()
  52. ->get(\Magento\Variable\Model\ResourceModel\Variable\CollectionFactory::class);
  53. $this->storesVariables = $storesVariables ?: ObjectManager::getInstance()
  54. ->get(\Magento\Variable\Model\Source\Variables::class);
  55. $this->encoder = $encoder ?: ObjectManager::getInstance()
  56. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  57. $this->_url = $url;
  58. $this->_assetRepo = $assetRepo;
  59. }
  60. /**
  61. * Prepare variable wysiwyg config
  62. *
  63. * @param \Magento\Framework\DataObject $config
  64. * @return array
  65. */
  66. public function getWysiwygPluginSettings($config)
  67. {
  68. $variableConfig = [];
  69. $onclickParts = [
  70. 'search' => ['html_id'],
  71. 'subject' => 'MagentovariablePlugin.loadChooser(\'' .
  72. $this->getVariablesWysiwygActionUrl() .
  73. '\', \'{{html_id}}\');',
  74. ];
  75. $variableWysiwyg = [
  76. [
  77. 'name' => 'magentovariable',
  78. 'src' => $this->getWysiwygJsPluginSrc(),
  79. 'options' => [
  80. 'title' => __('Insert Variable...'),
  81. 'url' => $this->getVariablesWysiwygActionUrl(),
  82. 'onclick' => $onclickParts,
  83. 'class' => 'add-variable plugin',
  84. 'placeholders' => $this->getVariablesWysiwygData()
  85. ],
  86. ],
  87. ];
  88. $configPlugins = (array) $config->getData('plugins');
  89. $variableConfig['plugins'] = array_merge($configPlugins, $variableWysiwyg);
  90. return $variableConfig;
  91. }
  92. /**
  93. * Return url to wysiwyg plugin
  94. *
  95. * @return string
  96. * @codeCoverageIgnore
  97. */
  98. public function getWysiwygJsPluginSrc()
  99. {
  100. $editorPluginJs = 'mage/adminhtml/wysiwyg/tiny_mce/plugins/magentovariable/editor_plugin.js';
  101. return $this->_assetRepo->getUrl($editorPluginJs);
  102. }
  103. /**
  104. * Return url of action to get variables
  105. *
  106. * @return string
  107. * @codeCoverageIgnore
  108. */
  109. public function getVariablesWysiwygActionUrl()
  110. {
  111. return $this->_url->getUrl('mui/index/render', ['namespace' => 'variables_modal']);
  112. }
  113. /**
  114. * Prepare default variables
  115. *
  116. * @return array
  117. */
  118. private function getDefaultVariables()
  119. {
  120. $variables = [];
  121. foreach ($this->storesVariables->getData() as $variable) {
  122. $variables[$variable['value']] = [
  123. 'code' => $variable['value'],
  124. 'variable_name' => $variable['label'],
  125. 'variable_type' => \Magento\Variable\Model\Source\Variables::DEFAULT_VARIABLE_TYPE
  126. ];
  127. }
  128. return $variables;
  129. }
  130. /**
  131. * Prepare custom variables
  132. *
  133. * @return array
  134. */
  135. private function getCustomVariables()
  136. {
  137. $customVariables = $this->collectionFactory->create();
  138. $variables = [];
  139. foreach ($customVariables->getData() as $variable) {
  140. $variables[$variable['code']] = [
  141. 'code' => $variable['code'],
  142. 'variable_name' => $variable['name'],
  143. 'variable_type' => 'custom'
  144. ];
  145. }
  146. return $variables;
  147. }
  148. /**
  149. * Return variable related wysiwyg data
  150. *
  151. * @return bool|string
  152. */
  153. private function getVariablesWysiwygData()
  154. {
  155. $variablesData = array_merge(
  156. $this->getCustomVariables(),
  157. $this->getDefaultVariables()
  158. );
  159. return $this->encoder->serialize($variablesData);
  160. }
  161. }