Skip to content

Commit

Permalink
update controller to use UserFileFinder class
Browse files Browse the repository at this point in the history
  • Loading branch information
nworr authored and github-actions[bot] committed Dec 2, 2024
1 parent f40d8c2 commit 57070a4
Showing 1 changed file with 22 additions and 60 deletions.
82 changes: 22 additions & 60 deletions lizmap/modules/view/controllers/lizMap.classic.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Lizmap\Project\ProjectFilesFinder;
use Lizmap\Request\RemoteStorageRequest;

/**
Expand Down Expand Up @@ -411,66 +412,27 @@ function f($x)
}
}

// Add JS files found in media/js
$jsDirArray = array('default', $project);
foreach ($jsDirArray as $dir) {
$jsUrls = array();
$mjsUrls = array();
$cssUrls = array();
$items = array(
'media/js/',
'../media/js/',
);
foreach ($items as $item) {
$jsPathRoot = realpath($repositoryPath.$item.$dir);
if (is_dir($jsPathRoot)) {
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($jsPathRoot)) as $filename) {
$fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
if ($fileExtension == 'js' || $fileExtension == 'mjs' || $fileExtension == 'css') {
$jsPath = realpath($filename);
$jsRelPath = $item.$dir.str_replace($jsPathRoot, '', $jsPath);
$url = 'view~media:getMedia';
if ($fileExtension == 'css') {
$url = 'view~media:getCssFile';
}
$jsUrl = jUrl::get(
$url,
array(
'repository' => $lrep->getKey(),
'project' => $project,
'mtime' => filemtime($filename),
'path' => $jsRelPath,
)
);
if ($fileExtension == 'js') {
$jsUrls[] = $jsUrl;
++$countUserJs;
} elseif ($fileExtension == 'mjs') {
$mjsUrls[] = $jsUrl;
++$countUserJs;
} else {
$cssUrls[] = $jsUrl;
}
}
}
}
}

// Add CSS, MJS and JS files ordered by name
sort($cssUrls);
foreach ($cssUrls as $cssUrl) {
$rep->addCSSLink($cssUrl);
}
sort($jsUrls);
foreach ($jsUrls as $jsUrl) {
// Use addHeadContent and not addJSLink to be sure it will be loaded after minified code
$rep->addContent('<script type="text/javascript" defer src="'.$jsUrl.'" ></script>');
}
sort($mjsUrls);
foreach ($mjsUrls as $mjsUrl) {
// Use addHeadContent and not addJSLink to be sure it will be loaded after minified code
$rep->addContent('<script type="module" defer src="'.$mjsUrl.'" ></script>');
}
$fileFinder = new ProjectFilesFinder();
$allURLS = $fileFinder->listFileURLS($lproj);

$cssUrls = $allURLS['css'];
$jsUrls = $allURLS['js'];
$mjsUrls = $allURLS['mjs'];
$countUserJs = count($jsUrls) + count($mjsUrls);
// Add CSS, MJS and JS files ordered by name
sort($cssUrls);
foreach ($cssUrls as $cssUrl) {
$rep->addCSSLink($cssUrl);
}
sort($jsUrls);
foreach ($jsUrls as $jsUrl) {
// Use addHeadContent and not addJSLink to be sure it will be loaded after minified code
$rep->addContent('<script type="text/javascript" defer src="'.$jsUrl.'" ></script>');
}
sort($mjsUrls);
foreach ($mjsUrls as $mjsUrl) {
// Use addHeadContent and not addJSLink to be sure it will be loaded after minified code
$rep->addContent('<script type="module" defer src="'.$mjsUrl.'" ></script>');
}
}
$rep->setBodyAttributes(array('data-lizmap-user-defined-js-count' => $countUserJs));
Expand Down

0 comments on commit 57070a4

Please sign in to comment.