Tree.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Block\Adminhtml\Wysiwyg\Images;
  7. /**
  8. * Directory tree renderer for Cms Wysiwyg Images
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Tree extends \Magento\Backend\Block\Template
  14. {
  15. /**
  16. * Core registry
  17. *
  18. * @var \Magento\Framework\Registry
  19. */
  20. protected $_coreRegistry = null;
  21. /**
  22. * Cms wysiwyg images
  23. *
  24. * @var \Magento\Cms\Helper\Wysiwyg\Images
  25. */
  26. protected $_cmsWysiwygImages = null;
  27. /**
  28. * @var \Magento\Framework\Serialize\Serializer\Json
  29. */
  30. private $serializer;
  31. /**
  32. * @param \Magento\Backend\Block\Template\Context $context
  33. * @param \Magento\Cms\Helper\Wysiwyg\Images $cmsWysiwygImages
  34. * @param \Magento\Framework\Registry $registry
  35. * @param array $data
  36. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  37. * @throws \RuntimeException
  38. */
  39. public function __construct(
  40. \Magento\Backend\Block\Template\Context $context,
  41. \Magento\Cms\Helper\Wysiwyg\Images $cmsWysiwygImages,
  42. \Magento\Framework\Registry $registry,
  43. array $data = [],
  44. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  45. ) {
  46. $this->_coreRegistry = $registry;
  47. $this->_cmsWysiwygImages = $cmsWysiwygImages;
  48. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  49. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  50. parent::__construct($context, $data);
  51. }
  52. /**
  53. * Json tree builder
  54. *
  55. * @return string
  56. */
  57. public function getTreeJson()
  58. {
  59. $storageRoot = $this->_cmsWysiwygImages->getStorageRoot();
  60. $collection = $this->_coreRegistry->registry(
  61. 'storage'
  62. )->getDirsCollection(
  63. $this->_cmsWysiwygImages->getCurrentPath()
  64. );
  65. $jsonArray = [];
  66. foreach ($collection as $item) {
  67. $data = [
  68. 'text' => $this->_cmsWysiwygImages->getShortFilename($item->getBasename(), 20),
  69. 'id' => $this->_cmsWysiwygImages->convertPathToId($item->getFilename()),
  70. 'path' => substr($item->getFilename(), strlen($storageRoot)),
  71. 'cls' => 'folder',
  72. ];
  73. $hasNestedDirectories = count(glob($item->getFilename() . '/*', GLOB_ONLYDIR)) > 0;
  74. // if no nested directories inside dir, add 'leaf' state so that jstree hides dropdown arrow next to dir
  75. if (!$hasNestedDirectories) {
  76. $data['state'] = 'leaf';
  77. }
  78. $jsonArray[] = $data;
  79. }
  80. return $this->serializer->serialize($jsonArray);
  81. }
  82. /**
  83. * Json source URL
  84. *
  85. * @return string
  86. */
  87. public function getTreeLoaderUrl()
  88. {
  89. $params = [];
  90. $currentTreePath = $this->getRequest()->getParam('current_tree_path');
  91. if (strlen($currentTreePath)) {
  92. $params['current_tree_path'] = $currentTreePath;
  93. }
  94. return $this->getUrl(
  95. 'cms/*/treeJson',
  96. $params
  97. );
  98. }
  99. /**
  100. * Root node name of tree
  101. *
  102. * @return \Magento\Framework\Phrase
  103. */
  104. public function getRootNodeName()
  105. {
  106. return __('Storage Root');
  107. }
  108. /**
  109. * Return tree node full path based on current path
  110. *
  111. * @return string
  112. */
  113. public function getTreeCurrentPath()
  114. {
  115. $treePath = ['root'];
  116. if ($idEncodedPath = $this->getRequest()->getParam('current_tree_path')) {
  117. $path = $this->_cmsWysiwygImages->idDecode($idEncodedPath);
  118. } else {
  119. $path = $this->_coreRegistry->registry('storage')->getSession()->getCurrentPath();
  120. }
  121. if (strlen($path)) {
  122. $path = str_replace($this->_cmsWysiwygImages->getStorageRoot(), '', $path);
  123. $relative = [];
  124. foreach (explode('/', $path) as $dirName) {
  125. if ($dirName) {
  126. $relative[] = $dirName;
  127. $treePath[] = $this->_cmsWysiwygImages->idEncode(implode('/', $relative));
  128. }
  129. }
  130. }
  131. return $treePath;
  132. }
  133. /**
  134. * Get tree widget options
  135. *
  136. * @return array
  137. */
  138. public function getTreeWidgetOptions()
  139. {
  140. return [
  141. "folderTree" => [
  142. "rootName" => $this->getRootNodeName(),
  143. "url" => $this->getTreeLoaderUrl(),
  144. "currentPath" => array_reverse($this->getTreeCurrentPath()),
  145. ]
  146. ];
  147. }
  148. }