<?php

// $Id: bb_fnc_showsections.php,v 1.10 2001/04/05 00:12:48 nick Exp $
// $Log: bb_fnc_showsections.php,v $
// Revision 1.10  2001/04/05 00:12:48  nick
// Fixed problems with UTC code.
//
// Revision 1.9  2001/04/04 00:54:59  nick
// Added UTC time handling.
//
// Revision 1.8  2001/03/16 04:31:03  nick
// Changed file suffices to .php rather than .php4
//
// Revision 1.7  2001/03/13 09:10:55  nick
// Minor fixes
//
// Revision 1.6  2001/03/09 10:32:55  nick
// Added pagination of posts
//
// Revision 1.5  2001/03/08 22:56:46  nick
// More enhancements
//
// Revision 1.4  2001/03/07 03:21:34  nick
// Changes for section moderators, adding topics, etc.
//
// Revision 1.3  2001/03/06 20:59:28  nick
// Further enhancements
//
// Revision 1.2  2001/03/04 22:49:12  nick
// Further bug fixes following production testing
//

// 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 AS excerpt "
         . " from bbsection WHERE section_description IS NOT NULL ORDER BY section_name";
  ShowList ($query, "bbsection_id", "", "section_name", false,
            "<UL>", "</UL>", "<LI>", "</LI>", $THIS_PAGE);   

// show recent posts

  $dateformat = $control ['dateformat'];
  $timeformat = $control ['timeformat'];
    
// 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 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, grouped by subject.</p>";

    }   // end of having recent posts

  echo "<hr><h4>Statistics</h4>\n";
    
  // user count                        
  $result = mysql_query ("SELECT count(*) FROM bbuser") 
      or Problem ("Select count failed: " . mysql_error ());
  $row = mysql_fetch_row ($result);
  $bbuser_count = $row [0];
  mysql_free_result ($result);

  // message count                        
  $result = mysql_query ("SELECT count(*) FROM bbpost") 
      or Problem ("Select count failed: " . mysql_error ());
  $row = mysql_fetch_row ($result);
  $bbpost_count = $row [0];
  mysql_free_result ($result);

  // subject count                        
  $result = mysql_query ("SELECT count(*) FROM bbsubject") 
      or Problem ("Select count failed: " . mysql_error ());
  $row = mysql_fetch_row ($result);
  $bbsubject_count = $row [0];
  mysql_free_result ($result);
    
  ShowTable (array 
              (
              'Registered users' => $bbuser_count,
              'Posts' => $bbpost_count,   
              'Subjects' => $bbsubject_count,
              ),  // 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";  
  
?>
