<?php


// contents of function ShowTopics

/*

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 $bbsection_id, $THIS_PAGE, $headingrow, $FORUM_URL, $foruminfo, $control,
         $PHP_SELF;
  
  if ($headingrow ['section_description'])
    echo "<p>" .  htmlspecialchars ($headingrow ['section_description']) . "</p>\n";
  echo $headingrow ['section_motd'];
  echo "<hr>\n";
  
//  echo "<h4>Topics</h4>";  
/*
  $query = "SELECT bbtopic_id, topic_name, topic_description AS excerpt "
         . "FROM  bbtopic "
         . "WHERE bbsection_id = $bbsection_id ORDER BY topic_name";
*/
  $query = "SELECT bbtopic.bbtopic_id, topic_name, "
          . "topic_description, "
          . "format(sum(bbsubject.count_posts),0) AS post_count, "   // post count
          . "format(sum(bbsubject.count_views),0) AS view_count " // view count
         // which tables
         . "FROM  bbtopic LEFT JOIN bbsubject ON bbsubject.bbtopic_id = bbtopic.bbtopic_id  "
         // match on the section
         . "WHERE bbsection_id = $bbsection_id "
         // group and sort
         . "GROUP BY bbtopic_id "
         . "ORDER BY topic_name";
         
  $result = mysql_query ($query) 
    or Problem ("Select of topics failed: " . mysql_error ());
        
  bTable (0, 2);
    
  $font = $control ['font'];
  
  echo $font;
  
  bRow ('#ADD8E6');
  tHeadH ("$font Topic</font>");
  tHeadH ("$font Description</font>" );
  tHeadH ("$font Posts</font>" );
  tHeadH ("$font Views</font>" );
  eRow ();
  while ($row = mysql_fetch_array ($result))
   {
   bRow ('#DEDEDE');
   shLink ($link, $font . htmlspecialchars ($row ['topic_name']) . "</font>",
           $PHP_SELF,
           "bbtopic_id=" . $row ['bbtopic_id']);
   tDataH ($link, -1, 'right');
   tDataH ($font . htmlspecialchars ($row ['topic_description']) . "</font>");
   tDataH ($font . htmlspecialchars ($row ['post_count']) . "</font>", -1, 'right');
   tDataH ($font . htmlspecialchars ($row ['view_count']) . "</font>", -1, 'right');
   
   eRow ();
   } // end of while
  
  eTable ();
  
  mysql_free_result ($result);
         

  $query = "SELECT * "
         . "FROM bbuser "
         . "WHERE (blocked IS NULL OR blocked = 0) "
         . "  AND moderator_section = $bbsection_id "
         . "ORDER BY username";

  $count = ShowList ($query, "bbuser_id", "", "username", false,
                     "<hr><h4>Moderators</h4><UL>", "</UL>", "<LI>", "</LI>", $FORUM_URL . "bbshowbio.php");   
 
  if ($foruminfo ['admin'] ||
      $foruminfo ['moderator_section'] == $bbsection_id) 
        hLink ("<p align=left><font size=1>Add another topic</font></p>", 
              "bbupdatetopic.php", "bbsection_id=$bbsection_id", false);
 
  
// 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(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, bbuser "
         . "WHERE (TO_DAYS(bbsubject.last_post_date) + $DAYS) > TO_DAYS(CURDATE()) "
         . "      AND bbtopic.bbsection_id = $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, 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);
    
  echo "<hr>\n";
  
 ?>
