Dotclear

source: inc/admin/class.dc.menu.php @ 3075:2efd73dc6db6

Revision 3075:2efd73dc6db6, 2.6 KB checked in by franck <carnet.franck.paul@…>, 10 years ago (diff)

Sort additionnal menu items (provided by 3rd party plugins) - preserve default menu items and favorites order

RevLine 
[0]1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
[1179]6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
[0]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
14class dcMenu
15{
16     private $id;
17     public $title;
[2566]18
[0]19     public function __construct($id,$title,$itemSpace='')
20     {
21          $this->id = $id;
22          $this->title = $title;
23          $this->itemSpace = $itemSpace;
[3075]24          $this->pinned = array();
[0]25          $this->items = array();
26     }
[2566]27
[3075]28     public function addItem($title,$url,$img,$active,$show=true,$id=null,$class=null,$pinned=false)
[0]29     {
30          if($show) {
[3075]31               $item = $this->itemDef($title,$url,$img,$active,$id,$class);
32               if ($pinned) {
33                    $this->pinned[] = $item;
34               } else {
35                    $this->items[$title] = $item;
36               }
[0]37          }
38     }
[2566]39
[3075]40     public function prependItem($title,$url,$img,$active,$show=true,$id=null,$class=null,$pinned=false)
[0]41     {
42          if ($show) {
[3075]43               $item = $this->itemDef($title,$url,$img,$active,$id,$class);
44               if ($pinned) {
45                    array_unshift($this->pinned,$item);
46               } else {
47                    $this->items[$title] = $item;
48               }
[0]49          }
50     }
[2566]51
[0]52     public function draw()
53     {
[3075]54          if (count($this->items) + count($this->pinned) == 0) {
[0]55               return '';
56          }
[2566]57
[0]58          $res =
59          '<div id="'.$this->id.'">'.
60          ($this->title ? '<h3>'.$this->title.'</h3>' : '').
61          '<ul>'."\n";
[2566]62
[3075]63          // 1. Display pinned items (unsorted)
64          for ($i=0; $i<count($this->pinned); $i++)
65          {
66               if ($i+1 < count($this->pinned) && $this->itemSpace != '') {
67                    $res .= preg_replace('|</li>$|',$this->itemSpace.'</li>',$this->pinned[$i]);
68                    $res .= "\n";
69               } else {
70                    $res .= $this->pinned[$i]."\n";
71               }
72          }
73
74          // 2. Display unpinned itmes (sorted)
75          $i = 0;
76          dcUtils::lexicalKeySort($this->items);
77          foreach ($this->items as $title => $item)
[0]78          {
79               if ($i+1 < count($this->items) && $this->itemSpace != '') {
[3075]80                    $res .= preg_replace('|</li>$|',$this->itemSpace.'</li>',$item);
[0]81                    $res .= "\n";
82               } else {
[3075]83                    $res .= $item."\n";
[0]84               }
[3075]85               $i++;
[0]86          }
[2566]87
[0]88          $res .= '</ul></div>'."\n";
[2566]89
[0]90          return $res;
91     }
[2566]92
[3]93     protected function itemDef($title,$url,$img,$active,$id=null,$class=null)
[0]94     {
95          if (is_array($url)) {
96               $link = $url[0];
97               $ahtml = (!empty($url[1])) ? ' '.$url[1] : '';
98          } else {
99               $link = $url;
100               $ahtml = '';
101          }
[2566]102
[691]103          $img = dc_admin_icon_url($img);
[2566]104
[0]105          return
[3]106          '<li'.(($active || $class) ? ' class="'.(($active) ? 'active ' : '').(($class) ? $class : '').'"' : '').
[0]107          (($id) ? ' id="'.$id.'"' : '').
108          (($img) ? ' style="background-image: url('.$img.');"' : '').
109          '>'.
[2566]110
[0]111          '<a href="'.$link.'"'.$ahtml.'>'.$title.'</a></li>'."\n";
112     }
113}
Note: See TracBrowser for help on using the repository browser.

Sites map