Files
PVM/public_html/classes/Controller.class.php
2021-10-20 16:32:11 +02:00

328 lines
12 KiB
PHP

<?php
class Controller {
public static $baseModule = "base";
public static $abs_root_project = null;
//public static $gestname = null;
public static function current_url() {
return (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] === "on" ? "https" : "http") . "://{$_SERVER["HTTP_HOST"]}{$_SERVER["REQUEST_URI"]}";
}
public static function current_module() { // è data dal nome della gestione .php
/*if(!is_null(self::$gestname)){
return self::$gestname;
}*/
// valutare ModuloUtils\CurrentModule\getId
return pathinfo($_SERVER["PHP_SELF"], PATHINFO_FILENAME);
}
public static function current_gestpath() {
return self::module_gestpath(self::current_module());
}
public static function module_gestpath($moduleName) {
return "gest-lib" . DIRECTORY_SEPARATOR . "{$moduleName}" . DIRECTORY_SEPARATOR;
}
public static function current_gestpath_resource($resource) {
return self::module_gestpath_resource(self::current_module(), $resource);
}
public static function module_gestpath_resource($moduleName, $resource) {
return self::module_gestpath($moduleName) . $resource . DIRECTORY_SEPARATOR;
}
// PATH SERVER-SIDE
public static function current_gestpath_classes($fileName = "") {
return self::module_gestpath_classes(self::current_module(), $fileName);
}
public static function module_gestpath_classes($moduleName, $fileName = "") {
return self::$abs_root_project . DIRECTORY_SEPARATOR . self::module_gestpath_resource($moduleName, "classes") . $fileName;
}
// PATH SERVER-SIDE
/*
public static function current_gestpath_widgets($fileName = ""){
return self::module_gestpath_classes(self::current_module(), $fileName);
}*/
public static function module_gestpath_widgets($moduleName, $fileName = "") {
return self::$abs_root_project . DIRECTORY_SEPARATOR . self::module_gestpath_resource($moduleName, "widgets") . $fileName;
}
// PATH SERVER-SIDE
public static function current_mainClasspath() {
return self::module_mainClasspath(self::current_module());
}
public static function module_mainClasspath($moduleName) {
$fileName = ucfirst(\Utility\Str::camelCaseEncode($moduleName)) . ".class.php";
return self::module_gestpath_classes($moduleName, $fileName);
}
// PATH SERVER-SIDE
public static function current_gestpath_sql($fileName = "") {
return self::module_gestpath_sql(self::current_module(), $fileName);
}
/* here you can understand what module name use to reach your sql file */
public static function module_gestpath_sql($moduleName, $fileName = "") {
return self::$abs_root_project . "/" . self::module_gestpath_resource($moduleName, "sql") . $fileName;
}
// PATH SERVER-SIDE
public static function current_gestpath_include($fileName = "") {
return self::module_gestpath_include(self::current_module(), $fileName);
}
public static function module_gestpath_include($moduleName, $fileName = "") {
return self::module_gestpath_resource($moduleName, "include") . $fileName;
}
// PATH SERVER-SIDE
/************************************* RELATION BETWEEN MAIN.PHP AND {GESTIONE}.PHP *************************************************/
public static function current_gestpath_mainPage() {
return self::module_gestpath_mainPage(self::current_module());
}
public static function module_gestpath_mainPage($moduleName) {
return self::module_gestpath_include($moduleName, "main.php");
}
// PATH SERVER-SIDE
public static function current_gestpath_certs($fileName = "") {
return self::module_gestpath_certs(self::current_module(), $fileName);
}
public static function module_gestpath_certs($moduleName, $fileName = "") {
return self::module_gestpath_resource($moduleName, "certs") . $fileName;
}
// PATH CLIENT-SIDE
public static function module_gestpath_js($moduleName, $fileName = "") {
return str_replace(DIRECTORY_SEPARATOR, "/", self::module_gestpath_resource($moduleName, "js")) . $fileName;
}
public static function module_gestpath_audio($moduleName, $fileName = "") {
return str_replace(DIRECTORY_SEPARATOR, "/", self::module_gestpath_resource($moduleName, "audio")) . $fileName;
}
public static function module_gestpath_css($moduleName, $fileName = "") {
return str_replace(DIRECTORY_SEPARATOR, "/", self::module_gestpath_resource($moduleName, "css")) . $fileName;
}
// PATH CLIENT-SIDE
public static function current_gestpath_images($fileName = "") {
return self::module_gestpath_images(self::current_module(), $fileName);
}
public static function module_gestpath_images($moduleName, $fileName = "") {
return self::module_gestpath_resource($moduleName, "images") . $fileName;
}
public static function current_gestpath_html($fileName = "") {
return self::module_gestpath_html(self::current_module(), $fileName);
}
public static function module_gestpath_html($moduleName, $fileName = "") {
return self::module_gestpath_resource($moduleName, "html") . $fileName;
}
public static function wwwpath() {
return (PVM::isDebugEnv() && file_exists("../public_html") ? "../" : "") . "../";
}
public static function endPointParsed($endPoint) {
$scheme = parse_url($endPoint, PHP_URL_SCHEME);
$host = parse_url($endPoint, PHP_URL_HOST);
$path = parse_url($endPoint, PHP_URL_PATH);
$port = parse_url($endPoint, PHP_URL_PORT);
if (is_null($host)) {
$host = $path;
}
$protocol = is_null($scheme) ? Rest::$protocol : $scheme;
$endPoint = $protocol . "://" . $host;
if (!is_null($port)) {
$endPoint .= ":" . $port;
}
return $endPoint;
}
private static function getCurrentUrlParameters() {
$currentUrl = self::current_url();
$params = array();
parse_str(parse_url($currentUrl, PHP_URL_QUERY), $params);
return array_keys($params);
}
private static function calcNavTabScore($navParams) {
$currentParams = self::getCurrentUrlParameters();
$navTabScore = 0;
foreach ($currentParams as $param) {
if (in_array($param, $navParams)) {
$navTabScore++;
}
}
return $navTabScore;
}
private static function getIdxActiveNavTab() {
$userNavTabs = self::getUserNavTabs();
$idx = $maxScore = 0;
foreach ($userNavTabs as $i => $userNavTab) {
if ($userNavTab["score"] > $maxScore) {
$idx = $i;
$maxScore = $userNavTab["score"];
}
}
return $idx;
}
private static function getUserNavTabs() {
$userNavTabs = array();
$currentUrl = self::current_url();
$Modulo = PVM\CurrentModule::getModulo();
if (!is_null($currentUrl) && !is_null($Modulo) && $Modulo->isLoaded()) {
foreach ($Modulo->getPosition() as $pItem) {
$flagView = PVM::processItemPolicyToCurrentUser($pItem);
if ($flagView) {
$navTab = array_pick($pItem, "title", "url");
$pQ = array();
parse_str(parse_url($navTab["url"], PHP_URL_QUERY), $pQ);
$navTab["score"] = self::calcNavTabScore(array_keys($pQ));
$userNavTabs[] = $navTab;
}
}
}
return $userNavTabs;
}
public static function getNavTabs() {
$navTabs = self::getUserNavTabs();
if (count($navTabs) > 0) {
$idxAttivo = self::getIdxActiveNavTab();
foreach ($navTabs as $i => $navTab) {
if (isset($navTab["usergroups"]) && !array_key_exists(User::get_current_group(), $navTab["usergroups"])) {
continue;
}
if ($idxAttivo === $i) {
$navTab["active"] = true;
$navTab["url"] = "#";
} else {
$navTab["active"] = false;
}
$navTabs[$i] = $navTab;
}
}
return $navTabs;
}
public static function get_js_files() {
$files = array();
$assetsLibs = self::loadCurrentAssetsLibs();
foreach ($assetsLibs as $lib) {
$files = array_merge($files, self::loadLibrary($lib, "js"));
}
if (empty($files)) {
$files = self::loadLibrary(self::$baseModule, "js");
}
return $files;
}
public static function get_css_files() {
$files = array();
$assetsLibs = self::loadCurrentAssetsLibs();
foreach ($assetsLibs as $lib) {
$files = array_merge($files, self::loadLibrary($lib, "css"));
}
if (empty($files)) {
$files = self::loadLibrary(self::$baseModule, "css");
}
return $files;
}
private static function loadCurrentAssetsLibs() {
$modules = PVM::get_userModulesTree();
$mergedModules = array();
foreach ($modules as $module) {
$mergedModules = array_merge($mergedModules, $module);
}
$currentActiveModuleName = array(self::current_module());
if (self::current_module() !== "login") {
$currentActiveModuleName = array_merge($currentActiveModuleName, \PVM\ModuloUtils::getIdModuliBase());
}
$libs = array();
foreach ($mergedModules as $commonModule) {
if (in_array($commonModule["id"], $currentActiveModuleName)) {
$libs = array_merge($libs, $commonModule["libs"]);
}
}
return array_values(array_unique($libs));
}
public static function loadLibrary($libName, $fileType) {
$libs = PVM::loadAssetsLibs();
$fileList = array();
if (isset($libs[$libName])) {
$lib = $libs[$libName];
if (isset($lib[$fileType])) {
foreach ($lib[$fileType] as $file) {
if (file_exists($file)) {
$fileList[] = $file;
}
}
}
if (isset($lib["children"])) {
foreach ($lib["children"] as $childLibName) {
$fileList = array_merge($fileList, self::loadLibrary($childLibName, $fileType));
}
}
}
return $fileList;
}
public static function get_build() {
return file_exists("build.json") ? json_decode(file_get_contents("build.json"), true) : null;
}
public static function is_ajaxRequest() {
return isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && \Utility\Str::ciEquals($_SERVER["HTTP_X_REQUESTED_WITH"], "xmlhttprequest");
}
public static function loginRedirect() {
header("Location: login.php");
}
public static function homeRedirect() {
$url = $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
if (strpos($url, "public_html") !== false) {
header("Location: ../");
} else {
header("Location: ./");
}
}
public static function get_audio_files() {
$files = array();
$assetsLibs = self::loadCurrentAssetsLibs();
foreach ($assetsLibs as $lib) {
$files = array_merge($files, self::loadLibrary($lib, "mp3"));
}
if (empty($files)) {
$files = self::loadLibrary(self::$baseModule, "mp3");
}
return $files;
}
}