PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-cms/Helper/Wysiwyg/Images.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 274 lines | 135 code | 22 blank | 117 comment | 12 complexity | 33c96750b033dd79e060d9c6ba5e765e MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Helper\Wysiwyg;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * Wysiwyg Images Helper
  10. */
  11. class Images extends \Magento\Framework\App\Helper\AbstractHelper
  12. {
  13. /**
  14. * Current directory path
  15. * @var string
  16. */
  17. protected $_currentPath;
  18. /**
  19. * Current directory URL
  20. * @var string
  21. */
  22. protected $_currentUrl;
  23. /**
  24. * Currenty selected store ID if applicable
  25. *
  26. * @var int
  27. */
  28. protected $_storeId = null;
  29. /**
  30. * @var \Magento\Framework\Filesystem\Directory\Write
  31. */
  32. protected $_directory;
  33. /**
  34. * Adminhtml data
  35. *
  36. * @var \Magento\Backend\Helper\Data
  37. */
  38. protected $_backendData;
  39. /**
  40. * Store manager
  41. *
  42. * @var \Magento\Store\Model\StoreManagerInterface
  43. */
  44. protected $_storeManager;
  45. /**
  46. * Construct
  47. *
  48. * @param \Magento\Framework\App\Helper\Context $context
  49. * @param \Magento\Backend\Helper\Data $backendData
  50. * @param \Magento\Framework\Filesystem $filesystem
  51. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  52. */
  53. public function __construct(
  54. \Magento\Framework\App\Helper\Context $context,
  55. \Magento\Backend\Helper\Data $backendData,
  56. \Magento\Framework\Filesystem $filesystem,
  57. \Magento\Store\Model\StoreManagerInterface $storeManager
  58. ) {
  59. parent::__construct($context);
  60. $this->_backendData = $backendData;
  61. $this->_storeManager = $storeManager;
  62. $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  63. $this->_directory->create(\Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY);
  64. }
  65. /**
  66. * Set a specified store ID value
  67. *
  68. * @param int $store
  69. * @return $this
  70. */
  71. public function setStoreId($store)
  72. {
  73. $this->_storeId = $store;
  74. return $this;
  75. }
  76. /**
  77. * Images Storage root directory
  78. *
  79. * @return string
  80. */
  81. public function getStorageRoot()
  82. {
  83. return $this->_directory->getAbsolutePath(\Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY);
  84. }
  85. /**
  86. * Images Storage base URL
  87. *
  88. * @return string
  89. */
  90. public function getBaseUrl()
  91. {
  92. return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
  93. }
  94. /**
  95. * Ext Tree node key name
  96. *
  97. * @return string
  98. */
  99. public function getTreeNodeName()
  100. {
  101. return 'node';
  102. }
  103. /**
  104. * Encode path to HTML element id
  105. *
  106. * @param string $path Path to file/directory
  107. * @return string
  108. */
  109. public function convertPathToId($path)
  110. {
  111. $path = str_replace($this->getStorageRoot(), '', $path);
  112. return $this->idEncode($path);
  113. }
  114. /**
  115. * Decode HTML element id
  116. *
  117. * @param string $id
  118. * @return string
  119. */
  120. public function convertIdToPath($id)
  121. {
  122. if ($id === \Magento\Theme\Helper\Storage::NODE_ROOT) {
  123. return $this->getStorageRoot();
  124. } else {
  125. return $this->getStorageRoot() . $this->idDecode($id);
  126. }
  127. }
  128. /**
  129. * Check whether using static URLs is allowed
  130. *
  131. * @return bool
  132. */
  133. public function isUsingStaticUrlsAllowed()
  134. {
  135. $checkResult = new \StdClass();
  136. $checkResult->isAllowed = false;
  137. $this->_eventManager->dispatch(
  138. 'cms_wysiwyg_images_static_urls_allowed',
  139. ['result' => $checkResult, 'store_id' => $this->_storeId]
  140. );
  141. return $checkResult->isAllowed;
  142. }
  143. /**
  144. * Prepare Image insertion declaration for Wysiwyg or textarea(as_is mode)
  145. *
  146. * @param string $filename Filename transferred via Ajax
  147. * @param bool $renderAsTag Leave image HTML as is or transform it to controller directive
  148. * @return string
  149. */
  150. public function getImageHtmlDeclaration($filename, $renderAsTag = false)
  151. {
  152. $fileurl = $this->getCurrentUrl() . $filename;
  153. $mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
  154. $mediaPath = str_replace($mediaUrl, '', $fileurl);
  155. $directive = sprintf('{{media url="%s"}}', $mediaPath);
  156. if ($renderAsTag) {
  157. $html = sprintf('<img src="%s" alt="" />', $this->isUsingStaticUrlsAllowed() ? $fileurl : $directive);
  158. } else {
  159. if ($this->isUsingStaticUrlsAllowed()) {
  160. $html = $fileurl; // $mediaPath;
  161. } else {
  162. $directive = $this->urlEncoder->encode($directive);
  163. $html = $this->_backendData->getUrl('cms/wysiwyg/directive', ['___directive' => $directive]);
  164. }
  165. }
  166. return $html;
  167. }
  168. /**
  169. * Return path of the current selected directory or root directory for startup
  170. * Try to create target directory if it doesn't exist
  171. *
  172. * @return string
  173. * @throws \Magento\Framework\Exception\LocalizedException
  174. */
  175. public function getCurrentPath()
  176. {
  177. if (!$this->_currentPath) {
  178. $currentPath = $this->_directory->getAbsolutePath() . \Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY;
  179. $path = $this->_getRequest()->getParam($this->getTreeNodeName());
  180. if ($path) {
  181. $path = $this->convertIdToPath($path);
  182. if ($this->_directory->isDirectory($this->_directory->getRelativePath($path))) {
  183. $currentPath = $path;
  184. }
  185. }
  186. try {
  187. $currentDir = $this->_directory->getRelativePath($currentPath);
  188. if (!$this->_directory->isExist($currentDir)) {
  189. $this->_directory->create($currentDir);
  190. }
  191. } catch (\Magento\Framework\Exception\FileSystemException $e) {
  192. $message = __('The directory %1 is not writable by server.', $currentPath);
  193. throw new \Magento\Framework\Exception\LocalizedException($message);
  194. }
  195. $this->_currentPath = $currentPath;
  196. }
  197. return $this->_currentPath;
  198. }
  199. /**
  200. * Return URL based on current selected directory or root directory for startup
  201. *
  202. * @return string
  203. */
  204. public function getCurrentUrl()
  205. {
  206. if (!$this->_currentUrl) {
  207. $path = $this->getCurrentPath();
  208. $mediaUrl = $this->_storeManager->getStore(
  209. $this->_storeId
  210. )->getBaseUrl(
  211. \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
  212. );
  213. $this->_currentUrl = $mediaUrl . $this->_directory->getRelativePath($path) . '/';
  214. }
  215. return $this->_currentUrl;
  216. }
  217. /**
  218. * Encode string to valid HTML id element, based on base64 encoding
  219. *
  220. * @param string $string
  221. * @return string
  222. */
  223. public function idEncode($string)
  224. {
  225. return strtr(base64_encode($string), '+/=', ':_-');
  226. }
  227. /**
  228. * Revert opration to idEncode
  229. *
  230. * @param string $string
  231. * @return string
  232. */
  233. public function idDecode($string)
  234. {
  235. $string = strtr($string, ':_-', '+/=');
  236. return base64_decode($string);
  237. }
  238. /**
  239. * Reduce filename by replacing some characters with dots
  240. *
  241. * @param string $filename
  242. * @param int $maxLength Maximum filename
  243. * @return string Truncated filename
  244. */
  245. public function getShortFilename($filename, $maxLength = 20)
  246. {
  247. if (strlen($filename) <= $maxLength) {
  248. return $filename;
  249. }
  250. return substr($filename, 0, $maxLength) . '...';
  251. }
  252. }