xref: /dokuwiki/inc/Ui/Media/Display.php (revision 6c16a3a9aa602bb7e269fb6d5d18e1353e17f97f)
1bf9684bcSAndreas Gohr<?php
2bf9684bcSAndreas Gohr
3bf9684bcSAndreas Gohrnamespace dokuwiki\Ui\Media;
4bf9684bcSAndreas Gohr
50603f506SAndreas Gohruse dokuwiki\File\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 */
13e2d055f5SAndreas Gohr    protected $relativeDisplay;
1479b00823SAndreas Gohr
159453716dSAndreas Gohr    /** @var bool scroll to this file on display? */
169453716dSAndreas Gohr    protected $scrollIntoView = false;
179453716dSAndreas Gohr
18bf9684bcSAndreas Gohr    /**
19bf9684bcSAndreas Gohr     * Display constructor.
20bf9684bcSAndreas Gohr     * @param MediaFile $mediaFile
21bf9684bcSAndreas Gohr     */
22bf9684bcSAndreas Gohr    public function __construct(MediaFile $mediaFile)
23bf9684bcSAndreas Gohr    {
24bf9684bcSAndreas Gohr        $this->mediaFile = $mediaFile;
25bf9684bcSAndreas Gohr    }
26bf9684bcSAndreas Gohr
27bf9684bcSAndreas Gohr    /**
28bf9684bcSAndreas Gohr     * Get the HTML to display a preview image if possible, otherwise show an icon
29bf9684bcSAndreas Gohr     *
30bf9684bcSAndreas Gohr     * @param int $w bounding box width to resize pixel based images to
31bf9684bcSAndreas Gohr     * @param int $h bounding box height to resize pixel based images to
32bf9684bcSAndreas Gohr     * @return string
33bf9684bcSAndreas Gohr     */
34bf9684bcSAndreas Gohr    public function getPreviewHtml($w, $h)
35bf9684bcSAndreas Gohr    {
36bf9684bcSAndreas Gohr        if ($this->mediaFile->isImage()) {
374f33babfSAndreas Gohr            $src = ml($this->mediaFile->getId(), ['w' => $w, 'h' => $h]);
38bf9684bcSAndreas Gohr        } else {
39bf9684bcSAndreas Gohr            $src = $this->getIconUrl();
40bf9684bcSAndreas Gohr        }
41bf9684bcSAndreas Gohr
428e9d8d55SAndreas Gohr        $attr = [
438e9d8d55SAndreas Gohr            'alt' => $this->mediaFile->getDisplayName(),
448e9d8d55SAndreas Gohr            'loading' => 'lazy',
458e9d8d55SAndreas Gohr            'width' => $w,
468e9d8d55SAndreas Gohr            'height' => $h,
478e9d8d55SAndreas Gohr        ];
488e9d8d55SAndreas Gohr
498e9d8d55SAndreas Gohr        return '<img src="' . $src . '" ' . buildAttributes($attr) . ' />';
50bf9684bcSAndreas Gohr    }
51bf9684bcSAndreas Gohr
52bf9684bcSAndreas Gohr    /**
53bf9684bcSAndreas Gohr     * Return the URL to the icon for this file
54bf9684bcSAndreas Gohr     *
55bf9684bcSAndreas Gohr     * @return string
56bf9684bcSAndreas Gohr     */
57bf9684bcSAndreas Gohr    public function getIconUrl()
58bf9684bcSAndreas Gohr    {
59bf9684bcSAndreas Gohr        $link = 'lib/images/fileicons/svg/' . $this->mediaFile->getIcoClass() . '.svg';
60bf9684bcSAndreas Gohr        if (!file_exists(DOKU_INC . $link)) $link = 'lib/images/fileicons/svg/file.svg';
61bf9684bcSAndreas Gohr        return DOKU_BASE . $link;
62bf9684bcSAndreas Gohr    }
63bf9684bcSAndreas Gohr
6479b00823SAndreas Gohr    /**
6579b00823SAndreas Gohr     * Show IDs relative to this namespace
6679b00823SAndreas Gohr     *
6779b00823SAndreas Gohr     * @param string|null $ns Use null to disable
6879b00823SAndreas Gohr     */
6979b00823SAndreas Gohr    public function relativeDisplay($ns)
7079b00823SAndreas Gohr    {
7179b00823SAndreas Gohr        $this->relativeDisplay = $ns;
7279b00823SAndreas Gohr    }
7379b00823SAndreas Gohr
749453716dSAndreas Gohr    /**
759453716dSAndreas Gohr     * Scroll to this file on display?
769453716dSAndreas Gohr     *
779453716dSAndreas Gohr     * @param bool $set
789453716dSAndreas Gohr     */
799453716dSAndreas Gohr    public function scrollIntoView($set = true)
809453716dSAndreas Gohr    {
819453716dSAndreas Gohr        $this->scrollIntoView = $set;
829453716dSAndreas Gohr    }
839453716dSAndreas Gohr
84bf9684bcSAndreas Gohr    /** @return string */
85bf9684bcSAndreas Gohr    protected function formatDate()
86bf9684bcSAndreas Gohr    {
87bf9684bcSAndreas Gohr        return dformat($this->mediaFile->getLastModified());
88bf9684bcSAndreas Gohr    }
89bf9684bcSAndreas Gohr
90bf9684bcSAndreas Gohr    /**
91bf9684bcSAndreas Gohr     * Output the image dimension if any
92bf9684bcSAndreas Gohr     *
93bf9684bcSAndreas Gohr     * @param string $empty what to show when no dimensions are available
94bf9684bcSAndreas Gohr     * @return string
95bf9684bcSAndreas Gohr     */
969a1a86fbSAndreas Gohr    protected function formatDimensions($empty = '&#160;')
97bf9684bcSAndreas Gohr    {
98bf9684bcSAndreas Gohr        $w = $this->mediaFile->getWidth();
99bf9684bcSAndreas Gohr        $h = $this->mediaFile->getHeight();
100bf9684bcSAndreas Gohr        if ($w && $h) {
101bf9684bcSAndreas Gohr            return $w . '&#215;' . $h;
102bf9684bcSAndreas Gohr        } else {
103bf9684bcSAndreas Gohr            return $empty;
104bf9684bcSAndreas Gohr        }
105bf9684bcSAndreas Gohr    }
106bf9684bcSAndreas Gohr
107bf9684bcSAndreas Gohr    /** @return string */
108bf9684bcSAndreas Gohr    protected function formatFileSize()
109bf9684bcSAndreas Gohr    {
110bf9684bcSAndreas Gohr        return filesize_h($this->mediaFile->getFileSize());
111bf9684bcSAndreas Gohr    }
11279b00823SAndreas Gohr
11379b00823SAndreas Gohr    /** @return string */
11479b00823SAndreas Gohr    protected function formatDisplayName()
11579b00823SAndreas Gohr    {
11679b00823SAndreas Gohr        if ($this->relativeDisplay !== null) {
11779b00823SAndreas Gohr            $id = $this->mediaFile->getId();
118*6c16a3a9Sfiwswe            if (str_starts_with($id, $this->relativeDisplay)) {
11979b00823SAndreas Gohr                $id = substr($id, strlen($this->relativeDisplay));
12079b00823SAndreas Gohr            }
12179b00823SAndreas Gohr            return ltrim($id, ':');
12279b00823SAndreas Gohr        } else {
12379b00823SAndreas Gohr            return $this->mediaFile->getDisplayName();
12479b00823SAndreas Gohr        }
12579b00823SAndreas Gohr    }
126bf9684bcSAndreas Gohr}
127