1bf9684bcSAndreas Gohr<?php 2bf9684bcSAndreas Gohr 3bf9684bcSAndreas Gohrnamespace dokuwiki\Ui\Media; 4bf9684bcSAndreas Gohr 5bf9684bcSAndreas Gohruse dokuwiki\Media\MediaFile; 6bf9684bcSAndreas Gohr 7bf9684bcSAndreas Gohrclass Display 8bf9684bcSAndreas Gohr{ 979b00823SAndreas Gohr /** @var MediaFile */ 10bf9684bcSAndreas Gohr protected $mediaFile; 11bf9684bcSAndreas Gohr 1279b00823SAndreas Gohr /** @var string should IDs be shown relative to this namespace? Used in search results */ 1379b00823SAndreas Gohr protected $relativeDisplay = null; 1479b00823SAndreas Gohr 15bf9684bcSAndreas Gohr /** 16bf9684bcSAndreas Gohr * Display constructor. 17bf9684bcSAndreas Gohr * @param MediaFile $mediaFile 18bf9684bcSAndreas Gohr */ 19bf9684bcSAndreas Gohr public function __construct(MediaFile $mediaFile) 20bf9684bcSAndreas Gohr { 21bf9684bcSAndreas Gohr $this->mediaFile = $mediaFile; 22bf9684bcSAndreas Gohr } 23bf9684bcSAndreas Gohr 24bf9684bcSAndreas Gohr /** 25bf9684bcSAndreas Gohr * Get the HTML to display a preview image if possible, otherwise show an icon 26bf9684bcSAndreas Gohr * 27bf9684bcSAndreas Gohr * @param int $w bounding box width to resize pixel based images to 28bf9684bcSAndreas Gohr * @param int $h bounding box height to resize pixel based images to 29bf9684bcSAndreas Gohr * @return string 30bf9684bcSAndreas Gohr */ 31bf9684bcSAndreas Gohr public function getPreviewHtml($w, $h) 32bf9684bcSAndreas Gohr { 33bf9684bcSAndreas Gohr if ($this->mediaFile->isImage()) { 344f33babfSAndreas Gohr $src = ml($this->mediaFile->getId(), ['w' => $w, 'h' => $h]); 35bf9684bcSAndreas Gohr } else { 36bf9684bcSAndreas Gohr $src = $this->getIconUrl(); 37bf9684bcSAndreas Gohr } 38bf9684bcSAndreas Gohr 39bf9684bcSAndreas Gohr return '<img src="' . $src . '" alt="' . hsc($this->mediaFile->getDisplayName()) . '" loading="lazy" />'; 40bf9684bcSAndreas Gohr } 41bf9684bcSAndreas Gohr 42bf9684bcSAndreas Gohr /** 43bf9684bcSAndreas Gohr * Return the URL to the icon for this file 44bf9684bcSAndreas Gohr * 45bf9684bcSAndreas Gohr * @return string 46bf9684bcSAndreas Gohr */ 47bf9684bcSAndreas Gohr public function getIconUrl() 48bf9684bcSAndreas Gohr { 49bf9684bcSAndreas Gohr $link = 'lib/images/fileicons/svg/' . $this->mediaFile->getIcoClass() . '.svg'; 50bf9684bcSAndreas Gohr if (!file_exists(DOKU_INC . $link)) $link = 'lib/images/fileicons/svg/file.svg'; 51bf9684bcSAndreas Gohr return DOKU_BASE . $link; 52bf9684bcSAndreas Gohr } 53bf9684bcSAndreas Gohr 5479b00823SAndreas Gohr /** 5579b00823SAndreas Gohr * Show IDs relative to this namespace 5679b00823SAndreas Gohr * 5779b00823SAndreas Gohr * @param string|null $ns Use null to disable 5879b00823SAndreas Gohr */ 5979b00823SAndreas Gohr public function relativeDisplay($ns) 6079b00823SAndreas Gohr { 6179b00823SAndreas Gohr $this->relativeDisplay = $ns; 6279b00823SAndreas Gohr } 6379b00823SAndreas Gohr 64bf9684bcSAndreas Gohr /** @return string */ 65bf9684bcSAndreas Gohr protected function formatDate() 66bf9684bcSAndreas Gohr { 67bf9684bcSAndreas Gohr return dformat($this->mediaFile->getLastModified()); 68bf9684bcSAndreas Gohr } 69bf9684bcSAndreas Gohr 70bf9684bcSAndreas Gohr /** 71bf9684bcSAndreas Gohr * Output the image dimension if any 72bf9684bcSAndreas Gohr * 73bf9684bcSAndreas Gohr * @param string $empty what to show when no dimensions are available 74bf9684bcSAndreas Gohr * @return string 75bf9684bcSAndreas Gohr */ 76*9a1a86fbSAndreas Gohr protected function formatDimensions($empty = ' ') 77bf9684bcSAndreas Gohr { 78bf9684bcSAndreas Gohr $w = $this->mediaFile->getWidth(); 79bf9684bcSAndreas Gohr $h = $this->mediaFile->getHeight(); 80bf9684bcSAndreas Gohr if ($w && $h) { 81bf9684bcSAndreas Gohr return $w . '×' . $h; 82bf9684bcSAndreas Gohr } else { 83bf9684bcSAndreas Gohr return $empty; 84bf9684bcSAndreas Gohr } 85bf9684bcSAndreas Gohr } 86bf9684bcSAndreas Gohr 87bf9684bcSAndreas Gohr /** @return string */ 88bf9684bcSAndreas Gohr protected function formatFileSize() 89bf9684bcSAndreas Gohr { 90bf9684bcSAndreas Gohr return filesize_h($this->mediaFile->getFileSize()); 91bf9684bcSAndreas Gohr } 9279b00823SAndreas Gohr 9379b00823SAndreas Gohr /** @return string */ 9479b00823SAndreas Gohr protected function formatDisplayName() 9579b00823SAndreas Gohr { 9679b00823SAndreas Gohr if ($this->relativeDisplay !== null) { 9779b00823SAndreas Gohr $id = $this->mediaFile->getId(); 9879b00823SAndreas Gohr if (substr($id, 0, strlen($this->relativeDisplay)) == $this->relativeDisplay) { 9979b00823SAndreas Gohr $id = substr($id, strlen($this->relativeDisplay)); 10079b00823SAndreas Gohr } 10179b00823SAndreas Gohr return ltrim($id, ':'); 10279b00823SAndreas Gohr } else { 10379b00823SAndreas Gohr return $this->mediaFile->getDisplayName(); 10479b00823SAndreas Gohr } 10579b00823SAndreas Gohr } 106bf9684bcSAndreas Gohr} 107