00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 class MenuItem
00027 {
00028 var $id;
00029 var $caption;
00030 var $link;
00031 var $parent;
00032 var $children;
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)
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 ?>