summaryrefslogtreecommitdiff
path: root/backend/php/src/setup/setup_library/xPandMenu.php
Unidiff
Diffstat (limited to 'backend/php/src/setup/setup_library/xPandMenu.php') (more/less context) (ignore whitespace changes)
-rw-r--r--backend/php/src/setup/setup_library/xPandMenu.php233
1 files changed, 233 insertions, 0 deletions
diff --git a/backend/php/src/setup/setup_library/xPandMenu.php b/backend/php/src/setup/setup_library/xPandMenu.php
new file mode 100644
index 0000000..d9c7d07
--- a/dev/null
+++ b/backend/php/src/setup/setup_library/xPandMenu.php
@@ -0,0 +1,233 @@
1<?php
2
3/********************************
4* xPandMenu MULTI-LEVEL class
5*********************************
6* Creates a tree-view menu.
7* The menu can be as deep as needed
8* The menu items are organised in HTML unordered lists
9* Container nodes can be expanded/collapsed thanks to Javascript actions
10*********************************
11* Patrick Brosset
12* patrickbrosset@gmail.com
13*********************************
14* 02/2005
15*********************************/
16
17
18
19// Path to default image files (directories and documents icons) -- (use absolute URL)
20define('NODE_DEFAULT_IMG','http://project.zoe.co.nz/patrick/xpand/multi/images/folder_win.gif');
21define('LEAF_DEFAULT_IMG','http://project.zoe.co.nz/patrick/xpand/multi/images/document_win.gif');
22define('NODE_DEFAULT_ALT_IMG','http://project.zoe.co.nz/patrick/xpand/multi/images/folder_win_o.gif');
23define('LEAF_DEFAULT_ALT_IMG','http://project.zoe.co.nz/patrick/xpand/multi/images/document_win_o.gif');
24
25// Reference to the File class for saving and loading the generated menu
26//include_once 'File.php';
27
28
29
30// Xmenu class
31class XMenu
32{
33
34
35 // Sub-nodes contained in this menu (references to Xnode objects)
36 var $items = array();
37
38 // Keeps track of the HTML code indent to use (formatting of UL and LI)
39 var $indent;
40
41 // Is it the first node ?
42 var $first;
43
44 // Used for assigning unique IDs to HTML elements (for the javascript function)
45 var $treeCount;
46
47 // Same for images
48 var $imgCount;
49
50 // Contains the generated HTML code
51 var $output;
52
53 // Contains the nodes to expand when generating tree
54 var $visibleNodes = array("1");
55
56
57
58 // Constructor
59 function XMenu()
60 {
61 $this->indent = 0;
62 $this->first = true;
63 $this->treeCount = 0;
64 $this->imgCount = 0;
65 $this->output = "";
66 }
67
68
69
70 // addItem, adds a child to this menu
71 // Takes a XNode object reference as argument
72 function &addItem(&$node)
73 {
74 $this->items[] = &$node;
75 return $this->items[count($this->items) - 1];
76 }
77
78
79
80 // generateTree, generates the HTML code (UL list) for the dynamic tree-view
81 function generateTree($root = false)
82 {
83 if(!$root)$root = $this;
84
85 if($this->first){
86 $this->output .= $this->codeIndent()."<ul id=\"XRoot\" class=\"XtreeRoot\">\n";
87 $this->first = false;
88 }else{
89 if (array_search($this->treeCount, $this->visibleNodes) !== false)
90 {
91 $this->output .= $this->codeIndent()."<ul id=\"Xtree".$this->treeCount."\" class=\"Xtree\" style=\"display:block;\">\n";
92 }
93 else
94 {
95 $this->output .= $this->codeIndent()."<ul id=\"Xtree".$this->treeCount."\" class=\"Xtree\" style=\"display:none;\">\n";
96 }
97 }
98 $this->treeCount ++;
99 foreach($root->items as $myChild){
100 $this->imgCount ++;
101 if($myChild->img){
102 if($myChild->alt_img){
103 $img_js = "xSwapImg(document.getElementById('Ximg".$this->imgCount."'),'".$myChild->img."','".$myChild->alt_img."');";
104 }else{
105 $img_js = "";
106 }
107 if (array_search($this->treeCount, $this->visibleNodes) !== false)
108 {
109 $img = "<img onClick=\"".$img_js."xMenuShowHide(document.getElementById('Xtree".$this->treeCount."'));\" id=\"Ximg".$this->imgCount."\" src=\"".$myChild->alt_img."\" border=\"0\">&nbsp;&nbsp;";
110 }
111 else
112 {
113 $img = "<img onClick=\"".$img_js."xMenuShowHide(document.getElementById('Xtree".$this->treeCount."'));\" id=\"Ximg".$this->imgCount."\" src=\"".$myChild->img."\" border=\"0\">&nbsp;&nbsp;";
114 }
115 }else{
116 $img = "";$img_js = "";
117 }
118 if($myChild->link){
119 $href_open = "<a href=\"".$myChild->link."\">";
120 $href_close = "</a>";
121 }else{
122 $href_open = "";
123 $href_close = "";
124 }
125 if(count($myChild->items) != 0){
126 $this->output .= $this->codeIndent()."<li class=\"Xnode\" id=\"Xnode".$this->treeCount."\"><div>".$href_open.$img.$myChild->name.$href_close."</div></li>\n";
127 $this->indent ++;
128 $this->generateTree($myChild);
129 }else{
130 $this->output .= $this->codeIndent()."<li class=\"Xleaf\"><div onClick=\"".$img_js."\">".$href_open.$img.$myChild->name.$href_close."</div></li>\n";
131 }
132 }
133 $this->output .= $this->codeIndent()."</ul>\n";
134 $this->indent --;
135
136 return $this->output;
137 }
138
139
140
141 // saveTree and restoreTree - thanks to Niels Fanoe (niels.f@noee.dk) for giving me the idea
142
143 // saveTree, save the generated HTML code to a file for future use without generating again
144 function saveTree($filename = "xMenuCache.html")
145 {
146 $file = new File();
147 $file->write($this->output,$filename);
148 $file->printError();
149 return $filename;
150 }
151
152
153
154 // restoreTree, returns the previously generated HTML code stored in a file
155 // Call this method STATICALLY for easier use: XMenu::restoreTree("xPandMenuCode.html");
156 function restoreTree($filename = "xMenuCache.html")
157 {
158 $file = new File();
159 $menu = $file->read($filename);
160 $error = $file->getError();
161 if(!empty($error))return false;
162 elsereturn $menu;
163 }
164
165
166
167 // codeIndent, only used to create a nice and readable HTML code (indents the UL and LI tags)
168 function codeIndent()
169 {
170 $str = "";
171 for($i=0;$i<$this->indent;$i++){
172 $str .= "";
173 }
174 return $str;
175 }
176
177
178}
179
180
181
182
183// XNode class: A node item in the menu
184class XNode
185{
186
187
188 // Name assigned to this node (Text shown on the item)
189 var $name;
190
191 // Link for the item (if any)
192 var $link;
193
194 // Sub-items of this node
195 var $items = array();
196
197 // Absolute URL of this node's icon
198 var $img;
199
200 // Absolute URL of this node's icon (alternate, used for expanded and collapsed states)
201 var $alt_img;
202
203
204
205 // constructor
206 // $name: text shown for this item
207 // $link: where does this item links to when clicked (optional)
208 // $img and $alt_img: images displayed next to this item (absolute paths to images must be used)
209 function XNode($name,$link = false,$img = LEAF_DEFAULT_IMG,$alt_img = LEAF_DEFAULT_ALT_IMG)
210 {
211 $this->name = $name;
212 $this->link = $link;
213 $this->img = $img;
214 $this->alt_img = $alt_img;
215 }
216
217
218
219 // addItem, adds a subnode under this node
220 // Takes a XNode object reference as argument
221 function &addItem(&$node)
222 {
223 if($this->img == LEAF_DEFAULT_IMG){$this->img = NODE_DEFAULT_IMG;}
224 if($this->alt_img == LEAF_DEFAULT_ALT_IMG){$this->alt_img = NODE_DEFAULT_ALT_IMG;}
225 $this->items[] = &$node;
226 return $this->items[count($this->items) - 1];
227 }
228
229
230
231}
232
233?> \ No newline at end of file