7
7
8
8
namespace Magento \MediaGalleryUi \Model \Directories ;
9
9
10
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
10
11
use Magento \Framework \App \Filesystem \DirectoryList ;
12
+ use Magento \Framework \App \ObjectManager ;
11
13
use Magento \Framework \Exception \ValidatorException ;
12
14
use Magento \Framework \Filesystem ;
13
15
use Magento \Framework \Filesystem \Directory \Read ;
18
20
*/
19
21
class GetDirectoryTree
20
22
{
23
+ private const XML_PATH_MEDIA_GALLERY_IMAGE_FOLDERS
24
+ = 'system/media_storage_configuration/allowed_resources/media_gallery_image_folders ' ;
21
25
/**
22
26
* @var Filesystem
23
27
*/
@@ -28,16 +32,24 @@ class GetDirectoryTree
28
32
*/
29
33
private $ isPathExcluded ;
30
34
35
+ /**
36
+ * @var ScopeConfigInterface
37
+ */
38
+ private $ coreConfig ;
39
+
31
40
/**
32
41
* @param Filesystem $filesystem
33
42
* @param IsPathExcludedInterface $isPathExcluded
43
+ * @param ScopeConfigInterface|null $coreConfig
34
44
*/
35
45
public function __construct (
36
46
Filesystem $ filesystem ,
37
- IsPathExcludedInterface $ isPathExcluded
47
+ IsPathExcludedInterface $ isPathExcluded ,
48
+ ?ScopeConfigInterface $ coreConfig = null
38
49
) {
39
50
$ this ->filesystem = $ filesystem ;
40
51
$ this ->isPathExcluded = $ isPathExcluded ;
52
+ $ this ->coreConfig = $ coreConfig ?? ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
41
53
}
42
54
43
55
/**
@@ -74,30 +86,54 @@ private function getDirectories(): array
74
86
{
75
87
$ directories = [];
76
88
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
+ }
87
113
}
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
- ];
97
114
}
115
+
98
116
return $ directories ;
99
117
}
100
118
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
+
101
137
/**
102
138
* Find parent directory
103
139
*
@@ -121,9 +157,9 @@ private function findParent(array &$node, array &$treeNode, int $level = 0): arr
121
157
$ tNodePathLength = count ($ tnode ['path_array ' ]);
122
158
$ found = false ;
123
159
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 ) {
125
162
$ level ++;
126
- $ found = true ;
127
163
} else {
128
164
break ;
129
165
}
0 commit comments