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 committed Nov 15, 2024
1 parent c0c0fcb commit 41501a1
Showing 1 changed file with 33 additions and 67 deletions.
100 changes: 33 additions & 67 deletions lizmap/modules/view/controllers/lizMap.classic.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

use Lizmap\Project\Project;
use Lizmap\Project\UnknownLizmapProjectException;
use Lizmap\Project\UserFilesFinder;
use Lizmap\Request\RemoteStorageRequest;
use Lizmap\Server\Server;

/**
* Displays a full featured map based on one Qgis project.
Expand All @@ -22,7 +26,7 @@ class lizMapCtrl extends jController
/**
* Used to pass project Object (no need to rebuild it).
*
* @var \Lizmap\Project\Project
* @var Project
*/
protected $projectObj;

Expand Down Expand Up @@ -66,7 +70,7 @@ public function index()
$rep->action = 'view~default:index';

// Check server status
$server = new \Lizmap\Server\Server();
$server = new Server();

// QGIS server version
$requiredQgisVersion = jApp::config()->minimumRequiredVersion['qgisServer'];
Expand Down Expand Up @@ -103,7 +107,7 @@ public function index()
return $rep;
}
$project = $lser->defaultProject;
} catch (\Lizmap\Project\UnknownLizmapProjectException $e) {
} catch (UnknownLizmapProjectException $e) {
jMessage::add('The parameter project is mandatory!', 'error');

return $rep;
Expand All @@ -118,7 +122,7 @@ public function index()

return $rep;
}
} catch (\Lizmap\Project\UnknownLizmapProjectException $e) {
} catch (UnknownLizmapProjectException $e) {
jMessage::add('The lizmap project '.strtoupper($project).' does not exist !', 'error');

return $rep;
Expand Down Expand Up @@ -153,7 +157,7 @@ public function index()
// the html response
/** @var AbstractLizmapHtmlResponse $rep */
$rep = $this->getResponse('htmlmap');
$rep->addJSLink((jUrl::get('view~translate:index')).'?lang='.jApp::config()->locale, array('defer' => ''));
$rep->addJSLink(jUrl::get('view~translate:index').'?lang='.jApp::config()->locale, array('defer' => ''));

$this->repositoryKey = $lrep->getKey();
$this->projectKey = $lproj->getKey();
Expand Down Expand Up @@ -411,67 +415,29 @@ 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 UserFilesFinder();
$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 Expand Up @@ -560,11 +526,11 @@ function f($x)

// Add Google Analytics ID
$assign['googleAnalyticsID'] = '';
if ($lser->googleAnalyticsID != '' && preg_match('/^UA-\\d+-\\d+$/', $lser->googleAnalyticsID) == 1) {
if ($lser->googleAnalyticsID != '' && preg_match('/^UA-\d+-\d+$/', $lser->googleAnalyticsID) == 1) {
$assign['googleAnalyticsID'] = $lser->googleAnalyticsID;
}

if (\jAcl2::check('lizmap.admin.access') || \jAcl2::check('lizmap.admin.server.information.view')) {
if (jAcl2::check('lizmap.admin.access') || jAcl2::check('lizmap.admin.server.information.view')) {
if ($lproj->qgisLizmapPluginUpdateNeeded()) {
$rep->setBodyAttributes(array('data-lizmap-plugin-update-warning-url' => jUrl::get('admin~qgis_projects:index')));
} elseif ($lproj->projectCountCfgWarnings() >= 1) {
Expand Down

0 comments on commit 41501a1

Please sign in to comment.