<?php


// contents of function ShowSubjects

/*

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 $bbtopic_id, $THIS_PAGE, $headingrow, $foruminfo, $control, $FORUM_URL, $page; 

  if ($headingrow ['topic_description'])
    echo "<p>" .  htmlspecialchars ($headingrow ['topic_description']) . "</p>\n";
  echo $headingrow ['topic_motd'];
  echo "<hr>\n";
  
  echo "<h4>Subjects</h4><p>";
  if (!$headingrow ['admin_only'] || $foruminfo ['admin'] ||
      $foruminfo ['moderator_topic'] == $headingrow ['bbtopic_id'] ||
      $foruminfo ['moderator_section'] == $headingrow ['bbsection_id'])
    hLink ("<img src=\"/images/new_subject.gif\" border=0 width=15 height=15 alt=\"[New subject]\">"
         . "&nbsp;Start a new subject", 
           $THIS_PAGE, 
           sprintf ("action=add&bbtopic_id=%d", $bbtopic_id));
  echo "</p>\n";
  $datetimeformat = $control ['datetimeformat'];
  $tz = $foruminfo ['time_zone']; 
  if (!$tz)
    $tz = 0;  // not logged in? Assume UTC
   
  $query = "SELECT bbsubject.bbsubject_id, subject_name, "
         . "  bbsubject.count_posts AS posts, "
         . "  bbsubject.count_views AS views, "
         . "  DATE_FORMAT(DATE_ADD(bbsubject.last_post_date, INTERVAL $tz HOUR), '$datetimeformat') AS formatted_last_post_date, "
         . "  T1.username AS author, bbsubject.author AS authorid, "
         . "  T1.blocked AS author_blocked, "
         . "  T2.username AS last_poster, bbsubject.last_post_user AS last_post_userid, "
         . "  T2.blocked AS last_poster_blocked "
         . "FROM bbsubject, bbuser AS T1, bbuser AS T2 "
         . "WHERE bbtopic_id = $bbtopic_id "
         . "  AND T1.bbuser_id = bbsubject.author "
         . "  AND T2.bbuser_id = bbsubject.last_post_user "
         . "ORDER BY bbsubject.last_post_date DESC, subject_name ";
//  echo $query . "<br>";
//  ShowList ($query, "bbsubject_id", "", "subject_name", false);   
    $result = mysql_query ($query) 
        or Problem ("Select of subjects failed: " . mysql_error ());

    $subjects_per_page = $control ['subjects_per_page'];
    // don't divide by zero, or have one subject per page :)
    if ($subjects_per_page < 5)
      $subjects_per_page = 20;

    $count_subjects = mysql_num_rows ($result);
    
    if ($count_subjects)
      {
        
      $subject_pages = floor ((($count_subjects - 1) / $subjects_per_page) + 1);
    
      // make sure requested page is in range
    
      if ($page < 1)
        $page = 1;  // too small, make 1
        
      if ($page > $subject_pages)
        $page = $subject_pages;   // too large, make maximum
      
      $first_subject = (($page - 1) * $subjects_per_page) + 1;
      $last_subject = $first_subject + $subjects_per_page - 1;
      
      // show them what pages there are
      
      if ($subject_pages > 1)
        {
        echo "<p align=right><b>Pages: ";
        for ($i = 1; $i <= $subject_pages; $i++)
          {
          if ($i == $page)
            echo "$i&nbsp;";
          else
            {
            hLink ("$i", $THIS_PAGE, "bbtopic_id=$bbtopic_id&page=$i", false);
            echo "&nbsp;";
            }       
          } // end of doing each page
          
        echo "</b></p>\n";                
        }
        
      echo "<table border=1 cellpadding=5 width=\"100%\">\n";
      
      // column names
    
      bRow ("lightblue");
        tHead ('Subject');
        tHead ('Author');
        tHead ('Posts');
        tHead ('Views');
        tHead ('Last post');
        tHead ('By');
      eRow ();

      $posts_per_page = $control ['posts_per_page'];
      
      // don't divide by zero, or have one post per page :)
      if ($posts_per_page < 5)
        $posts_per_page = 20;
        
      $item = 0;    
      while ($row = mysql_fetch_array ($result))
        {
        if (++$item < $first_subject)
          continue;   // skip ones from previous pages
        if ($item > $last_subject)
          break;      // have come to the end
                    
        // row data
        $bbsubject_id = $row ['bbsubject_id'];
        
        shLink ($postlink, htmlentities ($row ['subject_name']), 
                $THIS_PAGE, 
                "bbsubject_id=" . $bbsubject_id);
        
        // OK - time for multiple page stuff. Work out how many pages there are.
                  
        $pages = floor ((($row ['posts'] - 1) / $posts_per_page) + 1);

        if ($pages > 1)
          {
          $postlink .= "<br><font size=1>Pages: ";
          for ($i = 1; $i <= $pages; $i++)
            {
            shLink ($pagelink, "$i", $THIS_PAGE, "bbsubject_id=$bbsubject_id&page=$i", false);
            $postlink .= $pagelink . "&nbsp;";
            } // end of doing each page
            
          $postlink .= "</font>\n";                
          }
        
        if ($row ['author_blocked'])
          $authorlink = htmlentities ($row ['author']);
        else
          shLink ($authorlink, htmlentities ($row ['author']), 
                  $FORUM_URL . "bbshowbio.php", 
                  sprintf ("bbuser_id=%d", $row ['authorid']),
                  true);  // new window
        if ($row ['last_poster_blocked'])
          $lastposterlink = htmlentities ($row ['last_poster']);
        else
          shLink ($lastposterlink, htmlentities ($row ['last_poster']), 
                  $FORUM_URL . "bbshowbio.php", 
                  sprintf ("bbuser_id=%d", $row ['last_post_userid']),
                  true);  // new window
                
        $formatted_last_post_date = $row ['formatted_last_post_date'];
                
        // get rid of postings at exactly midnight
        if (substr ($formatted_last_post_date, -8) == "12:00 AM")
          $formatted_last_post_date = substr ($formatted_last_post_date, 0,
                                            strlen ($formatted_last_post_date) - 8);
                                            
        bRow ("azure");
          tDataH ($postlink);
          tDataH ($authorlink);
          tData (number_format ($row ['posts']), -1, "right");
          tData (number_format ($row ['views']), -1, "right");
          tDataH ("<font size=1>" . htmlentities ($formatted_last_post_date) . "</font>");
          tDataH ($lastposterlink);
        eRow ();
        } // end of loop
  
  
      eTable ();
          
      // show all pages again
      
      if ($subject_pages > 1)
        {
        echo "<p><b>This is page $page, topic is $subject_pages pages long: ";
        if ($page > 1)
          {
          echo "&nbsp;";
          hLink ("<img src=\"/images/thin_prev.gif\" alt=\"[Previous page]\" "
               . "align=bottom border=0 width=73 height=18>", 
                 $THIS_PAGE, "bbtopic_id=$bbtopic_id&page=" . ($page - 1), false);
          echo "&nbsp;";
          }
        for ($i = 1; $i <= $subject_pages; $i++)
          {
          if ($i == $page)
            echo "$i&nbsp;";
          else
            {
            hLink ("$i", $THIS_PAGE, "bbtopic_id=$bbtopic_id&page=$i", false);
            echo "&nbsp;";
            }       
          } // end of doing each page
        if ($page < $subject_pages)
          hLink ("<img src=\"/images/thin_next.gif\" alt=\"[Next page]\" "
               . "align=bottom border=0 width=73 height=18>", 
                 $THIS_PAGE, "bbtopic_id=$bbtopic_id&page=" . ($page + 1), false);
          
        echo "</b></p>\n";                
        }
      
      } // end of having any rows returned
      
    mysql_free_result ($result);

  if ($foruminfo ['admin'])
    {
      
    $bbuser_id = $foruminfo ['bbuser_id'];
    
    
    $query = "SELECT username, notify_new_thread, bbuser.bbuser_id "
           . "FROM bbuser LEFT JOIN bbnotify_topic "
           . "ON bbnotify_topic.bbuser_id = bbuser.bbuser_id "  // match on the relevant user
           . "WHERE bbuser.bbuser_id <> $bbuser_id "  // don't send to ourselves
           . "  AND  (bbuser.bounced IS NULL OR bbuser.bounced = 0) "  // can we send it? yes we can!
           . "  AND (  bbnotify_topic.bbtopic_id = $bbtopic_id "       // match in bbnotify_topic
           . "     OR  bbuser.notify_new_thread <> 0 "   // OR they want all new subjects
           . "      ) "
           . "GROUP BY bbuser.bbuser_id "    // only want each user once
           . "ORDER BY username";
           
    
              
    $result = mysql_query ($query) 
      or Problem ("Select of notification failed: " . mysql_error ());
  
    if (mysql_num_rows ($result) > 0)
      {
      echo "<p><b>Subscribed to by ...</b></p>\n";
      bList ();
      while ($row = mysql_fetch_array ($result))
        {
        $notifyname = $row ['username'];
        LI ();
        hLink (htmlspecialchars ($row ['username']),
               "bbshowbio.php",
               "bbuser_id=" . $row ['bbuser_id']
               );
    
        } // end of subscription loop
         
       eList ();
      } 
    mysql_free_result ($result);

  
  } // end of admin

  $bbsection_id = $headingrow ['bbsection_id'];

  if ($foruminfo ['admin'] ||
      $foruminfo ['moderator_section'] == $bbsection_id) 
        hLink ("<p align=left><font size=1>Amend topic description</font></p>", 
              "bbupdatetopic.php", "bbtopic_id=$bbtopic_id", false);
    
  $query = "SELECT * "
         . "FROM bbuser "
         . "WHERE (blocked IS NULL OR blocked = 0) "
         . "  AND (moderator_topic = $bbtopic_id "
         . "    OR moderator_section = $bbsection_id) "
         . "ORDER BY username";

  $count = ShowList ($query, "bbuser_id", "", "username", false,
                     "<h4>Moderators</h4><UL>", "</UL>", "<LI>", "</LI>", $FORUM_URL . "bbshowbio.php");   
 
  if ($count)
    echo "<hr>\n";
    
  if ($foruminfo)   // only if logged in
    {
    // have they subscribed to this topic?
    
    $bbuser_id = $foruminfo ['bbuser_id'];
    
    $query = "SELECT * "
           . "FROM bbnotify_topic "
           . "WHERE bbtopic_id = $bbtopic_id "
           . "AND   bbuser_id = $bbuser_id ";
                                 
    $result = mysql_query ($query) 
        or Problem ("Select of subscription failed: " . mysql_error ());
      
    $subscribed = mysql_fetch_array ($result);
      
    mysql_free_result ($result);
  
    echo "<p>";
    
    if ($subscribed)
      {
      hLink ("Unsubscribe", $THIS_PAGE, "action=unsubscribe&bbtopic_id=$bbtopic_id");
      echo "&nbsp;Click to stop receiving email notification of postings.</p>\n";    
      }
    else
      {
      hLink ("Subscribe", $THIS_PAGE, "action=subscribe&bbtopic_id=$bbtopic_id");
      echo "&nbsp;Click to receive email notification of postings.</p>\n";
      } // end of not subscribed yet  
    }   // end of logged in
    
?>
