From 7d3e96699d0df1ed107fbe59e3635217641492b9 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Mon, 5 Mar 2012 15:50:47 +0100 Subject: [PATCH 1/3] Fix: add load cache for currencies --- htdocs/core/class/html.form.class.php | 108 +++++++++++++++----------- 1 file changed, 63 insertions(+), 45 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 7b153182b93..e7b362c9f64 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -49,6 +49,7 @@ class Form var $cache_availability=array(); var $cache_demand_reason=array(); var $cache_type_fees=array(); + var $cache_currencies=array(); var $tva_taux_value; var $tva_taux_libelle; @@ -2713,6 +2714,50 @@ class Form { print $this->selectcurrency($selected,$htmlname); } + + /** + * Charge dans cache la liste des devises + * + * @return int Nb lignes chargees, 0 si deja chargees, <0 si ko + */ + function load_cache_currencies() + { + global $langs; + + $langs->load("dict"); + + if (count($this->cache_currencies)) return 0; // Cache deja charge + + $sql = "SELECT code, code_iso, label"; + $sql.= " FROM ".MAIN_DB_PREFIX."c_currencies"; + $sql.= " WHERE active = 1"; + $sql.= " ORDER BY code_iso ASC"; + + dol_syslog('Form::load_cache_currencies sql='.$sql, LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + $num = $this->db->num_rows($resql); + $i = 0; + while ($i < $num) + { + $obj = $this->db->fetch_object($resql); + + // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut + $this->cache_currencies[$obj->code_iso] = ($obj->code_iso && $langs->trans("Currency".$obj->code_iso)!="Currency".$obj->code_iso?$langs->trans("Currency".$obj->code_iso):($obj->label!='-'?$obj->label:'')); + $i++; + } + + asort($this->cache_currencies); + + return $num; + } + else + { + dol_print_error($this->db); + return -1; + } + } /** * Retourne la liste des devises, dans la langue de l'utilisateur @@ -2725,58 +2770,31 @@ class Form global $conf,$langs,$user; $langs->load("dict"); + + $this->load_cache_currencies(); $out=''; - $currencyArray=array(); - $label=array(); if ($selected=='euro' || $selected=='euros') $selected='EUR'; // Pour compatibilite - $sql = "SELECT code_iso, label"; - $sql.= " FROM ".MAIN_DB_PREFIX."c_currencies"; - $sql.= " WHERE active = 1"; - $sql.= " ORDER BY code_iso ASC"; - - $resql=$this->db->query($sql); - if ($resql) + $out.= ''; - $num = $this->db->num_rows($resql); - $i = 0; - if ($num) - { - $foundselected=false; - - while ($i < $num) { - $obj = $this->db->fetch_object($resql); - $currencyArray[$i]['code_iso'] = $obj->code_iso; - $currencyArray[$i]['label'] = ($obj->code_iso && $langs->trans("Currency".$obj->code_iso)!="Currency".$obj->code_iso?$langs->trans("Currency".$obj->code_iso):($obj->label!='-'?$obj->label:'')); - $label[$i] = $currencyArray[$i]['label']; - $i++; - } - - array_multisort($label, SORT_ASC, $currencyArray); - - foreach ($currencyArray as $row) { - if ($selected && $selected == $row['code_iso']) { - $foundselected=true; - $out.= ''; - } - } - $out.= ''; - if ($user->admin) $out.= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1); - return $out; - } - else - { - dol_print_error($this->db); + if ($selected && $selected == $code_iso) + { + $out.= ''; } + $out.= ''; + if ($user->admin) $out.= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1); + return $out; } /** From 513a0673d2724436d29ade0f87ed1edeeab5fa73 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Mon, 5 Mar 2012 15:51:44 +0100 Subject: [PATCH 2/3] Fix: remove unused field --- htdocs/core/class/html.form.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index e7b362c9f64..a97265c848f 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -2728,7 +2728,7 @@ class Form if (count($this->cache_currencies)) return 0; // Cache deja charge - $sql = "SELECT code, code_iso, label"; + $sql = "SELECT code_iso, label"; $sql.= " FROM ".MAIN_DB_PREFIX."c_currencies"; $sql.= " WHERE active = 1"; $sql.= " ORDER BY code_iso ASC"; From 38b3f592d2b3b38e66bf7ee916449823d4e982c2 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Mon, 5 Mar 2012 16:01:49 +0100 Subject: [PATCH 3/3] Fix: refactorize with code --- htdocs/core/class/html.form.class.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index a97265c848f..576d10b01bd 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -2728,7 +2728,7 @@ class Form if (count($this->cache_currencies)) return 0; // Cache deja charge - $sql = "SELECT code_iso, label"; + $sql = "SELECT code, code_iso, label"; $sql.= " FROM ".MAIN_DB_PREFIX."c_currencies"; $sql.= " WHERE active = 1"; $sql.= " ORDER BY code_iso ASC"; @@ -2743,12 +2743,14 @@ class Form { $obj = $this->db->fetch_object($resql); - // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut - $this->cache_currencies[$obj->code_iso] = ($obj->code_iso && $langs->trans("Currency".$obj->code_iso)!="Currency".$obj->code_iso?$langs->trans("Currency".$obj->code_iso):($obj->label!='-'?$obj->label:'')); + // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut + $this->cache_currencies[$obj->code]['code_iso'] = $obj->code_iso; + $this->cache_currencies[$obj->code]['label'] = ($obj->code_iso && $langs->trans("Currency".$obj->code_iso)!="Currency".$obj->code_iso?$langs->trans("Currency".$obj->code_iso):($obj->label!='-'?$obj->label:'')); + $label[$obj->code] = $this->cache_currencies[$obj->code]['label']; $i++; } - asort($this->cache_currencies); + array_multisort($label, SORT_ASC, $this->cache_currencies); return $num; } @@ -2778,18 +2780,18 @@ class Form if ($selected=='euro' || $selected=='euros') $selected='EUR'; // Pour compatibilite $out.= '';