[4.4] Fix ordering for files in the template view (#43335)

* Fix ordering for files in the template view

* Rewrite getDirectoryTree function using early return and uksort

* CS

* Use natural sorting instead of string comparison

---------
This commit is contained in:
Harald Leithner 2024-05-08 09:36:36 +02:00 committed by GitHub
parent 5f8305f0c9
commit 5768496747
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 17 deletions

View File

@ -409,27 +409,53 @@ class TemplateModel extends FormModel
{
$result = [];
$prefix = JPATH_ROOT . DIRECTORY_SEPARATOR . 'administrator' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->template->element;
$mediaPrefix = JPATH_ROOT . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'administrator' . DIRECTORY_SEPARATOR . $this->template->element;
if ($this->template->client_id === 0) {
$prefix = JPATH_ROOT . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->template->element;
$mediaPrefix = JPATH_ROOT . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'site' . DIRECTORY_SEPARATOR . $this->template->element;
}
$dirFiles = scandir($dir);
foreach ($dirFiles as $key => $value) {
if (!in_array($value, ['.', '..', 'node_modules'])) {
if (is_dir($dir . $value)) {
$relativePath = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . ($this->template->client_id === 0 ? 'site' : 'administrator') . DIRECTORY_SEPARATOR . $this->template->element, '', $dir . $value);
$relativePath = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR . ($this->template->client_id === 0 ? '' : 'administrator' . DIRECTORY_SEPARATOR) . 'templates' . DIRECTORY_SEPARATOR . $this->template->element, '', $relativePath);
$result[str_replace('\\', '//', $relativePath)] = $this->getDirectoryTree($dir . $value . '/');
} else {
$ext = pathinfo($dir . $value, PATHINFO_EXTENSION);
$allowedFormat = $this->checkFormat($ext);
foreach ($dirFiles as $value) {
if (in_array($value, ['.', '..', 'node_modules'])) {
continue;
}
if ($allowedFormat == true) {
$relativePath = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . ($this->template->client_id === 0 ? 'site' : 'administrator') . DIRECTORY_SEPARATOR . $this->template->element, '', $dir . $value);
$relativePath = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR . ($this->template->client_id === 0 ? '' : 'administrator' . DIRECTORY_SEPARATOR) . 'templates' . DIRECTORY_SEPARATOR . $this->template->element, '', $relativePath);
$result[] = $this->getFile($relativePath, $value);
}
}
$relativePath = str_replace([$prefix, $mediaPrefix], '', $dir . $value);
if (is_dir($dir . $value)) {
$result[str_replace('\\', '//', $relativePath)] = $this->getDirectoryTree($dir . $value . '/');
continue;
}
$ext = pathinfo($dir . $value, PATHINFO_EXTENSION);
if ($this->checkFormat($ext)) {
$result[] = $this->getFile($relativePath, $value);
}
}
// Sort directories first, then files alphabetically.
uksort($result, function ($a, $b) use ($result) {
if (\is_string($a)) {
if (\is_string($b)) {
return strnatcmp($a, $b);
}
return -1;
}
if (\is_string($b)) {
return 1;
}
return strnatcmp($result[$a]->name, $result[$b]->name);
});
return !empty($result) ? $result : ['.'];
}

View File

@ -12,7 +12,6 @@ defined('_JEXEC') or die;
use Joomla\CMS\Router\Route;
ksort($this->files, SORT_NATURAL);
?>
<ul class="directory-tree treeselect">

View File

@ -17,7 +17,6 @@ if (!count($this->mediaFiles)) {
return;
}
ksort($this->mediaFiles, SORT_STRING);
?>
<ul class="directory-tree treeselect">