Framework  3.9
tree.inc
Go to the documentation of this file.
1 <?php
5 /**************************************************************
6 
7  Copyright (c) 2007-2010 Sonjara, Inc
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  Except as contained in this notice, the name(s) of the above
22  copyright holders shall not be used in advertising or otherwise
23  to promote the sale, use or other dealings in this Software
24  without prior written authorization.
25 
26  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  OTHER DEALINGS IN THE SOFTWARE.
34 
35 *****************************************************************/
36 
49 
53 $_activeTree = null;
54 
55 
61 class TreeNode
62 {
63  var $children = array();
64 
65  var $id;
66  var $title;
67  var $value;
68  var $checked;
72  var $open;
73  var $link;
74  var $onDemand;
75  var $disabled;
76  var $target;
77  var $extras;
78 
91  function TreeNode($id, $title, $value = "", $checked = false, $closedStyle = "tree_node_closed", $openStyle = "tree_node_open", $link = null, $target="")
92  {
93  $this->id = $id;
94  $this->title = $title;
95  $this->value = $value;
96  $this->checked = $checked;
97  $this->closedStyle = $closedStyle;
98  $this->openStyle = $openStyle;
99  $this->open = false;
100  $this->link = $link;
101  $this->leafStyle = "tree_node_leaf";
102  $this->onDemand = "";
103  $this->disabled = false;
104  $this->target = $target;
105  $this->extras = "";
106  }
107 
112  function writeHTML()
113  {
114  global $_activeTree;
115 
116  $c = count($this->children);
117 
118  if ($this->link)
119  {
120  $title = "<a href='{$this->link}' target='{$this->target}'>{$this->title}</a>{$this->extras}";
121  }
122  else
123  {
124  $title = "<a href='#' onclick='";
125 
126  if ($_activeTree->onSelect)
127  {
128  $title .= "{$_activeTree->onSelect}(\"{$this->value}\");";
129  }
130  else if ($_activeTree->selectMode != 'navigation')
131  {
132  $title .= "new DOMEvent(event).stop(); Tree.toggleCheckbox(this, \"{$_activeTree->id}\", \"{$_activeTree->selectMode}\")";
133  }
134  $title .= "; return false'";
135 
136  if ($_activeTree->selectOnRightClick)
137  {
138  $title .= " oncontextmenu='Tree.selectCheckbox(this, \"{$_activeTree->id}\", \"{$_activeTree->selectOnRightClickMode}\");'";
139  }
140 
141  $title .= ">{$this->title}</a>{$this->extras}";
142  }
143 
144  if ($c == 0 && !$this->onDemand)
145  {
146  // Leaf node
147  echo "<div id='{$this->id}' class='{$this->leafStyle}'>";
148 
149  if (isset($this->value) && $this->value !== "" && $_activeTree->selectMode != 'navigation')
150  {
151 ?>
152  <input type="checkbox" class="checkbox" name="<? echo $this->id?>" value="<? echo $this->value?>"<?
153  if ($this->checked) echo " checked";
154  if ($this->disabled) echo " disabled";
155  if ($_activeTree->selectMode == "single")
156  {
157  echo " onclick=\"Tree.clearCheckBoxes('{$_activeTree->id}', this);";
158  if ($_activeTree->onSelect)
159  {
160  echo " {$_activeTree->onSelect}('{$this->value}');";
161  }
162  echo "\"";
163  }
164  else if ($_activeTree->onSelect)
165  {
166  echo " onclick=\"{$_activeTree->onSelect}('{$this->value}');\"";
167  }
168  ?>/>
169 <?
170  }
171 
172  echo "$title</div>";
173  }
174  else
175  {
176  if ($this->open)
177  {
178  $style = $this->openStyle;
179  $display = "block";
180  }
181  else
182  {
183  $style = $this->closedStyle;
184  $display = "none";
185  }
186 
187  if ($this->onDemand)
188  {
189  $cmd = "Tree.loadOnDemand('{$this->id}', '{$this->onDemand}');";
190 
191  if ($this->open)
192  {
193 ?>
194 <script type="text/javascript">
195  window.addEvent('domready', function() {Tree.loadOnDemand('<?echo $this->id?>', '<?echo $this->onDemand?>');});
196 </script>
197 <?
198  }
199  }
200  else
201  {
202  $cmd = "";
203  }
204 
205  $cmd .= "Tree.toggleFolder('{$this->id}', '{$this->openStyle}', '{$this->closedStyle}');";
206 ?>
207  <div id='<?echo $this->id?>' class='<?echo $style?>' onclick="<?echo $cmd ?>">
208 <?
209  if (isset($this->value) && $this->value !== "" && $_activeTree->selectMode != 'navigation')
210  {
211 ?>
212  <input type="checkbox" class="checkbox" name="<? echo $this->id?>" value="<? echo $this->value?>" <?
213  if ($this->checked) echo " checked";
214  if ($this->disabled) echo " disabled";
215  if ($_activeTree->selectMode == "single")
216  {
217  echo " onclick=\"Tree.clearCheckBoxes('{$_activeTree->id}', this);";
218  if ($_activeTree->onSelect)
219  {
220  echo " {$_activeTree->onSelect}('{$this->value}');";
221  }
222  echo "\"";
223  }
224  else if ($_activeTree->onSelect)
225  {
226  echo " onclick=\"{$_activeTree->onSelect}('{$this->value}');\"";
227  }
228  ?>/>
229 <?
230  }
231 ?>
232  <?echo $title?></div>
233  <div id='<?echo $this->id?>_contents' style='padding-left: <?echo $_activeTree->indent ?>; display: <? echo $display?>'>
234 
235 <?
236  for($i = 0; $i < $c; ++$i)
237  {
238  $this->children[$i]->writeHTML();
239  }
240 ?>
241  </div>
242 <?
243  }
244  }
245 
251  function add($n)
252  {
253  if (is_array($n))
254  {
255  for($i = 0; $i < count($n); ++$i)
256  {
257  $this->children[] = $n[$i];
258  }
259  }
260  else
261  {
262  $this->children[] = $n;
263  }
264  }
265 
272  {
273  if ($this->open) return true;
274  if ($this->checked)
275  {
276  $this->open = true;
277  return true;
278  }
279 
280  $c = count($this->children);
281 
282  for($i = 0; $i < $c; ++$i)
283  {
284  if ($this->children[$i]->calculateOpenState())
285  {
286  $this->open = true;
287  return true;
288  }
289  }
290 
291  $this->open = false;
292  return false;
293  }
294 
296  {
297  if ($value === '')
298  {
299  $this->checked = false;
300  }
301  else if ($this->value == $value)
302  {
303  $this->checked = true;
304  }
305 
306  $c = count($this->children);
307 
308  for($i = 0; $i < $c; ++$i)
309  {
310  $this->children[$i]->selectByValue($value);
311  }
312  }
313 }
314 
322 {
323  var $id;
324  var $title;
325  var $style;
326  var $scroll;
327  var $width;
328  var $height;
329  var $selectMode = "multi";
330  var $value;
331  var $indent = "16px";
333  var $selectOnRightClick = false;
334  var $selectOnRightClickMode = "context";
335 
347  function TreeControl($id, $title = "", $style="tree", $scroll=true,$width=500,$height=400)
348  {
349  $this->id = $id;
350  $this->title = $title;
351  $this->style = $style;
352  $this->scroll = $scroll;
353  $this->width = $width;
354  $this->height = $height;
355  }
356 
361  function writeScript()
362  {
363  }
364 
368  function writeHTML()
369  {
370  global $_activeTree;
371 
372  $_activeTree= $this;
373 
374  $this->writeScript();
375  $width = is_numeric($this->width) ? "{$this->width}px" : $this->width;
376  $height = is_numeric($this->height) ? "{$this->height}px" : $this->height;
377 
378  if ($this->selectMode == "single")
379  {
380 ?>
381 <input type='hidden' name='<?echo $this->id ?>' id='<?echo $this->id?>' value='<?echo $this->value ?>'/>
382 <?
383  }
384 ?><table id="<?echo $this->id?>_table" class="<? echo $this->style?>" cellpadding="0" cellspacing="0" <? if ($this->width) echo "style='width:{$width}'"; ?>>
385  <?
386  if ($this->title)
387  {
388  ?>
389  <tr>
390  <th><? echo $this->title?></th>
391  </tr>
392  <?
393  }
394 
395  $w = $width ? $width : "100%";
396  ?>
397  <tr>
398  <td>
399  <div style="padding: 0; margin: 0; width: <? echo $w?>;<? if ($this->height > 0) { ?> height: <?echo $this->height?>px;<? } ?><?if ($this->scroll) echo "overflow: auto"?>">
400 <?
401  $this->writeNodes();
402 ?>
403  </div>
404  </td>
405  </tr>
406  </table>
407 <?
408  $_activeTree = null;
409  }
410 
411  function setValue($value)
412  {
413  $this->value = $value;
414  $c = count($this->children);
415 
416  for($i = 0; $i < $c; ++$i)
417  {
418  $this->children[$i]->selectByValue($value);
419  }
420  }
421 
422  function writeNodes()
423  {
424  $c = count($this->children);
425 
426  for($i = 0; $i < $c; ++$i)
427  {
428  $this->children[$i]->calculateOpenState();
429  $this->children[$i]->writeHTML();
430  }
431  }
432 
438  function add($n)
439  {
440  if (is_array($n))
441  {
442  for($i = 0; $i < count($n); ++$i)
443  {
444  $this->children[] = $n[$i];
445  }
446  }
447  else
448  {
449  $this->children[] = $n;
450  }
451  }
452 }
453 ?>
The TreeControl provides a class that can be used to generate dynamic tree controls for use in PHP fo...
Definition: tree.inc:322
add($n)
Adds a child TreeNode (or an array of children)
Definition: tree.inc:438
writeHTML()
Outputs the TreeControl as HTML to the current output buffer.
Definition: tree.inc:368
setValue($value)
Definition: tree.inc:411
$selectOnRightClickMode
Definition: tree.inc:334
TreeControl($id, $title="", $style="tree", $scroll=true, $width=500, $height=400)
Create a new TreeControl.
Definition: tree.inc:347
writeScript()
Definition: tree.inc:361
$selectOnRightClick
Definition: tree.inc:333
writeNodes()
Definition: tree.inc:422
TreeNode represents a node in the tree.
Definition: tree.inc:62
$closedStyle
Definition: tree.inc:69
selectByValue($value)
Definition: tree.inc:295
$onDemand
Definition: tree.inc:74
add($n)
Add a child TreeNode (or an array of children)
Definition: tree.inc:251
$extras
Extra data/controls to be shown with this tree node.
Definition: tree.inc:77
$link
Definition: tree.inc:73
$leafStyle
Definition: tree.inc:71
$disabled
Definition: tree.inc:75
$open
Definition: tree.inc:72
$title
Definition: tree.inc:66
$openStyle
Definition: tree.inc:70
$target
Definition: tree.inc:76
$children
Definition: tree.inc:63
$checked
Definition: tree.inc:68
writeHTML()
Write the node out as HTML to the output buffer.
Definition: tree.inc:112
TreeNode($id, $title, $value="", $checked=false, $closedStyle="tree_node_closed", $openStyle="tree_node_open", $link=null, $target="")
Create a new TreeNode.
Definition: tree.inc:91
$value
Definition: tree.inc:67
calculateOpenState()
Recursively determine whether the branch of the tree of which this node is the root should be display...
Definition: tree.inc:271
$_writtenTreeControlScript
This file provides a class-based API for generating expanding tree controls.
Definition: tree.inc:48
$_activeTree
Global reference to currently drawing tree instance.
Definition: tree.inc:53