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

@ -15,6 +15,8 @@ Following changes may create regressions for some external modules, but were nec
* Old deprecated module "SimplePOS" has been completely removed. Use module "TakePOS" is you need a Point Of Sale.
* The method static ActionComm::getActions($db, ...) is no more static. Use $actioncomm->getActions(...) instead (without $db param).
* The 'action=delete&file=...' has been replaced with 'action=deletefile&file=...' to avoid confusion with deletion of object lines.
* Method getDictvalue has been renamed into getDictionaryValue to match camel case rule.
***** ChangeLog for 14.0.3 compared to 14.0.2 *****

View File

@ -1974,7 +1974,7 @@ if ($id) {
}
}
} elseif ($value == 'fk_c_exp_tax_cat') {
$valuetoshow = getDictvalue(MAIN_DB_PREFIX.'c_exp_tax_cat', 'label', $valuetoshow);
$valuetoshow = getDictionaryValue(MAIN_DB_PREFIX.'c_exp_tax_cat', 'label', $valuetoshow);
$valuetoshow = $langs->trans($valuetoshow);
} elseif ($tabname[$id] == MAIN_DB_PREFIX.'c_exp_tax_cat') {
$valuetoshow = $langs->trans($valuetoshow);

View File

@ -257,11 +257,12 @@ foreach ($rules as $rule) {
if ($rule->fk_c_type_fees == -1) {
echo $langs->trans('AllExpenseReport');
} else {
$key = getDictvalue(MAIN_DB_PREFIX.'c_type_fees', 'code', $rule->fk_c_type_fees, false, 'id');
if ($key != $langs->trans($key)) {
$key = getDictionaryValue(MAIN_DB_PREFIX.'c_type_fees', 'code', $rule->fk_c_type_fees, false, 'id');
if ($key && $key != $langs->trans($key)) {
echo $langs->trans($key);
} else {
echo $langs->trans(getDictvalue(MAIN_DB_PREFIX.'c_type_fees', 'label', $rule->fk_c_type_fees, false, 'id')); // TODO check to return trans of 'code'
$value = getDictionaryValue(MAIN_DB_PREFIX.'c_type_fees', 'label', $rule->fk_c_type_fees, false, 'id');
echo $langs->trans($value ? $value : 'Undefined'); // TODO check to return trans of 'code'
}
}
}

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 '';
}
}