<?php

/*

  Copyright © 2001 Nick Gammon.

  Author: Nick Gammon <nick@gammon.com.au>
  Web:    http://www.gammon.com.au/
  Date:   February 2001

  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. 

  You should have received a copy of the GNU General Public License 
  along with this program; if not, write to 

  The Free Software Foundation, Inc., 
  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


  The Free Software Foundation maintains a web page at: http://www.fsf.org

  See the file gpl.txt for the full GNU General Public License.

*/

require ("config.php");
require ($CONFIG_PATH . "general_config.php");
require ($INCLUDE_DIRECTORY . "common.php");

Init ("Add/amend topic description", "forum, update, subject"); 

$forum_name = $control ['forum_name'];

$MAX_TOPIC_SIZE = 80;
$MAX_DESCRIPTION_SIZE = 200;

?>

<h1><?php echo $forum_name; ?></h1>

<?php

CheckField ("topic identifier", $bbtopic_id, true);
if (!$bbtopic_id || $action == "Add")
  CheckField ("section identifier", $bbsection_id, false);

if (!$foruminfo)
  Problem ("You have not logged in");

echo "<hr>\n";
   
if ($bbtopic_id)
  {
  $query = "SELECT * "
         . "FROM  bbtopic, bbsection "
         . "WHERE bbtopic.bbtopic_id = $bbtopic_id "
         . "AND   bbsection.bbsection_id = bbtopic.bbsection_id ";
                                
  $result = mysql_query ($query) 
      or Problem ("Select of topic failed: " . mysql_error ());
  
  if (!($row = mysql_fetch_array ($result)))
    Problem ("Topic id $bbtopic_id is not on file");
    
  mysql_free_result ($result);

  if (!($foruminfo ['admin'] ||
       $foruminfo ['moderator_topic'] == $row ['bbtopic_id'] ||
       $foruminfo ['moderator_section'] == $row ['bbsection_id']))
    Problem ("You are not the moderator of this topic, nor an admin");

  $section_name = htmlentities ($row ['section_name']);
  $bbsection_id = $row ['bbsection_id'];

  echo "<h2>Amend topic description in $section_name</h2>\n";

  } // end of having a topic
else
  {
  $query = "SELECT * "
         . "FROM  bbsection "
         . "WHERE bbsection.bbsection_id = $bbsection_id ";
                                
  $result = mysql_query ($query) 
      or Problem ("Select of section failed: " . mysql_error ());
  
  if (!($row = mysql_fetch_array ($result)))
    Problem ("Section id $bbsection_id is not on file");
    
  mysql_free_result ($result);
  
  if (!($foruminfo ['admin'] ||
       $foruminfo ['moderator_section'] == $row ['bbsection_id']))
    Problem ("You are not the moderator of this section, nor an admin");

  $section_name = htmlentities ($row ['section_name']);

  echo "<h2>Add topic to $section_name</h2>\n";
  
  } // end of adding a topic
  
  
$specials = array
  (
  'topic_name'     => array (
                'input' => 'topic_name',
                'description' => 'Topic:',
                'comment' => "Maximum of $MAX_TOPIC_SIZE characters.",
                'size' => 80,
                'maxlength' => $MAX_TOPIC_SIZE
                ),    // topic_name
  'admin_only'     => array (
                'input' => 'admin_only',
                'type' => 'bool',
                'description' => 'Administrators only:',
                'comment' => "If checked, only administrators or moderators can post.",
                ),    // admin_only
  'topic_description' => array (
                'input' => 'topic_description',
                'description' => 'Description:',
                'comment' => "Description of topic. Maximum of $MAX_DESCRIPTION_SIZE characters. "
                           . "Text only please, no HTML.",
                'size' => 80,
                'maxlength' => $MAX_DESCRIPTION_SIZE
                ),    // topic_description
  'topic_motd' => array (
                'input' => 'topic_motd',
                'type' => 'multiline',
                'description' => 'MOTD:',
                'comment' => "Message Of The Day (MOTD) for this topic, in HTML.",
                'rows' => 10,
                'cols' => 60
                ),    // topic_motd
  );  // end of topic_name

if ($action == "Update" || $action == "Add")
  {
  ValidateOneField ('topic_name', 'text', true, $MAX_TOPIC_SIZE, $specials);      
  ValidateOneField ('admin_only', 'bool', false, 1, $specials);      
  ValidateOneField ('topic_description', 'text', true, $MAX_DESCRIPTION_SIZE, $specials);      
  ValidateOneField ('topic_motd', 'text', false, 0, $specials);      
  } // end of Update requested
else
  if ($bbtopic_id)
    {
    $topic_name = addslashes ($row ['topic_name']);
    $admin_only = $row ['admin_only'];
    $topic_description = addslashes ($row ['topic_description']);    
    $topic_motd = addslashes ($row ['topic_motd']);    
    }

if ($have_error)
  {
?>
<hr>
<h2>Error in submission</h2>
<p>There were some errors in processing your submission.</p>
<p>Please try again. Thank you.</p> 
  
<?php  
  } // end of error in fields
else
  if ($action == "Update")
    {
    $query = "UPDATE bbtopic SET "
           . " topic_name = '$topic_name', "
           . " admin_only = '$admin_only', "
           . " topic_description = '$topic_description', "
           . " topic_motd = '$topic_motd' "
           . "WHERE bbtopic_id = $bbtopic_id";
    
    $result = mysql_query ($query)
      or Problem ("Update of bbtopic failed: " . mysql_error ());

    if (mysql_affected_rows () > 0)
      echo "<p>Topic updated.</p>\n";
    else
      echo "<p>No changes made.</p>\n";

    echo "<hr><p>\n";
    hLink ("View topic $section_name : $topic_name", "bbshowpost.php", "bbtopic_id=$bbtopic_id");
    echo "</p><p>";
    hLink ("View section $section_name", "bbshowpost.php", "bbsection_id=$bbsection_id");
    echo "</p><p>";
    hLink ("Back to $forum_name", "bbshowpost.php");
    echo "</p>";
    
    MessageTail (false);
          
    
  } // end of Update 
else  if ($action == "Add")
    {
    $query = "INSERT INTO bbtopic (bbsection_id, topic_name, admin_only, topic_description, topic_motd) "
           . "VALUES  ("
           . "'$bbsection_id', "
           . "'$topic_name', "
           . "'$admin_only', "
           . "'$topic_description', "
           . "'$topic_motd'"
           . ") ";
    
    $result = mysql_query ($query)
      or Problem ("Insert into bbtopic failed: " . mysql_error ());

    $bbtopic_id = mysql_insert_id ();

    echo "<p>Topic added: <b>" . htmlentities ($topic_name) . "</b></p>\n";

    echo "<hr><p>\n";
    hLink ("View topic $section_name : $topic_name", "bbshowpost.php", "bbtopic_id=$bbtopic_id");
    echo "</p><p>";
    hLink ("Add another topic", $PHP_SELF, "bbsection_id=$bbsection_id", false);
    echo "</p><p>";
    hLink ("View section $section_name", "bbshowpost.php", "bbsection_id=$bbsection_id");
    echo "</p><p>";
    hLink ("Back to $forum_name", "bbshowpost.php");
    echo "</p>";
    
    MessageTail (false);
    
  } // end of Add 

echo "<form METHOD=post ACTION=\"$PHP_SELF\">\n";

$table ['topic_name'] = stripslashes ($topic_name);
$table ['admin_only'] = stripslashes ($admin_only);
$table ['topic_description'] = stripslashes ($topic_description);
$table ['topic_motd'] = stripslashes ($topic_motd);
  
$params = array
  (
  'LH' => 'valign=top bgcolor="#ADD8E6" align=right',
  'RH' => 'bgcolor="#FAF0E6" align=left'
  );

        
ShowTable ($table, $params, $specials);

echo "<p>";
if ($bbtopic_id)
  {
  echo "<input Type=submit Name=action Value=\"Update\">";
  echo "<input type=hidden name=action Value=\"Update\">";
  }
else
  {
  echo "<input Type=submit Name=action Value=\"Add\">";
  echo "<input type=hidden name=action Value=\"Add\">";
  }

echo "</p>";
?>


<input type=hidden name=bbtopic_id value=<?php echo ($bbtopic_id); ?>>
<input type=hidden name=bbsection_id value=<?php echo ($bbsection_id); ?>>
<?php FormSession (); ?>

</form>
    
<?php

echo "<hr>\n";

// view the topic again?

if ($bbtopic_id)
  {
  echo "<p>\n";
  hLink ("View topic $section_name : $topic_name", "bbshowpost.php", "bbtopic_id=$bbtopic_id");
  echo "</p>\n";
  }

// view the section again?

echo "<p>\n";
hLink ("View section $section_name", "bbshowpost.php", "bbsection_id=$bbsection_id");
echo "</p>\n";

// back to forum anyway

echo "<p>";
hLink ("Back to $forum_name", "bbshowpost.php");
echo "</p>";

MessageTail (false);
?>    
