Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions administrator/components/com_templates/src/Model/TemplateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,27 +409,53 @@ public function getDirectoryTree($dir)
{
$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 : ['.'];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use Joomla\CMS\Router\Route;

ksort($this->files, SORT_NATURAL);
?>

<ul class="directory-tree treeselect">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
return;
}

ksort($this->mediaFiles, SORT_STRING);
?>

<ul class="directory-tree treeselect">
Expand Down