Tree.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Block\Adminhtml\Wysiwyg\Files;
  7. /**
  8. * Files tree block
  9. *
  10. * @api
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. * @since 100.0.2
  13. */
  14. class Tree extends \Magento\Backend\Block\Template
  15. {
  16. /**
  17. * @var \Magento\Theme\Helper\Storage
  18. */
  19. protected $_storageHelper;
  20. /**
  21. * @var \Magento\Framework\Url\EncoderInterface
  22. */
  23. protected $urlEncoder;
  24. /**
  25. * @var \Magento\Framework\Serialize\Serializer\Json
  26. */
  27. private $serializer;
  28. /**
  29. * @param \Magento\Backend\Block\Template\Context $context
  30. * @param \Magento\Theme\Helper\Storage $storageHelper
  31. * @param \Magento\Framework\Url\EncoderInterface $urlEncoder
  32. * @param array $data
  33. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  34. * @throws \RuntimeException
  35. */
  36. public function __construct(
  37. \Magento\Backend\Block\Template\Context $context,
  38. \Magento\Theme\Helper\Storage $storageHelper,
  39. \Magento\Framework\Url\EncoderInterface $urlEncoder,
  40. array $data = [],
  41. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  42. ) {
  43. $this->_storageHelper = $storageHelper;
  44. $this->urlEncoder = $urlEncoder;
  45. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  46. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  47. parent::__construct($context, $data);
  48. }
  49. /**
  50. * Json source URL
  51. *
  52. * @return string
  53. */
  54. public function getTreeLoaderUrl()
  55. {
  56. return $this->getUrl('adminhtml/*/treeJson', $this->_storageHelper->getRequestParams());
  57. }
  58. /**
  59. * Get tree json
  60. *
  61. * @param array $data
  62. * @return string
  63. */
  64. public function getTreeJson($data)
  65. {
  66. return $this->serializer->serialize($data);
  67. }
  68. /**
  69. * Get root node name of tree
  70. *
  71. * @return \Magento\Framework\Phrase
  72. */
  73. public function getRootNodeName()
  74. {
  75. return __('Storage Root');
  76. }
  77. /**
  78. * Return tree node full path based on current path
  79. *
  80. * @return string
  81. */
  82. public function getTreeCurrentPath()
  83. {
  84. $treePath = '/root';
  85. $path = $this->_storageHelper->getSession()->getCurrentPath();
  86. if ($path) {
  87. $path = str_replace($this->_storageHelper->getStorageRoot(), '', $path);
  88. $relative = '';
  89. foreach (explode('/', $path) as $dirName) {
  90. if ($dirName) {
  91. $relative .= '/' . $dirName;
  92. $treePath .= '/' . $this->urlEncoder->encode($relative);
  93. }
  94. }
  95. }
  96. return $treePath;
  97. }
  98. }