tree.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?
  2. $tree_php = true;
  3. if (!isset($imap_php))
  4. include("../functions/imap.php");
  5. function findParentForChild($value, $treeIndexToStart, $tree) {
  6. if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]["value"]))) {
  7. if ($tree[$treeIndexToStart]["doIHaveChildren"]) {
  8. for ($i=0;$i< count($tree[$treeIndexToStart]["subNodes"]);$i++) {
  9. $result = findParentForChild($value, $tree[$treeIndexToStart]["subNodes"][$i], $tree);
  10. if ($result > -1) {
  11. echo "parent for $value is : " . $tree[$treeIndexToStart]["subNodes"][$i] . "<br>";
  12. return $result;
  13. }
  14. }
  15. return $treeIndexToStart;
  16. } else
  17. return $treeIndexToStart;
  18. } else {
  19. return -1;
  20. }
  21. }
  22. function addChildNodeToTree($comparisonValue, $value, &$tree) {
  23. $parentNode = findParentForChild($comparisonValue, 0, $tree);
  24. // create a new subNode
  25. $newNodeIndex = count($tree);
  26. $tree[$newNodeIndex]["value"] = $value;
  27. $tree[$newNodeIndex]["doIHaveChildren"] = false;
  28. if ($tree[$parentNode]["doIHaveChildren"] == false) {
  29. // make sure the parent knows it has children
  30. $tree[$parentNode]["subNodes"][0] = $newNodeIndex;
  31. $tree[$parentNode]["doIHaveChildren"] = true;
  32. } else {
  33. $nextSubNode = count($tree[$parentNode]["subNodes"]);
  34. // make sure the parent knows it has children
  35. $tree[$parentNode]["subNodes"][$nextSubNode] = $newNodeIndex;
  36. }
  37. }
  38. function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
  39. if ($tree[$index]["doIHaveChildren"]) {
  40. for ($j = 0; $j < count($tree[$index]["subNodes"]); $j++) {
  41. walkTreeInPreOrderDeleteFolders($tree[$index]["subNodes"][$j], $imap_stream, $tree);
  42. }
  43. sqimap_mailbox_delete($imap_stream, $tree[$index]["value"]);
  44. } else {
  45. sqimap_mailbox_delete($imap_stream, $tree[$index]["value"]);
  46. }
  47. }
  48. function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $dm, $topFolderName) {
  49. global $trash_folder;
  50. $position = strrpos($topFolderName, $dm) + 1;
  51. $subFolderName = substr($tree[$index]["value"], $position);
  52. if ($tree[$index]["doIHaveChildren"]) {
  53. sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, "");
  54. sqimap_mailbox_select($imap_stream, $tree[$index]["value"]);
  55. $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]["value"]);
  56. if ($messageCount > 0)
  57. sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $dm . $subFolderName);
  58. for ($j = 0;$j < count($tree[$index]["subNodes"]); $j++)
  59. walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]["subNodes"][$j], $imap_stream, $tree, $dm, $topFolderName);
  60. } else {
  61. sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, "");
  62. sqimap_mailbox_select($imap_stream, $tree[$index]["value"]);
  63. $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]["value"]);
  64. if ($messageCount > 0)
  65. sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $dm . $subFolderName);
  66. }
  67. }
  68. function simpleWalkTreePre($index, $tree) {
  69. if ($tree[$index]["doIHaveChildren"]) {
  70. for ($j = 0; $j < count($tree[$index]["subNodes"]); $j++) {
  71. simpleWalkTreePre($tree[$index]["subNodes"][$j], $tree);
  72. }
  73. echo $tree[$index]["value"] . "<br>";
  74. } else {
  75. echo $tree[$index]["value"] . "<br>";
  76. }
  77. }
  78. ?>