root/wifidogadmin/wifidog/classes/Menu.php

Revision 479, 10.7 kB (checked in by insultant, 10 months ago)

--

Line 
1 <?php
2
3
4 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5
6 // +-------------------------------------------------------------------+
7 // | WiFiDog Authentication Server                                     |
8 // | =============================                                     |
9 // |                                                                   |
10 // | The WiFiDog Authentication Server is part of the WiFiDog captive  |
11 // | portal suite.                                                     |
12 // +-------------------------------------------------------------------+
13 // | PHP version 5 required.                                           |
14 // +-------------------------------------------------------------------+
15 // | Homepage:     http://www.wifidog.org/                             |
16 // | Source Forge: http://sourceforge.net/projects/wifidog/            |
17 // +-------------------------------------------------------------------+
18 // | This program is free software; you can redistribute it and/or     |
19 // | modify it under the terms of the GNU General Public License as    |
20 // | published by the Free Software Foundation; either version 2 of    |
21 // | the License, or (at your option) any later version.               |
22 // |                                                                   |
23 // | This program is distributed in the hope that it will be useful,   |
24 // | but WITHOUT ANY WARRANTY; without even the implied warranty of    |
25 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
26 // | GNU General Public License for more details.                      |
27 // |                                                                   |
28 // | You should have received a copy of the GNU General Public License |
29 // | along with this program; if not, contact:                         |
30 // |                                                                   |
31 // | Free Software Foundation           Voice:  +1-617-542-5942        |
32 // | 59 Temple Place - Suite 330        Fax:    +1-617-542-2652        |
33 // | Boston, MA  02111-1307,  USA       gnu@gnu.org                    |
34 // |                                                                   |
35 // +-------------------------------------------------------------------+
36
37 /** This file contains the code for the MainUI class, as well as GUI exception handling.
38  * @package    WiFiDogAuthServer
39  * @author     Benoit Grégoire <bock@step.polymtl.ca>
40  * @copyright  2005-2007 Benoit Grégoire, Technologies Coeus inc.
41  * @version    Subversion $Id: MainUI.php 1246 2007-07-03 16:35:09Z benoitg $
42  * @link       http://www.wifidog.org/
43  */
44
45 /**
46  * Load required files
47  */
48
49 require_once('classes/SmartyWifidog.php');
50
51 define('MENU_ITEM_GROUPING','MENU_ITEM_GROUPING');
52 /**
53  * Singleton class for managing menu.
54  *
55  * @package    WiFiDogAuthServer
56  * @author     Benoit Grégoire <bock@step.polymtl.ca>
57  * @copyright  2005-2006 Benoit Grégoire, Technologies Coeus inc.
58  */
59 class Menu {
60     /** holder for the singleton */
61     private static $object;
62
63     /**
64      * Object for Smarty class
65      */
66     private $smarty;
67     /** The array from which the menu is generated */
68     private $menuArray;
69     /**
70      * Get the MainUI object
71      * @return object The MainUI object
72      */
73     public static function &getObject() {
74         if (self :: $object == null) {
75             self :: $object = new self();
76         }
77         return self :: $object;
78     }
79     /**
80      * Constructor
81      *
82      * @return void
83      *
84      * @access public
85      */
86     private function __construct() {
87         //$db = AbstractDb :: getObject();
88         // Init Smarty
89         $this->smarty = SmartyWifidog :: getObject();
90
91         // Init the menu array
92         $this->_menuArray = array('childrens'=>array());
93     }
94
95     /** Builds an HTML menu, appends it to $userData*/
96     static private function buildHtmlMenuItemCallback($menuItemArray, $menuItemIndex, &$userData) {
97         //echo "buildHtmlMenuItemCallback for: ";pretty_print_r($menuItemArray);
98         $html = '';
99         //echo "previous_level: {$userData['previous_level']} current_level: {$menuItemArray['level']}<br/>";
100         if(isset($userData['previous_level']) && $userData['previous_level']>$menuItemArray['level']) {
101             $html .= "</ul>\n</li>\n";
102         }
103         !empty($menuItemArray['title'])?$title=$menuItemArray['title']:$title=$menuItemArray['path'];
104         $class = preg_replace ("/.*?([^\/]*$)/", "$1", $menuItemArray['path']);//Only take the last fragment of the path as CSS class
105         if(!empty($menuItemArray['url'])) {
106             $html .= "<li class='$class'><a href='{$menuItemArray['url']}'>{$menuItemArray['title']}</a>\n";
107         }
108         else if(!empty($menuItemArray['childrens'])){
109             $html .= "<li class='$class'><a href='#'>{$menuItemArray['title']}</a>\n";
110         }
111
112         if(!empty($menuItemArray['childrens'])) {
113             $html .= "<ul class='$class'>\n";
114         } else if(!empty($menuItemArray['url'])){
115             $html .= "\n</li>\n";
116         }
117         $userData['previous_level'] = $menuItemArray['level'];
118         isset($userData['html'])?$userData['html'].=$html:$userData['html']=$html;
119     }
120     /** Takes an array_walk_recursive compatible callback.  Will be called for each menu item */
121     private static function menuArrayWalkRecursiveReal($menuArray, $menuItemIdx, $funcname, &$userdata = null) {
122         //echo "menuArrayWalkRecursive called with menuArray:"; pretty_print_r($menuArray);
123         $retval = true;
124         if(isset($menuArray['path'])){
125             //Only call if we are in a real menu item (and, among other, not at the root)
126             //echo "menuArrayWalkRecursive(): Calling callback.<br/>";
127             $retval = call_user_func_array($funcname, array($menuArray, $menuItemIdx, &$userdata));
128         }
129         foreach ($menuArray['childrens'] as $menuItemIdx => $menuItem) {
130             //pretty_print_r($menuItem);
131             //echo "Recusively calling for $menuItemIdx<br/>";
132             $retval = $retval & self::menuArrayWalkRecursiveReal($menuItem, $menuItemIdx, $funcname, $userdata);
133         }
134         return $retval;
135     }
136
137     /** Takes an array_walk_recursive compatible callback.  Will be called for each menu item */
138     public function menuArrayWalkRecursive($funcname, &$userdata = null) {
139         return self::menuArrayWalkRecursiveReal($this->_menuArray, 0, $funcname, $userdata);
140     }
141     /** Compare menu items according to it's title output passed to strcoll().  The toString
142      * output is converted to ISO-8859-1 before sorting to allow strcoll() to be used
143      *  for locale-specific sorting.
144      */
145     public static function titlestrcoll($object1, $object2)
146     {
147         //echo "CMP: ".$object1['title']." vs ". $object2['title']."<br/>\n";
148         return strcoll ( utf8_decode($object1['title']), utf8_decode($object2['title']) );
149     }
150
151     /** Sort the menu using a user defined sort function */
152     private static function menuArraySort(&$menuArray, $funcname, &$userdata = null) {
153         //echo "menuArraySort called with menuArray:"; pretty_print_r($menuArray);
154         $retval = true;
155         //Sort childrens
156         $retval = uasort($menuArray['childrens'], $funcname);
157         foreach ($menuArray['childrens'] as $menuItemIdx => &$menuItem) {
158             //Recursive call
159             //pretty_print_r($menuItem);
160             //echo "Recusively calling for $menuItemIdx<br/>";
161             $retval = $retval & self::menuArraySort($menuItem, $funcname, $userdata);
162         }
163         return $retval;
164     }
165
166     public function processHookMenu($classname) {
167         require_once("classes/$classname.php");
168         $hookArray = call_user_func(array($classname, 'hookMenu'));
169         //pretty_print_r($hookArray);
170         foreach($hookArray as $hookArrayItem) {
171
172             $parts = explode('/', $hookArrayItem['path']);
173             $menuItem=&$this->_menuArray;//This is just for initialisation, the foreach will set it to a leaf of menuitem.
174             foreach ($parts as $k => $part) {
175                 //echo "$k => $part<br/>";
176                 if (!isset($menuItem['childrens'][$part])){
177                     //echo "$part does not exist, setting it<br/>";
178                     $menuItem['childrens'][$part]=array("childrens"=>array(), "level"=>$k);
179                 }
180                 $menuItem=&$menuItem['childrens'][$part];
181             }
182             $menuItem = array_merge($menuItem, $hookArrayItem);
183             //echo "Setting menuItem to hookArrayItem of path {$hookArrayItem['path']}, new _menuArray:<br/>";
184             // pretty_print_r($this->_menuArray);
185         }
186     }
187     public function initMenu() {
188         $this->processHookMenu('Server');
189         $this->processHookMenu('Node');
190         $this->processHookMenu('Network');
191         $this->processHookMenu('User');
192         $this->processHookMenu('Content');
193         $this->processHookMenu('NodeList');
194         $this->processHookMenu('Role');
195         $this->processHookMenu('VirtualHost');
196         $this->processHookMenu('ContentTypeFilter');
197         $this->processHookMenu('ProfileTemplate');
198         $this->processHookMenu('DependenciesList');
199         self::menuArraySort($this->_menuArray, array('Menu','titlestrcoll'));
200         //pretty_print_r($this->_menuArray);
201     }
202
203     /**
204      * IE's braindeadness requires a javascript workaround to allow suckerfish menus to work.
205      *
206      * @return HTML markup
207      */
208 //     static public function getIEWorkaroundJS() {
209 //         $html = <<<EOT
210 //         <script type="text/javascript"><!--//--><![CDATA[//><!--
211 //
212 // sfHover = function() {
213 //  var sfEls = document.getElementById("nav").getElementsByTagName("LI");
214 //  for (var i=0; i<sfEls.length; i++) {
215 //      sfEls[i].onmouseover=function() {
216 //          this.className+=" sfhover";
217 //      }
218 //      sfEls[i].onmouseout=function() {
219 //          //alert(this.className);
220 //          this.className=this.className.replace(new RegExp(" sfhover"), "");
221 //          //alert(this.className);
222 //      }
223 //  }
224 // }
225 // if (window.attachEvent) window.attachEvent("onload", sfHover);
226 //
227 // //--><!]]></script>
228 // EOT;
229 //         return $html;
230 //     }
231
232     public function getUserUI()
233     {
234         $this->initMenu();
235         $html = '';
236         //Deal with internet explorer's baindeadness.  From http://www.htmldog.com/articles/suckerfish/dropdowns/example/vertical.html
237
238         $html .= "<ul id='nav'>\n";
239         $userData=null;
240         self::menuArrayWalkRecursive(array('Menu','buildHtmlMenuItemCallback'), $userData);
241         $html .= $userData['html'];
242         if(isset($userData['previous_level']) && $userData['previous_level']>0) {
243             $html .= "</ul>\n";
244         }
245         $html .= "</ul>\n";
246         $html .= "<br/ class='clearbr'>\n";
247         //echo htmlspecialchars($userData['html']);
248         return $html;
249     }
250 }
251
252 /*
253  * Local variables:
254  * tab-width: 4
255  * c-basic-offset: 4
256  * c-hanging-comment-ender-p: nil
257  * End:
258  */
Note: See TracBrowser for help on using the browser.