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; 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 { div.LSdebug {
top: 10px; top: 10px;
@ -36,7 +50,7 @@ div.LSerror {
color: #fff; color: #fff;
} }
#LSdebug_txt, #LSerror_txt, #LSinfos_txt, #LSjsConfig { #LSdebug, #LSerror, #LSinfos, #LSjsConfig {
display: none; display: none;
} }

View file

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

View file

@ -1698,19 +1698,24 @@ class LSsession {
LStemplate :: assign('displaySelfAccess',LSauth :: displaySelfAccess()); LStemplate :: assign('displaySelfAccess',LSauth :: displaySelfAccess());
// Infos // Infos
if((!empty($_SESSION['LSsession_infos']))&&(is_array($_SESSION['LSsession_infos']))) { LStemplate :: assign(
LStemplate :: assign('LSinfos',$_SESSION['LSsession_infos']); 'LSinfos',
$_SESSION['LSsession_infos']=array(); base64_encode(
} json_encode(
isset($_SESSION['LSsession_infos']) && is_array($_SESSION['LSsession_infos'])?
$_SESSION['LSsession_infos']:
array()
)
)
);
$_SESSION['LSsession_infos'] = array();
// Errors
LSerror :: display();
// LSdebug
LSdebug_print();
if (self :: $ajaxDisplay) {
LStemplate :: assign('LSerror_txt',LSerror :: getErrors());
LStemplate :: assign('LSdebug_txt',LSdebug_print(true));
}
else {
LSerror :: display();
LSdebug_print();
}
if (!self :: $template) if (!self :: $template)
self :: setTemplate('base_connected.tpl'); self :: setTemplate('base_connected.tpl');
@ -1750,43 +1755,43 @@ class LSsession {
if (isset($data['success']) && !$data['success'] && http_response_code() == 200) if (isset($data['success']) && !$data['success'] && http_response_code() == 200)
http_response_code(400); 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 // keep it in session and show it on next page
if (isset($data['LSredirect']) && (!LSdebugDefined()) ) { if (!isset($data['LSredirect']) || LSdebugDefined()) {
echo json_encode($data, (($pretty||isset($_REQUEST['pretty']))?JSON_PRETTY_PRINT:0)); if (!self :: $api_mode && class_exists('LStemplate'))
return; $data['LSjsConfig'] = LStemplate :: getJSconfigParam();
}
if (!self :: $api_mode && class_exists('LStemplate')) // Infos
$data['LSjsConfig'] = LStemplate :: getJSconfigParam(); if(
!empty($_SESSION['LSsession_infos']) &&
// Infos is_array($_SESSION['LSsession_infos'])
if((!empty($_SESSION['LSsession_infos']))&&(is_array($_SESSION['LSsession_infos']))) { ) {
if (self :: $api_mode) {
$data['messages'] = $_SESSION['LSsession_infos']; $data['messages'] = $_SESSION['LSsession_infos'];
$_SESSION['LSsession_infos'] = array();
} }
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()) { 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'])) { if (!self :: $api_mode && isset($_REQUEST['imgload'])) {
$data['imgload'] = $_REQUEST['imgload']; $data['imgload'] = $_REQUEST['imgload'];
} }
if (!self :: $api_mode && LSdebugDefined()) { echo json_encode(
$data['LSdebug'] = LSdebug_print(true,false); $data,
} (
$pretty || isset($_REQUEST['pretty'])?
JSON_PRETTY_PRINT:
0
)
);
return;
echo json_encode($data, (($pretty||isset($_REQUEST['pretty']))?JSON_PRETTY_PRINT:0)); echo json_encode($data, (($pretty||isset($_REQUEST['pretty']))?JSON_PRETTY_PRINT:0));
} }

View file

@ -279,45 +279,49 @@ function varDump($data) {
return $data; return $data;
} }
$GLOBALS['LSdebug_fields']=array(); /*
function LSdebug($data,$dump=false) { * LSdebug
if ($dump) { */
$data=varDump($data); $GLOBALS['LSdebug_fields'] = array();
} function LSdebug($data, $dump=false) {
if ($dump)
$data = varDump($data);
if (class_exists('LSlog')) if (class_exists('LSlog'))
LSlog :: debug($data); LSlog :: debug($data);
if (!is_array($data) && !is_object($data)) { $GLOBALS['LSdebug_fields'][] = htmlentities(strval($data));
$data="[$data]";
}
$GLOBALS['LSdebug_fields'][]=$data;
return true; return true;
} }
function LSdebug_print($return=false,$ul=true) { function LSdebug_print($return=false) {
if (( $GLOBALS['LSdebug_fields'] ) && (LSdebug)) { $result = array();
if ($ul) $txt='<ul>'; else $txt=""; if (LSdebugDefined())
foreach($GLOBALS['LSdebug_fields'] as $debug) { $result = $GLOBALS['LSdebug_fields'];
if (is_array($debug)||is_object($debug)) {
$txt.='<li><pre>'.htmlentities(print_r($debug,true)).'</pre></li>'; // Reset
} $GLOBALS['LSdebug_fields'] = array();
else { if ($return)
$txt.='<li><pre>'.htmlentities(strval($debug)).'</pre></li>'; return $result;
} LStemplate :: assign(
} 'LSdebug_content',
if ($ul) $txt.='</ul>'; base64_encode(
LStemplate :: assign('LSdebug',$txt); json_encode(
if ($return) { $result
return $txt; )
} )
} );
return; return;
} }
function LSdebugDefined() { function LSdebugDefined() {
if (!LSdebug) if (!LSdebug)
return; 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', name: 'LSdebug',
fxDuration: 600, fxDuration: 600,
closeBtn: 1, closeBtn: 1,
autoClose: 0 autoClose: 0,
pre: 1,
}); });
this.LSdebugInfos = $('LSdebug_txt'); this.LSdebugInfos = $('LSdebug');
// LSerror // LSerror
this.LSerror = new LSinfosBox({ this.LSerror = new LSinfosBox({
name: 'LSerror', name: 'LSerror',
opacity: 0.9, opacity: 0.9,
closeBtn: 1, closeBtn: 1,
autoClose: 0 autoClose: 0,
}); });
this.LSerror_div = $('LSerror_txt'); this.LSerror_div = $('LSerror');
// LSinfos // LSinfos
this.LSinfos = new LSinfosBox({name: 'LSinfos'}); this.LSinfos = new LSinfosBox({name: 'LSinfos'});
this.LSinfos_div = $('LSinfos_txt'); this.LSinfos_div = $('LSinfos');
// LSjsConfig // LSjsConfig
this.LSjsConfigEl = $('LSjsConfig'); this.LSjsConfigEl = $('LSjsConfig');
@ -46,16 +47,16 @@ var LSdefault = new Class({
} }
// Display Infos // Display Infos
if (this.LSdebugInfos.innerHTML != '') { if (this.LSdebugInfos.innerHTML) {
this.LSdebug.display(this.LSdebugInfos.innerHTML); this.LSdebug.display(JSON.decode(atob(this.LSdebugInfos.innerHTML)));
} }
if (this.LSerror_div.innerHTML != '') { if (this.LSerror_div.innerHTML) {
this.LSerror.display(this.LSerror_div.innerHTML); this.LSerror.display(JSON.decode(atob(this.LSerror_div.innerHTML)));
} }
if (this.LSinfos_div.innerHTML != '') { if (this.LSinfos_div.innerHTML) {
this.LSinfos.display(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) { checkAjaxReturn: function(data) {
this.LSerror.close(0);
if ($type(data) == 'object') { if ($type(data) == 'object') {
if (($type(data.LSredirect)) && (!$type(data.LSdebug)) ) { if (($type(data.LSredirect)) && (!$type(data.LSdebug)) ) {
document.location = data.LSredirect; document.location = data.LSredirect;
@ -172,12 +172,12 @@ var LSdefault = new Class({
this.LSdebug.displayOrAdd(data.LSdebug); this.LSdebug.displayOrAdd(data.LSdebug);
} }
if ($type(data.LSinfos)) { if ($type(data.messages)) {
this.LSinfos.displayOrAdd(data.LSinfos); this.LSinfos.displayOrAdd(data.messages);
} }
if ($type(data.LSerror)) { if ($type(data.errors)) {
this.LSerror.displayOrAdd(data.LSerror); this.LSerror.displayOrAdd(data.errors);
return; return;
} }
return true; return true;
@ -222,19 +222,17 @@ var LSdefault = new Class({
}, },
ajaxDisplayDebugAndError: function() { ajaxDisplayDebugAndError: function() {
var LSdebug_txt = $('LSdebug_txt_ajax'); var LSdebug_ajax = $('LSdebug_ajax');
if (LSdebug_txt) { if (LSdebug_ajax) {
var debug = LSdebug_txt.innerHTML; if (LSdebug_ajax.innerHTML) {
if (debug) { this.LSdebug.displayOrAdd(LSdebug_ajax.innerHTML);
this.LSdebug.displayOrAdd(debug);
} }
} }
var LSerror_txt = $('LSerror_txt_ajax'); var LSerror_ajax = $('LSerror_ajax');
if (LSerror_txt) { if (LSerror_ajax) {
var error=LSerror_txt.innerHTML; if (LSerror_ajax.innerHTML) {
if (error) { this.LSerror.displayOrAdd(LSerror_ajax.innerHTML);
this.LSerror.displayOrAdd(error);
} }
} }
}, },

View file

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

View file

@ -1,16 +1,10 @@
<div id='LSjsConfig'>{$LSjsConfig}</div> <div id='LSjsConfig'>{$LSjsConfig}</div>
<div id='LSinfos_txt'>{if isset($LSinfos) && is_array($LSinfos) && !empty($LSinfos)} <div id='LSinfos'>{$LSinfos}</div>
<ul>
{foreach $LSinfos as $info}
<li>{$info|escape:"htmlall"}</li>
{/foreach}
</ul>
{/if}</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> <div id="_smarty_console"></div>

View file

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