77
88namespace Magento \MediaGalleryUi \Model \Directories ;
99
10+ use Magento \Framework \App \Config \ScopeConfigInterface ;
1011use Magento \Framework \App \Filesystem \DirectoryList ;
12+ use Magento \Framework \App \ObjectManager ;
1113use Magento \Framework \Exception \ValidatorException ;
1214use Magento \Framework \Filesystem ;
1315use Magento \Framework \Filesystem \Directory \Read ;
1820 */
1921class GetDirectoryTree
2022{
23+ private const XML_PATH_MEDIA_GALLERY_IMAGE_FOLDERS
24+ = 'system/media_storage_configuration/allowed_resources/media_gallery_image_folders ' ;
2125 /**
2226 * @var Filesystem
2327 */
@@ -28,16 +32,24 @@ class GetDirectoryTree
2832 */
2933 private $ isPathExcluded ;
3034
35+ /**
36+ * @var ScopeConfigInterface
37+ */
38+ private $ coreConfig ;
39+
3140 /**
3241 * @param Filesystem $filesystem
3342 * @param IsPathExcludedInterface $isPathExcluded
43+ * @param ScopeConfigInterface|null $coreConfig
3444 */
3545 public function __construct (
3646 Filesystem $ filesystem ,
37- IsPathExcludedInterface $ isPathExcluded
47+ IsPathExcludedInterface $ isPathExcluded ,
48+ ?ScopeConfigInterface $ coreConfig = null
3849 ) {
3950 $ this ->filesystem = $ filesystem ;
4051 $ this ->isPathExcluded = $ isPathExcluded ;
52+ $ this ->coreConfig = $ coreConfig ?? ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
4153 }
4254
4355 /**
@@ -74,30 +86,54 @@ private function getDirectories(): array
7486 {
7587 $ directories = [];
7688
77- /** @var Read $directory */
78- $ directory = $ this ->filesystem ->getDirectoryRead (DirectoryList::MEDIA );
79-
80- if (!$ directory ->isDirectory ()) {
81- return $ directories ;
82- }
83-
84- foreach ($ directory ->readRecursively () as $ path ) {
85- if (!$ directory ->isDirectory ($ path ) || $ this ->isPathExcluded ->execute ($ path )) {
86- continue ;
89+ /** @var Read $mediaDirectory */
90+ $ mediaDirectory = $ this ->filesystem ->getDirectoryRead (DirectoryList::MEDIA );
91+
92+ if ($ mediaDirectory ->isDirectory ()) {
93+ $ imageFolderPaths = $ this ->coreConfig ->getValue (
94+ self ::XML_PATH_MEDIA_GALLERY_IMAGE_FOLDERS ,
95+ ScopeConfigInterface::SCOPE_TYPE_DEFAULT
96+ );
97+ sort ($ imageFolderPaths );
98+
99+ foreach ($ imageFolderPaths as $ imageFolderPath ) {
100+ $ imageDirectory = $ this ->filesystem ->getDirectoryReadByPath (
101+ $ mediaDirectory ->getAbsolutePath ($ imageFolderPath )
102+ );
103+ if ($ imageDirectory ->isDirectory ()) {
104+ $ directories [] = $ this ->getDirectoryData ($ imageFolderPath );
105+ foreach ($ imageDirectory ->readRecursively () as $ path ) {
106+ if ($ imageDirectory ->isDirectory ($ path )) {
107+ $ directories [] = $ this ->getDirectoryData (
108+ $ mediaDirectory ->getRelativePath ($ imageDirectory ->getAbsolutePath ($ path ))
109+ );
110+ }
111+ }
112+ }
87113 }
88-
89- $ pathArray = explode ('/ ' , $ path );
90- $ directories [] = [
91- 'text ' => count ($ pathArray ) > 0 ? end ($ pathArray ) : $ path ,
92- 'id ' => $ path ,
93- 'li_attr ' => ['data-id ' => $ path ],
94- 'path ' => $ path ,
95- 'path_array ' => $ pathArray
96- ];
97114 }
115+
98116 return $ directories ;
99117 }
100118
119+ /**
120+ * Return jstree data for given path
121+ *
122+ * @param string $path
123+ * @return array
124+ */
125+ private function getDirectoryData (string $ path ): array
126+ {
127+ $ pathArray = explode ('/ ' , $ path );
128+ return [
129+ 'text ' => count ($ pathArray ) > 0 ? end ($ pathArray ) : $ path ,
130+ 'id ' => $ path ,
131+ 'li_attr ' => ['data-id ' => $ path ],
132+ 'path ' => $ path ,
133+ 'path_array ' => $ pathArray
134+ ];
135+ }
136+
101137 /**
102138 * Find parent directory
103139 *
@@ -121,9 +157,9 @@ private function findParent(array &$node, array &$treeNode, int $level = 0): arr
121157 $ tNodePathLength = count ($ tnode ['path_array ' ]);
122158 $ found = false ;
123159 while ($ level < $ tNodePathLength ) {
124- if ($ node ['path_array ' ][$ level ] === $ tnode ['path_array ' ][$ level ]) {
160+ $ found = $ node ['path_array ' ][$ level ] === $ tnode ['path_array ' ][$ level ];
161+ if ($ found ) {
125162 $ level ++;
126- $ found = true ;
127163 } else {
128164 break ;
129165 }
0 commit comments