[Phplib-commit] CVS: php-lib-stable/php menu.inc,1.1,1.2
Brought to you by:
nhruby,
richardarcher
From: Richard A. <ric...@us...> - 2001-08-20 06:35:03
|
Update of /cvsroot/phplib/php-lib-stable/php In directory usw-pr-cvs1:/tmp/cvs-serv17800 Modified Files: menu.inc Log Message: Major overhaul of menu.inc: include sitemap() from -devel tree fix unset variable access warnings fix unquoted array subscript warnings add { and } around all one-line conditionals allow for new behaviour of dirname() in PHP4 normalise whitespace and comments change "pseudo" operation slightly and add documentation Index: menu.inc =================================================================== RCS file: /cvsroot/phplib/php-lib-stable/php/menu.inc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** menu.inc 2000/07/12 18:25:43 1.1 --- menu.inc 2001/08/20 06:35:00 1.2 *************** *** 36,41 **** var $title_delim = " : "; ! /***************************************************************************/ ! /* public: constructor */ function Menu() { --- 36,41 ---- var $title_delim = " : "; ! /*************************************************************************** ! * public: constructor */ function Menu() { *************** *** 43,53 **** } ! /* public: show current menu */ - function show() { print $this->get(); } function get() { global $PHP_SELF; --- 43,56 ---- } ! /* ! * public: show menu of items visible from the current page */ function show() { print $this->get(); } + /* + * public: build menu of items visible from the current page + */ function get() { global $PHP_SELF; *************** *** 72,79 **** $level = count(explode("/", $this->visible[$i])); ! if ($level > $oldlevel) $str .= $this->shift_in($oldlevel, $level); ! if ($level < $oldlevel) $str .= $this->shift_out($oldlevel, $level); $oldlevel = $level; --- 75,84 ---- $level = count(explode("/", $this->visible[$i])); ! if ($level > $oldlevel) { $str .= $this->shift_in($oldlevel, $level); ! } ! if ($level < $oldlevel) { $str .= $this->shift_out($oldlevel, $level); + } $oldlevel = $level; *************** *** 84,96 **** return $str; } ! /* public: show a single menu cell */ function get_cell($n, $level, $class = "") { $bold = ($this->visible[$n] == $this->map); ! for ($i=1; $i<$level; $i++) $dent .= " "; ! if ($this->item[$this->visible[$n]]["pseudo"]) { $str=sprintf("%s%s<br>\n", $dent, --- 89,150 ---- return $str; } + + /* + * public: show a menu of all items (sitemap) + */ + function show_sitemap() { + print $this->get_sitemap(); + } + + /* + * public: build a menu of all items (sitemap) + */ + function get_sitemap() { + $this->visible = array(); + + # Using PHP4, this could be faster, but we want to be portable. + reset($this->item); + while(list($k, $v) = each($this->item)) { + $this->visible[] = $k; + } + sort($this->visible); + + # create them + $str = ""; + + $start = $this->nomain?1:0; + $len = count($this->visible); + $oldlevel = 0; + $str .= $this->start_menu(); + for($i=$start; $i<$len; $i++) { + $level = count(explode("/", $this->visible[$i])); + + if ($level > $oldlevel) { + $str .= $this->shift_in($oldlevel, $level); + } + if ($level < $oldlevel) { + $str .= $this->shift_out($oldlevel, $level); + } + $oldlevel = $level; + + $str .= $this->get_cell($i, $level, $this->class); + } + $str .= $this->end_menu(); + + return $str; + } + ! /* ! * public: show a single menu cell */ function get_cell($n, $level, $class = "") { + $dent = ""; $bold = ($this->visible[$n] == $this->map); ! for ($i=1; $i<$level; $i++) { $dent .= " "; + } ! if (isset($this->item[$this->visible[$n]]["pseudo"])) { $str=sprintf("%s%s<br>\n", $dent, *************** *** 110,118 **** } ! /* public: build the title of the page based on ! its location in the menu hierarchy. */ function get_title() { global $PHP_SELF; ! unset($this->title); # Determine normalized current position in tree --- 164,174 ---- } ! /* ! * public: build the title of the current page based on its location ! * in the menu hierarchy. ! */ function get_title() { global $PHP_SELF; ! $this->title = ""; # Determine normalized current position in tree *************** *** 123,128 **** while(list($a, $b) = each($r)) { ! if ($this->title) $this->title .= $this->title_delim; $this->title .= $this->item[$b]["title"]; } --- 179,185 ---- while(list($a, $b) = each($r)) { ! if (isset($this->title) && $this->title != "") { $this->title .= $this->title_delim; + } $this->title .= $this->item[$b]["title"]; } *************** *** 131,134 **** --- 188,196 ---- } + /* + * public: These four functions can be used to customise layout. + * They are called when certain events happen during menu display. + * They should be overridden in your subclass. + */ function shift_in($oldlevel, $level) { ; } *************** *** 139,167 **** function end_menu() { ; } ! /***************************************************************************/ ! /* private: normalize current menu position */ function normalize_pos($pos) { ! if ( $m = $this->urlmap[basename($pos)] ) ! return($m); $m = $pos; ! while(substr($m, 0, 1)) { ! if ($this->urlmap[$m]) break; $m = dirname($m); } ! return $this->urlmap[$m]; } ! /* private: split a path /2/2 into components "", /2, /2/2 */ function split_path($p) { $path = ""; ! $r = explode("/", $p); reset($r); while(list($k, $v) = each($r)) { ! if ($v) $path .= "/$v"; $res[] = $path; } --- 201,236 ---- function end_menu() { ; } ! /* ! * private: normalize current menu position */ function normalize_pos($pos) { ! if ( isset($this->urlmap[basename($pos)]) ) { ! return $this->urlmap[basename($pos)]; ! } $m = $pos; ! while(strlen($m)) { ! if (isset($this->urlmap[$m])) { break; + } $m = dirname($m); + if ($m == "/") { + $m = ""; + } } ! return $this->urlmap[$m]; } ! /* ! * private: split a path /2/2 into components "", /2, /2/2 */ function split_path($p) { $path = ""; ! $r = explode("/", $p); reset($r); while(list($k, $v) = each($r)) { ! if ($v) { $path .= "/$v"; + } $res[] = $path; } *************** *** 170,192 **** } ! /* private: set up the visible array. */ function find_visible($r) { # at each level, add current children to visible ! $len = count($r); ! $this->visible= array(); ! for ($i=0; $i<$len; $i++) { # if current level has children, add them... ! if (is_array($this->children[$r[$i]]) ) { reset($this->children[$r[$i]]); while(list($k, $v) = each($this->children[$r[$i]])) { $this->visible[] = $v; ! if (isset($this->item[$v][pseudo]) && ! !ereg("^$v",$this->map)) ! while (list(,$w) = each($this->item[$v][pseudo])) { ! $this->visible[]=$w; } } } --- 239,283 ---- } ! /* ! * private: set up the visible array. */ function find_visible($r) { # at each level, add current children to visible ! $len = count($r); ! $this->visible = array(); for ($i=0; $i<$len; $i++) { # if current level has children, add them... ! if (isset($this->children[$r[$i]])) { reset($this->children[$r[$i]]); while(list($k, $v) = each($this->children[$r[$i]])) { $this->visible[] = $v; ! ! # The "pseudo" feature has not been documented in previous ! # releases. As such, it should not matter greatly that the ! # behaviour may have changed somewhat in this release. ! # Nevertheless, the changes and current behaviour are documented ! # here. ! # ! # Previously the array item $this->item[$v]["pseudo"] should have ! # contained an array of $this->item keys. The items referenced in ! # the "pseudo" array were made visible if the "pseudo" item was ! # visible. ! # ! # This has been changed somewhat. Now, if a "pseudo" array element ! # exists, the $this->children array is scanned and all children of ! # $this->item[$v] are made visible. The menu item $this->item[$v] ! # is displayed as text only, not as a hyperlink. The item acts as a ! # heading. ! ! # show children if this is a "pseudo" item ! if (isset($this->item[$v]["pseudo"]) && !ereg("^$v",$this->map)) { ! if (isset($this->children[$v]) && is_array($this->children[$v]) ) { ! reset($this->children[$v]); ! while (list($w, $ww) = each($this->children[$v])) { ! $this->visible[] = $ww; ! } } + } } } *************** *** 197,201 **** } ! /* private: find children of each menu item */ function setup() { --- 288,293 ---- } ! /* ! * private: find children of each menu item */ function setup() { *************** *** 203,206 **** --- 295,301 ---- while(list($k, $v) = each($this->urlmap)) { $base = dirname($v); + if ($base == "/") { + $base = ""; + } $this->children[$base][] = $v; $this->item[$v]["url"] = $k; |