added docs to the index in the doc/ directory
[squirrelmail.git] / functions / tree.php
1 <?
2 $tree_php = true;
3
4 if (!isset($imap_php))
5 include("../functions/imap.php");
6
7 // Recursive function to find the correct parent for a new node
8 function findParentForChild($value, $treeIndexToStart, $tree) {
9 // is $value in $tree[$treeIndexToStart]["value"]
10 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]["value"]))) {
11 // do I have children, if not then must be a childnode of the current node
12 if ($tree[$treeIndexToStart]["doIHaveChildren"]) {
13 // loop through each subNode checking to see if we are a subNode of one of them
14 for ($i=0;$i< count($tree[$treeIndexToStart]["subNodes"]);$i++) {
15 $result = findParentForChild($value, $tree[$treeIndexToStart]["subNodes"][$i], $tree);
16 if ($result > -1)
17 return $result;
18 }
19 // if we aren't a child of one of the subNodes, must be a child of current node
20 return $treeIndexToStart;
21 } else
22 return $treeIndexToStart;
23 } else {
24 // we aren't a child of this node at all
25 return -1;
26 }
27 }
28
29 function addChildNodeToTree($comparisonValue, $value, &$tree) {
30 $parentNode = findParentForChild($comparisonValue, 0, $tree);
31
32 // create a new subNode
33 $newNodeIndex = count($tree);
34 $tree[$newNodeIndex]["value"] = $value;
35 $tree[$newNodeIndex]["doIHaveChildren"] = false;
36
37 if ($tree[$parentNode]["doIHaveChildren"] == false) {
38 // make sure the parent knows it has children
39 $tree[$parentNode]["subNodes"][0] = $newNodeIndex;
40 $tree[$parentNode]["doIHaveChildren"] = true;
41 } else {
42 $nextSubNode = count($tree[$parentNode]["subNodes"]);
43 // make sure the parent knows it has children
44 $tree[$parentNode]["subNodes"][$nextSubNode] = $newNodeIndex;
45 }
46 }
47
48 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
49 if ($tree[$index]["doIHaveChildren"]) {
50 for ($j = 0; $j < count($tree[$index]["subNodes"]); $j++) {
51 walkTreeInPreOrderDeleteFolders($tree[$index]["subNodes"][$j], $imap_stream, $tree);
52 }
53 sqimap_mailbox_delete($imap_stream, $tree[$index]["value"]);
54 } else {
55 sqimap_mailbox_delete($imap_stream, $tree[$index]["value"]);
56 }
57 }
58
59 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $dm, $topFolderName) {
60 global $trash_folder;
61
62 $position = strrpos($topFolderName, $dm) + 1;
63 $subFolderName = substr($tree[$index]["value"], $position);
64
65 if ($tree[$index]["doIHaveChildren"]) {
66 sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, "");
67 sqimap_mailbox_select($imap_stream, $tree[$index]["value"]);
68
69 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]["value"]);
70 if ($messageCount > 0)
71 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $dm . $subFolderName);
72
73 for ($j = 0;$j < count($tree[$index]["subNodes"]); $j++)
74 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]["subNodes"][$j], $imap_stream, $tree, $dm, $topFolderName);
75 } else {
76 sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, "");
77 sqimap_mailbox_select($imap_stream, $tree[$index]["value"]);
78
79 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]["value"]);
80 if ($messageCount > 0)
81 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $dm . $subFolderName);
82 }
83 }
84
85 function simpleWalkTreePre($index, $tree) {
86 if ($tree[$index]["doIHaveChildren"]) {
87 for ($j = 0; $j < count($tree[$index]["subNodes"]); $j++) {
88 simpleWalkTreePre($tree[$index]["subNodes"][$j], $tree);
89 }
90 echo $tree[$index]["value"] . "<br>";
91 } else {
92 echo $tree[$index]["value"] . "<br>";
93 }
94 }
95 ?>