- LSform :

-> Envoie des données via une requête Ajax
- Correction des templates et des procédures modify et create pour l'utilisation
	de l'envoie des données via Ajax.
This commit is contained in:
Benjamin Renard 2008-11-09 23:24:46 +00:00
parent 14bb86529f
commit 102cf0ea31
7 changed files with 170 additions and 29 deletions

View file

@ -52,11 +52,37 @@ if($LSsession -> startLSsession()) {
if (!$GLOBALS['LSerror']->errorsDefined()) { if (!$GLOBALS['LSerror']->errorsDefined()) {
$GLOBALS['LSsession'] -> addInfo(_("L'objet a bien été ajouté.")); $GLOBALS['LSsession'] -> addInfo(_("L'objet a bien été ajouté."));
} }
if (isset($_REQUEST['ajax'])) {
$GLOBALS['LSsession'] -> displayAjaxReturn (
array(
'LSformRedirect' => 'view.php?LSobject='.$LSobject.'&dn='.$object -> getDn()
)
);
exit();
}
else {
if ((!LSdebugDefined()) && !$GLOBALS['LSerror']->errorsDefined()) { if ((!LSdebugDefined()) && !$GLOBALS['LSerror']->errorsDefined()) {
$GLOBALS['LSsession'] -> redirect('view.php?LSobject='.$LSobject.'&dn='.$object -> getDn()); $GLOBALS['LSsession'] -> redirect('view.php?LSobject='.$LSobject.'&dn='.$object -> getDn());
} }
} }
} }
else {
$GLOBALS['LSsession'] -> displayAjaxReturn (
array(
'LSformErrors' => $form -> getErrors()
)
);
exit();
}
}
else if (isset($_REQUEST['ajax']) && $form -> definedError()) {
$GLOBALS['LSsession'] -> displayAjaxReturn (
array(
'LSformErrors' => $form -> getErrors()
)
);
exit();
}
// Définition du Titre de la page // Définition du Titre de la page
$GLOBALS['Smarty'] -> assign('pagetitle',_('Nouveau').' : '.$object -> getLabel()); $GLOBALS['Smarty'] -> assign('pagetitle',_('Nouveau').' : '.$object -> getLabel());
$GLOBALS['LSsession'] -> setTemplate('create.tpl'); $GLOBALS['LSsession'] -> setTemplate('create.tpl');
@ -78,7 +104,6 @@ if($LSsession -> startLSsession()) {
else { else {
$GLOBALS['LSsession'] -> setTemplate('login.tpl'); $GLOBALS['LSsession'] -> setTemplate('login.tpl');
} }
// Affichage des retours d'erreurs
$GLOBALS['LSsession'] -> displayTemplate(); $GLOBALS['LSsession'] -> displayTemplate();
?> ?>

View file

@ -38,6 +38,10 @@ li.LSform_layout_current {
background-color: #433F3A; background-color: #433F3A;
} }
li.LSform_layout_errors {
background-color: #f59a67;
}
h2.LSform_layout_active { h2.LSform_layout_active {
display: none; display: none;
} }

View file

@ -38,6 +38,10 @@ li.LSform_layout_current {
background-color: #0072B8; background-color: #0072B8;
} }
li.LSform_layout_errors {
background-color: #f59a67;
}
h2.LSform_layout_active { h2.LSform_layout_active {
display: none; display: none;
} }

View file

@ -23,6 +23,20 @@ var LSform = new Class({
this._elements[ul.id] = new LSformElement(this,ul.id,ul); this._elements[ul.id] = new LSformElement(this,ul.id,ul);
}, this); }, this);
} }
LSforms = $$('form.LSform');
if ($type(LSforms[0])) {
this.LSform = LSforms[0];
this.LSformAjaxInput = new Element('input');
this.LSformAjaxInput.setProperties ({
type: 'hidden',
name: 'ajax',
value: '1'
});
this.LSformAjaxInput.injectInside(this.LSform);
this.LSform.addEvent('submit',this.ajaxSubmit.bindWithEvent(this));
}
}, },
initializeLSformLayout: function(el) { initializeLSformLayout: function(el) {
@ -125,6 +139,74 @@ var LSform = new Class({
},this); },this);
} }
return retVal; return retVal;
},
ajaxSubmit: function(event) {
event = new Event(event);
event.stop();
this.resetErrors();
this.LSform.set('send',{
data: this.LSform,
onSuccess: this.onAjaxSubmitComplete.bind(this),
url: this.LSform.get('action'),
imgload: varLSdefault.loadingImgDisplay($('LSform_title'),'inside')
});
this.LSform.send();
},
onAjaxSubmitComplete: function(responseText, responseXML) {
var data = JSON.decode(responseText);
if ( varLSdefault.checkAjaxReturn(data) ) {
if ($type(data.LSformRedirect)) {
if (!$type(data.LSdebug)) {
(function(addr){document.location = addr;}).delay(1000,this,data.LSformRedirect);
}
}
else if ($type(data.LSformErrors) == 'object') {
data.LSformErrors = new Hash(data.LSformErrors);
data.LSformErrors.each(this.addError,this);
}
}
},
resetErrors: function() {
$$('dd.LSform-errors').each(function(dd) {
dd.destroy();
});
$$('dt.LSform-errors').each(function(dt) {
dt.removeClass('LSform-errors');
});
$$('li.LSform_layout_errors').each(function(li) {
li.removeClass('LSform_layout_errors');
});
},
addError: function(errors,name) {
var ul = $(name);
if ($type(ul)) {
errors = new Array(errors);
errors.each(function(txt){
var dd = new Element('dd');
dd.addClass('LSform');
dd.addClass('LSform-errors');
dd.set('html',txt);
dd.injectAfter(this.getParent());
},ul);
var dt = ul.getParent().getPrevious('dt');
dt.addClass('LSform-errors');
var layout = ul.getParent('div.LSform_layout_active');
if ($type(layout)) {
var li = document.getElement('li.LSform_layout[title='+layout.title+']');
if($type(li)) {
li.addClass('LSform_layout_errors');
}
}
}
} }
}); });
window.addEvent(window.ie ? 'load' : 'domready', function() { window.addEvent(window.ie ? 'load' : 'domready', function() {

View file

@ -59,12 +59,38 @@ if($LSsession -> startLSsession()) {
else { else {
$GLOBALS['LSsession'] -> addInfo(_("L'objet a bien été modifié.")); $GLOBALS['LSsession'] -> addInfo(_("L'objet a bien été modifié."));
} }
if (isset($_REQUEST['ajax'])) {
$GLOBALS['LSsession'] -> displayAjaxReturn (
array(
'LSformRedirect' => 'view.php?LSobject='.$LSobject.'&dn='.$object -> getDn()
)
);
}
else {
if ((!LSdebugDefined()) && !$GLOBALS['LSerror']->errorsDefined()) { if ((!LSdebugDefined()) && !$GLOBALS['LSerror']->errorsDefined()) {
$GLOBALS['LSsession'] -> redirect('view.php?LSobject='.$LSobject.'&dn='.$object -> getDn()); $GLOBALS['LSsession'] -> redirect('view.php?LSobject='.$LSobject.'&dn='.$object -> getDn());
} }
else {
$GLOBALS['LSsession'] -> displayTemplate();
} }
} }
}
else {
$GLOBALS['LSsession'] -> displayAjaxReturn (
array(
'LSformErrors' => $form -> getErrors()
)
);
}
}
else if (isset($_REQUEST['ajax']) && $form -> definedError()) {
$GLOBALS['LSsession'] -> displayAjaxReturn (
array(
'LSformErrors' => $form -> getErrors()
)
);
}
else {
$LSview_actions[] = array( $LSview_actions[] = array(
'label' => _('Voir'), 'label' => _('Voir'),
'url' =>'view.php?LSobject='.$LSobject.'&dn='.$object -> getDn(), 'url' =>'view.php?LSobject='.$LSobject.'&dn='.$object -> getDn(),
@ -79,13 +105,13 @@ if($LSsession -> startLSsession()) {
); );
} }
$GLOBALS['LSsession'] -> addJSscript('LSsmoothbox.js'); $GLOBALS['LSsession'] -> addJSscript('LSsmoothbox.js');
$GLOBALS['LSsession'] -> addCssFile('LSsmoothbox.css'); $GLOBALS['LSsession'] -> addCssFile('LSsmoothbox.css');
$GLOBALS['Smarty'] -> assign('LSview_actions',$LSview_actions); $GLOBALS['Smarty'] -> assign('LSview_actions',$LSview_actions);
$GLOBALS['LSsession'] -> setTemplate('modify.tpl'); $GLOBALS['LSsession'] -> setTemplate('modify.tpl');
$form -> display(); $form -> display();
$GLOBALS['LSsession'] -> displayTemplate();
}
} }
else { else {
$GLOBALS['LSerror'] -> addErrorCode(1011); $GLOBALS['LSerror'] -> addErrorCode(1011);
@ -106,8 +132,8 @@ if($LSsession -> startLSsession()) {
} }
else { else {
$GLOBALS['LSsession'] -> setTemplate('login.tpl'); $GLOBALS['LSsession'] -> setTemplate('login.tpl');
$GLOBALS['LSsession'] -> displayTemplate();
} }
// Affichage des retours d'erreurs
$GLOBALS['LSsession'] -> displayTemplate();
?> ?>

View file

@ -1,5 +1,5 @@
{include file='top.tpl'} {include file='top.tpl'}
{if $pagetitle != ''}<h1>{$pagetitle}</h1>{/if} {if $pagetitle != ''}<h1 id='LSform_title'>{$pagetitle}</h1>{/if}
{include file='LSform.tpl'} {include file='LSform.tpl'}
{include file='bottom.tpl'} {include file='bottom.tpl'}

View file

@ -1,5 +1,5 @@
{include file='top.tpl'} {include file='top.tpl'}
{if $pagetitle != ''}<h1>{$pagetitle}</h1>{/if} {if $pagetitle != ''}<h1 id='LSform_title'>{$pagetitle}</h1>{/if}
{if $LSview_actions != ''} {if $LSview_actions != ''}
<ul class='LSview-actions'> <ul class='LSview-actions'>
{foreach from=$LSview_actions item=item} {foreach from=$LSview_actions item=item}