<?php


// contents of function MessageForm

/*

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 $action, $foruminfo, $MAX_SUBJECT_SIZE, $specials,
         $bbsection_id, $bbtopic_id, $bbsubject_id, $bbpost_id, 
         $THIS_PAGE, $headingrow, $have_error, $HTTP_POST_VARS,
         $subject, $username, $password, $contents, $forum_code,
         $control, $page;
  
  if ($have_error || ($action != 'Save' && $action != 'Save Changes'))
    {
    $subject = addslashes ($headingrow ['subject_name']);
  
    if ($have_error)
      $subject = $HTTP_POST_VARS ['subject'];  // get old subject back
      
    if ($bbpost_id && !$have_error)
      {
     
      $query = "SELECT * FROM bbpost, bbsubject, bbtopic "
             . "WHERE bbpost_id = $bbpost_id "
             . "  AND   bbsubject.bbsubject_id = bbpost.bbsubject_id "
             . "  AND   bbtopic.bbtopic_id = bbsubject.bbtopic_id ";
       
    //  echo $query;
                            
      $result = mysql_query ($query) 
          or Problem ("Select of message failed: " . mysql_error ());
  
      if (!($row = mysql_fetch_array ($result)))
          Problem ("Message $bbpost_id does not exist.");
  
      // slashes will be stripped when we display the message
      $contents = addslashes ($row ['post_text']);
      $forum_code = $row ['forum_code'];
      $html = $row ['html'];
      $post_date = $row ['post_date'];
              
      $poster = $row ['bbuser_id'];
      
      mysql_free_result ($result);
          
      if ($poster != $foruminfo ['bbuser_id'] && !($foruminfo ['admin'] ||
                     $foruminfo ['moderator_topic'] == $row ['bbtopic_id'] ||
                     $foruminfo ['moderator_section'] == $row ['bbsection_id']))
        Problem ("You did not post this message");
      
      
      } // end of needing to read in the message
    else
      {
      $contents = "";
      $forum_code = "";
      $html = "";
      $post_date = "";              
      $poster = "";
    	}	// end of no existing message
      
    if ($action == 'add')
      echo "<h5>(New message)</h5>\n";
    else
      echo "<h5>(Edit message)</h5>\n";
            
    }   // end of not saving the message

    
  echo "<form METHOD=post ACTION=\"$THIS_PAGE\">\n";
    
  $table ['subject'] = stripslashes ($subject);

// we will let admins post under a different name for importing legacy messages

  if (!$foruminfo || 
     (($action == 'add' || $have_error)
      && $foruminfo ['admin'] && $control ['post_under_different_name']))
    $table ['username'] = stripslashes ($username);

  if (!$foruminfo)
    $table ['password'] = stripslashes ($password);

  $table ['contents'] = stripslashes ($contents);
  $table ['forum_code'] = $forum_code;

  if ($foruminfo ['htmlpost'])
    $table ['html'] = $html;

  if ($foruminfo)
    {
      
    if ($bbsubject_id)
      {
      $bbuser_id = $foruminfo ['bbuser_id'];
      
      $query = "SELECT * "
             . "FROM bbnotify_subject "
             . "WHERE bbsubject_id = $bbsubject_id "
             . "AND   bbuser_id = $bbuser_id ";
                                   
      $result = mysql_query ($query) 
          or Problem ("Select of subscription failed: " . mysql_error ());
        
      $subscribed = mysql_fetch_array ($result);
      if ($subscribed)
        $subscribed = true;	// otherwise it is an array
      else
        $subscribed = false;	// otherwise it is an array

      mysql_free_result ($result);
      }
    else
      $subscribed = false;

    $table ['subscribe'] = $subscribed;

  } // end of being logged into the forum  
  
  if ($foruminfo ['admin'])
    {
   
    if ($control ['amend_post_date'])
      $table ['post_date'] = $post_date;
    
    } // end of being an administrator
      
  $params = array
    (
    'LH' => 'valign=top bgcolor="#ADD8E6" align=right',
    'RH' => 'bgcolor="#FAF0E6" align=left'
    );
  
        
  ShowTable ($table, $params, $specials);
  
  if ($action == 'amend')
    {
  ?>
  
  <p><b>Save changes ...</b></p>
  <p>
  <input Type=submit Name=action Value="Save Changes">
  </p>
  
  <?php
    } // end of amending message
  else
    { 
  ?>
  
  <p><b>Save this message ...</b></p>
  <p>
  <input Type=submit Name=action Value="Save">
  </p>
  
  <?php
    } // end of adding message
   
  ?>
         
  <input type=hidden name="bbpost_id" value=<?php echo ($bbpost_id); ?>>
  <input type=hidden name="bbtopic_id" value=<?php echo ($bbtopic_id); ?>>
  <input type=hidden name="bbsubject_id" value=<?php echo ($bbsubject_id); ?>>
  <input type=hidden name="page" value=<?php echo ($page); ?>>
  <input type=hidden name=action value=Save>
  <?php FormSession (); ?>
  
  </form>
      
  <?php

?>
