summaryrefslogtreecommitdiffstats
path: root/inc/Ui/Media/Display.php
blob: 4443e44ab5bf4649f982f3396fb862cbce67b8eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php

namespace dokuwiki\Ui\Media;

use dokuwiki\File\MediaFile;

class Display
{
    /** @var MediaFile */
    protected $mediaFile;

    /** @var string should IDs be shown relative to this namespace? Used in search results */
    protected $relativeDisplay;

    /** @var bool scroll to this file on display? */
    protected $scrollIntoView = false;

    /**
     * Display constructor.
     * @param MediaFile $mediaFile
     */
    public function __construct(MediaFile $mediaFile)
    {
        $this->mediaFile = $mediaFile;
    }

    /**
     * Get the HTML to display a preview image if possible, otherwise show an icon
     *
     * @param int $w bounding box width to resize pixel based images to
     * @param int $h bounding box height to resize pixel based images to
     * @return string
     */
    public function getPreviewHtml($w, $h)
    {
        if ($this->mediaFile->isImage()) {
            $src = ml($this->mediaFile->getId(), ['w' => $w, 'h' => $h]);
        } else {
            $src = $this->getIconUrl();
        }

        $attr = [
            'alt' => $this->mediaFile->getDisplayName(),
            'loading' => 'lazy',
            'width' => $w,
            'height' => $h,
        ];

        return '<img src="' . $src . '" ' . buildAttributes($attr) . ' />';
    }

    /**
     * Return the URL to the icon for this file
     *
     * @return string
     */
    public function getIconUrl()
    {
        $link = 'lib/images/fileicons/svg/' . $this->mediaFile->getIcoClass() . '.svg';
        if (!file_exists(DOKU_INC . $link)) $link = 'lib/images/fileicons/svg/file.svg';
        return DOKU_BASE . $link;
    }

    /**
     * Show IDs relative to this namespace
     *
     * @param string|null $ns Use null to disable
     */
    public function relativeDisplay($ns)
    {
        $this->relativeDisplay = $ns;
    }

    /**
     * Scroll to this file on display?
     *
     * @param bool $set
     */
    public function scrollIntoView($set = true)
    {
        $this->scrollIntoView = $set;
    }

    /** @return string */
    protected function formatDate()
    {
        return dformat($this->mediaFile->getLastModified());
    }

    /**
     * Output the image dimension if any
     *
     * @param string $empty what to show when no dimensions are available
     * @return string
     */
    protected function formatDimensions($empty = '&#160;')
    {
        $w = $this->mediaFile->getWidth();
        $h = $this->mediaFile->getHeight();
        if ($w && $h) {
            return $w . '&#215;' . $h;
        } else {
            return $empty;
        }
    }

    /** @return string */
    protected function formatFileSize()
    {
        return filesize_h($this->mediaFile->getFileSize());
    }

    /** @return string */
    protected function formatDisplayName()
    {
        if ($this->relativeDisplay !== null) {
            $id = $this->mediaFile->getId();
            if (str_starts_with($id, $this->relativeDisplay)) {
                $id = substr($id, strlen($this->relativeDisplay));
            }
            return ltrim($id, ':');
        } else {
            return $this->mediaFile->getDisplayName();
        }
    }
}