1<?php
2
3namespace dokuwiki\plugin\menuext;
4
5use dokuwiki\Input\Input;
6use dokuwiki\Utf8\PhpString;
7
8/**
9 * Custom Menu Item
10 */
11class MenuExtItem extends \dokuwiki\Menu\Item\AbstractItem
12{
13
14    protected $link;
15    protected $attributes;
16
17    /**
18     * Intitialize a Menu Item with the given data
19     *
20     * @inheritDoc
21     * @param array $data
22     */
23    public function __construct($data)
24    {
25        global $conf;
26
27        if (isset($data['label'][$conf['lang']])) $this->label = $data['label'][$conf['lang']];
28        if (isset($data['title'][$conf['lang']])) $this->title = $data['title'][$conf['lang']];
29        if (isset($data['link'])) $this->link = $data['link'];
30        if (isset($data['svg'])) $this->svg = $data['svg'];
31        if (isset($data['attr'])) $this->attributes = $data['attr'];
32
33        parent::__construct();
34    }
35
36    /** @inheritDoc */
37    public function getLink()
38    {
39        return $this->linkReplacements($this->link);
40    }
41
42    /**
43     * Auto-Download material design icons
44     * @inheritDoc
45     */
46    public function getSvg()
47    {
48        if (file_exists($this->svg)) return $this->svg;
49        $file = mediaFN($this->svg);
50        if (file_exists($file)) return $file;
51
52        $base = 'https://raw.githubusercontent.com/Templarian/MaterialDesign/master/svg/';
53        $file = getCacheName($this->svg, '.svg');
54        if (!file_exists($file)) {
55            io_download($base . $this->svg, $file);
56            clearstatcache(true, $file);
57        }
58        if (file_exists($file)) return $file;
59
60        return DOKU_INC . 'lib/images/menu/00-default_checkbox-blank-circle-outline.svg';
61    }
62
63    /** @@inheritDoc */
64    public function getLinkAttributes($classprefix = 'menuitem ')
65    {
66        $attr = parent::getLinkAttributes($classprefix);
67        if(is_array($this->attributes)) {
68            $attr = array_merge($attr, $this->attributes);
69        }
70        return $attr;
71    }
72
73    /**
74     * Replace all placeholders in given link
75     *
76     * Basically the same as parsePageTemplate but does URL encoding
77     *
78     * @param string $link the link with placeholders
79     * @return string
80     * @see parsePageTemplate
81     */
82    protected function linkReplacements($link)
83    {
84        global $ID;
85        global $USERINFO;
86        global $conf;
87        /* @var Input $INPUT */
88        global $INPUT;
89
90        // replace placeholders
91        $file = noNS($ID);
92        $page = strtr($file, $conf['sepchar'], ' ');
93        $link = strftime($link); // time first
94        $link = str_replace(
95            [
96                '@ID@',
97                '@NS@',
98                '@CURNS@',
99                '@!CURNS@',
100                '@!!CURNS@',
101                '@!CURNS!@',
102                '@FILE@',
103                '@!FILE@',
104                '@!FILE!@',
105                '@PAGE@',
106                '@!PAGE@',
107                '@!!PAGE@',
108                '@!PAGE!@',
109                '@USER@',
110                '@NAME@',
111                '@MAIL@',
112                '@DATE@',
113            ],
114            array_map('rawurlencode', [
115                $ID,
116                getNS($ID),
117                curNS($ID),
118                PhpString::ucfirst(curNS($ID)),
119                PhpString::ucwords(curNS($ID)),
120                PhpString::strtoupper(curNS($ID)),
121                $file,
122                PhpString::ucfirst($file),
123                PhpString::strtoupper($file),
124                $page,
125                PhpString::ucfirst($page),
126                PhpString::ucwords($page),
127                PhpString::strtoupper($page),
128                $INPUT->server->str('REMOTE_USER'),
129                $USERINFO ? $USERINFO['name'] : '',
130                $USERINFO ? $USERINFO['mail'] : '',
131                $conf['dformat'],
132            ]), $link
133        );
134
135        return $link;
136    }
137
138}
139