diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index afc52329bc..76857d3208 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -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 = []; diff --git a/core/dbobject.class.php b/core/dbobject.class.php index 0db1783d9c..d4b3440ef0 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -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)); } /**