xref: /dokuwiki/inc/Ui/Media/Display.php (revision 79b008230541e34a988cd3273ba7267a1eef6d28)
1<?php
2
3namespace dokuwiki\Ui\Media;
4
5use dokuwiki\Media\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 = null;
14
15    /**
16     * Display constructor.
17     * @param MediaFile $mediaFile
18     */
19    public function __construct(MediaFile $mediaFile)
20    {
21        $this->mediaFile = $mediaFile;
22    }
23
24    /**
25     * Get the HTML to display a preview image if possible, otherwise show an icon
26     *
27     * @param int $w bounding box width to resize pixel based images to
28     * @param int $h bounding box height to resize pixel based images to
29     * @return string
30     */
31    public function getPreviewHtml($w, $h)
32    {
33        if ($this->mediaFile->isImage()) {
34            $src = ml($this->mediaFile->getId(), ['w' => $w, 'h' => $h]);
35        } else {
36            $src = $this->getIconUrl();
37        }
38
39        return '<img src="' . $src . '" alt="' . hsc($this->mediaFile->getDisplayName()) . '" loading="lazy" />';
40    }
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    /**
55     * Show IDs relative to this namespace
56     *
57     * @param string|null $ns Use null to disable
58     */
59    public function relativeDisplay($ns)
60    {
61        $this->relativeDisplay = $ns;
62    }
63
64    /** @return string */
65    protected function formatDate()
66    {
67        return dformat($this->mediaFile->getLastModified());
68    }
69
70    /**
71     * Output the image dimension if any
72     *
73     * @param string $empty what to show when no dimensions are available
74     * @return string
75     */
76    protected function formatDimensions($empty = '&#160')
77    {
78        $w = $this->mediaFile->getWidth();
79        $h = $this->mediaFile->getHeight();
80        if ($w && $h) {
81            return $w . '&#215;' . $h;
82        } else {
83            return $empty;
84        }
85    }
86
87    /** @return string */
88    protected function formatFileSize()
89    {
90        return filesize_h($this->mediaFile->getFileSize());
91    }
92
93    /** @return string */
94    protected function formatDisplayName()
95    {
96        if ($this->relativeDisplay !== null) {
97            $id = $this->mediaFile->getId();
98            if (substr($id, 0, strlen($this->relativeDisplay)) == $this->relativeDisplay) {
99                $id = substr($id, strlen($this->relativeDisplay));
100            }
101            return ltrim($id, ':');
102        } else {
103            return $this->mediaFile->getDisplayName();
104        }
105    }
106}
107