| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Types; |
| 4 |
|
| 5 |
use AcyMailing\Core\AcymObject; |
| 6 |
|
| 7 |
class FileTreeType extends AcymObject |
| 8 |
{ |
| 9 |
public function display($folders, $currentFolder, $nameInput) |
| 10 |
{ |
| 11 |
$tree = []; |
| 12 |
foreach ($folders as $root => $children) { |
| 13 |
$tree = array_merge($tree, $this->searchChildren($children, $root)); |
| 14 |
} |
| 15 |
|
| 16 |
$treeView = '<div id="displaytree" class="cell medium-11"><input type="text" readonly name="currentPath" id="currentPath" value="'.acym_escape($currentFolder).'"></div>'; |
| 17 |
$treeView .= '<div class="cell" id="treefile" style="display: none;">'.$this->displayTree($tree, $currentFolder).'</div>'; |
| 18 |
$treeView .= '<input type="hidden" name="'.acym_escape($nameInput).'" id="'.acym_escape($nameInput).'" value="'.acym_escape($currentFolder).'">'; |
| 19 |
|
| 20 |
return $treeView; |
| 21 |
} |
| 22 |
|
| 23 |
private function searchChildren($folders, $root) |
| 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($tree, $pathValue, $path = '') |
| 48 |
{ |
| 49 |
if (empty($tree)) return ''; |
| 50 |
$results = '<ul>'; |
| 51 |
foreach ($tree as $key => $treeItem) { |
| 52 |
if (empty($path)) { |
| 53 |
$currentPath = $key; |
| 54 |
$title = '/'; |
| 55 |
} else { |
| 56 |
$currentPath = rtrim($path, '/').'/'.trim($key, '/').'/'; |
| 57 |
$title = $key; |
| 58 |
} |
| 59 |
|
| 60 |
$extraClass = 'tree-closed'; |
| 61 |
$icon = 'acymicon-folder'; |
| 62 |
|
| 63 |
if (strpos($pathValue, $currentPath) !== false) { |
| 64 |
$extraClass = $pathValue == $currentPath ? 'tree-current' : ''; |
| 65 |
$icon .= '-open'; |
| 66 |
} |
| 67 |
|
| 68 |
if (empty($treeItem)) $extraClass .= ' tree-empty'; |
| 69 |
|
| 70 |
$subTree = $this->displayTree($treeItem, $pathValue, $currentPath); |
| 71 |
$results .= '<li class="tree-child-item '.acym_escape($extraClass).'" data-path="'.acym_escape($currentPath).'"> |
| 72 |
<span class="tree-child-title"> |
| 73 |
<i class="'.acym_escape($icon).'"></i> '.$title.' |
| 74 |
</span>'.$subTree.' |
| 75 |
</li>'; |
| 76 |
} |
| 77 |
$results .= '</ul>'; |
| 78 |
|
| 79 |
return $results; |
| 80 |
} |
| 81 |
} |
| 82 |
|