From 102cf0ea31f052a1daf0c7501769466ceb5c5214 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sun, 9 Nov 2008 23:24:46 +0000 Subject: [PATCH] =?UTF-8?q?-=20LSform=20:=20=09->=20Envoie=20des=20donn?= =?UTF-8?q?=C3=A9es=20via=20une=20requ=C3=AAte=20Ajax=20-=20Correction=20d?= =?UTF-8?q?es=20templates=20et=20des=20proc=C3=A9dures=20modify=20et=20cre?= =?UTF-8?q?ate=20pour=20l'utilisation=20=09de=20l'envoie=20des=20donn?= =?UTF-8?q?=C3=A9es=20via=20Ajax.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trunk/create.php | 33 ++++++++++-- trunk/css/black/LSform.css | 4 ++ trunk/css/default/LSform.css | 4 ++ trunk/includes/js/LSform.js | 82 ++++++++++++++++++++++++++++++ trunk/modify.php | 72 +++++++++++++++++--------- trunk/templates/default/create.tpl | 2 +- trunk/templates/default/modify.tpl | 2 +- 7 files changed, 170 insertions(+), 29 deletions(-) diff --git a/trunk/create.php b/trunk/create.php index a5c5bf43..43af74a8 100644 --- a/trunk/create.php +++ b/trunk/create.php @@ -52,10 +52,36 @@ if($LSsession -> startLSsession()) { if (!$GLOBALS['LSerror']->errorsDefined()) { $GLOBALS['LSsession'] -> addInfo(_("L'objet a bien été ajouté.")); } - if ((!LSdebugDefined()) && !$GLOBALS['LSerror']->errorsDefined()) { - $GLOBALS['LSsession'] -> redirect('view.php?LSobject='.$LSobject.'&dn='.$object -> getDn()); + if (isset($_REQUEST['ajax'])) { + $GLOBALS['LSsession'] -> displayAjaxReturn ( + array( + 'LSformRedirect' => 'view.php?LSobject='.$LSobject.'&dn='.$object -> getDn() + ) + ); + exit(); + } + else { + if ((!LSdebugDefined()) && !$GLOBALS['LSerror']->errorsDefined()) { + $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 $GLOBALS['Smarty'] -> assign('pagetitle',_('Nouveau').' : '.$object -> getLabel()); @@ -78,7 +104,6 @@ if($LSsession -> startLSsession()) { else { $GLOBALS['LSsession'] -> setTemplate('login.tpl'); } - -// Affichage des retours d'erreurs $GLOBALS['LSsession'] -> displayTemplate(); + ?> diff --git a/trunk/css/black/LSform.css b/trunk/css/black/LSform.css index ad5330aa..7741b4db 100644 --- a/trunk/css/black/LSform.css +++ b/trunk/css/black/LSform.css @@ -38,6 +38,10 @@ li.LSform_layout_current { background-color: #433F3A; } +li.LSform_layout_errors { + background-color: #f59a67; +} + h2.LSform_layout_active { display: none; } diff --git a/trunk/css/default/LSform.css b/trunk/css/default/LSform.css index 467360ae..6c9db0b4 100644 --- a/trunk/css/default/LSform.css +++ b/trunk/css/default/LSform.css @@ -38,6 +38,10 @@ li.LSform_layout_current { background-color: #0072B8; } +li.LSform_layout_errors { + background-color: #f59a67; +} + h2.LSform_layout_active { display: none; } diff --git a/trunk/includes/js/LSform.js b/trunk/includes/js/LSform.js index efa5e25d..7907dd02 100644 --- a/trunk/includes/js/LSform.js +++ b/trunk/includes/js/LSform.js @@ -23,6 +23,20 @@ var LSform = new Class({ this._elements[ul.id] = new LSformElement(this,ul.id,ul); }, 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) { @@ -125,6 +139,74 @@ var LSform = new Class({ },this); } 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() { diff --git a/trunk/modify.php b/trunk/modify.php index 58356735..af8c488d 100644 --- a/trunk/modify.php +++ b/trunk/modify.php @@ -59,33 +59,59 @@ if($LSsession -> startLSsession()) { else { $GLOBALS['LSsession'] -> addInfo(_("L'objet a bien été modifié.")); } - if ((!LSdebugDefined()) && !$GLOBALS['LSerror']->errorsDefined()) { - $GLOBALS['LSsession'] -> redirect('view.php?LSobject='.$LSobject.'&dn='.$object -> getDn()); + if (isset($_REQUEST['ajax'])) { + $GLOBALS['LSsession'] -> displayAjaxReturn ( + array( + 'LSformRedirect' => 'view.php?LSobject='.$LSobject.'&dn='.$object -> getDn() + ) + ); + } + else { + if ((!LSdebugDefined()) && !$GLOBALS['LSerror']->errorsDefined()) { + $GLOBALS['LSsession'] -> redirect('view.php?LSobject='.$LSobject.'&dn='.$object -> getDn()); + } + else { + $GLOBALS['LSsession'] -> displayTemplate(); + } } } + else { + $GLOBALS['LSsession'] -> displayAjaxReturn ( + array( + 'LSformErrors' => $form -> getErrors() + ) + ); + } } - - $LSview_actions[] = array( - 'label' => _('Voir'), - 'url' =>'view.php?LSobject='.$LSobject.'&dn='.$object -> getDn(), - 'action' => 'view' - ); - - if ($GLOBALS['LSsession'] -> canRemove($LSobject,$object -> getDn())) { - $LSview_actions[] = array( - 'label' => _('Supprimer'), - 'url' => 'remove.php?LSobject='.$LSobject.'&dn='.$object -> getDn(), - 'action' => 'delete' + else if (isset($_REQUEST['ajax']) && $form -> definedError()) { + $GLOBALS['LSsession'] -> displayAjaxReturn ( + array( + 'LSformErrors' => $form -> getErrors() + ) ); } + else { + $LSview_actions[] = array( + 'label' => _('Voir'), + 'url' =>'view.php?LSobject='.$LSobject.'&dn='.$object -> getDn(), + 'action' => 'view' + ); - - - $GLOBALS['LSsession'] -> addJSscript('LSsmoothbox.js'); - $GLOBALS['LSsession'] -> addCssFile('LSsmoothbox.css'); - $GLOBALS['Smarty'] -> assign('LSview_actions',$LSview_actions); - $GLOBALS['LSsession'] -> setTemplate('modify.tpl'); - $form -> display(); + if ($GLOBALS['LSsession'] -> canRemove($LSobject,$object -> getDn())) { + $LSview_actions[] = array( + 'label' => _('Supprimer'), + 'url' => 'remove.php?LSobject='.$LSobject.'&dn='.$object -> getDn(), + 'action' => 'delete' + ); + } + + $GLOBALS['LSsession'] -> addJSscript('LSsmoothbox.js'); + $GLOBALS['LSsession'] -> addCssFile('LSsmoothbox.css'); + $GLOBALS['Smarty'] -> assign('LSview_actions',$LSview_actions); + $GLOBALS['LSsession'] -> setTemplate('modify.tpl'); + $form -> display(); + $GLOBALS['LSsession'] -> displayTemplate(); + } } else { $GLOBALS['LSerror'] -> addErrorCode(1011); @@ -106,8 +132,8 @@ if($LSsession -> startLSsession()) { } else { $GLOBALS['LSsession'] -> setTemplate('login.tpl'); + $GLOBALS['LSsession'] -> displayTemplate(); } -// Affichage des retours d'erreurs -$GLOBALS['LSsession'] -> displayTemplate(); + ?> diff --git a/trunk/templates/default/create.tpl b/trunk/templates/default/create.tpl index 56198876..34bbfa12 100644 --- a/trunk/templates/default/create.tpl +++ b/trunk/templates/default/create.tpl @@ -1,5 +1,5 @@ {include file='top.tpl'} - {if $pagetitle != ''}

{$pagetitle}

{/if} + {if $pagetitle != ''}

{$pagetitle}

{/if} {include file='LSform.tpl'} {include file='bottom.tpl'} diff --git a/trunk/templates/default/modify.tpl b/trunk/templates/default/modify.tpl index 3c9ab5d1..1318bed3 100644 --- a/trunk/templates/default/modify.tpl +++ b/trunk/templates/default/modify.tpl @@ -1,5 +1,5 @@ {include file='top.tpl'} - {if $pagetitle != ''}

{$pagetitle}

{/if} + {if $pagetitle != ''}

{$pagetitle}

{/if} {if $LSview_actions != ''}