123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Widget\Model\Widget;
- /**
- * Widgets Insertion Plugin Config for Editor HTML Element
- */
- class Config implements \Magento\Framework\Data\Wysiwyg\ConfigProviderInterface
- {
- /**
- * @var \Magento\Framework\View\Asset\Repository
- */
- protected $_assetRepo;
- /**
- * @var \Magento\Widget\Model\Widget
- */
- protected $_widget;
- /**
- * @var \Magento\Backend\Model\UrlInterface
- */
- protected $_backendUrl;
- /**
- * @var \Magento\Framework\Url\DecoderInterface
- */
- protected $urlDecoder;
- /**
- * @var \Magento\Widget\Model\WidgetFactory
- */
- protected $_widgetFactory;
- /**
- * @var \Magento\Framework\Url\EncoderInterface
- */
- protected $urlEncoder;
- /**
- * @var \Magento\Framework\Registry
- */
- private $registry;
- /**
- * @param \Magento\Backend\Model\UrlInterface $backendUrl
- * @param \Magento\Framework\Url\DecoderInterface $urlDecoder
- * @param \Magento\Framework\View\Asset\Repository $assetRepo
- * @param \Magento\Widget\Model\WidgetFactory $widgetFactory
- * @param \Magento\Framework\Url\EncoderInterface $urlEncoder
- * @param \Magento\Framework\Registry $registry
- */
- public function __construct(
- \Magento\Backend\Model\UrlInterface $backendUrl,
- \Magento\Framework\Url\DecoderInterface $urlDecoder,
- \Magento\Framework\View\Asset\Repository $assetRepo,
- \Magento\Widget\Model\WidgetFactory $widgetFactory,
- \Magento\Framework\Url\EncoderInterface $urlEncoder,
- \Magento\Framework\Registry $registry
- ) {
- $this->_backendUrl = $backendUrl;
- $this->urlDecoder = $urlDecoder;
- $this->_assetRepo = $assetRepo;
- $this->_widgetFactory = $widgetFactory;
- $this->urlEncoder = $urlEncoder;
- $this->registry = $registry;
- }
- /**
- * Return config settings for widgets insertion plugin based on editor element config
- *
- * @param \Magento\Framework\DataObject $config
- * @return \Magento\Framework\DataObject
- */
- public function getConfig(\Magento\Framework\DataObject $config) : \Magento\Framework\DataObject
- {
- $settings = $this->getPluginSettings($config);
- return $config->addData($settings);
- }
- /**
- * Return config settings for widgets insertion plugin based on editor element config
- *
- * @param \Magento\Framework\DataObject $config
- * @return array
- */
- public function getPluginSettings($config)
- {
- $widgetWysiwyg = [
- [
- 'name' => 'magentowidget',
- 'src' => $this->getWysiwygJsPluginSrc(),
- 'options' => [
- 'window_url' => $this->getWidgetWindowUrl($config),
- 'types' => $this->getAvailableWidgets($config),
- 'error_image_url' => $this->getErrorImageUrl(),
- 'placeholders' => $this->getWidgetPlaceholderImageUrls(),
- ],
- ]
- ];
- $configPlugins = (array) $config->getData('plugins');
- $widgetConfig['plugins'] = array_merge($configPlugins, $widgetWysiwyg);
- return $widgetConfig;
- }
- /**
- * Return list of available placeholders for widget
- *
- * @return array
- */
- public function getWidgetPlaceholderImageUrls()
- {
- return $this->_widgetFactory->create()->getPlaceholderImageUrls();
- }
- /**
- * Return url to error image
- * @return string
- */
- public function getErrorImageUrl()
- {
- return $this->_assetRepo->getUrl('Magento_Widget::error.png');
- }
- /**
- * Return url to wysiwyg plugin
- * @return string
- */
- public function getWysiwygJsPluginSrc()
- {
- return $this->_assetRepo->getUrl('mage/adminhtml/wysiwyg/tiny_mce/plugins/magentowidget/editor_plugin.js');
- }
- /**
- * Return Widgets Insertion Plugin Window URL
- *
- * @param \Magento\Framework\DataObject $config Editor element config
- * @return string
- */
- public function getWidgetWindowUrl($config)
- {
- $params = [];
- $skipped = is_array($config->getData('skip_widgets')) ? $config->getData('skip_widgets') : [];
- if ($config->hasData('widget_filters')) {
- $all = $this->_widgetFactory->create()->getWidgets();
- $filtered = $this->_widgetFactory->create()->getWidgets($config->getData('widget_filters'));
- foreach ($all as $code => $widget) {
- if (!isset($filtered[$code])) {
- $skipped[] = $widget['@']['type'];
- }
- }
- }
- if (count($skipped) > 0) {
- $params['skip_widgets'] = $this->encodeWidgetsToQuery($skipped);
- }
- return $this->_backendUrl->getUrl('adminhtml/widget/index', $params);
- }
- /**
- * Encode list of widget types into query param
- *
- * @param string[]|string $widgets List of widgets
- * @return string Query param value
- */
- public function encodeWidgetsToQuery($widgets)
- {
- $widgets = is_array($widgets) ? $widgets : [$widgets];
- $param = implode(',', $widgets);
- return $this->urlEncoder->encode($param);
- }
- /**
- * Decode URL query param and return list of widgets
- *
- * @param string $queryParam Query param value to decode
- * @return string[] Array of widget types
- */
- public function decodeWidgetsFromQuery($queryParam)
- {
- $param = $this->urlDecoder->decode($queryParam);
- return preg_split('/\s*\,\s*/', $param, 0, PREG_SPLIT_NO_EMPTY);
- }
- /**
- * @param \Magento\Framework\DataObject $config Editor element config
- * @return array
- */
- public function getAvailableWidgets($config)
- {
- if (!$config->hasData('widget_types')) {
- $result = [];
- $allWidgets = $this->_widgetFactory->create()->getWidgetsArray();
- $skipped = $this->_getSkippedWidgets();
- foreach ($allWidgets as $widget) {
- if (is_array($skipped) && in_array($widget['type'], $skipped)) {
- continue;
- }
- $result[] = $widget['name']->getText();
- }
- }
- return $result;
- }
- /**
- * Return array of widgets disabled for selection
- *
- * @return string[]
- */
- protected function _getSkippedWidgets()
- {
- return $this->registry->registry('skip_widgets');
- }
- }
|