Fix: uniformize function (bug again)

This commit is contained in:
Regis Houssin
2010-10-13 07:38:48 +00:00
parent 09dd9acc67
commit f99d5a687c
4 changed files with 65 additions and 40 deletions

View File

@@ -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";
}
global $conf;
/**
* 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";
$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";
}
}

View File

@@ -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,10 +168,11 @@ 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;
}
?>

View File

@@ -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);
}

View File

@@ -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