Dotclear

source: plugins/simpleMenu/_public.php @ 1062:8f51b2e77efe

Revision 1062:8f51b2e77efe, 3.7 KB checked in by franck <carnet.franck.paul@…>, 13 years ago (diff)

New behaviour publicSimpleMenuItem in SimpleMenu? plugin, let other plays with menu item before rendering it. Addresses #1295

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2011 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          $class = isset($attr['class']) ? trim($attr['class']) : '';
25          $id = isset($attr['id']) ? trim($attr['id']) : '';
26          $description = isset($attr['description']) ? trim($attr['description']) : '';
27         
28          if (!preg_match('#^(title|span)$#',$description)) {
29               $description = '';
30          }
31         
32          return '<?php echo tplSimpleMenu::displayMenu('.
33                    "'".addslashes($class)."',".
34                    "'".addslashes($id)."',".
35                    "'".addslashes($description)."'".
36               '); ?>';
37     }
38     
39     # Widget function
40     public static function simpleMenuWidget($w)
41     {
42          global $core, $_ctx;
43         
44          if (($w->homeonly == 1 && $core->url->type != 'default') ||
45               ($w->homeonly == 2 && $core->url->type == 'default')) {
46               return;
47          }
48
49          $menu = tplSimpleMenu::displayMenu('','','title');
50          if ($menu == '') {
51               return;
52          }
53
54          return '<div class="simple-menu'.
55               ($w->class ? ' '.html::escapeHTML($w->class) : '').'">'.
56               ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').$menu.'</div>';
57     }
58     
59     public static function displayMenu($class='',$id='',$description='')
60     {
61          global $core;
62
63          $ret = '';
64
65          $menu = $GLOBALS['core']->blog->settings->system->get('simpleMenu');
66          $menu = @unserialize($menu);
67
68          if (is_array($menu)) 
69          {
70               // Current relative URL
71               $url = $_SERVER['REQUEST_URI'];
72               $abs_url = http::getHost().$url;
73         
74               // Home recognition var
75               $home_url = html::stripHostURL($GLOBALS['core']->blog->url);
76               $home_directory = dirname($home_url);
77               if ($home_directory != '/')
78                    $home_directory = $home_directory.'/';
79
80               // Menu items loop
81               foreach ($menu as $i => $m) {
82                    # $href = lien de l'item de menu
83                    $href = $m['url'];
84                    $href = html::escapeHTML($href);
85
86                    # Active item test
87                    $active = false;
88                    if (($url == $href) || 
89                         ($abs_url == $href) || 
90                         ($_SERVER['URL_REQUEST_PART'] == $href) || 
91                         (($_SERVER['URL_REQUEST_PART'] == '') && (($href == $home_url) || ($href == $home_directory)))) {
92                         $active = true;
93                    }
94                    $title = $span = '';
95                    if ($m['descr']) {
96                         if ($description == 'title') {
97                              $title = ' title="'.__($m['descr']).'"';
98                         } else {
99                              $span = ' <span>'.__($m['descr']).'</span>';
100                         }
101                    }
102
103                    $item = new ArrayObject(array(
104                         'url' => $href,                         // URL
105                         'label' => __($m['label']),        // <a> link label
106                         'title' => $title,                 // <a> link title (optional)
107                         'span' => $span,                   // description (will be displayed after <a> link)
108                         'active' => $active,               // status (true/false)
109                         'class' => ''                      // additional <li> class (optional)
110                         ));
111
112                    # --BEHAVIOR-- publicSimpleMenuItem
113                    $core->callBehavior('publicSimpleMenuItem',$i,$item);
114
115                    $ret .= '<li class="li'.($i+1).
116                                   ($item['active'] ? ' active' : '').
117                                   ($i == 0 ? ' li-first' : '').
118                                   ($i == count($menu)-1 ? ' li-last' : '').
119                                   ($item['class'] ? $item['class'] : '').
120                              '">'.
121                              '<a href="'.$href.'"'.$item['title'].'>'.$item['label'].$item['span'].'</a>'.
122                              '</li>';
123               }
124               
125               // Final rendering
126               if ($ret) {
127                    $ret = '<ul '.($id ? 'id="'.$id.'"' : '').' class="simple-menu'.($class ? ' '.$class : '').'">'."\n".$ret."\n".'</ul>';
128               }
129          }
130
131          return $ret;
132     }
133}
134
135?>
Note: See TracBrowser for help on using the repository browser.

Sites map