From 47d8e3563925e0db90826d5ebb7334a256c74538 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Tue, 17 Dec 2019 09:58:35 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B0330=20Improve=20\ormDocument::GetFormatt?= =?UTF-8?q?edSize=20*=20Fix=20typo=20in=20method=20name=20(many=20thanks?= =?UTF-8?q?=20@jbostoen=20!)=20*=20Use=20\utils::BytesToFriendlyFormat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/utils.inc.php | 9 ++++++--- core/ormdocument.class.inc.php | 18 ++++++++---------- .../renderers.itop-attachments.php | 2 +- .../portal/src/Controller/ObjectController.php | 2 +- .../bsfileuploadfieldrenderer.class.inc.php | 4 +++- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/application/utils.inc.php b/application/utils.inc.php index ef9a22a5e..975bce7a9 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -591,18 +591,21 @@ class utils * Format a value into a more friendly format (KB, MB, GB, TB) instead a juste a Bytes amount. * * @param float $value + * @param int $iPrecision + * * @return string */ - public static function BytesToFriendlyFormat($value) + public static function BytesToFriendlyFormat($value, $iPrecision = 0) { $sReturn = ''; - $iPrecision = 0; // Kilobytes if ($value >= 1024) { $sReturn = 'K'; $value = $value / 1024; - $iPrecision = 1; + if ($iPrecision === 0) { + $iPrecision = 1; + } } // Megabytes if ($value >= 1024) diff --git a/core/ormdocument.class.inc.php b/core/ormdocument.class.inc.php index 0a3322542..aa8b91eba 100644 --- a/core/ormdocument.class.inc.php +++ b/core/ormdocument.class.inc.php @@ -85,18 +85,16 @@ class ormDocument return strlen($this->m_data); } - public function GetFormatedSize($precision = 2) + /** + * @param int $precision + * + * @return string + * @uses utils::BytesToFriendlyFormat() + */ + public function GetFormattedSize($precision = 2) { $bytes = $this->GetSize(); - $units = array('B', 'KB', 'MB', 'GB', 'TB'); - - $bytes = max($bytes, 0); - $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); - $pow = min($pow, count($units) - 1); - - $bytes /= pow(1024, $pow); - - return round($bytes, $precision).' '.$units[$pow]; + return utils::BytesToFriendlyFormat($bytes, $precision); } public function GetData() { diff --git a/datamodels/2.x/itop-attachments/renderers.itop-attachments.php b/datamodels/2.x/itop-attachments/renderers.itop-attachments.php index 9c5e605b8..2dd16e086 100644 --- a/datamodels/2.x/itop-attachments/renderers.itop-attachments.php +++ b/datamodels/2.x/itop-attachments/renderers.itop-attachments.php @@ -445,7 +445,7 @@ CSS $sFileName = utils::HtmlEntities($oDoc->GetFileName()); $sTrId = $this->GetAttachmentContainerId($iAttachmentId); $sAttachmentMeta = $this->GetAttachmentHiddenInput($iAttachmentId, $bIsDeletedAttachment); - $sFileSize = $oDoc->GetFormatedSize(); + $sFileSize = $oDoc->GetFormattedSize(); $bIsTempAttachment = ($oAttachment->Get('item_id') === 0); $sAttachmentDate = ''; if (!$bIsTempAttachment) diff --git a/datamodels/2.x/itop-portal-base/portal/src/Controller/ObjectController.php b/datamodels/2.x/itop-portal-base/portal/src/Controller/ObjectController.php index 24af64e02..e0621e016 100644 --- a/datamodels/2.x/itop-portal-base/portal/src/Controller/ObjectController.php +++ b/datamodels/2.x/itop-portal-base/portal/src/Controller/ObjectController.php @@ -1164,7 +1164,7 @@ class ObjectController extends BrickController $aData['icon'] = utils::GetAbsoluteUrlAppRoot().'env-'.utils::GetCurrentEnvironment().'/itop-attachments/icons/image.png'; $aData['att_id'] = $iAttId; $aData['preview'] = $oDocument->IsPreviewAvailable() ? 'true' : 'false'; - $aData['file_size'] = $oDocument->GetFormatedSize(); + $aData['file_size'] = $oDocument->GetFormattedSize(); $aData['creation_date'] = $oAttachment->Get('creation_date'); $aData['user_id_friendlyname'] = $oAttachment->Get('user_id_friendlyname'); $aData['file_type'] = $oDocument->GetMimeType(); diff --git a/sources/renderer/bootstrap/fieldrenderer/bsfileuploadfieldrenderer.class.inc.php b/sources/renderer/bootstrap/fieldrenderer/bsfileuploadfieldrenderer.class.inc.php index a5153dde7..c8c55bd39 100644 --- a/sources/renderer/bootstrap/fieldrenderer/bsfileuploadfieldrenderer.class.inc.php +++ b/sources/renderer/bootstrap/fieldrenderer/bsfileuploadfieldrenderer.class.inc.php @@ -314,6 +314,7 @@ JS HTML ); + /** @var Attachment $oAttachment */ while ($oAttachment = $oSet->Fetch()) { $iAttId = $oAttachment->GetKey(); @@ -322,6 +323,7 @@ HTML $sAttachmentMeta = ''; + /** @var \ormDocument $oDoc */ $oDoc = $oAttachment->Get('contents'); $sFileName = htmlentities($oDoc->GetFileName(), ENT_QUOTES, 'UTF-8'); @@ -338,7 +340,7 @@ HTML } } - $sFileSize = $oDoc->GetFormatedSize(); + $sFileSize = $oDoc->GetFormattedSize(); $sFileType = $oDoc->GetMimeType(); $bIsTempAttachment = ($oAttachment->Get('item_id') === 0);