Improve handling Ajax mode

- LSurlRequest: add ajax property to check if Ajax mode is enabled
- LSurl: handle_request() now handle Ajax mode detection
- LSsession :: setApiMode now trigger LSsession :: setAjaxDisplay()
- Fix LStemplate::fatal_error() to correctly handle Ajax mode
- LSform: fix reporting failure on Ajax submit
This commit is contained in:
Benjamin Renard 2021-11-16 13:59:17 +01:00
parent d15fdc5d8e
commit b42093cf45
7 changed files with 42 additions and 20 deletions

View file

@ -197,6 +197,7 @@ class LSform extends LSlog_staticLoggerClass {
$JSconfig = array (
'ajaxSubmit' => intval($this -> getConfig('ajaxSubmit', true, 'boolean')),
'onFailureMessage' => _('Unexpected error occurred submiting this form. Please retry again or contact support.'),
);
if (!empty($this -> warnings)) {

View file

@ -1870,6 +1870,7 @@ class LSsession {
*/
public static function setApiMode($val=true) {
self :: $api_mode = (boolean)$val;
self :: setAjaxDisplay(self :: $api_mode);
}
/**

View file

@ -467,7 +467,7 @@ class LStemplate extends LSlog_staticLoggerClass {
**/
public static function fatal_error($error=null) {
http_response_code(500);
if (LSsession :: get('api_mode')) {
if (LSsession :: get('api_mode') || LSsession :: getAjaxDisplay()) {
header('Content-Type: application/json');
$errors = array(_("A fatal error occured. If problem persist, please contact support."));
if ($error)

View file

@ -277,10 +277,15 @@ class LSurl extends LSlog_staticLoggerClass {
self :: log_fatal(_("This request could not be handled."));
}
if (self :: $request -> api_mode)
if (self :: $request -> api_mode) {
LSsession :: setApiMode();
elseif (class_exists('LStemplate'))
LStemplate :: assign('request', self :: $request);
}
else {
if (self :: $request -> ajax)
LSsession :: setAjaxDisplay();
if (class_exists('LStemplate'))
LStemplate :: assign('request', self :: $request);
}
// Check authentication (if need)
if(self :: $request -> authenticated && ! LSsession :: startLSsession()) {

View file

@ -71,6 +71,8 @@ class LSurlRequest extends LSlog_staticLoggerClass {
return $this -> get_referer();
if ($key == 'http_method')
return $_SERVER['REQUEST_METHOD'];
if ($key == 'ajax')
return isset($_REQUEST['ajax']) && boolval($_REQUEST['ajax']);
if (array_key_exists($key, $this->url_params)) {
return urldecode($this->url_params[$key]);
}

View file

@ -329,7 +329,8 @@ var LSform = new Class({
this.LSform.set('send',{
data: this.LSform,
onSuccess: this.onAjaxSubmitComplete.bind(this),
onSuccess: this.onAjaxSubmitSuccess.bind(this),
onFailure: this.onAjaxSubmitFailure.bind(this),
url: this.LSform.get('action'),
imgload: varLSdefault.loadingImgDisplay($('LSform_title'),'inside')
});
@ -355,12 +356,18 @@ var LSform = new Class({
}, this);
},
onAjaxSubmitComplete: function(responseText, responseXML) {
onAjaxSubmitComplete: function(data) {
if (!this.submitting) return;
this.submitting = false;
this.LSform.removeClass('submitting');
var data = JSON.decode(responseText);
// Handle common Ajax return checks
varLSdefault.checkAjaxReturn(data);
},
onAjaxSubmitSuccess: function(responseText, responseXML) {
if (!this.submitting) return;
var data = JSON.decode(responseText);
this.onAjaxSubmitComplete(data);
// Handle LSform errors
this.resetErrors();
@ -370,6 +377,18 @@ var LSform = new Class({
}
},
onAjaxSubmitFailure: function(xhr) {
if (!this.submitting) return;
var data;
if (xhr.response) {
data = JSON.decode(xhr.response);
}
else {
varLSdefault.LSerror.displayOrAdd(this.param.onFailureMessage);
}
this.onAjaxSubmitComplete(data);
},
resetErrors: function() {
$$('dd.LSform-errors').each(function(dd) {
dd.destroy();

View file

@ -746,7 +746,7 @@ function handle_LSobject_select($request) {
LStemplate :: assign('LSobject_list_objectname', $object -> getLabel());
// Set & display template
LSsession :: setTemplate(isset($_REQUEST['ajax'])?'select_table.tpl':'select.tpl');
LSsession :: setTemplate($request->ajax?'select_table.tpl':'select.tpl');
LSsession :: setAjaxDisplay();
LSsession :: displayTemplate();
$LSsearch->afterUsingResult();
@ -954,7 +954,7 @@ function handle_LSobject_create($request) {
if (!LSerror::errorsDefined()) {
LSsession :: addInfo(_("Object has been added."));
}
if (isset($_REQUEST['ajax'])) {
if ($request->ajax) {
LSsession :: displayAjaxReturn (
array(
'LSredirect' => "object/$LSobject/".urlencode($object -> getDn())
@ -968,7 +968,7 @@ function handle_LSobject_create($request) {
}
}
else {
if (isset($_REQUEST['ajax'])) {
if ($request->ajax) {
LSsession :: displayAjaxReturn (
array(
'LSformErrors' => $form -> getErrors()
@ -978,7 +978,7 @@ function handle_LSobject_create($request) {
}
}
}
else if (isset($_REQUEST['ajax']) && $form -> definedError()) {
else if ($request->ajax && $form -> definedError()) {
LSsession :: displayAjaxReturn (
array(
'LSformErrors' => $form -> getErrors()
@ -1157,7 +1157,7 @@ function handle_LSobject_modify($request) {
else {
LSsession :: addInfo(_("The object has been modified successfully."));
}
if (isset($_REQUEST['ajax'])) {
if ($request->ajax) {
LSsession :: displayAjaxReturn (
array(
'LSredirect' => "object/$LSobject/".urlencode($object -> getDn())
@ -1172,7 +1172,7 @@ function handle_LSobject_modify($request) {
}
}
else {
if (isset($_REQUEST['ajax'])) {
if ($request->ajax) {
LSsession :: displayAjaxReturn (
array(
'LSformErrors' => $form -> getErrors()
@ -1182,7 +1182,7 @@ function handle_LSobject_modify($request) {
}
}
}
else if (isset($_REQUEST['ajax']) && $form -> definedError()) {
else if ($request->ajax && $form -> definedError()) {
LSsession :: displayAjaxReturn (
array(
'LSformErrors' => $form -> getErrors()
@ -1557,7 +1557,6 @@ function get_LSobject_from_API_request($request, $instanciate=true, $check_acces
* @retval void
**/
function handle_api_LSobject_search($request) {
LSsession :: setAjaxDisplay();
$object = get_LSobject_from_API_request($request);
if (!$object)
return;
@ -1702,7 +1701,6 @@ LSurl :: add_handler('#^api/1.0/object/(?P<LSobject>[^/]+)/?$#', 'handle_api_LSo
* @retval void
**/
function handle_api_LSobject_create($request) {
LSsession :: setAjaxDisplay();
$object = get_LSobject_from_API_request(
$request,
true, // instanciate object
@ -1832,7 +1830,6 @@ LSurl :: add_handler('#^api/1.0/object/(?P<LSobject>[^/]+)/export/?$#', 'handle_
* @retval void
**/
function handle_api_LSobject_show($request) {
LSsession :: setAjaxDisplay();
$object = get_LSobject_from_API_request($request);
if (!$object)
return;
@ -1879,7 +1876,6 @@ LSurl :: add_handler('#^api/1.0/object/(?P<LSobject>[^/]+)/?(?P<dn>[^/]+)/?$#',
* @retval void
**/
function handle_api_LSobject_modify($request) {
LSsession :: setAjaxDisplay();
$object = get_LSobject_from_API_request(
$request,
true, // instanciate object
@ -1928,7 +1924,6 @@ LSurl :: add_handler('#^api/1.0/object/(?P<LSobject>[^/]+)/(?P<dn>[^/]+)/modify/
* @retval void
**/
function handle_api_LSobject_remove($request) {
LSsession :: setAjaxDisplay();
$object = get_LSobject_from_API_request(
$request,
true, // instanciate object
@ -1964,7 +1959,6 @@ LSurl :: add_handler('#^api/1.0/object/(?P<LSobject>[^/]+)/(?P<dn>[^/]+)/remove/
* @retval void
**/
function handle_api_LSobject_relation($request) {
LSsession :: setAjaxDisplay();
$object = get_LSobject_from_API_request(
$request,
true // instanciate object