<?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 ("Amend subject description", "forum, update, subject"); 

$forum_name = $control ['forum_name'];

$MAX_SUBJECT_SIZE = 80;

?>

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

<?php

CheckField ("subject identifier", $bbsubject_id, false);

if (!$foruminfo)
  Problem ("You have not logged in");
   
$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 ";
                              
$result = mysql_query ($query) 
    or Problem ("Select of subject failed: " . mysql_error ());

if (!($row = mysql_fetch_array ($result)))
  Problem ("Subject id $bbsubject_id is not on file");
  
mysql_free_result ($result);
  
if ($row ['author'] != $foruminfo ['bbuser_id'] &&
                    !($foruminfo ['admin'] ||
                     $foruminfo ['moderator_topic'] == $row ['bbtopic_id'] ||
                     $foruminfo ['moderator_section'] == $row ['bbsection_id']))
  Problem ("You are not the author of this subject");
  
echo "<hr>\n";

echo "<h2>Amend subject description</h2>\n";

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

echo "<p><b>$section_name : $topic_name : $subject_name</b></p>\n";
     
$specials = array
  (
  'subject'     => array (
                'input' => 'subject',
                'description' => 'Subject:',
                'comment' => "Maximum of $MAX_SUBJECT_SIZE characters.",
                'size' => 60,
                'maxlength' => $MAX_SUBJECT_SIZE
                ),    // subject
  );  // end of specials


if ($action == "Update")
  {
  ValidateOneField ('subject', 'text', true, $MAX_SUBJECT_SIZE, $specials);      
  } // end of Update requested
else
  $subject = addslashes ($row ['subject_name']);

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 bbsubject SET subject_name = '$subject' "
           . "WHERE bbsubject_id = $bbsubject_id";
    
    $result = mysql_query ($query)
      or Problem ("Update of bbsubject failed: " . mysql_error ());

    if (mysql_affected_rows () > 0)
      echo "<p>Subject changed to: <b>" . htmlentities ($subject) . "</b></p>\n";
    else
      echo "<p>No changes made.</p>\n";

    echo "<hr><p>\n";
    hLink ("View this subject", "bbshowpost.php", "bbsubject_id=$bbsubject_id", false);
    echo "</p><p>";
    hLink ("Back to $forum_name", "bbshowpost.php");
    echo "</p>";
    
    MessageTail (false);
          
    
  } // end of Update 

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

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

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

echo "<p>";
echo "<input Type=submit Name=action Value=\"Update\">";
echo "<input type=hidden name=action Value=\"Update\">";
echo "</p>";
?>


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

</form>
    
<?php

echo "<hr><p>\n";
hLink ("View this subject", "bbshowpost.php", "bbsubject_id=$bbsubject_id", false);
echo "</p><p>";
hLink ("Back to $forum_name", "bbshowpost.php");
echo "</p>";

MessageTail (false);
?>    
