Rewrite getDictionaryValue (Use a cache file and consistent return code)

This commit is contained in:
Laurent Destailleur
2021-10-20 14:41:40 +02:00
parent e4d0bf7a91
commit 4a5293fc9c
4 changed files with 29 additions and 23 deletions

View File

@@ -9563,21 +9563,24 @@ function dol_mimetype($file, $default = 'application/octet-stream', $mode = 0)
}
/**
* Return value from dictionary
* Return the value of a filed into a dictionary for the record $id.
* This also set all the values into a cache for a next search.
*
* @param string $tablename name of dictionary
* @param string $field the value to return
* @param int $id id of line
* @param bool $checkentity add filter on entity
* @param string $rowidfield name of the column rowid
* @return string
* @param string $tablename Name of dictionary
* @param string $field The name of field where to find the value to return
* @param int $id Id of line record
* @param bool $checkentity Add filter on entity
* @param string $rowidfield Name of the column rowid (to use for the filter on $id)
* @return string The value of field $field. This also set $dictvalues cache.
*/
function getDictvalue($tablename, $field, $id, $checkentity = false, $rowidfield = 'rowid')
function getDictionaryValue($tablename, $field, $id, $checkentity = false, $rowidfield = 'rowid')
{
global $dictvalues, $db, $langs;
global $conf, $db;
if (!isset($dictvalues[$tablename])) {
$dictvalues[$tablename] = array();
$dictvalues = (isset($conf->cache['dictvalues_'.$tablename]) ? $conf->cache['dictvalues_'.$tablename] : null);
if (is_null($dictvalues)) {
$dictvalues = array();
$sql = "SELECT * FROM ".$tablename." WHERE 1 = 1"; // Here select * is allowed as it is generic code and we don't have list of fields
if ($checkentity) {
@@ -9587,20 +9590,20 @@ function getDictvalue($tablename, $field, $id, $checkentity = false, $rowidfield
$resql = $db->query($sql);
if ($resql) {
while ($obj = $db->fetch_object($resql)) {
$dictvalues[$tablename][$obj->{$rowidfield}] = $obj;
$dictvalues[$obj->{$rowidfield}] = $obj;
}
} else {
dol_print_error($db);
}
$conf->cache['dictvalues_'.$tablename] = $dictvalues;
}
if (!empty($dictvalues[$tablename][$id])) {
return $dictvalues[$tablename][$id]->{$field}; // Found
} else // Not found
{
if ($id > 0) {
return $id;
}
if (!empty($dictvalues[$id])) {
// Found
return $dictvalues[$id]->{$field};
} else {
// Not found
return '';
}
}