Display.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace dokuwiki\Ui\Media;
  3. use dokuwiki\File\MediaFile;
  4. class Display
  5. {
  6. /** @var MediaFile */
  7. protected $mediaFile;
  8. /** @var string should IDs be shown relative to this namespace? Used in search results */
  9. protected $relativeDisplay = null;
  10. /** @var bool scroll to this file on display? */
  11. protected $scrollIntoView = false;
  12. /**
  13. * Display constructor.
  14. * @param MediaFile $mediaFile
  15. */
  16. public function __construct(MediaFile $mediaFile)
  17. {
  18. $this->mediaFile = $mediaFile;
  19. }
  20. /**
  21. * Get the HTML to display a preview image if possible, otherwise show an icon
  22. *
  23. * @param int $w bounding box width to resize pixel based images to
  24. * @param int $h bounding box height to resize pixel based images to
  25. * @return string
  26. */
  27. public function getPreviewHtml($w, $h)
  28. {
  29. if ($this->mediaFile->isImage()) {
  30. $src = ml($this->mediaFile->getId(), ['w' => $w, 'h' => $h]);
  31. } else {
  32. $src = $this->getIconUrl();
  33. }
  34. $attr = [
  35. 'alt' => $this->mediaFile->getDisplayName(),
  36. 'loading' => 'lazy',
  37. 'width' => $w,
  38. 'height' => $h,
  39. ];
  40. return '<img src="' . $src . '" ' . buildAttributes($attr) . ' />';
  41. }
  42. /**
  43. * Return the URL to the icon for this file
  44. *
  45. * @return string
  46. */
  47. public function getIconUrl()
  48. {
  49. $link = 'lib/images/fileicons/svg/' . $this->mediaFile->getIcoClass() . '.svg';
  50. if (!file_exists(DOKU_INC . $link)) $link = 'lib/images/fileicons/svg/file.svg';
  51. return DOKU_BASE . $link;
  52. }
  53. /**
  54. * Show IDs relative to this namespace
  55. *
  56. * @param string|null $ns Use null to disable
  57. */
  58. public function relativeDisplay($ns)
  59. {
  60. $this->relativeDisplay = $ns;
  61. }
  62. /**
  63. * Scroll to this file on display?
  64. *
  65. * @param bool $set
  66. */
  67. public function scrollIntoView($set = true)
  68. {
  69. $this->scrollIntoView = $set;
  70. }
  71. /** @return string */
  72. protected function formatDate()
  73. {
  74. return dformat($this->mediaFile->getLastModified());
  75. }
  76. /**
  77. * Output the image dimension if any
  78. *
  79. * @param string $empty what to show when no dimensions are available
  80. * @return string
  81. */
  82. protected function formatDimensions($empty = '&#160;')
  83. {
  84. $w = $this->mediaFile->getWidth();
  85. $h = $this->mediaFile->getHeight();
  86. if ($w && $h) {
  87. return $w . '&#215;' . $h;
  88. } else {
  89. return $empty;
  90. }
  91. }
  92. /** @return string */
  93. protected function formatFileSize()
  94. {
  95. return filesize_h($this->mediaFile->getFileSize());
  96. }
  97. /** @return string */
  98. protected function formatDisplayName()
  99. {
  100. if ($this->relativeDisplay !== null) {
  101. $id = $this->mediaFile->getId();
  102. if (substr($id, 0, strlen($this->relativeDisplay)) == $this->relativeDisplay) {
  103. $id = substr($id, strlen($this->relativeDisplay));
  104. }
  105. return ltrim($id, ':');
  106. } else {
  107. return $this->mediaFile->getDisplayName();
  108. }
  109. }
  110. }