PageRenderTime 30ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-widget/Block/Adminhtml/Widget/Chooser.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 230 lines | 150 code | 18 blank | 62 comment | 7 complexity | a067200ae365b0af142403be4ab20225 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * WYSIWYG widget options form
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Widget\Block\Adminhtml\Widget;
  12. class Chooser extends \Magento\Backend\Block\Template
  13. {
  14. /**
  15. * @var \Magento\Framework\Data\Form\Element\Factory
  16. */
  17. protected $_elementFactory;
  18. /**
  19. * @var \Magento\Framework\Json\EncoderInterface
  20. */
  21. protected $_jsonEncoder;
  22. /**
  23. * @param \Magento\Backend\Block\Template\Context $context
  24. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  25. * @param \Magento\Framework\Data\Form\Element\Factory $elementFactory
  26. * @param array $data
  27. */
  28. public function __construct(
  29. \Magento\Backend\Block\Template\Context $context,
  30. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  31. \Magento\Framework\Data\Form\Element\Factory $elementFactory,
  32. array $data = []
  33. ) {
  34. $this->_jsonEncoder = $jsonEncoder;
  35. $this->_elementFactory = $elementFactory;
  36. parent::__construct($context, $data);
  37. }
  38. /**
  39. * Chooser source URL getter
  40. *
  41. * @return string
  42. */
  43. public function getSourceUrl()
  44. {
  45. return $this->_getData('source_url');
  46. }
  47. /**
  48. * Chooser form element getter
  49. *
  50. * @return \Magento\Framework\Data\Form\Element\AbstractElement
  51. */
  52. public function getElement()
  53. {
  54. return $this->_getData('element');
  55. }
  56. /**
  57. * Convert Array config to Object
  58. *
  59. * @return \Magento\Framework\DataObject
  60. */
  61. public function getConfig()
  62. {
  63. if ($this->_getData('config') instanceof \Magento\Framework\DataObject) {
  64. return $this->_getData('config');
  65. }
  66. $configArray = $this->_getData('config');
  67. $config = new \Magento\Framework\DataObject();
  68. $this->setConfig($config);
  69. if (!is_array($configArray)) {
  70. return $this->_getData('config');
  71. }
  72. // define chooser label
  73. if (isset($configArray['label'])) {
  74. $config->setData('label', __($configArray['label']));
  75. }
  76. // chooser control buttons
  77. $buttons = ['open' => __('Choose...'), 'close' => __('Close')];
  78. if (isset($configArray['button']) && is_array($configArray['button'])) {
  79. foreach ($configArray['button'] as $id => $label) {
  80. $buttons[$id] = __($label);
  81. }
  82. }
  83. $config->setButtons($buttons);
  84. return $this->_getData('config');
  85. }
  86. /**
  87. * Unique identifier for block that uses Chooser
  88. *
  89. * @return string
  90. */
  91. public function getUniqId()
  92. {
  93. return $this->_getData('uniq_id');
  94. }
  95. /**
  96. * Form element fieldset id getter for working with form in chooser
  97. *
  98. * @return string
  99. */
  100. public function getFieldsetId()
  101. {
  102. return $this->_getData('fieldset_id');
  103. }
  104. /**
  105. * Flag to indicate include hidden field before chooser or not
  106. *
  107. * @return bool
  108. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  109. */
  110. public function getHiddenEnabled()
  111. {
  112. return $this->hasData('hidden_enabled') ? (bool)$this->_getData('hidden_enabled') : true;
  113. }
  114. /**
  115. * Return chooser HTML and init scripts
  116. *
  117. * @return string
  118. */
  119. protected function _toHtml()
  120. {
  121. $element = $this->getElement();
  122. /* @var $fieldset \Magento\Framework\Data\Form\Element\Fieldset */
  123. $fieldset = $element->getForm()->getElement($this->getFieldsetId());
  124. $chooserId = $this->getUniqId();
  125. $config = $this->getConfig();
  126. // add chooser element to fieldset
  127. $chooser = $fieldset->addField(
  128. 'chooser' . $element->getId(),
  129. 'note',
  130. ['label' => $config->getLabel() ? $config->getLabel() : '', 'value_class' => 'value2']
  131. );
  132. $hiddenHtml = '';
  133. if ($this->getHiddenEnabled()) {
  134. $hidden = $this->_elementFactory->create('hidden', ['data' => $element->getData()]);
  135. $hidden->setId("{$chooserId}value")->setForm($element->getForm());
  136. if ($element->getRequired()) {
  137. $hidden->addClass('required-entry');
  138. }
  139. $hiddenHtml = $hidden->getElementHtml();
  140. $element->setValue('');
  141. }
  142. $buttons = $config->getButtons();
  143. $chooseButton = $this->getLayout()->createBlock(
  144. 'Magento\Backend\Block\Widget\Button'
  145. )->setType(
  146. 'button'
  147. )->setId(
  148. $chooserId . 'control'
  149. )->setClass(
  150. 'btn-chooser'
  151. )->setLabel(
  152. $buttons['open']
  153. )->setOnclick(
  154. $chooserId . '.choose()'
  155. )->setDisabled(
  156. $element->getReadonly()
  157. );
  158. $chooser->setData('after_element_html', $hiddenHtml . $chooseButton->toHtml());
  159. // render label and chooser scripts
  160. $configJson = $this->_jsonEncoder->encode($config->getData());
  161. return '
  162. <label class="widget-option-label" id="' .
  163. $chooserId .
  164. 'label">' .
  165. ($this->getLabel() ? $this->getLabel() : __(
  166. 'Not Selected'
  167. )) .
  168. '</label>
  169. <div id="' .
  170. $chooserId .
  171. 'advice-container" class="hidden"></div>
  172. <script>
  173. require(["prototype", "mage/adminhtml/wysiwyg/widget"], function(){
  174. //<![CDATA[
  175. (function() {
  176. var instantiateChooser = function() {
  177. window.' .
  178. $chooserId .
  179. ' = new WysiwygWidget.chooser(
  180. "' .
  181. $chooserId .
  182. '",
  183. "' .
  184. $this->getSourceUrl() .
  185. '",
  186. ' .
  187. $configJson .
  188. '
  189. );
  190. if ($("' .
  191. $chooserId .
  192. 'value")) {
  193. $("' .
  194. $chooserId .
  195. 'value").advaiceContainer = "' .
  196. $chooserId .
  197. 'advice-container";
  198. }
  199. }
  200. if (document.loaded) { //allow load over ajax
  201. instantiateChooser();
  202. } else {
  203. document.observe("dom:loaded", instantiateChooser);
  204. }
  205. })();
  206. //]]>
  207. });
  208. </script>
  209. ';
  210. }
  211. }