Framework  3.9
directory_tree.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8  Copyright (c) 2007-2010 Sonjara, Inc
9 
10  Permission is hereby granted, free of charge, to any person
11  obtaining a copy of this software and associated documentation
12  files (the "Software"), to deal in the Software without
13  restriction, including without limitation the rights to use,
14  copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following
17  conditions:
18 
19  The above copyright notice and this permission notice shall be
20  included in all copies or substantial portions of the Software.
21 
22  Except as contained in this notice, the name(s) of the above
23  copyright holders shall not be used in advertising or otherwise
24  to promote the sale, use or other dealings in this Software
25  without prior written authorization.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
39 
40 class FileNode
41 {
42  var $id;
43  var $title;
44  var $value;
45 
46  function FileNode($id, $title, $value)
47  {
48  $this->id = $id;
49  $this->title = $title;
50  $this->value = $value;
51  }
52 
53 
54  function calculateOpenState()
55  {
56  return false;
57  }
58 
59  function writeHTML()
60  {
61  $icon_image = $this->_tree->getIcon($this->title);
62  $class = ($this->value == $this->_tree->selectedValue) ? "selected" : "file";
63 ?>
64  <div><img src="<?echo $this->_tree->images['toggle_blank']?>" align="absmiddle"><img src="<?echo $icon_image?>" align="absmiddle"><a id="<?echo $this->id?>_link" class="<?echo $class?>" href="javascript:DirectoryTree.selectItem('<?echo $this->_tree->id?>', '<?echo $this->id?>', '<?echo jsSafe($this->value)?>')" ondblclick="<?echo $this->_tree->id?>_onDoubleClick()"><?echo $this->title?></a></div>
65 <?
66  }
67 }
68 
69 
71 {
72  var $children = array();
73 
74  var $id;
75  var $title;
76  var $value;
77  var $checked;
80  var $open;
81 
82  function FolderNode($id, $title, $value = "", $checked = false, $closedStyle = "tree_node_closed", $openStyle = "tree_node_open", $leafStyle = "tree_node_plain")
83  {
84  $this->id = $id;
85  $this->title = $title;
86  $this->value = $value;
87  $this->checked = $checked;
88  $this->closedStyle = $closedStyle;
89  $this->openStyle = $openStyle;
90  $this->leafStyle = $leafStyle;
91  $this->open = false;
92  }
93 
94  function writeHTML()
95  {
96  ksort($this->children);
97 
98  $c = count($this->children);
99 
100  if ($c == 0)
101  {
102  echo "<div class=\"{$this->leafStyle}\"><img src=\"{$this->_tree->images['toggle_blank']}\" align=\"absmiddle\"><img src=\"{$this->_tree->images['folder_closed']}\" align=\"absbottom\">";
103  // Leaf node
104  if ($this->value)
105  {
106  if ($this->_tree->selectMode == "folder" || $this->_tree->selectMode = "both")
107  {
108  if ($this->_tree->multiSelect)
109  {
110 ?>
111  <input type="checkbox" class="checkbox" name="<? echo $this->id?>" value="<? echo $this->value?>"<? if ($this->checked) echo " checked";?>> <?echo $this->title?>
112 <?
113  }
114  else
115  {
116  $class = ($this->value == $this->_tree->selectedValue) ? "selected" : "folder";
117 ?>
118  <a id="<?echo $this->id?>_link" class="<? echo $class?>" href="javascript:DirectoryTree.selectItem('<?echo $this->_tree->id?>', '<?echo $this->id?>', '<?echo jsSafe($this->value)?>')"><?echo $this->title?></a>
119 <?
120  }
121  }
122  else
123  {
124 ?>
125  <a id="<?echo $this->id?>_link" class="folder" href="" onclick="return false;"><?echo $this->title?></a>
126 <?
127  }
128 
129  }
130 
131  echo "</div>";
132  }
133  else
134  {
135  if ($this->open)
136  {
137  $toggle = $this->_tree->images['toggle_open'];
138  $folder = $this->_tree->images['folder_open'];
139  $display = "block";
140  }
141  else
142  {
143  $toggle = $this->_tree->images['toggle_closed'];
144  $folder = $this->_tree->images['folder_closed'];
145  $display = "none";
146  }
147 ?>
148  <div id='<?echo $this->id?>'><img id="<?echo $this->id?>_toggle" src="<?echo $toggle?>" align="absmiddle" onclick="DirectoryTree.toggleDirectoryFolder('<?echo $this->id?>')"><img id="<?echo $this->id?>_folder" align="absmiddle" src="<?echo $folder?>" onclick="DirectoryTree.toggleDirectoryFolder('<?echo $this->id?>')">
149 <?
150  if ($this->value)
151  {
152  if ($this->_tree->selectMode == "folder" || $this->_tree->selectMode == "both")
153  {
154  if ($this->_tree->multiSelect)
155  {
156 ?>
157  <input type="checkbox" class="checkbox" name="<? echo $this->id?>" value="<? echo $this->value?>"<? if ($this->checked) echo " checked";?>> <?echo $this->title?>
158 <?
159  }
160  else
161  {
162  $class = ($this->value == $this->_tree->selectedValue) ? "selected" : "folder";
163 ?>
164  <a id="<?echo $this->id?>_link" class="folder" href="javascript:DirectoryTree.selectItem('<?echo $this->_tree->id?>', '<?echo $this->id?>', '<?echo jsSafe($this->value)?>')"><?echo $this->title?></a>
165 <?
166  }
167  }
168  else
169  {
170 ?>
171  <a id="<?echo $this->id?>_link" class="folder" href="javascript:DirectoryTree.toggleDirectoryFolder('<?echo $this->id?>')"><?echo $this->title?></a>
172 <?
173  } }
174 ?>
175  </div>
176  <div id='<?echo $this->id?>_contents' style='padding-left: 16px; display: <? echo $display?>'>
177 
178 <?
179  foreach($this->children as $key => $child)
180  {
181  $child->_tree =& $this->_tree;
182  $child->writeHTML();
183  }
184 ?>
185  </div>
186 <?
187  }
188  }
189 
190  function add(&$n)
191  {
192  if (!$n) return;
193 
194  trace($this->id." adding ".$n->id, 3);
195  if (is_array($n))
196  {
197  for($i = 0; $i < count($n); ++$i)
198  {
199  $this->children[$n[$i]->title] = $n[$i];
200  }
201  }
202  else
203  {
204  $this->children[$n->title] = $n;
205  }
206  }
207 
209  {
210  if ($this->open) return true;
211  if ($this->checked)
212  {
213  $this->open = true;
214  return true;
215  }
216 
217  $c = count($this->children);
218 
219  foreach($this->children as $key => $child)
220  {
221  if ($child->calculateOpenState())
222  {
223  $this->open = true;
224  return true;
225  }
226  }
227 
228  $this->open = false;
229  return false;
230  }
231 }
232 
233 
235 {
236  var $id;
237  var $title;
238  var $style;
239  var $scroll;
240  var $width;
241  var $height;
242  var $showFiles = true;
244  var $multiSelect = false;
245  var $selectMode = "folder";
246 
248  var $images = array("toggle_open" => "/fakoli/images/toggle_open.png",
249  "toggle_closed" => "/fakoli/images/toggle_closed.png",
250  "toggle_blank" => "/fakoli/images/toggle_blank.png",
251  "folder_open" => "/fakoli/images/folder_open.png",
252  "folder_closed" => "/fakoli/images/folder_closed.png",
253  "generic_file" => "/fakoli/images/file.png");
254 
255  var $icons = array( ".doc" => "/fakoli/images/msword_icon.png",
256  ".docx" => "/fakoli/images/msword_icon.png",
257  ".xls" => "/fakoli/images/msexcel_icon.png",
258  ".xlsx" => "/fakoli/images/msexcel_icon.png",
259  ".pdf" => "/fakoli/images/pdf_icon.png",
260  ".ppt" => "/fakoli/images/ppt_icon.png",
261  ".pptx" => "/fakoli/images/ppt_icon.png",
262  ".gif" => "/fakoli/images/image_icon.png",
263  ".png" => "/fakoli/images/image_icon.png",
264  ".jpg" => "/fakoli/images/image_icon.png",
265  "default" => "/fakoli/images/file_icon.png");
266 
267 
268  function DirectoryTreeControl($id, $title = "", $showFiles = true, $style="tree", $scroll=true,$width=500,$height=400)
269  {
270  $this->id = $id;
271  $this->title = $title;
272  $this->style = $style;
273  $this->scroll = $scroll;
274  $this->width = $width;
275  $this->height = $height;
276  $this->showFiles = $showFiles;
277  }
278 
279 
280  function buildFolderTree($folder, $base)
281  {
282  trace("Building Folder Tree: $folder, $base", 3);
283  $root = $this->_buildFolderTreeAux($folder, $base);
284  $this->children[$folder] = $root;
285  }
286 
287  function _buildFolderTreeAux($folder, $base)
288  {
289  $f = $base . DIRECTORY_SEPARATOR . $folder;
290  trace("Directory tree: buildFolderTreeAux f $f", 3);
291  $dir = opendir($f);
292 
293  $id = $this->id."_".str_replace(DIRECTORY_SEPARATOR, "_", $folder);
294  if ($this->permissionCallback && !call_user_func($this->permissionCallback, $folder)) return null;
295 
296  $node = new FolderNode($id, basename($folder), $folder);
297  while( $directory = readdir($dir))
298  {
299  if ($directory == "." || $directory == "..") continue;
300 
301  if (is_dir($base . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $directory))
302  {
303  $node->add($this->_buildFolderTreeAux($folder . DIRECTORY_SEPARATOR . $directory, $base));
304  }
305  }
306 
307  closedir($dir);
308  $this->folders[$folder] = $node;
309 
310  return $node;
311  }
312 
313  function addFiles($folder, $base, $filter = null)
314  {
315  $f = $base . DIRECTORY_SEPARATOR . $folder;
316  $dir = opendir($f);
317 
318  $id = str_replace(DIRECTORY_SEPARATOR, "_", $folder);
319  if ($this->permissionCallback && !call_user_func($this->permissionCallback, $folder)) return null;
320 
321  while( $file = readdir($dir))
322  {
323  if ($file == "." || $file == "..") continue;
324 
325  $f = $base . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $file;
326  if (is_dir($f))
327  {
328  $this->addFiles($folder . DIRECTORY_SEPARATOR . $file, $base, $filter);
329  }
330  else if (is_file($f))
331  {
332  if ($filter && !call_user_func($filter, $f))
333  {
334  continue;
335  }
336 
337  trace("$f", 3);
338 
339  $node = new FileNode($id . "_" . str_replace(".", "_", $file), $file, $folder . DIRECTORY_SEPARATOR . $file);
340  $this->findFolder($folder)->add($node);
341  }
342  }
343  }
344 
345  function writeScript()
346  {
347  // Event Hooks
348 ?>
349 <script type="text/javascript">
350 function <?echo $this->id?>_onSelectItem(itemID, itemValue)
351 {
352 <?
353  if ($this->onSelectionChanged)
354  {
355 ?>
356  <?echo $this->onSelectionChanged?>(itemID, itemValue);
357 <?
358  }
359 ?>
360 }
361 
362 function <?echo $this->id?>_onClearSelection()
363 {
364 <?
365  if ($this->onSelectionChanged)
366  {
367 ?>
368  <? echo $this->onSelectionChanged?>(null, null);
369 <?
370  }
371 ?>
372 }
373 
374 function <?echo $this->id?>_onDoubleClick(itemID, itemValue)
375 {
376 <?
377  if ($this->onDoubleClick)
378  {
379 ?>
380  <? echo $this->onDoubleClick?>(itemID, itemValue);
381 <?
382  }
383 ?>
384 }
385 
386 </script>
387 <?
388  }
389 
390  function writeHTML()
391  {
392  $width = is_numeric($this->width) ? "{$this->width}px" : $this->width;
393  $height = is_numeric($this->height) ? "{$this->height}px" : $this->height;
394  //ksort($this->children);
395  $this->writeScript();
396 ?>
397  <input type="hidden" name="<?echo $this->id?>_value" id="<?echo $this->id?>_value" value="<?echo $this->selectedValue?>"/>
398  <input type="hidden" name="<?echo $this->id?>_node" id="<?echo $this->id?>_node" value=""/>
399  <table id="<?echo $this->id?>" class="<? echo $this->style?>" style="width: <? echo $width?>;height: <?echo $height?>" cellpadding="0" cellspacing="0">
400  <?
401  if ($this->title)
402  {
403  ?>
404  <tr>
405  <th><? echo $this->title?></th>
406  </tr>
407  <?
408  }
409  ?>
410  <tr>
411  <td>
412  <div style="margin: 0; padding: 0;width: 100%;height: <?echo $height?>;<?if ($this->scroll) echo "overflow: auto"?>">
413 <?
414  $c = count($this->children);
415  foreach($this->children as $key => $child)
416  {
417  trace("Writing $key,{$child->title}", 3);
418  try
419  {
420  $child->_tree =& $this;
421  $child->calculateOpenState();
422  $child->writeHTML();
423  }
424  catch(Error $e)
425  {
426  var_dump($child);
427  throw $e;
428  }
429  }
430 ?>
431  </div>
432  </td>
433  </tr>
434  </table>
435 <?
436  }
437 
438  function getIcon($file)
439  {
440  $ext = substr($file, strrpos($file, "."));
441  $icon = $this->icons[$ext];
442  trace("getIcon($file) $ext $icon", 4);
443 
444  if (!isset($icon))
445  {
446  $icon = $this->icons["default"];
447  }
448 
449  return $icon;
450  }
451 
452  function add(&$n)
453  {
454  trace($this->id." adding ".$n->id, 3);
455  if (is_array($n))
456  {
457  for($i = 0; $i < count($n); ++$i)
458  {
459  $this->children[$n[$i]->title] = &$n[$i];
460  }
461  }
462  else
463  {
464  $this->children[$n->title] =& $n;
465  }
466  }
467 
468  function findFolder($path)
469  {
470  return $this->folders[$path];
471  }
472 
473  function &findNode($path)
474  {
475  trace("DirectoryTree:findNode($path)", 4);
476  $elts = explode(DIRECTORY_SEPARATOR, $path);
477 
478  $node =& $this;
479 
480  foreach($elts as $elt)
481  {
482  $match = false;
483 
484  if (isset($node->children[$elt]))
485  {
486  $node =& $node->children[$elt];
487  }
488  else return null;
489  }
490 
491  if ($node === $this) $node = null;
492 
493  trace("Found Node: {$node->id}", 3);
494  return $node;
495  }
496 }
497 ?>
DirectoryTreeControl($id, $title="", $showFiles=true, $style="tree", $scroll=true, $width=500, $height=400)
_buildFolderTreeAux($folder, $base)
FolderNode($id, $title, $value="", $checked=false, $closedStyle="tree_node_closed", $openStyle="tree_node_open", $leafStyle="tree_node_plain")
addFiles($folder, $base, $filter=null)
buildFolderTree($folder, $base)
FileNode($id, $title, $value)
$_writtenDirectoryTreeControlScript
calculateOpenState()
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010