Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tree.php
Go to the documentation of this file.
1 <?php
7 
15 {
21  protected $_coreRegistry = null;
22 
28  protected $_cmsWysiwygImages = null;
29 
33  private $serializer;
34 
43  public function __construct(
44  \Magento\Backend\Block\Template\Context $context,
45  \Magento\Cms\Helper\Wysiwyg\Images $cmsWysiwygImages,
46  \Magento\Framework\Registry $registry,
47  array $data = [],
48  \Magento\Framework\Serialize\Serializer\Json $serializer = null
49  ) {
50  $this->_coreRegistry = $registry;
51  $this->_cmsWysiwygImages = $cmsWysiwygImages;
52  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
53  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
54  parent::__construct($context, $data);
55  }
56 
62  public function getTreeJson()
63  {
64  $storageRoot = $this->_cmsWysiwygImages->getStorageRoot();
65  $collection = $this->_coreRegistry->registry(
66  'storage'
67  )->getDirsCollection(
68  $this->_cmsWysiwygImages->getCurrentPath()
69  );
70  $jsonArray = [];
71  foreach ($collection as $item) {
72  $data = [
73  'text' => $this->_cmsWysiwygImages->getShortFilename($item->getBasename(), 20),
74  'id' => $this->_cmsWysiwygImages->convertPathToId($item->getFilename()),
75  'path' => substr($item->getFilename(), strlen($storageRoot)),
76  'cls' => 'folder',
77  ];
78 
79  $hasNestedDirectories = count(glob($item->getFilename() . '/*', GLOB_ONLYDIR)) > 0;
80 
81  // if no nested directories inside dir, add 'leaf' state so that jstree hides dropdown arrow next to dir
82  if (!$hasNestedDirectories) {
83  $data['state'] = 'leaf';
84  }
85 
86  $jsonArray[] = $data;
87  }
88  return $this->serializer->serialize($jsonArray);
89  }
90 
96  public function getTreeLoaderUrl()
97  {
98  $params = [];
99 
100  $currentTreePath = $this->getRequest()->getParam('current_tree_path');
101 
102  if (strlen($currentTreePath)) {
103  $params['current_tree_path'] = $currentTreePath;
104  }
105 
106  return $this->getUrl(
107  'cms/*/treeJson',
108  $params
109  );
110  }
111 
117  public function getRootNodeName()
118  {
119  return __('Storage Root');
120  }
121 
127  public function getTreeCurrentPath()
128  {
129  $treePath = ['root'];
130 
131  if ($idEncodedPath = $this->getRequest()->getParam('current_tree_path')) {
132  $path = $this->_cmsWysiwygImages->idDecode($idEncodedPath);
133  } else {
134  $path = $this->_coreRegistry->registry('storage')->getSession()->getCurrentPath();
135  }
136 
137  if (strlen($path)) {
138  $path = str_replace($this->_cmsWysiwygImages->getStorageRoot(), '', $path);
139  $relative = [];
140  foreach (explode('/', $path) as $dirName) {
141  if ($dirName) {
142  $relative[] = $dirName;
143  $treePath[] = $this->_cmsWysiwygImages->idEncode(implode('/', $relative));
144  }
145  }
146  }
147 
148  return $treePath;
149  }
150 
156  public function getTreeWidgetOptions()
157  {
158  return [
159  "folderTree" => [
160  "rootName" => $this->getRootNodeName(),
161  "url" => $this->getTreeLoaderUrl(),
162  "currentPath" => array_reverse($this->getTreeCurrentPath()),
163  ]
164  ];
165  }
166 }
__()
Definition: __.php:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Cms\Helper\Wysiwyg\Images $cmsWysiwygImages, \Magento\Framework\Registry $registry, array $data=[], \Magento\Framework\Serialize\Serializer\Json $serializer=null)
Definition: Tree.php:43