Files
FHC-Core/application/helpers/hlp_header_helper.php
T

468 lines
11 KiB
PHP

<?php
/**
* FH-Complete
*
* @package FHC-Helper
* @author FHC-Team
* @copyright Copyright (c) 2016 fhcomplete.org
* @license GPLv3
* @since Version 1.0.0
*/
/**
* FHC Helper
*
* @subpackage Helpers
* @category Helpers
*/
if (! defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------
// Functions needed in the view FHC-Header
// ------------------------------------------------------------------------
/**
* Print the given title of the page
* NOTE: this is a required field, must be specified otherwise an error is shown
*/
function printPageTitle($title)
{
if ($title != null)
{
echo $title;
}
else
{
show_error('The title for this page is not set');
}
}
/**
* Print the meta tag http-equiv refresh having as content the value of the given parameter
*/
function printRefreshMeta($refresh)
{
if ($refresh != null)
{
if (is_numeric($refresh) && $refresh > 0)
{
echo '<meta http-equiv="refresh" content="'.$refresh.'">';
}
else
{
show_error('The provided refresh parameter has to be a number greater then 0');
}
}
}
/**
* Generates tags for the style sheets you want to include, the parameter could by a string or an array of strings
*/
function generateCSSsInclude($CSSs)
{
$cssLink = '<link rel="stylesheet" type="text/css" href="%s" />';
$ci =& get_instance();
$cachetoken = '?'.$ci->config->item('fhcomplete_build_version');
if (isset($CSSs))
{
$tmpCSSs = is_array($CSSs) ? $CSSs : array($CSSs);
for ($tmpCSSsCounter = 0; $tmpCSSsCounter < count($tmpCSSs); $tmpCSSsCounter++)
{
$toPrint = sprintf($cssLink, base_url($tmpCSSs[$tmpCSSsCounter]).$cachetoken).PHP_EOL;
if ($tmpCSSsCounter > 0) $toPrint = "\t\t".$toPrint;
echo $toPrint;
}
}
}
/**
* Generates global JS-Object to pass parms to other javascripts
*/
function generateJSDataStorageObject($indexPage, $calledPath, $calledMethod)
{
$ci =& get_instance();
$ci->load->config('theme');
$ci->load->model('system/Sprache_model','SpracheModel');
$server_language = getData($ci->SpracheModel->loadWhere(['content' => true]));
$server_language = array_map(function($language){
return ['sprache'=>$language->sprache, 'LC_Time'=>$language->locale, 'bezeichnung'=>$language->bezeichnung[$language->index-1]];
}, $server_language);
$user_language = getUserLanguage();
$ci->load->config('javascript');
$systemerror_mailto = $ci->config->item('systemerror_mailto');
$FHC_JS_DATA_STORAGE_OBJECT = array(
'app_root' => APP_ROOT,
'ci_router' => $indexPage,
'called_path' => $calledPath,
'called_method' => $calledMethod,
'server_languages' => $server_language,
'user_language' => $user_language,
'timezone' => date_default_timezone_get(),
'systemerror_mailto' => $systemerror_mailto,
'theme' => [
'name'=>$ci->config->item('theme_name'),
'modes'=>$ci->config->item('theme_modes'),
]
);
$toPrint = "\n";
$toPrint .= '<script type="text/javascript">';
$toPrint .= '
var FHC_JS_DATA_STORAGE_OBJECT = '.json_encode($FHC_JS_DATA_STORAGE_OBJECT).';';
$toPrint .= "\n";
$toPrint .= '</script>';
$toPrint .= "\n\n";
echo $toPrint;
}
/**
* Generates global JS-Object to pass phrases to other javascripts
*/
function generateJSPhrasesStorageObject($phrases)
{
$ci =& get_instance();
$ci->load->library('PhrasesLib', array($phrases), 'pj');
$toPrint = "\n";
$toPrint .= '<script type="text/javascript">';
$toPrint .= "\n";
$toPrint .= ' var FHC_JS_PHRASES_STORAGE_OBJECT = '.$ci->pj->getJSON().';';
$toPrint .= "\n";
$toPrint .= '</script>';
$toPrint .= "\n\n";
echo $toPrint;
}
/**
* Generates tags for the javascripts you want to include, the parameter could by a string or an array of strings
*/
function generateJSsInclude($JSs)
{
$jsInclude = '<script type="text/javascript" src="%s"></script>';
$ci =& get_instance();
$cachetoken = '?'.$ci->config->item('fhcomplete_build_version');
if (isset($JSs))
{
$tmpJSs = is_array($JSs) ? $JSs : array($JSs);
for ($tmpJSsCounter = 0; $tmpJSsCounter < count($tmpJSs); $tmpJSsCounter++)
{
$toPrint = sprintf($jsInclude, base_url($tmpJSs[$tmpJSsCounter].$cachetoken)).PHP_EOL;
if ($tmpJSsCounter > 0) $toPrint = "\t\t".$toPrint;
echo $toPrint;
}
}
}
/**
* Generates tags for the javascript modules you want to include, the parameter could by a string or an array of strings
*/
function generateJSModulesInclude($JSModules)
{
$jsInclude = '<script type="module" src="%s"></script>';
$ci =& get_instance();
$cachetoken = '?'.$ci->config->item('fhcomplete_build_version');
if (isset($JSModules))
{
$tmpJSs = is_array($JSModules) ? $JSModules : array($JSModules);
for ($tmpJSsCounter = 0; $tmpJSsCounter < count($tmpJSs); $tmpJSsCounter++)
{
if($ci->config->item('use_fhcomplete_build_version_in_path'))
{
$relurl = preg_replace('#public/#', 'public/' . $ci->config->item('fhcomplete_build_version') . '/', $tmpJSs[$tmpJSsCounter]);
$toPrint = sprintf($jsInclude, base_url($relurl)).PHP_EOL;
}
else
{
$toPrint = sprintf($jsInclude, base_url($tmpJSs[$tmpJSsCounter].$cachetoken)).PHP_EOL;
}
if ($tmpJSsCounter > 0) $toPrint = "\t\t".$toPrint;
echo $toPrint;
}
}
}
/**
* Generates all the includes needed by the Addons
*/
function generateAddonsJSsInclude($calledFrom)
{
$aktive_addons = array_filter(explode(";", ACTIVE_ADDONS));
// For each active addon
foreach ($aktive_addons as $addon)
{
// Build the path to the hook file
$hookfile = DOC_ROOT.'addons/'.$addon.'/hooks.config.inc.php';
// If the hook file exists
if (file_exists($hookfile))
{
$js_hooks = array(); // default value
include($hookfile); // include the hook file where the array js_hooks should be setup
// If it contains the provided key calledFrom
if (key_exists($calledFrom, $js_hooks))
{
foreach ($js_hooks[$calledFrom] as $js_file)
{
generateJSsInclude('addons/'.$addon.'/'.$js_file);
}
}
}
}
}
/**
* This function merely print some useful HTML to help some vacuous browsers to handle modern JS features
*/
function generateBackwardCompatibleJSMsIe($js)
{
echo "<!--[if lt IE 9]>\n";
echo ' <script type="text/javascript" src="'.$js.'"></script>'."\n";
echo "<![endif]-->\n";
}
/**
* Constructs an accessibility skipLink https://www.w3schools.com/accessibility/accessibility_skip_links.php
*/
function generateSkipLink($skipID)
{
$toPrint = '<a id="skiplink" href="';
$toPrint.=$skipID;
$toPrint.='" class="fhcSkipLink" aria-label="Skip to main content"></a>';
echo $toPrint;
}
function absoluteJsImportUrl($relurl)
{
$ci =& get_instance();
$ci->load->config('javascript');
if($ci->config->item('use_fhcomplete_build_version_in_path'))
{
$url = base_url(preg_replace('#^public/#', 'public/' . $ci->config->item('fhcomplete_build_version') . '/', $relurl));
}
else
{
$url = base_url($relurl) . '?'. $ci->config->item('fhcomplete_build_version');
}
return $url;
}
/*
* Manipulate CI views includes Array to load
* - public/js/FhcApps.js via customJSs and
* - app customisation js and/or css from extensions via customJSModules
* if customJSModules contains at least one vuejs app and customisation files
* exist in extensions
*/
class ExtendableAppsHelper
{
private static $instance = null;
protected $extensions;
protected $customCSSs;
protected $customJSs;
protected $customJSModules;
protected $initialised;
protected $appscount;
protected $extCustomCSSs;
protected $extCustomJSs;
protected $extCustomJSModules;
private function __construct()
{
$this->extensions = array();
$this->customCSSs = null;
$this->customJSs = null;
$this->customJSModules = null;
$this->initialised = false;
$this->appscount = 0;
$this->extCustomCSSs = null;
$this->extCustomJSs = null;
$this->extCustomJSModules = null;
}
public static function getInstance()
{
if(self::$instance === null)
{
self::$instance = new ExtendableAppsHelper();
}
return self::$instance;
}
public function init($customCSSs, $customJSs, $customJSModules)
{
if($this->initialised)
{
return;
}
$this->customCSSs = $customCSSs;
$this->customJSs = $customJSs;
$this->customJSModules = $customJSModules;
$this->initialised = true;
if(!isset($this->customJSModules))
{
return;
}
if(!is_array($this->customJSModules))
{
$this->customJSModules = array($this->customJSModules);
}
if(count($this->customJSModules) < 1)
{
return;
}
$this->buildExtensionsList();
$this->prepareExtendedArrays();
}
public function getCustomCSSs()
{
if(is_null($this->extCustomCSSs))
{
return $this->customCSSs;
}
return $this->extCustomCSSs;
}
public function getCustomJSs()
{
if(is_null($this->extCustomJSs))
{
return $this->customJSs;
}
return $this->extCustomJSs;
}
public function getCustomJSModules()
{
if(is_null($this->extCustomJSModules))
{
return $this->customJSModules;
}
return $this->extCustomJSModules;
}
protected function buildExtensionsList()
{
$this->extensions = array();
$fsiterator = new FilesystemIterator(FHCPATH . 'application/extensions');
foreach ($fsiterator as $fsitem)
{
if(preg_match('/^FHC-Core-/', $fsitem->getBasename()))
{
$this->extensions[] = $fsitem->getBasename();
}
}
}
protected function prepareExtendedArrays()
{
$this->appscount = 0;
$this->initExtCustomCSSs();
$this->extCustomJSModules = array();
foreach($this->customJSModules as $item)
{
$matches = array();
if(preg_match('#^public/(extensions/FHC-Core-.+)?js/apps/(.*)\.js$#', $item, $matches))
{
$this->appscount++;
$fhcextension = $matches[1];
$app = $matches[2];
$extend_js_suffix = 'js/extend_app/' . $fhcextension . $app . '.js';
$extend_css_suffix = 'css/extend_app/' . $fhcextension . $app . '.css';
foreach($this->extensions as $extension)
{
$extend_js = 'public/extensions/' . $extension . '/' . $extend_js_suffix;
$extend_css = 'public/extensions/' . $extension . '/' . $extend_css_suffix;
if(is_readable(FHCPATH . $extend_js))
{
array_push($this->extCustomJSModules, $extend_js);
}
if(is_readable(FHCPATH . $extend_css))
{
array_push($this->extCustomCSSs, $extend_css);
}
}
}
array_push($this->extCustomJSModules, $item);
}
if($this->appscount > 0)
{
$this->addFhcAppsJs();
}
}
protected function initExtCustomCSSs()
{
if(!isset($this->customCSSs))
{
$this->extCustomCSSs = array();
}
elseif(!is_array($this->customCSSs))
{
$this->extCustomCSSs = array($this->customCSSs);
}
else
{
$this->extCustomCSSs = $this->customCSSs;
}
}
protected function addFhcAppsJs()
{
if(!isset($this->customJSs))
{
$this->extCustomJSs = array();
}
elseif(!is_array($this->customJSs))
{
$this->extCustomJSs = array($this->customJSs);
}
else
{
$this->extCustomJSs = $this->customJSs;
}
array_push($this->extCustomJSs, 'public/js/FhcApps.js');
}
}