Dotclear

source: plugins/simpleMenu/_public.php @ 2773:65aa733ce8a4

Revision 2773:65aa733ce8a4, 4.0 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Add activation setting for simple menu, fixes #1958

Line 
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 -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14require dirname(__FILE__).'/_widgets.php';
15
16# Simple menu template functions
17$core->tpl->addValue('SimpleMenu',array('tplSimpleMenu','simpleMenu'));
18
19class 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)$#',$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          if (!(boolean) $core->blog->settings->system->simpleMenu_active)
50               return;
51
52          if (($w->homeonly == 1 && $core->url->type != 'default') ||
53               ($w->homeonly == 2 && $core->url->type == 'default')) {
54               return;
55          }
56
57          $menu = tplSimpleMenu::displayMenu('','','title');
58          if ($menu == '') {
59               return;
60          }
61
62          return $w->renderDiv($w->content_only,'simple-menu '.$w->class,'',
63               ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '').$menu);
64     }
65
66     public static function displayMenu($class='',$id='',$description='')
67     {
68          global $core;
69
70          $ret = '';
71
72          if (!(boolean) $core->blog->settings->system->simpleMenu_active)
73               return $ret;
74
75          $menu = $GLOBALS['core']->blog->settings->system->get('simpleMenu');
76          $menu = @unserialize($menu);
77
78          if (is_array($menu))
79          {
80               // Current relative URL
81               $url = $_SERVER['REQUEST_URI'];
82               $abs_url = http::getHost().$url;
83
84               // Home recognition var
85               $home_url = html::stripHostURL($GLOBALS['core']->blog->url);
86               $home_directory = dirname($home_url);
87               if ($home_directory != '/')
88                    $home_directory = $home_directory.'/';
89
90               // Menu items loop
91               foreach ($menu as $i => $m) {
92                    # $href = lien de l'item de menu
93                    $href = $m['url'];
94                    $href = html::escapeHTML($href);
95
96                    # Active item test
97                    $active = false;
98                    if (($url == $href) ||
99                         ($abs_url == $href) ||
100                         ($_SERVER['URL_REQUEST_PART'] == $href) ||
101                         (($_SERVER['URL_REQUEST_PART'] == '') && (($href == $home_url) || ($href == $home_directory)))) {
102                         $active = true;
103                    }
104                    $title = $span = '';
105                    if ($m['descr']) {
106                         if ($description == 'title') {
107                              $title = ' title="'.html::escapeHTML(__($m['descr'])).'"';
108                         } else {
109                              $span = ' <span>'.html::escapeHTML(__($m['descr'])).'</span>';
110                         }
111                    }
112                    $label = html::escapeHTML(__($m['label']));
113
114                    $item = new ArrayObject(array(
115                         'url' => $href,                         // URL
116                         'label' => $label,                 // <a> link label
117                         'title' => $title,                 // <a> link title (optional)
118                         'span' => $span,                   // description (will be displayed after <a> link)
119                         'active' => $active,               // status (true/false)
120                         'class' => ''                      // additional <li> class (optional)
121                         ));
122
123                    # --BEHAVIOR-- publicSimpleMenuItem
124                    $core->callBehavior('publicSimpleMenuItem',$i,$item);
125
126                    $ret .= '<li class="li'.($i+1).
127                                   ($item['active'] ? ' active' : '').
128                                   ($i == 0 ? ' li-first' : '').
129                                   ($i == count($menu)-1 ? ' li-last' : '').
130                                   ($item['class'] ? $item['class'] : '').
131                              '">'.
132                              '<a href="'.$href.'"'.$item['title'].'>'.$item['label'].$item['span'].'</a>'.
133                              '</li>';
134               }
135
136               // Final rendering
137               if ($ret) {
138                    $ret = '<ul '.($id ? 'id="'.$id.'"' : '').' class="simple-menu'.($class ? ' '.$class : '').'" role="navigation">'."\n".$ret."\n".'</ul>';
139               }
140          }
141
142          return $ret;
143     }
144}
Note: See TracBrowser for help on using the repository browser.

Sites map