/** * Json tree builder * * @return string */ public function getTreeJson() { $storageRoot = $this->_cmsWysiwygImages->getStorageRoot(); $collection = $this->_coreRegistry->registry('storage')->getDirsCollection($this->_cmsWysiwygImages->getCurrentPath()); $jsonArray = array(); foreach ($collection as $item) { $jsonArray[] = array('text' => $this->_cmsWysiwygImages->getShortFilename($item->getBasename(), 20), 'id' => $this->_cmsWysiwygImages->convertPathToId($item->getFilename()), 'path' => substr($item->getFilename(), strlen($storageRoot)), 'cls' => 'folder'); } return \Zend_Json::encode($jsonArray); }
/** * Create new directory in storage * * @param string $name New directory name * @param string $path Parent directory path * @return array New directory info * @throws \Magento\Framework\Exception\LocalizedException */ public function createDirectory($name, $path) { if (!preg_match(self::DIRECTORY_NAME_REGEXP, $name)) { throw new \Magento\Framework\Exception\LocalizedException(__('Please rename the folder using only letters, numbers, underscores and dashes.')); } $relativePath = $this->_directory->getRelativePath($path); if (!$this->_directory->isDirectory($relativePath) || !$this->_directory->isWritable($relativePath)) { $path = $this->_cmsWysiwygImages->getStorageRoot(); } $newPath = $path . '/' . $name; $relativeNewPath = $this->_directory->getRelativePath($newPath); if ($this->_directory->isDirectory($relativeNewPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('We found a directory with the same name. Please try another folder name.')); } $this->_directory->create($relativeNewPath); try { if ($this->_coreFileStorageDb->checkDbUsage()) { $relativePath = $this->_coreFileStorageDb->getMediaRelativePath($newPath); $this->_directoryDatabaseFactory->create()->createRecursive($relativePath); } $result = ['name' => $name, 'short_name' => $this->_cmsWysiwygImages->getShortFilename($name), 'path' => $newPath, 'id' => $this->_cmsWysiwygImages->convertPathToId($newPath)]; return $result; } catch (\Magento\Framework\Exception\FileSystemException $e) { throw new \Magento\Framework\Exception\LocalizedException(__('We cannot create a new directory.')); } }
public function testConvertPathToId() { $pathOne = '/test_path'; $pathTwo = $this->getAbsolutePath(WysiwygConfig::IMAGE_DIRECTORY) . '/test_path'; $this->assertEquals($this->imagesHelper->convertPathToId($pathOne), $this->imagesHelper->convertPathToId($pathTwo)); }