1bf9684bcSAndreas Gohr<?php 2bf9684bcSAndreas Gohr 3bf9684bcSAndreas Gohrnamespace dokuwiki\Ui\Media; 4bf9684bcSAndreas Gohr 5bf9684bcSAndreas Gohruse dokuwiki\Media\MediaFile; 6bf9684bcSAndreas Gohr 7bf9684bcSAndreas Gohrclass Display 8bf9684bcSAndreas Gohr{ 9*79b00823SAndreas Gohr /** @var MediaFile */ 10bf9684bcSAndreas Gohr protected $mediaFile; 11bf9684bcSAndreas Gohr 12*79b00823SAndreas Gohr /** @var string should IDs be shown relative to this namespace? Used in search results */ 13*79b00823SAndreas Gohr protected $relativeDisplay = null; 14*79b00823SAndreas 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 54*79b00823SAndreas Gohr /** 55*79b00823SAndreas Gohr * Show IDs relative to this namespace 56*79b00823SAndreas Gohr * 57*79b00823SAndreas Gohr * @param string|null $ns Use null to disable 58*79b00823SAndreas Gohr */ 59*79b00823SAndreas Gohr public function relativeDisplay($ns) 60*79b00823SAndreas Gohr { 61*79b00823SAndreas Gohr $this->relativeDisplay = $ns; 62*79b00823SAndreas Gohr } 63*79b00823SAndreas 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 */ 76bf9684bcSAndreas 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 } 92*79b00823SAndreas Gohr 93*79b00823SAndreas Gohr /** @return string */ 94*79b00823SAndreas Gohr protected function formatDisplayName() 95*79b00823SAndreas Gohr { 96*79b00823SAndreas Gohr if ($this->relativeDisplay !== null) { 97*79b00823SAndreas Gohr $id = $this->mediaFile->getId(); 98*79b00823SAndreas Gohr if (substr($id, 0, strlen($this->relativeDisplay)) == $this->relativeDisplay) { 99*79b00823SAndreas Gohr $id = substr($id, strlen($this->relativeDisplay)); 100*79b00823SAndreas Gohr } 101*79b00823SAndreas Gohr return ltrim($id, ':'); 102*79b00823SAndreas Gohr } else { 103*79b00823SAndreas Gohr return $this->mediaFile->getDisplayName(); 104*79b00823SAndreas Gohr } 105*79b00823SAndreas Gohr } 106bf9684bcSAndreas Gohr} 107