From 1f6a51b31fbb01b4efac4d9253f5df5e1d35be79 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Fri, 28 Sep 2018 17:58:14 +0200 Subject: [PATCH] Some refactoring in AttributeImage URL generation --- core/attributedef.class.inc.php | 63 ++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index e2004cd82..6e22a32e6 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -7278,31 +7278,62 @@ class AttributeImage extends AttributeBlob return true; } + /** + * @param \ormDocument $value + * @param \DBObject $oHostObject + * @param bool $bLocalize + * + * @return string + */ public function GetAsHTML($value, $oHostObject = null, $bLocalize = true) { + $sRet = ''; + $iMaxWidthPx = $this->Get('display_max_width').'px'; $iMaxHeightPx = $this->Get('display_max_height').'px'; - $sUrl = $this->Get('default_image'); - $sRet = ($sUrl !== null) ? '' : ''; - if (is_object($value) && !$value->IsEmpty()) - { - if ($oHostObject->IsNew() || ($oHostObject->IsModified() && (array_key_exists($this->GetCode(), - $oHostObject->ListChanges())))) - { - // If the object is modified (or not yet stored in the database) we must serve the content of the image directly inline - // otherwise (if we just give an URL) the browser will be given the wrong content... and may cache it - $sUrl = 'data:'.$value->GetMimeType().';base64,'.base64_encode($value->GetData()); - } - else - { - $sUrl = $value->GetDownloadURL(get_class($oHostObject), $oHostObject->GetKey(), $this->GetCode()); - } - $sRet = ''; + + $sDefaultImageUrl = $this->Get('default_image'); + if ($sDefaultImageUrl !== null) { + $sRet = $this->GetHtmlForImageUrl($sDefaultImageUrl, $iMaxWidthPx, $iMaxHeightPx); + } + + $sCustomImageUrl = $this->GetAttributeImageFileUrl($value, $oHostObject); + if ($sCustomImageUrl !== null) { + $sRet = $this->GetHtmlForImageUrl($sCustomImageUrl, $iMaxWidthPx, $iMaxHeightPx); } return '
'.$sRet.'
'; } + private function GetHtmlForImageUrl($sUrl, $iMaxWidthPx, $iMaxHeightPx) { + return ''; + } + + /** + * @param \ormDocument $value + * @param \DBObject $oHostObject + * + * @return null|string + */ + private function GetAttributeImageFileUrl($value, $oHostObject) { + if (!is_object($value)) { + return null; + } + if ($value->IsEmpty()) { + return null; + } + + $bExistingImageModified = ($oHostObject->IsModified() && (array_key_exists($this->GetCode(), $oHostObject->ListChanges()))); + if ($oHostObject->IsNew() || ($bExistingImageModified)) + { + // If the object is modified (or not yet stored in the database) we must serve the content of the image directly inline + // otherwise (if we just give an URL) the browser will be given the wrong content... and may cache it + return 'data:'.$value->GetMimeType().';base64,'.base64_encode($value->GetData()); + } + + return $value->GetDownloadURL(get_class($oHostObject), $oHostObject->GetKey(), $this->GetCode()); + } + static public function GetFormFieldClass() { return '\\Combodo\\iTop\\Form\\Field\\ImageField';