2
0
forked from Wavyzz/dolibarr
Files
dolibarr-fork/htdocs/lib/ajax.lib.php
2008-07-11 14:37:51 +00:00

139 lines
5.9 KiB
PHP

<?php
/* Copyright (C) 2007 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2007 Regis Houssin <regis@dolibarr.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* or see http://www.gnu.org/
*
* $Id$
*/
function ajax_indicator($htmlname,$indicator='working')
{
$script.='<span id="indicator'.$htmlname.'" style="display: none">'.img_picto('Working...',$indicator.'.gif').'</span>';
return $script;
}
/**
\brief Recupere la valeur d'un champ, effectue un traitement Ajax et affiche le resultat
\param htmlname nom et id du champ
\param keysearch nom et id complementaire du champ de collecte
\param url chemin du fichier de reponse : /chemin/fichier.php
\param option champ supplementaire de recherche dans les parametres
\param indicator Nom de l'image gif sans l'extension
\return script script complet
*/
function ajax_updater($htmlname,$keysearch,$url,$option='',$indicator='working')
{
$script = '<input type="hidden" name="'.$htmlname.'" id="'.$htmlname.'" value="">';
if ($indicator) $script.=ajax_indicator($htmlname,$indicator);
$script.='<script type="text/javascript">';
$script.='var myIndicator'.$htmlname.' = {
onCreate: function(){
if($F("'.$keysearch.$htmlname.'")){
Element.show(\'indicator'.$htmlname.'\');
}
},
onComplete: function() {
if(Ajax.activeRequestCount == 0){
Element.hide(\'indicator'.$htmlname.'\');
}
}
};';
$script.='Ajax.Responders.register(myIndicator'.$htmlname.');';
$script.='new Form.Element.Observer($("'.$keysearch.$htmlname.'"), 1,
function(){
var myAjax = new Ajax.Updater( {
success: \'ajdynfield'.$htmlname.'\'},
\''.DOL_URL_ROOT.$url.'\', {
method: \'get\',
parameters: "'.$keysearch.'="+$F("'.$keysearch.$htmlname.'")+"&htmlname='.$htmlname.$option.'"
});
});';
$script.='</script>';
$script.='<div class="nocellnopadd" id="ajdynfield'.$htmlname.'"></div>';
return $script;
}
/**
\brief Récupére la valeur d'un champ, effectue un traitement Ajax et affiche le résultat
\param htmlname nom et id du champ
\param keysearch nom et id complémentaire du champ de collecte
\param id ID du champ a modifier
\param url chemin du fichier de réponse : /chemin/fichier.php
\param option champ supplémentaire de recherche dans les paramétres
\param indicator Nom de l'image gif sans l'extension
\return script script complet
*/
function ajax_updaterWithID($htmlname,$keysearch,$id,$url,$option='',$indicator='working')
{
$script = '<input type="hidden" name="'.$htmlname.'" id="'.$htmlname.'" value="">';
if ($indicator) $script.=ajax_indicator($htmlname,$indicator);
$script.='<script type="text/javascript">';
$script.='var myIndicator'.$htmlname.' = {
onCreate: function(){
if($F("'.$keysearch.$htmlname.'")){
Element.show(\'indicator'.$htmlname.'\');
}
},
onComplete: function() {
if(Ajax.activeRequestCount == 0){
Element.hide(\'indicator'.$htmlname.'\');
}
}
};';
$script.='Ajax.Responders.register(myIndicator'.$htmlname.');';
$script.='new Form.Element.DelayedObserver($("'.$keysearch.$htmlname.'"), 1,
function(){
var elementHTML = $(\''.$id.'\');
var url = \''.DOL_URL_ROOT.$url.'\';
o_options = new Object();
o_options = {method: \'get\',parameters: "'.$keysearch.'="+$F("'.$keysearch.$htmlname.'")+"'.$option.'"};
var myAjax = new Ajax.Updater(elementHTML,url,o_options);
});';
$script.='</script>';
return $script;
}
/**
\brief Récupére la valeur d'un champ, effectue un traitement Ajax et affiche le résultat
\param htmlname nom et id du champ
\param url chemin du fichier de réponse : /chemin/fichier.php
\param indicator nom de l'image gif sans l'extension
\return script script complet
*/
function ajax_autocompleter($selected='',$htmlname,$url,$indicator='working')
{
if ($indicator) $script.= ajax_indicator($htmlname,$indicator);
$script.= '<input type="hidden" name="'.$htmlname.'_id" id="'.$htmlname.'_id" value="'.$selected.'" />';
$script.= '</div>';
$script.= '<div id="result'.$htmlname.'" class="autocomplete"></div>';
$script.= '<script type="text/javascript">';
$script.= 'new Ajax.Autocompleter(\''.$htmlname.'\',\'result'.$htmlname.'\',\''.DOL_URL_ROOT.$url.'\',{
method: \'post\',
paramName: \''.$htmlname.'\',
indicator: \'indicator'.$htmlname.'\',
afterUpdateElement: ac_return
});';
$script.= '</script>';
return $script;
}
?>