1 | <?php |
---|
2 | /** |
---|
3 | * @brief simpleMenu, a plugin for Dotclear 2 |
---|
4 | * |
---|
5 | * @package Dotclear |
---|
6 | * @subpackage Plugins |
---|
7 | * |
---|
8 | * @copyright Olivier Meunier & Association Dotclear |
---|
9 | * @copyright GPL-2.0-only |
---|
10 | */ |
---|
11 | |
---|
12 | if (!defined('DC_RC_PATH')) {return;} |
---|
13 | |
---|
14 | require dirname(__FILE__) . '/_widgets.php'; |
---|
15 | |
---|
16 | # Simple menu template functions |
---|
17 | $core->tpl->addValue('SimpleMenu', array('tplSimpleMenu', 'simpleMenu')); |
---|
18 | |
---|
19 | class tplSimpleMenu |
---|
20 | { |
---|
21 | # Template function |
---|
22 | public static function simpleMenu($attr) |
---|
23 | { |
---|
24 | global $core; |
---|
25 | |
---|
26 | if (!(boolean) $core->blog->settings->system->simpleMenu_active) { |
---|
27 | return ''; |
---|
28 | } |
---|
29 | |
---|
30 | $class = isset($attr['class']) ? trim($attr['class']) : ''; |
---|
31 | $id = isset($attr['id']) ? trim($attr['id']) : ''; |
---|
32 | $description = isset($attr['description']) ? trim($attr['description']) : ''; |
---|
33 | |
---|
34 | if (!preg_match('#^(title|span|both|none)$#', $description)) { |
---|
35 | $description = ''; |
---|
36 | } |
---|
37 | |
---|
38 | return '<?php echo tplSimpleMenu::displayMenu(' . |
---|
39 | "'" . addslashes($class) . "'," . |
---|
40 | "'" . addslashes($id) . "'," . |
---|
41 | "'" . addslashes($description) . "'" . |
---|
42 | '); ?>'; |
---|
43 | } |
---|
44 | |
---|
45 | # Widget function |
---|
46 | public static function simpleMenuWidget($w) |
---|
47 | { |
---|
48 | global $core, $_ctx; |
---|
49 | |
---|
50 | $descr_type = array(0 => 'span', 1 => 'title', 2 => 'both', 3 => 'none'); |
---|
51 | |
---|
52 | if (!(boolean) $core->blog->settings->system->simpleMenu_active) { |
---|
53 | return; |
---|
54 | } |
---|
55 | |
---|
56 | if ($w->offline) { |
---|
57 | return; |
---|
58 | } |
---|
59 | |
---|
60 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
61 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
62 | return; |
---|
63 | } |
---|
64 | |
---|
65 | $description = 'title'; |
---|
66 | if (isset($descr_type[$w->description])) { |
---|
67 | $description = $descr_type[$w->description]; |
---|
68 | } |
---|
69 | $menu = tplSimpleMenu::displayMenu('', '', $description); |
---|
70 | if ($menu == '') { |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | return $w->renderDiv($w->content_only, 'simple-menu ' . $w->class, '', |
---|
75 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . $menu); |
---|
76 | } |
---|
77 | |
---|
78 | public static function displayMenu($class = '', $id = '', $description = '') |
---|
79 | { |
---|
80 | global $core; |
---|
81 | |
---|
82 | $ret = ''; |
---|
83 | |
---|
84 | if (!(boolean) $core->blog->settings->system->simpleMenu_active) { |
---|
85 | return $ret; |
---|
86 | } |
---|
87 | |
---|
88 | $menu = $core->blog->settings->system->simpleMenu; |
---|
89 | if (is_array($menu)) { |
---|
90 | // Current relative URL |
---|
91 | $url = $_SERVER['REQUEST_URI']; |
---|
92 | $abs_url = http::getHost() . $url; |
---|
93 | |
---|
94 | // Home recognition var |
---|
95 | $home_url = html::stripHostURL($core->blog->url); |
---|
96 | $home_directory = dirname($home_url); |
---|
97 | if ($home_directory != '/') { |
---|
98 | $home_directory = $home_directory . '/'; |
---|
99 | } |
---|
100 | |
---|
101 | // Menu items loop |
---|
102 | foreach ($menu as $i => $m) { |
---|
103 | # $href = lien de l'item de menu |
---|
104 | $href = $m['url']; |
---|
105 | $href = html::escapeHTML($href); |
---|
106 | |
---|
107 | # Cope with request only URL (ie ?query_part) |
---|
108 | $href_part = ''; |
---|
109 | if ($href != '' && substr($href, 0, 1) == '?') { |
---|
110 | $href_part = substr($href, 1); |
---|
111 | } |
---|
112 | |
---|
113 | $targetBlank = ((isset($m['targetBlank'])) && ($m['targetBlank'])) ? true : false; |
---|
114 | |
---|
115 | # Active item test |
---|
116 | $active = false; |
---|
117 | if (($url == $href) || |
---|
118 | ($abs_url == $href) || |
---|
119 | ($_SERVER['URL_REQUEST_PART'] == $href) || |
---|
120 | (($href_part != '') && ($_SERVER['URL_REQUEST_PART'] == $href_part)) || |
---|
121 | (($_SERVER['URL_REQUEST_PART'] == '') && (($href == $home_url) || ($href == $home_directory)))) { |
---|
122 | $active = true; |
---|
123 | } |
---|
124 | $title = $span = ''; |
---|
125 | |
---|
126 | if ($m['descr']) { |
---|
127 | if (($description == 'title' || $description == 'both') && $targetBlank) { |
---|
128 | $title = html::escapeHTML(__($m['descr'])) . ' (' . |
---|
129 | __('the link will open a new window') . ')'; |
---|
130 | } elseif ($description == 'title' || $description == 'both') { |
---|
131 | $title = html::escapeHTML(__($m['descr'])); |
---|
132 | } |
---|
133 | if ($description == 'span' || $description == 'both') { |
---|
134 | $span = ' <span class="simple-menu-descr">' . html::escapeHTML(__($m['descr'])) . '</span>'; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | if (empty($title) && $targetBlank) { |
---|
139 | $title = __('the link will open a new window'); |
---|
140 | } |
---|
141 | if ($active && !$targetBlank) { |
---|
142 | $title = (empty($title) ? __('Active page') : $title . ' (' . __('active page') . ')'); |
---|
143 | } |
---|
144 | |
---|
145 | $label = html::escapeHTML(__($m['label'])); |
---|
146 | |
---|
147 | $item = new ArrayObject(array( |
---|
148 | 'url' => $href, // URL |
---|
149 | 'label' => $label, // <a> link label |
---|
150 | 'title' => $title, // <a> link title (optional) |
---|
151 | 'span' => $span, // description (will be displayed after <a> link) |
---|
152 | 'active' => $active, // status (true/false) |
---|
153 | 'class' => '' // additional <li> class (optional) |
---|
154 | )); |
---|
155 | |
---|
156 | # --BEHAVIOR-- publicSimpleMenuItem |
---|
157 | $core->callBehavior('publicSimpleMenuItem', $i, $item); |
---|
158 | |
---|
159 | $ret .= '<li class="li' . ($i + 1) . |
---|
160 | ($item['active'] ? ' active' : '') . |
---|
161 | ($i == 0 ? ' li-first' : '') . |
---|
162 | ($i == count($menu) - 1 ? ' li-last' : '') . |
---|
163 | ($item['class'] ? ' ' . $item['class'] : '') . |
---|
164 | '">' . |
---|
165 | '<a href="' . $href . '"' . |
---|
166 | (!empty($item['title']) ? ' title="' . $item['title'] . '"' : '') . |
---|
167 | (($targetBlank) ? ' target="_blank" rel="noopener noreferrer"' : '') . '>' . |
---|
168 | '<span class="simple-menu-label">' . $item['label'] . '</span>' . |
---|
169 | $item['span'] . '</a>' . |
---|
170 | '</li>'; |
---|
171 | } |
---|
172 | // Final rendering |
---|
173 | if ($ret) { |
---|
174 | $ret = '<nav role="navigation"><ul ' . ($id ? 'id="' . $id . '"' : '') . ' class="simple-menu' . ($class ? ' ' . $class : '') . '">' . "\n" . $ret . "\n" . '</ul></nav>'; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | return $ret; |
---|
179 | } |
---|
180 | } |
---|