Compare commits

...

3 Commits

Author SHA1 Message Date
XavierGR
1c2f923811 Fix missing dot 2025-04-18 11:17:54 +02:00
XavierGR
24b2852830 Log warning when integer is set to invalid value 2025-04-18 11:16:52 +02:00
XavierGR
5ba44a03ea Add computed tag for attribute computation from OQL Expression 2025-04-18 08:04:38 +02:00
4 changed files with 67 additions and 8 deletions

View File

@@ -181,6 +181,23 @@ abstract class AttributeDefinition
return $this->GetSearchType() != static::SEARCH_WIDGET_TYPE_RAW;
}
/**
* @return bool
*/
public function IsComputed()
{
return $this->IsParam('expression');
}
/**
* @return array
* @throws \OQLException
*/
protected function GetComputedPrerequisiteAttributes(): array {
$oExpression = Expression::FromOQL($this->m_aParams['expression']);
return $oExpression->ListRequiredFields();
}
/** @var string */
protected $m_sCode;
/** @var array */
@@ -2717,7 +2734,11 @@ class AttributeDBFieldVoid extends AttributeDefinition
public function GetPrerequisiteAttributes($sClass = null)
{
return $this->Get("depends_on");
$aPrerequisiteAttributes = $this->Get("depends_on");
if($this->IsComputed()) {
$aPrerequisiteAttributes = array_merge($aPrerequisiteAttributes, $this->GetComputedPrerequisiteAttributes());
}
return $aPrerequisiteAttributes;
}
public static function IsBasedOnDBColumns()
@@ -2732,7 +2753,7 @@ class AttributeDBFieldVoid extends AttributeDefinition
public function IsWritable()
{
return !$this->IsMagic();
return !$this->IsMagic() && !$this->IsComputed();
}
public function GetSQLExpr()
@@ -2962,14 +2983,12 @@ class AttributeInteger extends AttributeDBField
public function MakeRealValue($proposedValue, $oHostObj)
{
if (is_null($proposedValue))
if (is_null($proposedValue) || $proposedValue === '')
{
return null;
} elseif(gettype($proposedValue) !== 'integer') {
IssueLog::Warning("Trying to set integer attribute ".$this->GetCode()." to type".gettype($proposedValue).".");
}
if ($proposedValue === '')
{
return null;
} // 0 is transformed into '' !
return (int)$proposedValue;
}

View File

@@ -706,6 +706,8 @@ abstract class DBObject implements iDisplay
}
$this->_Set($sAttCode, $realvalue);
$this->UpdateDependentComputedAttributes($sAttCode);
$this->UpdateMetaAttributes(array($sAttCode));
// The object has changed, reset caches
@@ -6992,5 +6994,28 @@ abstract class DBObject implements iDisplay
{
return array_key_exists($sSection, $this->aContext);
}
/**
* @param string $sAttCode
*
* @return void
* @throws CoreException
* @throws OQLException
* @throws Exception
*/
private function UpdateDependentComputedAttributes(string $sAttCode): void
{
foreach (MetaModel::GetDependentAttributes(get_class($this), $sAttCode) as $sCode) {
$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sCode);
if ($oAttDef->IsComputed()) {
$oExpression = Expression::FromOQL($oAttDef->GetParams()['expression']);
$value = $this->EvaluateExpression($oExpression);
$aAllowedValues = $oAttDef->GetAllowedValues();
if(is_null($aAllowedValues) || in_array($value, $aAllowedValues)) {
$this->_Set($sCode, $oAttDef->MakeRealValue($value, $this));
} ;
}
}
}
}

View File

@@ -2661,7 +2661,7 @@ abstract class MetaModel
*/
public static function GetAttributeFlags($sClass, $sState, $sAttCode)
{
$iFlags = 0; // By default (if no life cycle) no flag at all
$iFlags = 0;
if (self::HasLifecycle($sClass)) {
$aStates = MetaModel::EnumStates($sClass);
if (!array_key_exists($sState, $aStates)) {

View File

@@ -2134,6 +2134,7 @@ EOF
$this->CompileCommonProperty('default_value', $oField, $aParameters, $sModuleRelativeDir, '');
$this->CompileCommonProperty('is_null_allowed', $oField, $aParameters, $sModuleRelativeDir, false);
$this->CompileCommonProperty('allowed_values', $oField, $aParameters, $sModuleRelativeDir);
$this->CompileCommonProperty('computed', $oField, $aParameters, $sModuleRelativeDir, false);
$aParameters['depends_on'] = $sDependencies;
} elseif ($sAttType == 'AttributeEnum') {
$this->CompileAttributeEnumValues($sModuleRelativeDir, $sClass, $sAttCode, $oField, $aParameters, $sCss);
@@ -2141,6 +2142,7 @@ EOF
$this->CompileCommonProperty('sql', $oField, $aParameters, $sModuleRelativeDir);
$this->CompileCommonProperty('default_value', $oField, $aParameters, $sModuleRelativeDir, '');
$this->CompileCommonProperty('is_null_allowed', $oField, $aParameters, $sModuleRelativeDir, false);
$this->CompileCommonProperty('computed', $oField, $aParameters, $sModuleRelativeDir, false);
$aParameters['depends_on'] = $sDependencies;
} elseif ($sAttType == 'AttributeMetaEnum') {
$this->CompileAttributeEnumValues($sModuleRelativeDir, $sClass, $sAttCode, $oField, $aParameters, $sCss);
@@ -2269,6 +2271,7 @@ EOF
$this->CompileCommonProperty('is_null_allowed', $oField, $aParameters, $sModuleRelativeDir, false);
$this->CompileCommonProperty('default_value', $oField, $aParameters, $sModuleRelativeDir, '');
$this->CompileCommonProperty('allowed_values', $oField, $aParameters, $sModuleRelativeDir);
$this->CompileCommonProperty('computed', $oField, $aParameters, $sModuleRelativeDir, false);
$aParameters['depends_on'] = $sDependencies;
}
@@ -2413,7 +2416,19 @@ EOF
}
$aParameters['thresholds'] = 'array('.implode(', ', $aThresholds).')';
break;
case 'computed':
$oComputed = $oField->GetOptionalElement('computed');
if(is_null($oComputed)) {
break;
}
$sExpression = self::QuoteForPHP($oComputed->GetChildText('expression'));
if(is_null($sExpression) || $sExpression === '') {
throw new DOMFormatException("missing (or empty) mandatory tag expression under the tag '".$oField->nodeName."'");
}
$aParameters['expression'] = $sExpression;
break;
default:
return false;
}