| @@ -1,0 +1,84 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Types; | |
| 4 | + | |
| 5 | +use AcyMailing\Core\AcymObject; | |
| 6 | + | |
| 7 | +class FileTreeType extends AcymObject | |
| 8 | +{ | |
| 9 | + public function display(array $folders, string $currentFolder, string $nameInput): void | |
| 10 | + { | |
| 11 | + $tree = []; | |
| 12 | + foreach ($folders as $root => $children) { | |
| 13 | + $tree = array_merge($tree, $this->searchChildren($children, $root)); | |
| 14 | + } | |
| 15 | + | |
| 16 | + echo '<div id="displaytree" class="cell medium-11"><input type="text" readonly name="currentPath" id="currentPath" value="'.esc_attr($currentFolder).'"></div>'; | |
| 17 | + echo '<div class="cell" id="treefile" style="display: none;">'; | |
| 18 | + $this->displayTree($tree, $currentFolder); | |
| 19 | + echo '</div>'; | |
| 20 | + echo '<input type="hidden" name="'.esc_attr($nameInput).'" id="'.esc_attr($nameInput).'" value="'.esc_attr($currentFolder).'">'; | |
| 21 | + } | |
| 22 | + | |
| 23 | + private function searchChildren(array $folders, string $root): array | |
| 24 | + { | |
| 25 | + $tree = []; | |
| 26 | + $tree[$root] = []; | |
| 27 | + | |
| 28 | + foreach ($folders as $folder) { | |
| 29 | + $folder = trim(str_replace($root, '', $folder), '/\\'); | |
| 30 | + if (empty($folder)) { | |
| 31 | + continue; | |
| 32 | + } | |
| 33 | + | |
| 34 | + $pathParts = explode('/', $folder); | |
| 35 | + $variable = &$tree[$root]; | |
| 36 | + foreach ($pathParts as $pathPart) { | |
| 37 | + if (empty($variable[$pathPart])) { | |
| 38 | + $variable[$pathPart] = []; | |
| 39 | + } | |
| 40 | + $variable = &$variable[$pathPart]; | |
| 41 | + } | |
| 42 | + } | |
| 43 | + | |
| 44 | + return $tree; | |
| 45 | + } | |
| 46 | + | |
| 47 | + private function displayTree(array $tree, string $pathValue, string $path = ''): void | |
| 48 | + { | |
| 49 | + if (empty($tree)) { | |
| 50 | + return; | |
| 51 | + } | |
| 52 | + | |
| 53 | + echo '<ul>'; | |
| 54 | + foreach ($tree as $key => $treeItem) { | |
| 55 | + if (empty($path)) { | |
| 56 | + $currentPath = $key; | |
| 57 | + $title = '/'; | |
| 58 | + } else { | |
| 59 | + $currentPath = rtrim($path, '/').'/'.trim($key, '/').'/'; | |
| 60 | + $title = $key; | |
| 61 | + } | |
| 62 | + | |
| 63 | + $extraClass = 'tree-closed'; | |
| 64 | + $icon = 'acymicon-folder'; | |
| 65 | + | |
| 66 | + if (strpos($pathValue, $currentPath) !== false) { | |
| 67 | + $extraClass = $pathValue == $currentPath ? 'tree-current' : ''; | |
| 68 | + $icon .= '-open'; | |
| 69 | + } | |
| 70 | + | |
| 71 | + if (empty($treeItem)) { | |
| 72 | + $extraClass .= ' tree-empty'; | |
| 73 | + } | |
| 74 | + | |
| 75 | + echo '<li class="tree-child-item '.esc_attr($extraClass).'" data-path="'.esc_attr($currentPath).'"> | |
| 76 | + <span class="tree-child-title"> | |
| 77 | + <i class="'.esc_attr($icon).'"></i> '.esc_html($title).' | |
| 78 | + </span>'; | |
| 79 | + $this->displayTree($treeItem, $pathValue, $currentPath); | |
| 80 | + echo '</li>'; | |
| 81 | + } | |
| 82 | + echo '</ul>'; | |
| 83 | + } | |
| 84 | +} | |