Improve/fix LSdefaut / LSsession / LSerror / LSdebug / LSinfosBox

This commit is contained in:
Benjamin Renard 2021-08-25 16:50:29 +02:00
parent 26c0026cfc
commit a3aa857c78
8 changed files with 227 additions and 195 deletions

View file

@ -19,6 +19,20 @@ span.LSinfosBox {
padding: 2px;
}
div.LSinfosBox p {
padding: 0 1em;
}
div.LSinfosBox ul {
padding-left: 1em;
}
div.LSinfosBox ul li.separator {
list-style-type: none;
border-bottom: 1px solid;
margin-left: -1em;
}
div.LSdebug {
top: 10px;
@ -36,7 +50,7 @@ div.LSerror {
color: #fff;
}
#LSdebug_txt, #LSerror_txt, #LSinfos_txt, #LSjsConfig {
#LSdebug, #LSerror, #LSinfos, #LSjsConfig {
display: none;
}

View file

@ -87,12 +87,9 @@ class LSerror {
*/
public static function display($return=False) {
$errors = self :: getErrors();
if ($errors) {
if ($return) {
if ($errors && $return)
return $errors;
}
LStemplate :: assign('LSerrors', $errors);
}
LStemplate :: assign('LSerrors', base64_encode(json_encode($errors)));
return;
}
@ -123,20 +120,15 @@ class LSerror {
*
* @retvat string Le texte des erreurs
*/
public static function getErrors($raw=false) {
if(!empty($_SESSION['LSerror'])) {
if ($raw)
$return = $_SESSION['LSerror'];
else {
$return = '';
foreach ($_SESSION['LSerror'] as $error)
$return .= $error."<br />\n";
}
public static function getErrors() {
$return = (
self :: errorsDefined()?
$_SESSION['LSerror']:
array()
);
self :: resetError();
return $return;
}
return;
}
/**
* Format error message
@ -176,7 +168,11 @@ class LSerror {
* @retvat boolean
*/
public static function errorsDefined() {
return !empty($_SESSION['LSerror']);
return (
isset($_SESSION['LSerror']) &&
is_array($_SESSION['LSerror']) &&
!empty($_SESSION['LSerror'])
);
}
/**
@ -187,6 +183,7 @@ class LSerror {
* @retvat void
*/
private static function resetError() {
if (isset($_SESSION['LSerror']))
unset ($_SESSION['LSerror']);
}

View file

@ -1698,19 +1698,24 @@ class LSsession {
LStemplate :: assign('displaySelfAccess',LSauth :: displaySelfAccess());
// Infos
if((!empty($_SESSION['LSsession_infos']))&&(is_array($_SESSION['LSsession_infos']))) {
LStemplate :: assign('LSinfos',$_SESSION['LSsession_infos']);
LStemplate :: assign(
'LSinfos',
base64_encode(
json_encode(
isset($_SESSION['LSsession_infos']) && is_array($_SESSION['LSsession_infos'])?
$_SESSION['LSsession_infos']:
array()
)
)
);
$_SESSION['LSsession_infos'] = array();
}
if (self :: $ajaxDisplay) {
LStemplate :: assign('LSerror_txt',LSerror :: getErrors());
LStemplate :: assign('LSdebug_txt',LSdebug_print(true));
}
else {
// Errors
LSerror :: display();
// LSdebug
LSdebug_print();
}
if (!self :: $template)
self :: setTemplate('base_connected.tpl');
@ -1750,43 +1755,43 @@ class LSsession {
if (isset($data['success']) && !$data['success'] && http_response_code() == 200)
http_response_code(400);
// If redirection set, just redirect user before handling messages/errors to
// If redirection set, just redirect user and not handling messages/errors to
// keep it in session and show it on next page
if (isset($data['LSredirect']) && (!LSdebugDefined()) ) {
echo json_encode($data, (($pretty||isset($_REQUEST['pretty']))?JSON_PRETTY_PRINT:0));
return;
}
if (!isset($data['LSredirect']) || LSdebugDefined()) {
if (!self :: $api_mode && class_exists('LStemplate'))
$data['LSjsConfig'] = LStemplate :: getJSconfigParam();
// Infos
if((!empty($_SESSION['LSsession_infos']))&&(is_array($_SESSION['LSsession_infos']))) {
if (self :: $api_mode) {
if(
!empty($_SESSION['LSsession_infos']) &&
is_array($_SESSION['LSsession_infos'])
) {
$data['messages'] = $_SESSION['LSsession_infos'];
}
else {
$txt_infos="<ul>\n";
foreach($_SESSION['LSsession_infos'] as $info) {
$txt_infos.="<li>$info</li>\n";
}
$txt_infos.="</ul>\n";
$data['LSinfos'] = $txt_infos;
}
$_SESSION['LSsession_infos'] = array();
}
if (LSerror :: errorsDefined()) {
$data[(self :: $api_mode?'errors':'LSerror')] = LSerror :: getErrors(self :: $api_mode);
$data['errors'] = LSerror :: getErrors(self :: $api_mode);
}
if (!self :: $api_mode && LSdebugDefined()) {
$data['LSdebug'] = LSdebug_print(true);
}
}
if (!self :: $api_mode && isset($_REQUEST['imgload'])) {
$data['imgload'] = $_REQUEST['imgload'];
}
if (!self :: $api_mode && LSdebugDefined()) {
$data['LSdebug'] = LSdebug_print(true,false);
}
echo json_encode(
$data,
(
$pretty || isset($_REQUEST['pretty'])?
JSON_PRETTY_PRINT:
0
)
);
return;
echo json_encode($data, (($pretty||isset($_REQUEST['pretty']))?JSON_PRETTY_PRINT:0));
}

View file

@ -279,45 +279,49 @@ function varDump($data) {
return $data;
}
/*
* LSdebug
*/
$GLOBALS['LSdebug_fields'] = array();
function LSdebug($data, $dump=false) {
if ($dump) {
if ($dump)
$data = varDump($data);
}
if (class_exists('LSlog'))
LSlog :: debug($data);
if (!is_array($data) && !is_object($data)) {
$data="[$data]";
}
$GLOBALS['LSdebug_fields'][]=$data;
$GLOBALS['LSdebug_fields'][] = htmlentities(strval($data));
return true;
}
function LSdebug_print($return=false,$ul=true) {
if (( $GLOBALS['LSdebug_fields'] ) && (LSdebug)) {
if ($ul) $txt='<ul>'; else $txt="";
foreach($GLOBALS['LSdebug_fields'] as $debug) {
if (is_array($debug)||is_object($debug)) {
$txt.='<li><pre>'.htmlentities(print_r($debug,true)).'</pre></li>';
}
else {
$txt.='<li><pre>'.htmlentities(strval($debug)).'</pre></li>';
}
}
if ($ul) $txt.='</ul>';
LStemplate :: assign('LSdebug',$txt);
if ($return) {
return $txt;
}
}
function LSdebug_print($return=false) {
$result = array();
if (LSdebugDefined())
$result = $GLOBALS['LSdebug_fields'];
// Reset
$GLOBALS['LSdebug_fields'] = array();
if ($return)
return $result;
LStemplate :: assign(
'LSdebug_content',
base64_encode(
json_encode(
$result
)
)
);
return;
}
function LSdebugDefined() {
if (!LSdebug)
return;
return (!empty($GLOBALS['LSdebug_fields']));
return (
isset($GLOBALS['LSdebug_fields']) &&
is_array($GLOBALS['LSdebug_fields']) &&
!empty($GLOBALS['LSdebug_fields'])
);
}
/**

View file

@ -10,22 +10,23 @@ var LSdefault = new Class({
name: 'LSdebug',
fxDuration: 600,
closeBtn: 1,
autoClose: 0
autoClose: 0,
pre: 1,
});
this.LSdebugInfos = $('LSdebug_txt');
this.LSdebugInfos = $('LSdebug');
// LSerror
this.LSerror = new LSinfosBox({
name: 'LSerror',
opacity: 0.9,
closeBtn: 1,
autoClose: 0
autoClose: 0,
});
this.LSerror_div = $('LSerror_txt');
this.LSerror_div = $('LSerror');
// LSinfos
this.LSinfos = new LSinfosBox({name: 'LSinfos'});
this.LSinfos_div = $('LSinfos_txt');
this.LSinfos_div = $('LSinfos');
// LSjsConfig
this.LSjsConfigEl = $('LSjsConfig');
@ -46,16 +47,16 @@ var LSdefault = new Class({
}
// Display Infos
if (this.LSdebugInfos.innerHTML != '') {
this.LSdebug.display(this.LSdebugInfos.innerHTML);
if (this.LSdebugInfos.innerHTML) {
this.LSdebug.display(JSON.decode(atob(this.LSdebugInfos.innerHTML)));
}
if (this.LSerror_div.innerHTML != '') {
this.LSerror.display(this.LSerror_div.innerHTML);
if (this.LSerror_div.innerHTML) {
this.LSerror.display(JSON.decode(atob(this.LSerror_div.innerHTML)));
}
if (this.LSinfos_div.innerHTML != '') {
this.LSinfos.display(this.LSinfos_div.innerHTML);
if (this.LSinfos_div.innerHTML) {
this.LSinfos.display(JSON.decode(atob(this.LSinfos_div.innerHTML)));
}
// :)
@ -154,7 +155,6 @@ var LSdefault = new Class({
},
checkAjaxReturn: function(data) {
this.LSerror.close(0);
if ($type(data) == 'object') {
if (($type(data.LSredirect)) && (!$type(data.LSdebug)) ) {
document.location = data.LSredirect;
@ -172,12 +172,12 @@ var LSdefault = new Class({
this.LSdebug.displayOrAdd(data.LSdebug);
}
if ($type(data.LSinfos)) {
this.LSinfos.displayOrAdd(data.LSinfos);
if ($type(data.messages)) {
this.LSinfos.displayOrAdd(data.messages);
}
if ($type(data.LSerror)) {
this.LSerror.displayOrAdd(data.LSerror);
if ($type(data.errors)) {
this.LSerror.displayOrAdd(data.errors);
return;
}
return true;
@ -222,19 +222,17 @@ var LSdefault = new Class({
},
ajaxDisplayDebugAndError: function() {
var LSdebug_txt = $('LSdebug_txt_ajax');
if (LSdebug_txt) {
var debug = LSdebug_txt.innerHTML;
if (debug) {
this.LSdebug.displayOrAdd(debug);
var LSdebug_ajax = $('LSdebug_ajax');
if (LSdebug_ajax) {
if (LSdebug_ajax.innerHTML) {
this.LSdebug.displayOrAdd(LSdebug_ajax.innerHTML);
}
}
var LSerror_txt = $('LSerror_txt_ajax');
if (LSerror_txt) {
var error=LSerror_txt.innerHTML;
if (error) {
this.LSerror.displayOrAdd(error);
var LSerror_ajax = $('LSerror_ajax');
if (LSerror_ajax) {
if (LSerror_ajax.innerHTML) {
this.LSerror.displayOrAdd(LSerror_ajax.innerHTML);
}
}
},

View file

@ -6,12 +6,13 @@ var LSinfosBox = new Class({
name: '',
fxDuration: 500,
opacity: 0.8,
autoClose: 3000
autoClose: 3000,
pre: false,
};
// Load options from argument
if ($type(options)=='object') {
$each(options,function(val,name) {
Object.each(options, function(val, name) {
if ($type(this._options[name])) {
this._options[name]=val;
}
@ -20,7 +21,8 @@ var LSinfosBox = new Class({
this.build();
this.opened=0;
this.opened = false;
this.autoClose_timeout = false;
},
build: function() {
@ -58,6 +60,7 @@ var LSinfosBox = new Class({
);
this.core.inject(document.body,'top');
this.ul = false;
},
isOpened: function() {
@ -67,23 +70,27 @@ var LSinfosBox = new Class({
open: function() {
this.core.setStyle('top',getScrollTop()+10);
if (this._options.autoClose>0) {
if (this._options.autoClose) {
this.closeTime = (new Date()).getTime();
this.autoClose.delay((this._options.autoClose+this._options.fxDuration),this,this.closeTime);
if (this.autoClose_timeout) {
clearTimeout(this.autoClose_timeout);
}
this.autoClose_timeout = this.close.delay(this._options.autoClose, this);
}
if (this.opened) {
return true;
console.log('LSinfoBox('+this._options.name+'): already opened');
return;
}
console.log('LSinfoBox('+this._options.name+'): open');
this.opened = true;
this.fx.start(0, this._options.opacity);
this.opened = 1;
},
close: function(withoutEffect) {
if (this.opened) {
this.opened = 0;
console.log('LSinfoBox('+this._options.name+'): close');
this.opened = false;
if (withoutEffect==1) {
this.fx.set(0);
}
@ -91,70 +98,83 @@ var LSinfosBox = new Class({
this.fx.start(this._options.opacity, 0);
}
}
},
autoClose: function(time) {
if (time==this.closeTime) {
this.close();
this.closeTime=0;
else {
console.log('LSinfoBox('+this._options.name+'): already closed');
}
},
addInfo: function(html, clear) {
addInfo: function(info, clear) {
if (!info || ($type(info) == 'array' && !info.length)) return;
if (clear) this.clear();
var ul = this.content.getLast("ul");
var add = 1;
if (!$type(ul)) {
add=0;
ul = new Element('ul');
if (this.content.innerHTML!="") {
if (this.content.innerHTML) {
// If content is not already in ul, put it in
if (!this.ul) {
this.ul = new Element('ul');
if (this.content.innerHTML) {
var c_li = new Element('li');
c_li.set('html', this.content.innerHTML);
c_li.injectInside(ul);
add=1;
c_li.injectInside(this.ul);
}
this.content.empty();
ul.injectInside(this.content);
this.ul.injectInside(this.content);
}
if (add) {
// Add li.separator to separate old/new content
var b_li = new Element('li');
b_li.set('html','<hr/>');
b_li.injectInside(ul);
b_li.addClass('separator');
b_li.injectInside(this.ul);
}
if ($type(info) == "string") {
if (this.ul) {
var li = new Element('li');
li.set('html',html);
li.injectInside(ul);
this.open();
},
display: function(html) {
if ($type(html)) {
this.content.empty();
this.content.set('html',html);
}
this.open();
},
displayInUl: function(html) {
if ($type(html)) {
ul = new Element('ul');
this.content.empty();
ul.set('html',html);
ul.inject(this.content);
}
this.open();
},
displayOrAdd: function(html) {
if (this.isOpened()) {
this.addInfo(html);
if (this._options.pre) {
var pre = new Element('pre');
pre.set('html', info);
pre.injectInside(li);
}
else {
this.displayInUl(html);
li.set('html', info);
}
li.injectInside(this.ul);
}
else {
this.content.set('html', info);
}
}
else if ($type(info) == 'array') {
if (!this.ul) {
this.ul = new Element('ul');
this.ul.injectInside(this.content);
}
Array.each(info, function(msg) {
var li = new Element('li');
if (this._options.pre) {
var pre = new Element('pre');
pre.set('html', msg);
pre.injectInside(li);
}
else {
li.set('html', msg);
}
li.injectInside(this.ul);
}, this);
}
this.open();
},
display: function(info) {
this.addInfo(info, true);
},
displayOrAdd: function(info) {
console.log('LSinfoBox('+this._options.name+').displayOrAdd(): open='+this.opened);
this.addInfo(info, !this.opened);
},
clear: function() {
console.log('LSinfoBox('+this._options.name+'): clear');
this.content.empty();
this.ul = false;
}
});

View file

@ -1,16 +1,10 @@
<div id='LSjsConfig'>{$LSjsConfig}</div>
<div id='LSinfos_txt'>{if isset($LSinfos) && is_array($LSinfos) && !empty($LSinfos)}
<ul>
{foreach $LSinfos as $info}
<li>{$info|escape:"htmlall"}</li>
{/foreach}
</ul>
{/if}</div>
<div id='LSinfos'>{$LSinfos}</div>
<div id='LSerror_txt'>{if isset($LSerrors) && $LSerrors}{$LSerrors}{/if}</div>
<div id='LSerror'>{$LSerrors}</div>
<div id='LSdebug_txt'>{if isset($LSdebug) && $LSdebug}{$LSdebug}{/if}</div>
<div id='LSdebug'>{$LSdebug_content}</div>
<div id="_smarty_console"></div>

View file

@ -45,5 +45,5 @@
{include file='ls:pagination.tpl'}
<div id='LSdebug_txt_ajax' style='display: none'>{$LSdebug_txt}</div>
<div id='LSerror_txt_ajax' style='display: none'>{$LSerror_txt}</div>
<div id='LSdebug_ajax' style='display: none'>{$LSdebug}</div>
<div id='LSerror_ajax' style='display: none'>{$LSerror}</div>