home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / email / treeview.inc < prev   
Text File  |  2004-03-08  |  9KB  |  294 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12.  
  13. $tv_image['mlastnode'] = '<img src="'.$GO_THEME->images['mlastnode'].'" border="0" height="22" width="16" />';
  14. $tv_image['emptylastnode'] = '<img src="'.$GO_THEME->images['emptylastnode'].'" border="0" height="22" width="16" />';
  15. $tv_image['plastnode'] = '<img src="'.$GO_THEME->images['plastnode'].'" border="0" height="22" width="16" />';
  16. $tv_image['mnode'] = '<img src="'.$GO_THEME->images['mnode'].'" border="0" height="22" width="16" />';
  17. $tv_image['emptynode'] = '<img src="'.$GO_THEME->images['emptynode'].'" border="0" height="22" width="16" />';
  18. $tv_image['pnode'] = '<img src="'.$GO_THEME->images['pnode'].'" border="0" height="22" width="16" />';
  19. $tv_image['vertline'] = '<img src="'.$GO_THEME->images['vertline'].'" border="0" height="22" width="16" />';
  20. $tv_image['blank'] = '<img src="'.$GO_THEME->images['blank'].'" border="0" height="22" width="16" />';
  21. $tv_image['group'] = '<img src="'.$GO_THEME->images['workgroup'].'" border="0" height="22" width="24" />';
  22. $tv_image['user'] = '<img src="'.$GO_THEME->images['workstation'].'" border="0" height="22" width="20" />';
  23.  
  24. $tv_image['opened_folder'] = '<img src="'.$GO_THEME->images['folderopen'].'" border="0" height="22" width="24" />';
  25. $tv_image['closed_folder'] = '<img src="'.$GO_THEME->images['folderclosed'].'" border="0" height="22" width="24" />';
  26.  
  27. /*
  28. prints the folders in a tree
  29. $folders is an array of associve arrays containing an 'id' and 'name'
  30. */
  31. function print_tree($folders, $prefix='')
  32. {
  33.     global $tv_image, $folder_id, $email2, $mailbox, $mail, $total_unseen, $account_id;
  34.  
  35.     $count = count($folders);
  36.     for ($i=0;$i<$count;$i++)
  37.     {
  38.         //for each folder check if there are subfolders and
  39.         //add them to an array for the next instance of this recursive function
  40.         $subfolders = array();
  41.         $subfolders_count = $email2->get_folders($folders[$i]['account_id'], $folders[$i]['id']);
  42.         while($email2->next_record())
  43.         {
  44.             $subfolders[] = $email2->Record;
  45.         }
  46.         //is this folder opened in the tree?
  47.         $open = in_array($folders[$i]['id'], $_SESSION['expanded']);
  48.  
  49.         //determine the image and node to display
  50.         if ($subfolders_count > 0)
  51.         {
  52.             if ($i < ($count-1))
  53.             {
  54.                 $new_image = $tv_image['vertline'];
  55.                 $node = $open ? $tv_image['mnode'] : $tv_image['pnode'];
  56.  
  57.             }else
  58.             {
  59.                 $new_image = $tv_image['blank'];
  60.                 $node = $open ? $tv_image['mlastnode'] : $tv_image['plastnode'];
  61.             }
  62.         }else
  63.         {
  64.             if ($i < ($count-1))
  65.             {
  66.                 $new_image = $tv_image['vertline'];
  67.                 $node = $tv_image['emptynode'];
  68.             }else
  69.             {
  70.                 $new_image = $tv_image['blank'];
  71.                 $node = $tv_image['emptylastnode'];
  72.             }
  73.         }
  74.  
  75.         //if the current folder is the current opened folder then show an opened folder
  76.         if ($mailbox == $folders[$i]['name'])
  77.         {
  78.             $folder_image = $tv_image['opened_folder'];
  79.         }else
  80.         {
  81.             $folder_image = $tv_image['closed_folder'];
  82.         }
  83.  
  84.         //actually print the current folder
  85.         //if the delimiter is found in the path then this is a subfolder cut the
  86.         //location to print it user friendly
  87.         if ($pos = strrpos($folders[$i]['name'], $folders[$i]['delimiter']))
  88.         {
  89.             $folder_name = substr($folders[$i]['name'],$pos+1);
  90.         }else
  91.         {
  92.             $folder_name = $folders[$i]['name'];
  93.         }
  94.         $short_name = cut_string($folder_name, 30);
  95.  
  96.         //check for unread mail
  97.         $status = $mail->status($folders[$i]['name']);
  98.         if ($status->unseen > 0)
  99.         {
  100.             $status = ' ('.$status->unseen.')';
  101.         }else
  102.         {
  103.             $status = '';
  104.         }
  105.  
  106.         echo '<table border="0" cellpadding="0" cellspacing="0">';
  107.         echo '<tr><td nowrap><a href="'.$_SERVER['PHP_SELF'].'?account_id='.$account_id.'&expand_id='.$folders[$i]['id'].'&mailbox='.urlencode($mailbox).'">'.$prefix.$node.$folder_image.'</a></td>';
  108.         echo '<td nowrap class="count"><a class="Table1" href="'.$_SERVER['PHP_SELF'].'?account_id='.$account_id.'&folder_id='.$folders[$i]['id'].'&mailbox='.urlencode($folders[$i]['name']).'" title="'.$folders[$i]['name'].'">'.$short_name.'</a>'.$status.'</td></tr></table>';
  109.  
  110.         if ($open)
  111.         {
  112.             print_tree($subfolders, $prefix.$new_image);
  113.         }
  114.     }
  115. }
  116.  
  117. function set_expanded($expanded, $expand_id, $folder_id)
  118. {
  119.     //fill the first value of the expanded with -1 because of key 0
  120.     //results into false at the array_search() function and behaves differently at different PHP versions
  121.     if (empty($expanded))
  122.     {
  123.         $expanded[]=-1;
  124.     }
  125.  
  126.     //expand or collaps the expand id which is passed when a user clicks a node
  127.     if ($expand_id > 0)
  128.     {
  129.         $expand_path = smartstrip($expand_id);
  130.         $key = array_search($expand_path, $expanded);
  131.         if (!$key)
  132.         {
  133.             $expanded[] = $expand_path;
  134.         }else
  135.         {
  136.             unset($expanded[$key]);
  137.         }
  138.     }elseif ($folder_id > 0)
  139.     {
  140.         //always expand folder clicks
  141.         $key = array_search($folder_id, $expanded);
  142.         if (!$key)
  143.         {
  144.             $expanded[] = $folder_id;
  145.         }
  146.     }
  147.     return $expanded;
  148. }
  149.  
  150. $folder_id = isset($_REQUEST['folder_id']) ? $_REQUEST['folder_id'] : 0;
  151. $expand_id = isset($_REQUEST['expand_id']) ? $_REQUEST['expand_id'] : 0;
  152.  
  153. //$account_expanded = isset($account_expanded) ? $account_expanded : array();
  154. $_SESSION['expanded'] = isset($_SESSION['expanded']) ? $_SESSION['expanded'] : array($account['id']);
  155. $_SESSION['expanded'] = set_expanded($_SESSION['expanded'], $expand_id, $folder_id);
  156.  
  157.  
  158. $email2 = new email();
  159. $count = $email->get_accounts($GO_SECURITY->user_id);
  160. if ($count > 0)
  161. {
  162.     //print an image that represents the email accounts
  163.     echo '<table border="0" cellpadding="0" cellspacing="0">';
  164.     echo '<tr><td nowrap><img src="'.$GO_THEME->images['newmail'].'" border="0" height="16" width="16" /></td>';
  165.     echo '</table>';
  166.  
  167.     //loop thorugh accounts
  168.     $i=0;
  169.     while($email->next_record())
  170.     {
  171.         //if it is the last account then print mlastnode
  172.         $i++;
  173.  
  174.         $folder_count =0;
  175.         if ($email->f('type') == 'imap')
  176.         {
  177.             $folder_count = $email2->get_folders($email->f('id'), 0);
  178.         }
  179.         if ($i == $count)
  180.         {
  181.  
  182.             if ($email->f('id') == $account['id'])
  183.             {
  184.                 if ($folder_count > 0)
  185.                 {
  186.                     $node = $tv_image['mlastnode'];
  187.                 }else
  188.                 {
  189.                     $node = $tv_image['emptylastnode'];
  190.                 }
  191.             }else
  192.             {
  193.                 if ($folder_count > 0)
  194.                 {
  195.                     $node = $tv_image['plastnode'];
  196.                 }else
  197.                 {
  198.                     $node = $tv_image['emptylastnode'];
  199.                 }
  200.             }
  201.  
  202.             $prefix = $tv_image['blank'];
  203.         }else
  204.         {
  205.             if ($email->f('id') == $account['id'])
  206.             {
  207.                 if ($folder_count > 0)
  208.                 {
  209.                     $node = $tv_image['mnode'];
  210.                 }else
  211.                 {
  212.                     $node = $tv_image['emptynode'];
  213.                 }
  214.             }else
  215.             {
  216.                 if ($folder_count > 0)
  217.                 {
  218.                     $node = $tv_image['pnode'];
  219.                 }else
  220.                 {
  221.                     $node = $tv_image['emptynode'];
  222.                 }
  223.             }
  224.  
  225.             $prefix = $tv_image['vertline'];
  226.         }
  227.  
  228.         $short_name = cut_string($email->f('email'), 30);
  229.         echo '<table border="0" cellpadding="0" cellspacing="0">';
  230.         echo '<tr><td nowrap><a href="'.$_SERVER['PHP_SELF'].'?account_id='.$email->f('id').'">'.$node.'<img src="'.$GO_THEME->images['inbox'].'" border="0" /></a></td>';
  231.         echo '<td nowrap class="count"> <a class="Table1" href="'.$_SERVER['PHP_SELF'].'?account_id='.$email->f('id').'" title="'.$email->f('email').'">'.$short_name.'</a></td></tr></table>';
  232.  
  233.         if ($email->f('type') == 'imap')
  234.         {
  235.             if($root_folder = $email2->get_folder($email->f('id'), $email->f('mbroot')))
  236.             {
  237.                 $root_folder = $root_folder['id'];
  238.             }else
  239.             {
  240.                 $root_folder = 0;
  241.             }
  242.             //add the inbox folder first
  243.             //only imap accounts have folders
  244.  
  245.             //if this is the active account then print the tree
  246.  
  247.             $folder_count = $email2->get_folders($account['id'], $root_folder);
  248.  
  249.  
  250.             if ($email->f('id') == $account['id'])
  251.             {
  252.                 if ($root_folder > 0)
  253.                 {
  254.                     if ($folder_count > 0)
  255.                     {
  256.                         $node = $tv_image['emptynode'];
  257.                     }else
  258.                     {
  259.                         $node = $tv_image['emptylastnode'];
  260.                     }
  261.                     if ($mailbox == 'INBOX')
  262.                     {
  263.                         $folder_image = $tv_image['opened_folder'];
  264.                     }else
  265.                     {
  266.                         $folder_image = $tv_image['closed_folder'];
  267.                     }
  268.  
  269.                     //check for unread mail
  270.                     $status = $mail->status('INBOX');
  271.                     if ($status->unseen > 0)
  272.                     {
  273.                         $status = ' ('.$status->unseen.')';
  274.                     }else
  275.                     {
  276.                         $status = '';
  277.                     }
  278.                     echo '<table border="0" cellpadding="0" cellspacing="0">';
  279.                     echo '<tr><td nowrap>'.$prefix.$node.$folder_image.'</a></td>';
  280.                     echo '<td nowrap class="count"><a class="Table1" href="'.$_SERVER['PHP_SELF'].'?account_id='.$account_id.'&mailbox=INBOX" title="'.$ml_inbox.'">'.$ml_inbox.'</a>'.$status.'</td></tr></table>';
  281.                 }
  282.  
  283.                 $folders = array();
  284.                 while($email2->next_record())
  285.                 {
  286.                     $folders[] = $email2->Record;
  287.                 }
  288.  
  289.                 print_tree($folders, $prefix);
  290.             }
  291.         }
  292.     }
  293. }
  294. ?>