class.MenuItem.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003 This file  is part  of HGSM.   HGSM is a  web application  for keeping
00004 information about a hierarchical structure (in this case a grid).
00005 
00006 Copyright 2005, 2006 Dashamir Hoxha, dashohoxha@users.sourceforge.net
00007 
00008 HGSM is free software; you  can redistribute it and/or modify it under
00009 the terms of  the GNU General Public License as  published by the Free
00010 Software  Foundation; either  version 2  of the  License, or  (at your
00011 option) any later version.
00012 
00013 HGSM is  distributed in the hope  that it will be  useful, but WITHOUT
00014 ANY WARRANTY; without even  the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License
00016 for more details.
00017 
00018 You  should have received  a copy  of the  GNU General  Public License
00019 along with HGSM; if not,  write to the Free Software Foundation, Inc.,
00020 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021 */
00022 
00026 class MenuItem
00027 {
00028   var $id;       //identifies this menu element
00029   var $caption;  //text that will be shown in the menu
00030   var $link;     //link associated with this menu item
00031   var $parent;   //parent of this element in the hierarchy of the menu
00032   var $children; //array of subelements
00033 
00034   function MenuItem($id =UNDEFINED, $caption ='menu item', $link =UNDEFINED)
00035     {
00036       $this->id = $id ;
00037       $this->caption = $caption;
00038       if ($link=='')  $link=UNDEFINED;
00039       $this->link = $link;
00040       $this->parent = UNDEFINED;
00041       $this->children = array();
00042     }
00043 
00044   function add_child($child)
00045     {
00046       $this->children[] = $child;
00047       $child->parent = &$this;
00048     }
00049         
00050   function nr_children()
00051     {
00052       return sizeof($this->children);
00053     }
00054 
00055   function has_children()
00056     {
00057       $nr_ch = $this->nr_children();
00058       return ($nr_ch <> 0);
00059     }
00060 
00064   function level()
00065     {
00066       $level = 0;
00067       $item = &$this;
00068       while ($item->parent<>UNDEFINED);
00069         {
00070           $item = &$item->parent;
00071           $level++;
00072         }
00073 
00074       return $level;
00075     }
00076 
00078         
00080   function to_xml($indent)
00081     {
00082       $xml_menu = $indent.'<item id="'.$this->id.'"'
00083         . ' caption="'.$this->caption.'"';
00084       if ($this->link<>UNDEFINED)
00085         {
00086           $xml_menu .= ' link="'.$this->link.'"';
00087         }
00088 
00089       if (!$this->has_children())
00090         {
00091           $xml_menu .= " />\n";
00092         }
00093       else
00094         {
00095           $xml_menu .= ">\n";
00096           $xml_menu .= $this->children_to_xml("  ".$indent);
00097           $xml_menu .= $indent."</item>\n";
00098         }
00099       return $xml_menu;
00100     }
00101 
00103   function children_to_xml($indent)
00104     {
00105       for ($i=0; $i < $this->nr_children(); $i++)
00106         {
00107           $child = $this->children[$i];
00108           $xml_menu .= $child->to_xml($indent);
00109         }
00110       return $xml_menu;
00111     }
00112 
00117   function to_text($indent)                //debug
00118     {
00119       $txt_menu = $indent . "> '".$this->id."' -> '".$this->caption."'";
00120       if ($this->link<>UNDEFINED)
00121         {
00122           $txt_menu .= " -> '".$this->link."'";
00123         }
00124       $txt_menu .= "\n";
00125 
00126       for ($i=0; $i < $this->nr_children(); $i++)
00127         {
00128           $child = $this->children[$i];
00129           $txt_menu .= $child->to_text("    ".$indent);
00130         }
00131 
00132       return $txt_menu;
00133     }
00134 
00139   function to_js_arr($indent)
00140     {
00141       $caption = "'$this->caption'";
00142       $link = $this->link;
00143       if ($link==UNDEFINED)  $link = "javascript:select(\'$this->id\')";
00144       if ($link!='null')     $link = "'javascript:$link'";
00145 
00146       $js_arr = $indent."[$caption, $link, null";
00147 
00148       if (!$this->has_children())
00149         {
00150           $js_arr .= "],\n";
00151         }
00152       else
00153         {
00154           $js_arr .= ",\n";
00155           for ($i=0; $i < $this->nr_children(); $i++)
00156             {
00157               $child = $this->children[$i];
00158               $js_arr .= $child->to_js_arr('  '.$indent);
00159             }
00160           $js_arr .= $indent." ],\n";
00161         }
00162 
00163       return $js_arr;
00164     }
00165 }
00166 ?>

Generated on Fri Jan 20 10:34:52 2006 for HGSM by  doxygen 1.4.5