N°9759 - Truncate AttributeText don't work as expected in case of multibytes characters

This commit is contained in:
Anne-Cath
2026-07-07 11:39:01 +02:00
parent 4b4fe55060
commit 89384852c1
2 changed files with 30 additions and 7 deletions

View File

@@ -897,6 +897,19 @@ abstract class AttributeDefinition
return null;
}
public function TrimValue(string $sValue)
{
$iMaxSize = $this->GetMaxSize();
$sLength = mb_strlen($sValue);
if ($iMaxSize && ($sLength > $iMaxSize)) {
$sMessage = " -truncated ($sLength chars)";
return mb_substr($sValue, 0, $iMaxSize - mb_strlen($sMessage)).$sMessage;
}
return $sValue;
}
/**
* @return mixed|null
* @deprecated never used
@@ -4219,6 +4232,21 @@ class AttributeText extends AttributeString
return "TEXT".CMDBSource::GetSqlStringColumnDefinition();
}
public function TrimValue(string $sValue)
{
$iMaxSize = $this->GetMaxSize();
$sLength = strlen($sValue);
if ($iMaxSize && ($sLength > $iMaxSize)) {
$sMessage = " -truncated ($sLength chars)";
$sVal = substr($sValue, 0, $iMaxSize - strlen($sMessage));
//executes the "mb_substr" function to ensure a character is not truncated in the middle.
return mb_substr($sValue, 0, mb_strlen($sVal) - 1).$sMessage;
}
return $sValue;
}
public function GetSQLColumns($bFullSpec = false)
{
$aColumns = [];

View File

@@ -732,13 +732,8 @@ abstract class DBObject implements iDisplay
public function SetTrim($sAttCode, $sValue)
{
$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
$iMaxSize = $oAttDef->GetMaxSize();
$sLength = mb_strlen($sValue);
if ($iMaxSize && ($sLength > $iMaxSize)) {
$sMessage = " -truncated ($sLength chars)";
$sValue = mb_substr($sValue, 0, $iMaxSize - mb_strlen($sMessage)).$sMessage;
}
$this->Set($sAttCode, $sValue);
$this->Set($sAttCode, $oAttDef->TrimValue($sValue));
}
/**