Compare commits

...

1 Commits

Author SHA1 Message Date
rquetiez
1004dd9afb N°1649 - Support blobs and images as external fields 2020-07-03 22:11:11 +02:00
6 changed files with 101 additions and 38 deletions

View File

@@ -742,7 +742,7 @@ abstract class AttributeDefinition
* *
* @return mixed a value out of suffix/value pairs, for SELECT result interpretation * @return mixed a value out of suffix/value pairs, for SELECT result interpretation
*/ */
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
return null; return null;
} }
@@ -2402,9 +2402,9 @@ class AttributeDBFieldVoid extends AttributeDefinition
return $aColumns; return $aColumns;
} }
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
$value = $this->MakeRealValue($aCols[$sPrefix.''], null); $value = $this->MakeRealValue($aCols[$sPrefix.''], $oHostObject);
return $value; return $value;
} }
@@ -3913,7 +3913,7 @@ class AttributeEncryptedString extends AttributeString
* @return string * @return string
* @throws \Exception * @throws \Exception
*/ */
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
$oSimpleCrypt = new SimpleCrypt(self::$sLibrary); $oSimpleCrypt = new SimpleCrypt(self::$sLibrary);
$sValue = $oSimpleCrypt->Decrypt(self::$sKey, $aCols[$sPrefix]); $sValue = $oSimpleCrypt->Decrypt(self::$sKey, $aCols[$sPrefix]);
@@ -4283,7 +4283,7 @@ class AttributeText extends AttributeString
* *
* @return string * @return string
*/ */
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
$value = $aCols[$sPrefix.'']; $value = $aCols[$sPrefix.''];
if ($this->GetOptional('format', null) != null) if ($this->GetOptional('format', null) != null)
@@ -4577,7 +4577,7 @@ class AttributeCaseLog extends AttributeLongText
* @return \ormCaseLog * @return \ormCaseLog
* @throws \MissingColumnException * @throws \MissingColumnException
*/ */
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
if (!array_key_exists($sPrefix, $aCols)) if (!array_key_exists($sPrefix, $aCols))
{ {
@@ -7378,18 +7378,18 @@ class AttributeExternalField extends AttributeDefinition
//public function GetSQLExpressions($sPrefix = '') {return array();} //public function GetSQLExpressions($sPrefix = '') {return array();}
// Here, we get the data... // Here, we get the data...
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
$oExtAttDef = $this->GetExtAttDef(); $oExtAttDef = $this->GetExtAttDef();
return $oExtAttDef->FromSQLToValue($aCols, $sPrefix); return $oExtAttDef->FromSQLToValue($aCols, $sPrefix, $oHostObject, $sHostAttCode);
} }
public function GetAsHTML($value, $oHostObject = null, $bLocalize = true) public function GetAsHTML($value, $oHostObject = null, $bLocalize = true)
{ {
$oExtAttDef = $this->GetExtAttDef(); $oExtAttDef = $this->GetExtAttDef();
return $oExtAttDef->GetAsHTML($value, null, $bLocalize); return $oExtAttDef->GetAsHTML($value, $oHostObject, $bLocalize);
} }
public function GetAsXML($value, $oHostObject = null, $bLocalize = true) public function GetAsXML($value, $oHostObject = null, $bLocalize = true)
@@ -7645,6 +7645,10 @@ class AttributeBlob extends AttributeDefinition
if (is_object($proposedValue)) if (is_object($proposedValue))
{ {
if (!$proposedValue instanceof ormDocument)
{
throw new Exception(__METHOD__.": unexpected class ".get_class($proposedValue));
}
$proposedValue = clone $proposedValue; $proposedValue = clone $proposedValue;
} }
else else
@@ -7661,6 +7665,7 @@ class AttributeBlob extends AttributeDefinition
} }
} }
$proposedValue->SetHostObject($oHostObj, $this->GetCode());
return $proposedValue; return $proposedValue;
} }
@@ -7679,7 +7684,7 @@ class AttributeBlob extends AttributeDefinition
return $aColumns; return $aColumns;
} }
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
if (!array_key_exists($sPrefix, $aCols)) if (!array_key_exists($sPrefix, $aCols))
{ {
@@ -7703,7 +7708,7 @@ class AttributeBlob extends AttributeDefinition
$sFileName = isset($aCols[$sPrefix.'_filename']) ? $aCols[$sPrefix.'_filename'] : ''; $sFileName = isset($aCols[$sPrefix.'_filename']) ? $aCols[$sPrefix.'_filename'] : '';
$value = new ormDocument($data, $sMimeType, $sFileName); $value = new ormDocument($data, $sMimeType, $sFileName);
$value->SetHostObject($oHostObject, $sHostAttCode);
return $value; return $value;
} }
@@ -8219,7 +8224,7 @@ class AttributeStopWatch extends AttributeDefinition
return date("Y-m-d H:i:s", $iSeconds); return date("Y-m-d H:i:s", $iSeconds);
} }
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
$aExpectedCols = array($sPrefix, $sPrefix.'_started', $sPrefix.'_laststart', $sPrefix.'_stopped'); $aExpectedCols = array($sPrefix, $sPrefix.'_started', $sPrefix.'_laststart', $sPrefix.'_stopped');
foreach($this->ListThresholds() as $iThreshold => $aFoo) foreach($this->ListThresholds() as $iThreshold => $aFoo)
@@ -9056,7 +9061,7 @@ class AttributeSubItem extends AttributeDefinition
// //
// protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside) // protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
} }
@@ -9274,7 +9279,7 @@ class AttributeOneWayPassword extends AttributeDefinition
return $aColumns; return $aColumns;
} }
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
if (!array_key_exists($sPrefix, $aCols)) if (!array_key_exists($sPrefix, $aCols))
{ {
@@ -9471,7 +9476,7 @@ class AttributeTable extends AttributeDBField
return $proposedValue; return $proposedValue;
} }
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
try try
{ {
@@ -9486,11 +9491,11 @@ class AttributeTable extends AttributeDBField
} }
if ($value === false) if ($value === false)
{ {
$value = $this->MakeRealValue($aCols[$sPrefix.''], null); $value = $this->MakeRealValue($aCols[$sPrefix.''], $oHostObject);
} }
} catch (Exception $e) } catch (Exception $e)
{ {
$value = $this->MakeRealValue($aCols[$sPrefix.''], null); $value = $this->MakeRealValue($aCols[$sPrefix.''], $oHostObject);
} }
return $value; return $value;
@@ -9885,11 +9890,11 @@ abstract class AttributeSet extends AttributeDBFieldVoid
* @return mixed * @return mixed
* @throws \Exception * @throws \Exception
*/ */
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
$sValue = $aCols["$sPrefix"]; $sValue = $aCols["$sPrefix"];
return $this->MakeRealValue($sValue, null, true); return $this->MakeRealValue($sValue, $oHostObject, true);
} }
/** /**
@@ -11034,7 +11039,7 @@ class AttributeTagSet extends AttributeSet
* @throws \CoreException * @throws \CoreException
* @throws \Exception * @throws \Exception
*/ */
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
$sValue = $aCols["$sPrefix"]; $sValue = $aCols["$sPrefix"];
@@ -11616,7 +11621,7 @@ class AttributeFriendlyName extends AttributeDefinition
return $sLabel; return $sLabel;
} }
public function FromSQLToValue($aCols, $sPrefix = '') public function FromSQLToValue($aCols, $sPrefix = '', $oHostObject = null, $sHostAttCode = null)
{ {
$sValue = $aCols[$sPrefix]; $sValue = $aCols[$sPrefix];

View File

@@ -461,7 +461,7 @@ abstract class DBObject implements iDisplay
if (array_key_exists($sAttRef, $aRow)) if (array_key_exists($sAttRef, $aRow))
{ {
$value = $oAttDef->FromSQLToValue($aRow, $sAttRef); $value = $oAttDef->FromSQLToValue($aRow, $sAttRef, $this, $sAttCode);
$bIsDefined = true; $bIsDefined = true;
} }
} }

View File

@@ -1643,6 +1643,35 @@ class FieldExpression extends UnaryExpression
// Has been resolved into an SQL expression // Has been resolved into an SQL expression
class FieldExpressionResolved extends FieldExpression class FieldExpressionResolved extends FieldExpression
{ {
protected $m_aAdditionalExpressions;
public function __construct($mExpression, $sParent = '')
{
$this->m_aAdditionalExpressions = array();
if (is_array($mExpression))
{
foreach ($mExpression as $sSuffix => $sExpression)
{
if ($sSuffix == '')
{
$sName = $sExpression;
}
$this->m_aAdditionalExpressions[$sSuffix] = new FieldExpressionResolved($sExpression, $sParent);
}
}
else
{
$sName = $mExpression;
}
parent::__construct($sName, $sParent);
}
public function AdditionalExpressions()
{
return $this->m_aAdditionalExpressions;
}
public function GetUnresolvedFields($sAlias, &$aUnresolved) public function GetUnresolvedFields($sAlias, &$aUnresolved)
{ {
} }

View File

@@ -38,6 +38,8 @@ class ormDocument
protected $m_data; protected $m_data;
protected $m_sMimeType; protected $m_sMimeType;
protected $m_sFileName; protected $m_sFileName;
protected $m_oHostObject;
protected $m_sHostObjectAttcode;
/** /**
* Constructor * Constructor
@@ -47,6 +49,15 @@ class ormDocument
$this->m_data = $data; $this->m_data = $data;
$this->m_sMimeType = $sMimeType; $this->m_sMimeType = $sMimeType;
$this->m_sFileName = $sFileName; $this->m_sFileName = $sFileName;
$this->m_oHostObject = null;
$this->m_sHostObjectAttcode = null;
}
public function SetHostObject(DBObject $oHostObject, $sAttCode)
{
$this->m_oHostObject = $oHostObject;
$this->m_sHostObjectAttcode = $sAttCode;
} }
public function __toString() public function __toString()
@@ -138,6 +149,12 @@ class ormDocument
*/ */
public function GetDownloadLink($sClass, $Id, $sAttCode) public function GetDownloadLink($sClass, $Id, $sAttCode)
{ {
if ($this->m_oHostObject)
{
$sClass = get_class($this->m_oHostObject);
$Id = $this->m_oHostObject->GetKey();
$sAttCode = $this->m_sHostObjectAttcode;
}
return "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/ajax.document.php?operation=download_document&class=$sClass&id=$Id&field=$sAttCode\">".htmlentities($this->GetFileName(), ENT_QUOTES, 'UTF-8')."</a>\n"; return "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/ajax.document.php?operation=download_document&class=$sClass&id=$Id&field=$sAttCode\">".htmlentities($this->GetFileName(), ENT_QUOTES, 'UTF-8')."</a>\n";
} }
@@ -147,6 +164,12 @@ class ormDocument
*/ */
public function GetDisplayURL($sClass, $Id, $sAttCode) public function GetDisplayURL($sClass, $Id, $sAttCode)
{ {
if ($this->m_oHostObject)
{
$sClass = get_class($this->m_oHostObject);
$Id = $this->m_oHostObject->GetKey();
$sAttCode = $this->m_sHostObjectAttcode;
}
// TODO: When refactoring this with the URLMaker system, mind to also change calls in the portal (look for the "p_object_document_display" route) // TODO: When refactoring this with the URLMaker system, mind to also change calls in the portal (look for the "p_object_document_display" route)
return utils::GetAbsoluteUrlAppRoot() . "pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode"; return utils::GetAbsoluteUrlAppRoot() . "pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode";
} }
@@ -157,6 +180,12 @@ class ormDocument
*/ */
public function GetDownloadURL($sClass, $Id, $sAttCode) public function GetDownloadURL($sClass, $Id, $sAttCode)
{ {
if ($this->m_oHostObject)
{
$sClass = get_class($this->m_oHostObject);
$Id = $this->m_oHostObject->GetKey();
$sAttCode = $this->m_sHostObjectAttcode;
}
// Compute a signature to reset the cache anytime the data changes (this is acceptable if used only with icon files) // Compute a signature to reset the cache anytime the data changes (this is acceptable if used only with icon files)
$sSignature = md5($this->GetData()); $sSignature = md5($this->GetData());
// TODO: When refactoring this with the URLMaker system, mind to also change calls in the portal (look for the "p_object_document_display" route) // TODO: When refactoring this with the URLMaker system, mind to also change calls in the portal (look for the "p_object_document_display" route)

View File

@@ -178,6 +178,14 @@ class QueryBuilderExpressions
foreach ($this->m_aSelectExpr as $sColAlias => $oExpr) foreach ($this->m_aSelectExpr as $sColAlias => $oExpr)
{ {
$this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved); $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
if ($this->m_aSelectExpr[$sColAlias] instanceof FieldExpressionResolved)
{
// Split the field with the relevant alias
foreach ($this->m_aSelectExpr[$sColAlias]->AdditionalExpressions() as $sSuffix => $oAdditionalExpr)
{
$this->m_aSelectExpr[$sColAlias.$sSuffix] = $oAdditionalExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
}
}
} }
if ($this->m_aGroupByExpr) if ($this->m_aGroupByExpr)
{ {

View File

@@ -239,24 +239,16 @@ class SQLObjectQueryBuilder
continue; continue;
} }
$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode); $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
foreach ($oAttDef->GetSQLExpressions() as $sColId => $sSQLExpr) $oFieldSQLExp = new FieldExpressionResolved($oAttDef->GetSQLExpressions(), $sClassAlias);
/**
* @var string $sPluginClass
* @var iQueryModifier $oQueryModifier
*/
foreach (MetaModel::EnumPlugins('iQueryModifier') as $sPluginClass => $oQueryModifier)
{ {
if (!empty($sColId)) $oFieldSQLExp = $oQueryModifier->GetFieldExpression($oBuild, $sClass, $sAttCode, '', $oFieldSQLExp, $oBaseSQLQuery);
{
// Multi column attributes
$oBuild->m_oQBExpressions->AddSelect($sSelectedClassAlias.$sAttCode.$sColId, new FieldExpression($sAttCode.$sColId, $sClassAlias));
}
$oFieldSQLExp = new FieldExpressionResolved($sSQLExpr, $sClassAlias);
/**
* @var string $sPluginClass
* @var iQueryModifier $oQueryModifier
*/
foreach (MetaModel::EnumPlugins('iQueryModifier') as $sPluginClass => $oQueryModifier)
{
$oFieldSQLExp = $oQueryModifier->GetFieldExpression($oBuild, $sClass, $sAttCode, $sColId, $oFieldSQLExp, $oBaseSQLQuery);
}
$aTranslation[$sClassAlias][$sAttCode.$sColId] = $oFieldSQLExp;
} }
$aTranslation[$sClassAlias][$sAttCode] = $oFieldSQLExp;
} }
// Translate the selected columns // Translate the selected columns