mirror of
https://github.com/Dolibarr/dolibarr.git
synced 2025-12-15 22:11:36 +01:00
Rewrite getDictionaryValue (Use a cache file and consistent return code)
This commit is contained in:
@@ -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 '';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user