From e14802e8cb381bd3a8db88123f28fed16bdaced9 Mon Sep 17 00:00:00 2001 From: Regis Houssin <regis@dolibarr.fr> Date: Tue, 12 Oct 2010 17:14:53 +0000 Subject: [PATCH] Test: auto fill zip and town with jquery --- htdocs/core/class/html.formcompany.class.php | 12 ++--- htdocs/lib/ajax.lib.php | 55 ++++++++++++++++++++ htdocs/societe/ajaxziptown.php | 16 ++++-- htdocs/societe/soc.php | 14 +++-- 4 files changed, 82 insertions(+), 15 deletions(-) diff --git a/htdocs/core/class/html.formcompany.class.php b/htdocs/core/class/html.formcompany.class.php index c4855fa5416..41528814026 100644 --- a/htdocs/core/class/html.formcompany.class.php +++ b/htdocs/core/class/html.formcompany.class.php @@ -610,19 +610,19 @@ class FormCompany /** * Retourne la liste deroulante des codes postaux et des villes associƩes */ - function select_zipcode($selected='',$htmlname='zipcode') + function select_zipcode($selected='',$field1='zipcode',$field2='town',$field3='fk_pays') { - print ajax_autocompleter('',$htmlname,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n"; - print '<input id="search_'.$htmlname.'" type="text" name="'.$htmlname.'" size="6" value="'.$selected.'">'."\n"; + print ajax_autocompleter_ziptown($field1,$field2,$field3,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n"; + print '<input id="'.$field1.'" type="text" name="'.$field1.'" size="6" value="'.$selected.'">'."\n"; } /** * Retourne la liste deroulante des villes et des codes postaux associƩs */ - function select_town($selected='',$htmlname='town') + function select_town($selected='',$field1='town',$field2='zipcode',$field3='fk_pays') { - print ajax_autocompleter('',$htmlname,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n"; - print '<input id="search_'.$htmlname.'" type="text" name="'.$htmlname.'" value="'.$selected.'">'."\n"; + print ajax_autocompleter_ziptown($field1,$field2,$field3,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n"; + print '<input id="'.$field1.'" type="text" name="'.$field1.'" value="'.$selected.'">'."\n"; } } diff --git a/htdocs/lib/ajax.lib.php b/htdocs/lib/ajax.lib.php index dc3c857e4c6..331cd90b897 100644 --- a/htdocs/lib/ajax.lib.php +++ b/htdocs/lib/ajax.lib.php @@ -128,6 +128,61 @@ function ajax_autocompleter($selected='',$htmlname,$url,$option='') return $script; } +/** + * \brief Get value of field, do Ajax process and return result + * \param htmlname nom et id du champ + * \param url chemin du fichier de reponse : /chemin/fichier.php + * \return string script complet + */ +function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='') +{ + $script=''; + + $script.= '<script type="text/javascript">'; + $script.= 'jQuery(document).ready(function() { + jQuery("input#'.$field1.'").blur(function() { + //console.log(this.value.length); + if (this.value.length == 0) + { + jQuery("#'.$field1.'").val(""); + jQuery("#'.$field2.'").val(""); + } + }); + jQuery("input#'.$field1.'").autocomplete({ + source: function( request, response ) { + jQuery.get("'.$url.($option?'?'.$option:'').'", { '.$field1.': request.term }, function(data){ + response( jQuery.map( data, function( item ) { + if (data.length == 1) { + jQuery("#'.$field1.'").val(item.value); + jQuery("#'.$field2.'").val(item.field2); + if (item.field3 > 0) { + jQuery("#'.$field3.'").val(ui.item.field3); + } + } + return { + label: item.label, + value: item.value, + field2: item.field2, + field3: item.field3 + } + })); + }, "json"); + }, + dataType: "json", + minLength: 2, + select: function( event, ui ) { + jQuery("#'.$field2.'").val(ui.item.field2); + if (ui.item.field3 > 0) { + jQuery("#'.$field3.'").val(ui.item.field3); + } + } + }); + });'; + $script.= '</script>'; + + return $script; +} + /** * Show an ajax dialog * @param title Title of dialog box diff --git a/htdocs/societe/ajaxziptown.php b/htdocs/societe/ajaxziptown.php index cffdc426b1a..b582c27ab0d 100644 --- a/htdocs/societe/ajaxziptown.php +++ b/htdocs/societe/ajaxziptown.php @@ -95,10 +95,18 @@ if (! empty($_GET['zipcode']) || ! empty($_GET['town'])) { $country = $row['fk_country']?($langs->trans('Country'.$row['country_code'])!='Country'.$row['country_code']?$langs->trans('Country'.$row['country_code']):$row['country']):''; - $row_array['label'] = $row['zip'].' '.$row['town'].' ('.$country.')'; - $row_array['zip'] = $row['zip']; - $row_array['town'] = $row['town']; - $row_array['fk_country'] = $row['fk_country']; + $row_array['label'] = $row['zip'].' '.$row['town'].' ('.$country.')'; + if ($zipcode) + { + $row_array['value'] = $row['zip']; + $row_array['field2'] = $row['town']; + } + if ($town) + { + $row_array['value'] = $row['town']; + $row_array['field2'] = $row['zip']; + } + $row_array['field3'] = $row['fk_country']; array_push($return_arr,$row_array); } diff --git a/htdocs/societe/soc.php b/htdocs/societe/soc.php index 0b17186f0fb..26bb6f95ea1 100644 --- a/htdocs/societe/soc.php +++ b/htdocs/societe/soc.php @@ -3,7 +3,7 @@ * Copyright (C) 2003 Brian Fraval <brian@fraval.org> * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com> - * Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr> + * Copyright (C) 2005-2010 Regis Houssin <regis@dolibarr.fr> * Copyright (C) 2008 Patrick Raguin <patrick.raguin@auguria.net> * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es> * @@ -767,10 +767,14 @@ else print '</textarea></td></tr>'; // Zip / Town - print '<tr><td>'.$langs->trans('Zip').'</td><td><input size="6" type="text" name="cp" value="'.$soc->cp.'">'; - if ($conf->use_javascript_ajax && $conf->global->MAIN_AUTOFILL_TOWNFROMZIP) print ' <input class="button" type="button" name="searchpostalcode" value="'.$langs->trans('FillTownFromZip').'" onclick="autofilltownfromzip_PopupPostalCode(\''.DOL_URL_ROOT.'\',cp.value,ville,pays_id,departement_id)">'; - print '</td>'; - print '<td>'.$langs->trans('Town').'</td><td><input type="text" name="ville" value="'.$soc->ville.'"></td></tr>'; + print '<tr><td>'.$langs->trans('Zip').'</td><td>'; + $formcompany->select_zipcode($soc->cp,'zipcode','town','selectpays_id'); + //print '<input size="6" type="text" name="cp" value="'.$soc->cp.'">'; + //if ($conf->use_javascript_ajax && $conf->global->MAIN_AUTOFILL_TOWNFROMZIP) print ' <input class="button" type="button" name="searchpostalcode" value="'.$langs->trans('FillTownFromZip').'" onclick="autofilltownfromzip_PopupPostalCode(\''.DOL_URL_ROOT.'\',cp.value,ville,pays_id,departement_id)">'; + print '</td><td>'.$langs->trans('Town').'</td><td>'; + $formcompany->select_zipcode($soc->ville,'town','zipcode','selectpays_id'); + //print '<input type="text" name="ville" value="'.$soc->ville.'">'; + print '</td></tr>'; // Country print '<tr><td width="25%">'.$langs->trans('Country').'</td><td colspan="3">'; -- GitLab