PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Widget/Model/Widget/Config.php

https://github.com/speedupmate/Magento-CE-Mirror
PHP | 151 lines | 68 code | 13 blank | 70 comment | 5 complexity | 3ac9eeeaa96aa07e46f83d3e813fb214 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Widget
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Widgets Insertion Plugin Config for Editor HTML Element
  28. *
  29. * @category Mage
  30. * @package Mage_Widget
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Widget_Model_Widget_Config extends Varien_Object
  34. {
  35. /**
  36. * Return config settings for widgets insertion plugin based on editor element config
  37. *
  38. * @param Varien_Object $config
  39. * @return array
  40. */
  41. public function getPluginSettings($config)
  42. {
  43. $settings = array(
  44. 'widget_plugin_src' => Mage::getBaseUrl('js').'mage/adminhtml/wysiwyg/tiny_mce/plugins/magentowidget/editor_plugin.js',
  45. 'widget_images_url' => $this->getPlaceholderImagesBaseUrl(),
  46. 'widget_placeholders' => $this->getAvailablePlaceholderFilenames(),
  47. 'widget_window_url' => $this->getWidgetWindowUrl($config)
  48. );
  49. return $settings;
  50. }
  51. /**
  52. * Return Widget placeholders images URL
  53. *
  54. * @return string
  55. */
  56. public function getPlaceholderImagesBaseUrl()
  57. {
  58. return Mage::getDesign()->getSkinUrl('images/widget/');
  59. }
  60. /**
  61. * Return Widget placeholders images dir
  62. *
  63. * @return string
  64. */
  65. public function getPlaceholderImagesBaseDir()
  66. {
  67. return Mage::getDesign()->getSkinBaseDir() . DS . 'images' . DS . 'widget';
  68. }
  69. /**
  70. * Return list of existing widget image placeholders
  71. *
  72. * @return array
  73. */
  74. public function getAvailablePlaceholderFilenames()
  75. {
  76. $result = array();
  77. $targetDir = $this->getPlaceholderImagesBaseDir();
  78. if (is_dir($targetDir) && is_readable($targetDir)) {
  79. $collection = new Varien_Data_Collection_Filesystem();
  80. $collection->addTargetDir($targetDir)
  81. ->setCollectDirs(false)
  82. ->setCollectFiles(true)
  83. ->setCollectRecursively(false);
  84. foreach ($collection as $file) {
  85. $result[] = $file->getBasename();
  86. }
  87. }
  88. return $result;
  89. }
  90. /**
  91. * Return Widgets Insertion Plugin Window URL
  92. *
  93. * @param Varien_Object Editor element config
  94. * @return string
  95. */
  96. public function getWidgetWindowUrl($config)
  97. {
  98. $params = array();
  99. $skipped = is_array($config->getData('skip_widgets')) ? $config->getData('skip_widgets') : array();
  100. if ($config->hasData('widget_filters')) {
  101. $all = Mage::getModel('widget/widget')->getWidgetsXml();
  102. $filtered = Mage::getModel('widget/widget')->getWidgetsXml($config->getData('widget_filters'));
  103. $reflection = new ReflectionObject($filtered);
  104. foreach ($all as $code => $widget) {
  105. if (!$reflection->hasProperty($code)) {
  106. $skipped[] = $widget->getAttribute('type');
  107. }
  108. }
  109. }
  110. if (count($skipped) > 0) {
  111. $params['skip_widgets'] = $this->encodeWidgetsToQuery($skipped);
  112. }
  113. return Mage::getSingleton('adminhtml/url')->getUrl('*/widget/index', $params);
  114. }
  115. /**
  116. * Encode list of widget types into query param
  117. *
  118. * @param array $widgets List of widgets
  119. * @return string Query param value
  120. */
  121. public function encodeWidgetsToQuery($widgets)
  122. {
  123. $widgets = is_array($widgets) ? $widgets : array($widgets);
  124. $param = implode(',', $widgets);
  125. return Mage::helper('core')->urlEncode($param);
  126. }
  127. /**
  128. * Decode URL query param and return list of widgets
  129. *
  130. * @param string $queryParam Query param value to decode
  131. * @return array Array of widget types
  132. */
  133. public function decodeWidgetsFromQuery($queryParam)
  134. {
  135. $param = Mage::helper('core')->urlDecode($queryParam);
  136. return preg_split('/\s*\,\s*/', $param, 0, PREG_SPLIT_NO_EMPTY);
  137. }
  138. }