<?php


// contents of function ShowSections

/*

Copyright © 2001 Nick Gammon.

  This program is free software; you can redistribute it and/or modify 
  it under the terms of the GNU General Public License as published by 
  the Free Software Foundation; either version 2 of the License, 
  or (at your option) any later version. 

  This program is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  See the GNU General Public License for more details. 

*/

  global $THIS_PAGE, $FORUM_URL, $control, $PHP_SELF, $foruminfo;
  
  echo "<hr>\n";
  echo "<h4>Sections</h4>\n";
  $query = "SELECT bbsection_id, section_name, section_description  "
         . " from bbsection WHERE section_description IS NOT NULL ORDER BY section_name";

  $result = mysql_query ($query) 
    or Problem ("Select of sections failed: " . mysql_error ());

  bTable (0, 3);
  
  $font = $control ['font'];
  
  echo $font;
  
  while ($row = mysql_fetch_array ($result))
   {
   bRow ('#DEDEDE');
   shLink ($link, $font . htmlspecialchars ($row ['section_name']) . "</font>",
           $PHP_SELF,
           "bbsection_id=" . $row ['bbsection_id']);
   tDataH ($link, -1, 'right');
   tDataH ($font . htmlspecialchars ($row ['section_description']) . "</font>");
   
   eRow ();
   } // end of while
  
  eTable ();

  mysql_free_result ($result);
  
// show recent posts

  $dateformat = $control ['dateformat'];
  $shortdateformat = $control ['shortdateformat'];
  $timeformat = $control ['timeformat'];
  $datetimeformat = $control ['datetimeformat'];
  $shortdatetimeformat = $control ['shortdatetimeformat'];
    
// limit of counts from the control file
    
  $LIMIT = $control ['recent_posts_count'];
  if (!$LIMIT)
    $LIMIT = 20;

// limit of days from the control file
    
  $DAYS = $control ['recent_posts_days'];
  if (!$DAYS)
    $DAYS = 30;
  
  $tz = $foruminfo ['time_zone'];
  if (!$tz)
    $tz = 0;  // not logged in? Assume UTC
      
  $query = "SELECT bbsubject.bbsubject_id, "
         . "  CONCAT(section_name, ': ', topic_name, ': ', subject_name) AS fullname, "
         . "  DATE_FORMAT(DATE_ADD(bbsubject.last_post_date, INTERVAL $tz HOUR), '$dateformat') AS formatted_date, "
         . "  DATE_FORMAT(DATE_ADD(bbsubject.last_post_date, INTERVAL $tz HOUR), '$timeformat') AS formatted_time, "
         . "  TO_DAYS(bbsubject.last_post_date) AS days, "
         . "  bbuser.username, bbuser.bbuser_id "
         . "FROM bbpost, bbsubject, bbtopic, bbsection, bbuser "
         . "WHERE (TO_DAYS(bbsubject.last_post_date) + $DAYS) > TO_DAYS(CURDATE()) "
         . "      AND bbsection.bbsection_id = bbtopic.bbsection_id  "
         . "      AND bbtopic.bbtopic_id = bbsubject.bbtopic_id "
         . "      AND bbsubject.bbsubject_id = bbpost.bbsubject_id "
         . "      AND bbsubject.last_post_user = bbuser.bbuser_id "
         . "GROUP BY bbsubject_id "
         . "ORDER BY bbsubject.last_post_date DESC, section_name, topic_name, subject_name "
         . "LIMIT $LIMIT";
  
  $result = mysql_query ($query) 
    or Problem ("Select of recent posts failed: " . mysql_error ());
  $count = mysql_num_rows ($result);

  if ($count)
    {
    echo "<hr><h4>Recent posts</h4>\n";
        
    $old_date = "";
    
    while ($row = mysql_fetch_array ($result))
      {
      $this_date = $row ['formatted_date'];
      if ($this_date != $old_date)
        {
        if ($old_date)
          eList ();   // end of old list
        $old_date = $this_date;
        echo "<p><b>" . htmlentities ($this_date) . "</b></p>\n";
        bList (); // start new list
        }   // end of date change
        
      LI ();
      hLink (htmlspecialchars ($row ['fullname']), $THIS_PAGE, "bbsubject_id=" 
            . $row ['bbsubject_id']
            . "&page=999999");    // default to end of subject
      echo "&nbsp;<font size=1>latest by ";
      hLink (htmlentities ($row ['username']), 
              $FORUM_URL . "bbshowbio.php", "bbuser_id=" 
            . $row ['bbuser_id']);
       
      echo " at "
         . htmlentities ($row ['formatted_time'])          
         . "</font>\n";
    
      } // end of reading each row
    eList ();   // end of list
 
    if ($count >= $LIMIT)
      echo "<p>List limited to $count items.</p>";
  
    echo "<p>The list shows posts in the last $DAYS days, most recent first.</p>";

    }   // end of having recent posts

  mysql_free_result ($result);

// --------------------------------------- 
//   recent posters

// limit of days from the control file
    
  $DAYS = $control ['recent_posters_days'];
  if (!$DAYS)
    $DAYS = 7;

  $query = "SELECT bbuser_id, "
         . "  DATE_FORMAT(DATE_ADD(last_post_date, INTERVAL $tz HOUR), '$shortdatetimeformat') AS excerpt, "
         . "  TO_DAYS(last_post_date) AS days, "
         . "  username "
         . "FROM  bbuser "
         . "WHERE (TO_DAYS(last_post_date) + $DAYS) > TO_DAYS(CURDATE()) "
         . "ORDER BY last_post_date DESC "
         . "LIMIT $LIMIT";

//	ShowList ($query, "bbuser_id", "", "username", false, 
//	          "<hr><h4>Recent posts by ...</h4><ul>", "</UL>",
//	          "<LI>", "</LI>", "bbshowbio.php"); 

  $result = mysql_query ($query) 
    or Problem ("Select of recent posters failed: " . mysql_error ());
  $count = mysql_num_rows ($result);

  if ($count)
    {
    echo "<hr><h4>Recent posts by ...</h4>\n";
    bList (); // start new list
    while ($row = mysql_fetch_array ($result))
      {
      LI ();
      hLink (htmlentities ($row ['username']), 
              $FORUM_URL . "bbshowbio.php", "bbuser_id=" 
            . $row ['bbuser_id']);
      echo "&nbsp;<font size=1>"
         . "at "
         . htmlentities ($row ['excerpt'])          
         . "</font>\n";
    
      } // end of reading each row
    eList ();   // end of list
 
    if ($count >= $LIMIT)
      echo "<p>List limited to $count items.</p>";
  
//    echo "<p>The list shows posters in the last $DAYS days, most recent first.</p>";

    }   // end of having recent posts

  mysql_free_result ($result);

// --------------------------------------- 
//   recent joiners

// limit of days from the control file
    
  $DAYS = $control ['recent_joiners_days'];
  if (!$DAYS)
    $DAYS = 7;

  $query = "SELECT bbuser_id, "
         . "  DATE_FORMAT(DATE_ADD(date_registered, INTERVAL $tz HOUR), '$shortdateformat') AS excerpt, "
         . "  TO_DAYS(date_registered) AS days, "
         . "  username "
         . "FROM  bbuser "
         . "WHERE (TO_DAYS(date_registered) + $DAYS) > TO_DAYS(CURDATE()) "
         . "ORDER BY date_registered DESC "
         . "LIMIT $LIMIT";

//	ShowList ($query, "bbuser_id", "", "username", false, 
//	          "<hr><h4>Forum recently joined by ...</h4><ul>", "</UL>",
//	          "<LI>", "</LI>", "bbshowbio.php"); 
  
  $result = mysql_query ($query) 
    or Problem ("Select of recent joiners failed: " . mysql_error ());
  $count = mysql_num_rows ($result);

  if ($count)
    {
    echo "<hr><h4>Forum recently joined by ...</h4>\n";
    bList (); // start new list
    while ($row = mysql_fetch_array ($result))
      {
      LI ();
      hLink (htmlentities ($row ['username']), 
              $FORUM_URL . "bbshowbio.php", "bbuser_id=" 
            . $row ['bbuser_id']);
      echo "&nbsp;<font size=1>"
         . "on "
         . htmlentities ($row ['excerpt'])          
         . "</font>\n";
    
      } // end of reading each row
    eList ();   // end of list
 
    if ($count >= $LIMIT)
      echo "<p>List limited to $count items.</p>";
  
//    echo "<p>The list shows posters in the last $DAYS days, most recent first.</p>";

    }   // end of having recent posts

  mysql_free_result ($result);

  echo "<hr><h4>Statistics</h4>\n";
    
  $table = array 
              (
              'Registered users' => GetSQLcount ("bbuser"),
              'Posts' => GetSQLcount ("bbpost"),   
              'Subjects' => GetSQLcount ("bbsubject"),
              'Views' => GetSQLcount ("bbsubject", "SELECT sum(count_views) FROM "),
              );
    
// extra information for admins :)
    
  if ($foruminfo ['admin'])
    {      
      
    $table ['Topics'] = GetSQLcount ("bbtopic");    // topics
    $table ['Sections'] = GetSQLcount ("bbsection"); // sections
    $table ['Users with no posts']  // lurkers
            = GetSQLcount ("bbuser WHERE count_posts = 0");
    $table ['Users with between 1 and 9 posts']   // done some posts
            = GetSQLcount ("bbuser WHERE count_posts > 0 AND count_posts < 10");
    $table ['Users with 10 or more posts']  // lots of posts
            = GetSQLcount ("bbuser WHERE count_posts >= 10");
    $table ['Topics subscribed to'] = GetSQLcount ("bbnotify_topic");  // new topics mailout 
    $table ['Subjects subscribed to'] = GetSQLcount ("bbnotify_subject"); // new subjects mailout
    $table ['Users who want notification of new threads'] // users with 'notify new thread'
            = GetSQLcount ("bbuser WHERE notify_new_thread <> 0");
    $table ['Users who have sent mail'] = GetSQLcount ("bbuser WHERE count_mail_sent > 0");
    $table ['Users who have received mail'] = GetSQLcount ("bbuser WHERE count_mail_received > 0");
    $table ['Mail messages sent'] = GetSQLcount ("SELECT sum(count_mail_sent) from bbuser", "");
    $table ['Users with a bio']  // done a bio?
            = GetSQLcount ("bbuser WHERE bio IS NOT NULL");
    $table ['Users with a signature']  // done a signature?
            = GetSQLcount ("bbuser WHERE signature IS NOT NULL");
                
    } // end of admin user
              
                
      
  ShowTable ($table,     // the data
            array (     // table parameters
                  'table' => 'border=0 cellpadding=5"',
                  'LH' => 'valign=top bgcolor="#ADD8E6" align=right',
                  'RH' => 'bgcolor="#FAF0E6" align=right'
                  ),
            ""  // specials
            );
  
   echo "\n<p>";  
   hLink ("View list of registered users", 
          $FORUM_URL . "bbuserlist.php", "", 
          false); 
   echo "</p>\n";  
  
?>
