From 3ae3b1b7616f7249c719d0ffef37e5c1db7641dc Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 15 Nov 2019 17:07:35 +0900 Subject: [PATCH] Simplify language calls There are no more lang vars passed on to any class calls The new order is the following $OVERRIDE_LANG > _SESSION > SITE_LANG > DEFAULT_LANG Todo: make the setLang better so we do not have the same method in Backend/Generic/SmartyExtended --- www/admin/class_test.php | 6 +-- www/admin/namespace_test.php | 3 +- www/includes/admin_header.php | 12 ++---- www/includes/edit_base.php | 12 ++---- www/lib/CoreLibs/ACL/Login.php | 11 +++-- www/lib/CoreLibs/Admin/Backend.php | 24 ++++++----- www/lib/CoreLibs/Output/Form/Generate.php | 50 ++++++++++++++++++++-- www/lib/CoreLibs/Template/SmartyExtend.php | 17 +++++--- 8 files changed, 88 insertions(+), 47 deletions(-) diff --git a/www/admin/class_test.php b/www/admin/class_test.php index 28c2841e..15878160 100644 --- a/www/admin/class_test.php +++ b/www/admin/class_test.php @@ -24,12 +24,10 @@ if (!defined('SET_SESSION_NAME')) { } // define log file id $LOG_FILE_ID = 'classTest'; -// set language for l10n -$lang = 'en_utf8'; // init login & backend class -$login = new CoreLibs\ACL\Login(DB_CONFIG, $lang); -$basic = new CoreLibs\Admin\Backend(DB_CONFIG, $lang); +$login = new CoreLibs\ACL\Login(DB_CONFIG); +$basic = new CoreLibs\Admin\Backend(DB_CONFIG); $basic->dbInfo(true); ob_end_flush(); diff --git a/www/admin/namespace_test.php b/www/admin/namespace_test.php index e8248c92..89e90e13 100755 --- a/www/admin/namespace_test.php +++ b/www/admin/namespace_test.php @@ -14,8 +14,7 @@ $SET_SESSION_NAME = EDIT_SESSION_NAME; echo "DIR: ".DIR."
ROOT: ".ROOT."
BASE: ".BASE."
"; -$lang = 'ja_utf8'; -$base = new CoreLibs\Admin\Backend(DB_CONFIG, $lang); +$base = new CoreLibs\Admin\Backend(DB_CONFIG); ob_end_flush(); if ($base->getConnectionStatus()) { die("Cannot connect to database"); diff --git a/www/includes/admin_header.php b/www/includes/admin_header.php index 7decbbe4..7bf28c0c 100644 --- a/www/includes/admin_header.php +++ b/www/includes/admin_header.php @@ -38,12 +38,6 @@ if (!isset($ZIP_STREAM)) { if (!isset($ENCODING) || !$ENCODING) { $ENCODING = DEFAULT_ENCODING; } -// set the default lang, if not given -if (session_id() && isset($_SESSION['DEFAULT_LANG']) && $_SESSION['DEFAULT_LANG']) { - $LANG = $_SESSION['DEFAULT_LANG']; -} else { - $LANG = defined('SITE_LANG') ? SITE_LANG : DEFAULT_LANG; -} // end the stop of the output flow, but only if we didn't request a csv file download if (isset($_POST['action']) && $_POST['action'] != 'download_csv' && !$AJAX_PAGE) { header("Content-type: text/html; charset=".$ENCODING); @@ -55,15 +49,15 @@ if ($AJAX_PAGE && !$ZIP_STREAM) { //------------------------------ class init start // login & page access check -$login = new CoreLibs\ACL\Login(DB_CONFIG, $LANG); +$login = new CoreLibs\ACL\Login(DB_CONFIG); // post login lang check if (isset($_SESSION['DEFAULT_LANG'])) { $LANG = $_SESSION['DEFAULT_LANG']; } // create smarty object -$smarty = new CoreLibs\Template\SmartyExtend($LANG); +$smarty = new CoreLibs\Template\SmartyExtend(); // create new DB class -$cms = new CoreLibs\Admin\Backend(DB_CONFIG, $LANG); +$cms = new CoreLibs\Admin\Backend(DB_CONFIG); // the menu show flag (what menu to show) $cms->menu_show_flag = 'main'; // db nfo diff --git a/www/includes/edit_base.php b/www/includes/edit_base.php index 34a20b82..14432796 100644 --- a/www/includes/edit_base.php +++ b/www/includes/edit_base.php @@ -36,23 +36,19 @@ if (!DEBUG) { $ECHO_ALL = 0; } -// set default lang if not set otherwise -if (!isset($lang)) { - $lang = DEFAULT_LANG; -} // should be utf8 header("Content-type: text/html; charset=".DEFAULT_ENCODING); ob_end_flush(); -$login = new CoreLibs\ACL\Login(DB_CONFIG, $lang); +$login = new CoreLibs\ACL\Login(DB_CONFIG); // create form class -$form = new CoreLibs\Output\Form\Generate(DB_CONFIG, $lang); +$form = new CoreLibs\Output\Form\Generate(DB_CONFIG); if ($form->mobile_phone) { echo "I am sorry, but this page cannot be viewed by a mobile phone"; exit; } // smarty template engine (extended Translation version) -$smarty = new CoreLibs\Template\SmartyExtend($lang); +$smarty = new CoreLibs\Template\SmartyExtend(); // $form->debug('POST', $form->printAr($_POST)); @@ -453,7 +449,7 @@ if (is_dir(BASE.TEMPLATES_C)) { if (is_dir(BASE.CACHE)) { $smarty->setCacheDir(BASE.CACHE); } -$smarty->display($EDIT_TEMPLATE, 'editAdmin_'.$lang, 'editAdmin_'.$lang); +$smarty->display($EDIT_TEMPLATE, 'editAdmin_'.$smarty->lang, 'editAdmin_'.$smarty->lang); // debug output echo $login->printErrorMsg(); diff --git a/www/lib/CoreLibs/ACL/Login.php b/www/lib/CoreLibs/ACL/Login.php index 4c764939..f3be44b2 100644 --- a/www/lib/CoreLibs/ACL/Login.php +++ b/www/lib/CoreLibs/ACL/Login.php @@ -115,10 +115,9 @@ class Login extends \CoreLibs\DB\IO /** * constructor, does ALL, opens db, works through connection checks, closes itself * @param array $db_config db config array - * @param string $lang language string (default en_utf8) * @param int $set_control_flag class variable check flags */ - public function __construct(array $db_config, string $lang = 'en_utf8', int $set_control_flag = 0) + public function __construct(array $db_config, int $set_control_flag = 0) { // log login data for this class only $this->log_per_class = 1; @@ -151,7 +150,13 @@ class Login extends \CoreLibs\DB\IO // set global is ajax page for if we show the data directly, or need to pass it back // to the continue AJAX class for output back to the user $this->login_is_ajax_page = isset($GLOBALS['AJAX_PAGE']) && $GLOBALS['AJAX_PAGE'] ? true : false; - + // set the default lang + $lang = 'en_utf8'; + if (session_id() && isset($_SESSION['DEFAULT_LANG']) && $_SESSION['DEFAULT_LANG']) { + $lang = $_SESSION['DEFAULT_LANG']; + } else { + $lang = defined('SITE_LANG') ? SITE_LANG : DEFAULT_LANG; + } $this->l = new \CoreLibs\Language\L10n($lang); // if we have a search path we need to set it, to use the correct DB to login diff --git a/www/lib/CoreLibs/Admin/Backend.php b/www/lib/CoreLibs/Admin/Backend.php index 7dbe594f..38bffc36 100644 --- a/www/lib/CoreLibs/Admin/Backend.php +++ b/www/lib/CoreLibs/Admin/Backend.php @@ -57,26 +57,25 @@ class Backend extends \CoreLibs\DB\IO public $lang; public $lang_short; public $encoding; + // language + public $l; // smarty publics [end processing in smarty class] public $DATA; public $HEADER; public $DEBUG_DATA; public $CONTENT_DATA; - // language - public $l; // CONSTRUCTOR / DECONSTRUCTOR |====================================> /** * main class constructor * @param array $db_config db config array - * @param string $lang language string * @param int|integer $set_control_flag class variable check flag */ - public function __construct(array $db_config, string $lang, int $set_control_flag = 0) + public function __construct(array $db_config, int $set_control_flag = 0) { $this->setLangEncoding(); // get the language sub class & init it - $this->l = new \CoreLibs\Language\L10n($lang); + $this->l = new \CoreLibs\Language\L10n($this->lang); // init the database class parent::__construct($db_config, $set_control_flag); @@ -104,9 +103,6 @@ class Backend extends \CoreLibs\DB\IO // INTERNAL METHODS |===============================================> - - // PUBLIC METHODS |=================================================> - /** * set the language encoding and language settings * the default charset from _SESSION login or from @@ -116,7 +112,7 @@ class Backend extends \CoreLibs\DB\IO * creates short lang (only first two chars) from the lang * @return void */ - public function setLangEncoding(): void + private function setLangEncoding(): void { // just emergency fallback for language // set encoding @@ -125,10 +121,14 @@ class Backend extends \CoreLibs\DB\IO } else { $this->encoding = DEFAULT_ENCODING; } - // just emergency fallback for language - if (isset($_SESSION['DEFAULT_LANG'])) { + // gobal override + if (isset($GLOBALS['OVERRIDE_LANG'])) { + $this->lang = $GLOBALS['OVERRIDE_LANG']; + } elseif (isset($_SESSION['DEFAULT_LANG'])) { + // session (login) $this->lang = $_SESSION['DEFAULT_LANG']; } else { + // mostly default SITE LANG or DEFAULT LANG $this->lang = defined('SITE_LANG') ? SITE_LANG : DEFAULT_LANG; } // create the char lang encoding @@ -137,6 +137,8 @@ class Backend extends \CoreLibs\DB\IO $this->lang_dir = BASE.INCLUDES.LANG.CONTENT_PATH; } + // PUBLIC METHODS |=================================================> + /** * set internal ACL from login ACL * @param array $acl login acl array diff --git a/www/lib/CoreLibs/Output/Form/Generate.php b/www/lib/CoreLibs/Output/Form/Generate.php index 88e57ba9..52c68f6c 100644 --- a/www/lib/CoreLibs/Output/Form/Generate.php +++ b/www/lib/CoreLibs/Output/Form/Generate.php @@ -242,6 +242,11 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO public $security_level; // layout publics public $table_width; + // internal lang & encoding vars + public $lang_dir = ''; + public $lang; + public $lang_short; + public $encoding; // language public $l; @@ -251,15 +256,15 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO /** * construct form generator * @param array $db_config db config array - * @param string $lang interface language * @param int|integer $table_width table/div width (default 750) * @param int|integer $set_control_flag basic class set/get variable error flags */ - public function __construct(array $db_config, string $lang, int $table_width = 750, int $set_control_flag = 0) + public function __construct(array $db_config, int $table_width = 750, int $set_control_flag = 0) { $this->my_page_name = $this->getPageName(1); + $this->setLangEncoding(); // init the language class - $this->l = new \CoreLibs\Language\L10n($lang); + $this->l = new \CoreLibs\Language\L10n($this->lang); // load config array // get table array definitions for current page name // WARNING: auto spl load does not work with this as it is an array and not a function/object @@ -348,6 +353,45 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO parent::__destruct(); } + // INTERNAL METHODS |===============================================> + + /** + * ORIGINAL in \CoreLibs\Admin\Backend + * set the language encoding and language settings + * the default charset from _SESSION login or from + * config DEFAULT ENCODING + * the lang full name for mo loading from _SESSION login + * or SITE LANG or DEFAULT LANG from config + * creates short lang (only first two chars) from the lang + * @return void + */ + private function setLangEncoding(): void + { + // just emergency fallback for language + // set encoding + if (isset($_SESSION['DEFAULT_CHARSET'])) { + $this->encoding = $_SESSION['DEFAULT_CHARSET']; + } else { + $this->encoding = DEFAULT_ENCODING; + } + // gobal override + if (isset($GLOBALS['OVERRIDE_LANG'])) { + $this->lang = $GLOBALS['OVERRIDE_LANG']; + } elseif (isset($_SESSION['DEFAULT_LANG'])) { + // session (login) + $this->lang = $_SESSION['DEFAULT_LANG']; + } else { + // mostly default SITE LANG or DEFAULT LANG + $this->lang = defined('SITE_LANG') ? SITE_LANG : DEFAULT_LANG; + } + // create the char lang encoding + $this->lang_short = substr($this->lang, 0, 2); + // set the language folder + $this->lang_dir = BASE.INCLUDES.LANG.CONTENT_PATH; + } + + // PUBLIC METHODS |=================================================> + /** * dumps all values into output (for error msg) * @return string full table array data output as string html formatted diff --git a/www/lib/CoreLibs/Template/SmartyExtend.php b/www/lib/CoreLibs/Template/SmartyExtend.php index 20c31bf5..0cd96926 100644 --- a/www/lib/CoreLibs/Template/SmartyExtend.php +++ b/www/lib/CoreLibs/Template/SmartyExtend.php @@ -89,10 +89,9 @@ class SmartyExtend extends SmartyBC /** * constructor class, just sets the language stuff * calls L10 for pass on internaly in smarty - * also registers the getvar caller pliugin - * @param string $lang language string to set + * also registers the getvar caller plugin */ - public function __construct(string $lang) + public function __construct() { // call basic smarty parent::__construct(); @@ -121,7 +120,7 @@ class SmartyExtend extends SmartyBC * creates short lang (only first two chars) from the lang * @return void */ - public function setLangEncoding(): void + private function setLangEncoding(): void { // just emergency fallback for language // set encoding @@ -130,10 +129,14 @@ class SmartyExtend extends SmartyBC } else { $this->encoding = DEFAULT_ENCODING; } - // just emergency fallback for language - if (isset($_SESSION['DEFAULT_LANG'])) { + // gobal override + if (isset($GLOBALS['OVERRIDE_LANG'])) { + $this->lang = $GLOBALS['OVERRIDE_LANG']; + } elseif (isset($_SESSION['DEFAULT_LANG'])) { + // session (login) $this->lang = $_SESSION['DEFAULT_LANG']; } else { + // mostly default SITE LANG or DEFAULT LANG $this->lang = defined('SITE_LANG') ? SITE_LANG : DEFAULT_LANG; } // create the char lang encoding @@ -405,7 +408,7 @@ class SmartyExtend extends SmartyBC /** * render smarty data (can be called sepparate) - * @return [type] [description] + * @return void */ public function renderSmarty(): void {