From f99d5a687c446c78745e5cbf6a461b1946e2fa45 Mon Sep 17 00:00:00 2001 From: Regis Houssin <regis@dolibarr.fr> Date: Wed, 13 Oct 2010 07:38:48 +0000 Subject: [PATCH] Fix: uniformize function (bug again) --- htdocs/core/class/html.formcompany.class.php | 20 +++--- htdocs/lib/ajax.lib.php | 65 +++++++++++++++----- htdocs/societe/ajaxziptown.php | 6 +- htdocs/societe/soc.php | 14 ++--- 4 files changed, 65 insertions(+), 40 deletions(-) diff --git a/htdocs/core/class/html.formcompany.class.php b/htdocs/core/class/html.formcompany.class.php index 0ca46e0f6b5..5b82d235968 100644 --- a/htdocs/core/class/html.formcompany.class.php +++ b/htdocs/core/class/html.formcompany.class.php @@ -610,19 +610,15 @@ class FormCompany /** * Retourne la liste deroulante des codes postaux et des villes associées */ - function select_zipcode($selected='',$field1='zipcode',$field2='town',$field3='fk_pays') + function select_ziptown($selected='',$htmlname='zipcode',$fields='',$fieldsize=0) { - 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='',$field1='town',$field2='zipcode',$field3='fk_pays') - { - 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"; + global $conf; + + $size=''; + if (!empty($fieldsize)) $size='size="'.$fieldsize.'"'; + + if ($conf->use_javascript_ajax) print ajax_multiautocompleter($htmlname,$fields,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n"; + print '<input id="'.$htmlname.'" type="text" name="'.$htmlname.'" '.$size.' value="'.$selected.'">'."\n"; } } diff --git a/htdocs/lib/ajax.lib.php b/htdocs/lib/ajax.lib.php index c5d249a4c2b..d7ab2a135d3 100644 --- a/htdocs/lib/ajax.lib.php +++ b/htdocs/lib/ajax.lib.php @@ -134,28 +134,33 @@ function ajax_autocompleter($selected='',$htmlname,$url,$option='') * \param url chemin du fichier de reponse : /chemin/fichier.php * \return string script complet */ -function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='') +function ajax_multiautocompleter($htmlname,$fields,$url,$option='') { $script=''; + + $fields = php2js($fields); $script.= '<script type="text/javascript">'; $script.= 'jQuery(document).ready(function() { - jQuery("input#'.$field1.'").autocomplete({ + var fields = '.$fields.'; + var length = fields.length; + //alert(fields + " " + length); + + jQuery("input#'.$htmlname.'").autocomplete({ source: function( request, response ) { - jQuery.get("'.$url.($option?'?'.$option:'').'", { '.$field1.': request.term }, function(data){ + jQuery.get("'.$url.($option?'?'.$option:'').'", { '.$htmlname.': request.term }, function(data){ response( jQuery.map( data, function( item ) { if (data.length == 1) { - jQuery("input#'.$field1.'").val(item.value); - jQuery("input#'.$field2.'").val(item.field2); - if (item.field3 > 0) { - jQuery("#'.$field3.'").val(item.field3); + jQuery("#'.$htmlname.'").val(item.value); + for (i=0;i<length;i++) { + if (item[fields[i]]) { + jQuery("#" + fields[i]).val(item[fields[i]]); + } } } return { label: item.label, - value: item.value, - field2: item.field2, - field3: item.field3 + value: item.value } })); }, "json"); @@ -163,11 +168,12 @@ function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='') dataType: "json", minLength: 2, select: function( event, ui ) { - jQuery("input#'.$field1.'").val(ui.item.value); - jQuery("input#'.$field2.'").val(ui.item.field2); - if (ui.item.field3 > 0) { - jQuery("#'.$field3.'").val(ui.item.field3); - } + for (i=0;i<length;i++) { + //alert(fields[i] + " = " + ui.item[fields[i]]); + if (ui.item[fields[i]]) { + jQuery("#" + fields[i]).val(ui.item[fields[i]]); + } + } } }); });'; @@ -211,4 +217,33 @@ function ajax_dialog($title,$message,$w=350,$h=150) return $msg; } +/** + * + * Enter description here ... + * @param unknown_type $var + */ +function php2js($var) +{ + if (is_array($var)) { + $res = "["; + $array = array(); + foreach ($var as $a_var) { + $array[] = php2js($a_var); + } + return "[" . join(",", $array) . "]"; + } + elseif (is_bool($var)) { + return $var ? "true" : "false"; + } + elseif (is_int($var) || is_integer($var) || is_double($var) || is_float($var)) { + return $var; + } + elseif (is_string($var)) { + return "\"" . addslashes(stripslashes($var)) . "\""; + } + // autres cas: objets, on ne les gère pas + return FALSE; +} + + ?> \ No newline at end of file diff --git a/htdocs/societe/ajaxziptown.php b/htdocs/societe/ajaxziptown.php index b582c27ab0d..e7125326f70 100644 --- a/htdocs/societe/ajaxziptown.php +++ b/htdocs/societe/ajaxziptown.php @@ -99,14 +99,14 @@ if (! empty($_GET['zipcode']) || ! empty($_GET['town'])) if ($zipcode) { $row_array['value'] = $row['zip']; - $row_array['field2'] = $row['town']; + $row_array['town'] = $row['town']; } if ($town) { $row_array['value'] = $row['town']; - $row_array['field2'] = $row['zip']; + $row_array['zipcode'] = $row['zip']; } - $row_array['field3'] = $row['fk_country']; + $row_array['selectpays_id'] = $row['fk_country']; array_push($return_arr,$row_array); } diff --git a/htdocs/societe/soc.php b/htdocs/societe/soc.php index 4cc24a0bc0d..0c65269bf5f 100644 --- a/htdocs/societe/soc.php +++ b/htdocs/societe/soc.php @@ -768,12 +768,9 @@ else // Zip / Town 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)">'; + $formcompany->select_ziptown($soc->cp,'zipcode',array('town','selectpays_id'),6); 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.'">'; + $formcompany->select_ziptown($soc->ville,'town',array('zipcode','selectpays_id')); print '</td></tr>'; // Country @@ -1184,12 +1181,9 @@ else // Zip / Town 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)">'; + $formcompany->select_ziptown($soc->cp,'zipcode',array('town','selectpays_id'),6); 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.'">'; + $formcompany->select_ziptown($soc->ville,'town',array('zipcode','selectpays_id')); print '</td></tr>'; // Country -- GitLab