<?php


  // contents of function GenerateHeader

/*

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, $bbtopic_id, $bbsubject_id, $bbpost_id, $THIS_PAGE, $headingrow,
         $control, $motd;

  // custom forum header
  $header = "<h3>" . htmlentities ($control ['forum_name']) . "</h3>\n";
  
  // system message of the day
  $header .= $control ['motd'];
  
  // find name of section, topic and subject in a single query
  if ($bbpost_id)
    $query = "SELECT * "
           . "FROM  bbpost, bbsubject, bbtopic, bbsection "
           . "WHERE bbpost.bbpost_id = $bbpost_id "
           . "AND   bbsubject.bbsubject_id = bbpost.bbsubject_id "
           . "AND   bbtopic.bbtopic_id = bbsubject.bbtopic_id "
           . "AND   bbsection.bbsection_id = bbtopic.bbsection_id ";
  else if ($bbsubject_id)
    $query = "SELECT * "
           . "FROM bbsubject, bbtopic, bbsection "
           . "WHERE bbsubject.bbsubject_id = $bbsubject_id "
           . "AND   bbtopic.bbtopic_id = bbsubject.bbtopic_id "
           . "AND   bbsection.bbsection_id = bbtopic.bbsection_id ";
  else if ($bbtopic_id)
    $query = "SELECT * "
           . "FROM  bbtopic, bbsection "
           . "WHERE bbtopic.bbtopic_id = $bbtopic_id "
           . "AND   bbsection.bbsection_id = bbtopic.bbsection_id ";     
  else if ($bbsection_id)
    $query = "SELECT * "
           . "FROM  bbsection "
           . "WHERE bbsection.bbsection_id = $bbsection_id "; 
  else
    $query = "";    
      
  if ($query)
    {     
    // echo $query;
                          
    $result = mysql_query ($query) 
        or Problem ("Select of subject/topic/section names failed: " . mysql_error ());
      
    if (!($headingrow = mysql_fetch_array ($result)))
        Problem ("That message has been deleted.");
    
    mysql_free_result ($result);
      
    // now we should know all the ids
    
    $bbsection_id = $headingrow ['bbsection_id'];
    $bbtopic_id =   $headingrow ['bbtopic_id'];
    $bbsubject_id = $headingrow ['bbsubject_id'];
    }   // end of having a query to do
  else
    $headingrow = "";
    
  // start with the forum - we always have that

  if ($bbsection_id)  // only do a link if we have a lower level
    shLink ($forumlink, "Entire forum", $THIS_PAGE);
  else
    $forumlink = "Entire forum";
  $header .= "<img src=\"/images/open_folder.gif\" width=15 height=15 border=0 alt=\"[Folder]\">\n";
  $header .= "&nbsp;<b>" . $forumlink    . "</b><br>\n";
 
  if ($bbsection_id)
    {
    $section_name = htmlspecialchars ($headingrow ['section_name']);
    if ($bbtopic_id) 
      shLink ($sectionlink, $section_name, $THIS_PAGE, "bbsection_id=$bbsection_id");
    else
      $sectionlink = $section_name;
    $header .= "<img src=\"/images/bar.gif\" width=15 height=15 border=0 alt=\"-&gt;\">\n";
    $header .= "<img src=\"/images/open_folder.gif\" width=15 height=15 border=0 alt=\"[Folder]\">\n";
    $header .= "&nbsp;<b>" . $sectionlink  . "</b><br>\n";
    } // end of having a section

  if ($bbtopic_id)
    {
    $topic_name = htmlspecialchars ($headingrow ['topic_name']);
    if ($bbsubject_id)  // only do a link if we have a lower level
      shLink ($topiclink, $topic_name, $THIS_PAGE, "bbtopic_id=$bbtopic_id");
    else
      $topiclink = $topic_name;
    $header .= "<img src=\"/images/blank.gif\" width=15 height=15 border=0 alt=\".\">\n";
    $header .= "<img src=\"/images/bar.gif\" width=15 height=15 border=0 alt=\"-&gt;\">\n";
    $header .= "<img src=\"/images/open_folder.gif\" width=15 height=15 border=0 alt=\"[Folder]\">\n";
    $header .= "&nbsp;<b>" . $topiclink  . "</b><br>\n";
    } // end of having a topic

  if ($bbsubject_id)
    {
    $subject_name = htmlspecialchars ($headingrow ['subject_name']);
//    shLink ($subjectlink, $subject_name, $THIS_PAGE, "bbsubject_id=$bbsubject_id");
    $header .= "<img src=\"/images/blank.gif\" width=15 height=15 border=0 alt=\".\">\n";
    $header .= "<img src=\"/images/blank.gif\" width=15 height=15 border=0 alt=\".\">\n";
    $header .= "<img src=\"/images/bar.gif\" width=15 height=15 border=0 alt=\"-&gt;\">\n";
    $header .= "<img src=\"/images/open_subject.gif\" width=15 height=15 border=0  alt=\"[Subject]\">\n";
    $header .= "&nbsp;<b>" . $subject_name  . "</b><br>\n";
    } // end of having a subject

?>
