1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
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 | $class = isset($attr['class']) ? trim($attr['class']) : ''; |
---|
30 | $id = isset($attr['id']) ? trim($attr['id']) : ''; |
---|
31 | $description = isset($attr['description']) ? trim($attr['description']) : ''; |
---|
32 | |
---|
33 | if (!preg_match('#^(title|span|both|none)$#',$description)) { |
---|
34 | $description = ''; |
---|
35 | } |
---|
36 | |
---|
37 | return '<?php echo tplSimpleMenu::displayMenu('. |
---|
38 | "'".addslashes($class)."',". |
---|
39 | "'".addslashes($id)."',". |
---|
40 | "'".addslashes($description)."'". |
---|
41 | '); ?>'; |
---|
42 | } |
---|
43 | |
---|
44 | # Widget function |
---|
45 | public static function simpleMenuWidget($w) |
---|
46 | { |
---|
47 | global $core, $_ctx; |
---|
48 | |
---|
49 | $descr_type = array(0 => 'span',1 => 'title',2 => 'both',3 => 'none'); |
---|
50 | |
---|
51 | if (!(boolean) $core->blog->settings->system->simpleMenu_active) |
---|
52 | return; |
---|
53 | |
---|
54 | if ($w->offline) |
---|
55 | return; |
---|
56 | |
---|
57 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
---|
58 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
---|
59 | return; |
---|
60 | } |
---|
61 | |
---|
62 | $description = 'title'; |
---|
63 | if (isset($descr_type[$w->description])) { |
---|
64 | $description = $descr_type[$w->description]; |
---|
65 | } |
---|
66 | $menu = tplSimpleMenu::displayMenu('','',$description); |
---|
67 | if ($menu == '') { |
---|
68 | return; |
---|
69 | } |
---|
70 | |
---|
71 | return $w->renderDiv($w->content_only,'simple-menu '.$w->class,'', |
---|
72 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '').$menu); |
---|
73 | } |
---|
74 | |
---|
75 | public static function displayMenu($class='',$id='',$description='') |
---|
76 | { |
---|
77 | global $core; |
---|
78 | |
---|
79 | $ret = ''; |
---|
80 | |
---|
81 | if (!(boolean) $core->blog->settings->system->simpleMenu_active) |
---|
82 | return $ret; |
---|
83 | |
---|
84 | $menu = $core->blog->settings->system->simpleMenu; |
---|
85 | if (is_array($menu)) |
---|
86 | { |
---|
87 | // Current relative URL |
---|
88 | $url = $_SERVER['REQUEST_URI']; |
---|
89 | $abs_url = http::getHost().$url; |
---|
90 | |
---|
91 | // Home recognition var |
---|
92 | $home_url = html::stripHostURL($core->blog->url); |
---|
93 | $home_directory = dirname($home_url); |
---|
94 | if ($home_directory != '/') |
---|
95 | $home_directory = $home_directory.'/'; |
---|
96 | |
---|
97 | // Menu items loop |
---|
98 | foreach ($menu as $i => $m) { |
---|
99 | # $href = lien de l'item de menu |
---|
100 | $href = $m['url']; |
---|
101 | $href = html::escapeHTML($href); |
---|
102 | |
---|
103 | # Active item test |
---|
104 | $active = false; |
---|
105 | if (($url == $href) || |
---|
106 | ($abs_url == $href) || |
---|
107 | ($_SERVER['URL_REQUEST_PART'] == $href) || |
---|
108 | (($_SERVER['URL_REQUEST_PART'] == '') && (($href == $home_url) || ($href == $home_directory)))) { |
---|
109 | $active = true; |
---|
110 | } |
---|
111 | $title = $span = ''; |
---|
112 | if ($m['descr']) { |
---|
113 | if ($description == 'title' || $description == 'both') { |
---|
114 | $title = ' title="'.html::escapeHTML(__($m['descr'])).'"'; |
---|
115 | } |
---|
116 | if ($description == 'span' || $description == 'both') { |
---|
117 | $span = ' <span>'.html::escapeHTML(__($m['descr'])).'</span>'; |
---|
118 | } |
---|
119 | } |
---|
120 | $label = html::escapeHTML(__($m['label'])); |
---|
121 | |
---|
122 | $item = new ArrayObject(array( |
---|
123 | 'url' => $href, // URL |
---|
124 | 'label' => $label, // <a> link label |
---|
125 | 'title' => $title, // <a> link title (optional) |
---|
126 | 'span' => $span, // description (will be displayed after <a> link) |
---|
127 | 'active' => $active, // status (true/false) |
---|
128 | 'class' => '' // additional <li> class (optional) |
---|
129 | )); |
---|
130 | |
---|
131 | # --BEHAVIOR-- publicSimpleMenuItem |
---|
132 | $core->callBehavior('publicSimpleMenuItem',$i,$item); |
---|
133 | |
---|
134 | $ret .= '<li class="li'.($i+1). |
---|
135 | ($item['active'] ? ' active' : ''). |
---|
136 | ($i == 0 ? ' li-first' : ''). |
---|
137 | ($i == count($menu)-1 ? ' li-last' : ''). |
---|
138 | ($item['class'] ? $item['class'] : ''). |
---|
139 | '">'. |
---|
140 | '<a href="'.$href.'"'.$item['title'].'>'.$item['label'].$item['span'].'</a>'. |
---|
141 | '</li>'; |
---|
142 | } |
---|
143 | |
---|
144 | // Final rendering |
---|
145 | if ($ret) { |
---|
146 | $ret = '<nav role="navigation"><ul '.($id ? 'id="'.$id.'"' : '').' class="simple-menu'.($class ? ' '.$class : '').'">'."\n".$ret."\n".'</ul></nav>'; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | return $ret; |
---|
151 | } |
---|
152 | } |
---|