Skip to content
Snippets Groups Projects
Commit f99d5a68 authored by Regis Houssin's avatar Regis Houssin
Browse files

Fix: uniformize function (bug again)

parent 09dd9acc
Branches
No related tags found
No related merge requests found
...@@ -610,19 +610,15 @@ class FormCompany ...@@ -610,19 +610,15 @@ class FormCompany
/** /**
* Retourne la liste deroulante des codes postaux et des villes associées * 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"; global $conf;
print '<input id="'.$field1.'" type="text" name="'.$field1.'" size="6" value="'.$selected.'">'."\n";
} $size='';
if (!empty($fieldsize)) $size='size="'.$fieldsize.'"';
/**
* Retourne la liste deroulante des villes et des codes postaux associés 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";
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";
} }
} }
......
...@@ -134,28 +134,33 @@ function ajax_autocompleter($selected='',$htmlname,$url,$option='') ...@@ -134,28 +134,33 @@ function ajax_autocompleter($selected='',$htmlname,$url,$option='')
* \param url chemin du fichier de reponse : /chemin/fichier.php * \param url chemin du fichier de reponse : /chemin/fichier.php
* \return string script complet * \return string script complet
*/ */
function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='') function ajax_multiautocompleter($htmlname,$fields,$url,$option='')
{ {
$script=''; $script='';
$fields = php2js($fields);
$script.= '<script type="text/javascript">'; $script.= '<script type="text/javascript">';
$script.= 'jQuery(document).ready(function() { $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 ) { 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 ) { response( jQuery.map( data, function( item ) {
if (data.length == 1) { if (data.length == 1) {
jQuery("input#'.$field1.'").val(item.value); jQuery("#'.$htmlname.'").val(item.value);
jQuery("input#'.$field2.'").val(item.field2); for (i=0;i<length;i++) {
if (item.field3 > 0) { if (item[fields[i]]) {
jQuery("#'.$field3.'").val(item.field3); jQuery("#" + fields[i]).val(item[fields[i]]);
}
} }
} }
return { return {
label: item.label, label: item.label,
value: item.value, value: item.value
field2: item.field2,
field3: item.field3
} }
})); }));
}, "json"); }, "json");
...@@ -163,11 +168,12 @@ function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='') ...@@ -163,11 +168,12 @@ function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='')
dataType: "json", dataType: "json",
minLength: 2, minLength: 2,
select: function( event, ui ) { select: function( event, ui ) {
jQuery("input#'.$field1.'").val(ui.item.value); for (i=0;i<length;i++) {
jQuery("input#'.$field2.'").val(ui.item.field2); //alert(fields[i] + " = " + ui.item[fields[i]]);
if (ui.item.field3 > 0) { if (ui.item[fields[i]]) {
jQuery("#'.$field3.'").val(ui.item.field3); jQuery("#" + fields[i]).val(ui.item[fields[i]]);
} }
}
} }
}); });
});'; });';
...@@ -211,4 +217,33 @@ function ajax_dialog($title,$message,$w=350,$h=150) ...@@ -211,4 +217,33 @@ function ajax_dialog($title,$message,$w=350,$h=150)
return $msg; 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
...@@ -99,14 +99,14 @@ if (! empty($_GET['zipcode']) || ! empty($_GET['town'])) ...@@ -99,14 +99,14 @@ if (! empty($_GET['zipcode']) || ! empty($_GET['town']))
if ($zipcode) if ($zipcode)
{ {
$row_array['value'] = $row['zip']; $row_array['value'] = $row['zip'];
$row_array['field2'] = $row['town']; $row_array['town'] = $row['town'];
} }
if ($town) if ($town)
{ {
$row_array['value'] = $row['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); array_push($return_arr,$row_array);
} }
......
...@@ -768,12 +768,9 @@ else ...@@ -768,12 +768,9 @@ else
// Zip / Town // Zip / Town
print '<tr><td>'.$langs->trans('Zip').'</td><td>'; print '<tr><td>'.$langs->trans('Zip').'</td><td>';
$formcompany->select_zipcode($soc->cp,'zipcode','town','selectpays_id'); $formcompany->select_ziptown($soc->cp,'zipcode',array('town','selectpays_id'),6);
//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>'; print '</td><td>'.$langs->trans('Town').'</td><td>';
$formcompany->select_zipcode($soc->ville,'town','zipcode','selectpays_id'); $formcompany->select_ziptown($soc->ville,'town',array('zipcode','selectpays_id'));
//print '<input type="text" name="ville" value="'.$soc->ville.'">';
print '</td></tr>'; print '</td></tr>';
// Country // Country
...@@ -1184,12 +1181,9 @@ else ...@@ -1184,12 +1181,9 @@ else
// Zip / Town // Zip / Town
print '<tr><td>'.$langs->trans('Zip').'</td><td>'; print '<tr><td>'.$langs->trans('Zip').'</td><td>';
$formcompany->select_zipcode($soc->cp,'zipcode','town','selectpays_id'); $formcompany->select_ziptown($soc->cp,'zipcode',array('town','selectpays_id'),6);
//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>'; print '</td><td>'.$langs->trans('Town').'</td><td>';
$formcompany->select_zipcode($soc->ville,'town','zipcode','selectpays_id'); $formcompany->select_ziptown($soc->ville,'town',array('zipcode','selectpays_id'));
//print '<input type="text" name="ville" value="'.$soc->ville.'">';
print '</td></tr>'; print '</td></tr>';
// Country // Country
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment