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