<?php


include ("$www_dir/classes/topics.class.php");
$SBBTopics = new SBBTopics();

$FORM_VAL_ACTION = "add_topic";

$TEMPLATE_FLAGS["EDITTOPICTITLE"] = false;
$TEMPLATE_FLAGS["DEFINE_TOPIC_TYPE"] = true;

if ($tid) { //get fid
  $SBBTopics->tid = $tid;
  $SBBTopics->GetPosts(false);    //w/o pagination
  $maintopic = $SBBTopics->posts["main"];
  $fid = $maintopic->fid;
}
  if (!$forum[$fid]) {  //forum not avail
     /************
     * initiate template and assign variables
     ************/         
    $QuickSkin = new QuickSkin("status_messages.html");
    $QuickSkin->assign("INFO",lang("forum_notfound"));
    $content .= $QuickSkin->result();
    unset($QuickSkin);
  } elseif (
      (!$maintopic && !$forum[$fid]->allowed2post) || //no permission to create new topics
      ($maintopic && !$forum[$fid]->allowed2reply) //no permission to reply to topics
      ) {
     /************
     * initiate template and assign variables
     ************/         
    $QuickSkin = new QuickSkin("status_messages.html");
    $QuickSkin->assign("ERROR",lang("error_noperm"));
    $content .= $QuickSkin->result();
    unset($QuickSkin);  
  } else {              //forum avail    
    //QUOTE CHECK
    if ($_REQUEST["quote"]) {
      if (intval($_REQUEST["pid"])) $quote_answer = true; //answer will be quoted in the answer loop
      else $message = "[quote]" . $maintopic->detail . "[/quote]"; 
    }
    $breadcrumps[] = array("link" => $board_config->seo ? "$main_path/sbbf" . $fid . adddescr2link($forum[$fid]->title) . ".html" : "$main_script?todo=show_topics&amp;fid=" . $fid, 
                           "descr" => $forum[$fid]->title);
  
    if (!$f_auth_uid) $TEMPLATE_FLAGS["GUEST_NAME"] = true;
  
    if ($tid && $maintopic) {
      
      $breadcrumps[] = array("link" => $board_config->seo ? "$main_path/sbbt" . $tid . adddescr2link($maintopic->topic) . ".html" : "$main_script?todo=viewtopic&amp;tid=$tid", 
                           "descr" => $maintopic->topic);
      
      $sbb_add2title .= " - " . he($maintopic->topic);
      
      //Template Vars
      $TOPIC_TITLE = $maintopic->topic;
      $TOPIC_ID = $tid;
      $HEADLINE_FORM = lang("reply_to_topic");
      
      $reply = true;
      
      //hide poll part
      $TEMPLATE_FLAGS["VOTING_PART"] = false;

      $TEMPLATE_FLAGS["TOPICFOUND"] = true;
  
    } else {
      //Template Vars
      $HEADLINE_FORM = lang("createtopic");
      $is_topic = true;
      
      //hide poll part????
      if ($board_config->en_polls) $TEMPLATE_FLAGS["VOTING_PART"] = true;
      
    }
    
    
    $FORUM_ID = $fid;
    
    //topic locked?
    if ($maintopic->locked) {
      $TEMPLATE_FLAGS["TOPICLOCKED"] = true;
      $TOPIC_LOCKED = lang("error_topiclocked");
    } else {
      $TEMPLATE_FLAGS["TOPICNOTLOCKED"] = true;      
    }
    
    if ($board_config->en_attachments) $TEMPLATE_FLAGS["ATTACHMENT_PART"] = true;
    
    if (!$is_mod) $TEMPLATE_FLAGS["DEFINE_TOPIC_TYPE"] = false;
    elseif (!$is_topic) $TEMPLATE_FLAGS["DEFINE_TOPIC_TYPE"] = false;
    
    
    //checkout external smilies
    $smilies = smilies();
    if ($smilies["count"]) {
      $SMILIES = $smilies["files"]; //template vars will be set within smilies() -> functions.php
      $TEMPLATE_FLAGS["EXTERNAL_SMILIES"] = true;
    }    
    
    //Template Vars
    $DESCR_TITLE = lang("title");
    $DESCR_MESSAGE = lang("your_message");
    $DESCR_SUBMIT = lang("submit");
    $DESCR_TOPICTYPE = lang("createas");
    $DESCR_STICKYOFF = lang("normal");
    $DESCR_STICKYON = lang("sticky");
    $DESCR_NICKNAME = lang("nickname");
    
    $HEADLINE_VOTING = lang("create_poll");
    $DESCR_CREATE_POLL_INFO = lang("create_poll_info");
    $DESCR_VOTEQ = lang("poll_question");
    $DESCR_VOTEA = lang("poll_options");
    $DESCR_VOTEA_INFO = lang("poll_options_info");
    if ($board_config->max_poll_options) $DESCR_VOTEA_SUBINFO = sprintf(lang("poll_options_subinfo"), $board_config->max_poll_options);
    $DESCR_VOTEMULTIA = lang("poll_multi");
    $DESCR_ENMULTI = lang("poll_multi_info");
    $DESCR_DURATION = lang("poll_duration");
    $DESCR_DURATION_INFO = lang("poll_duration_info");
    
    $HEADLINE_ATTACHMENT = lang("add_attachment");
    $DESCR_ATTACHMENT_INFO = $board_config->en_attachments && $board_config->max_attachment_size ? sprintf(lang("descr_attachment_info"),$board_config->max_attachment_size) : false;
    $DESCR_ATT_FILE = lang("descr_att_file");
    $DESCR_ATT_DESCR = lang("descr_att_descr");
    $DESCR_OPTIONAL = lang("optional");
    $DESCR_DELETE = lang("delete");
    
    $DESCR_MORE_SMILIES = lang("more_smilies");
    $HEADLINE_SMILIES = lang("hl_more_smilies");
    
    $BB_LISTENTRY_TITLE = lang("listitem");
    $FORM_VAL_TOPIC = he($topic);
    $FORM_VAL_MESSAGE = $message;
    $FORM_VAL_FTS = time();
    
    $FORM_VAL_GUEST_NAME = $guest_name;
    
    $FORM_VAL_VOTEQ = $poll_title;
    $FORM_VAL_VOTEA = stripslashes($poll_options);
    if ($poll_multiple) $FORM_CHECKED_ENMULTI = "checked='checked'";
    $FORM_VAL_DURATION = $poll_duration;
    
    $FORM_CHECKED_STICKYOFF = (!$sticky ? "checked='checked'" : "");
    $FORM_CHECKED_STICKYON = ($sticky ? "checked='checked'" : "");
    
    $FORM_VAL_ATT_DESCR = $att_descr;

    if ($tid) {
      //topics overview
      
      //Template Vars
      $HEADLINE_TOPICSOVERVIEW = lang("topic_overview");
      $DESCR_TOPIC = lang("topics");
      $DESCR_AUTHOR = lang("author");
      $DESCR_COMPOSED = lang("composedon");
      
      //replies
      if ($SBBTopics->posts["replies"]) {
        for ($i=($SBBTopics->count-1);$i>=0;$i--) {
          $row = $SBBTopics->posts["replies"][$i];
          $this_user = forum_user($row->a_uid);
          
          $POSTS[] = array("POSTER_CLASS" => nick_class($this_user->is_admin, $this_user->is_mod),
                           "POSTER" => ($this_user ? $this_user->user : lang("guest")),
                           "POST_DATE" => date($date_time_format,strtotime($row->a_datetime)),
                           "DESCR_POST_TITLE" => ($row->a_topic != "Re: $this_post_title" ? lang("title") : ""),
                           "POST_TITLE" => ($row->a_topic != "Re: $this_post_title" ? $row->a_topic : ""),
                           "POST_MESSAGE" => bbencode($row->a_answer),
                          );
          //for quoting an answer
          if ($quote_answer && $row->a_id == $_REQUEST["pid"]) {
            $FORM_VAL_MESSAGE = "[quote]" . $row->a_answer . "[/quote]"; 
          }
        }
      }
      //the maintopic (1st post)
      $this_user = forum_user($maintopic->uid);
      $POSTS[] = array("POSTER_CLASS" => nick_class($this_user->is_admin, $this_user->is_mod),
                       "POSTER" => ($this_user ? $this_user->user : lang("guest")),
                       "POST_DATE" =>  date($date_time_format,strtotime($maintopic->datetime)),
                       "DESCR_POST_TITLE" => lang("title"),
                       "POST_TITLE" => $maintopic->topic,
                       "POST_MESSAGE" => bbencode($maintopic->detail),
                      );
    }
    /************
     * initiate template and assign variables
     ************/         
    $addtopicVARS = array('DESCR_TITLE' => $DESCR_TITLE,
                          'DESCR_MESSAGE' => $DESCR_MESSAGE,
                          'DESCR_AUTHOR' => $DESCR_AUTHOR,
                          'DESCR_COMPOSED' => $DESCR_COMPOSED,
                          'BB_LISTENTRY_TITLE' => $BB_LISTENTRY_TITLE,
                          'DESCR_SUBMIT' => $DESCR_SUBMIT,
                          'DESCR_POST_TITLE' => $DESCR_POST_TITLE,
                          'HEADLINE_TOPICSOVERVIEW' => $HEADLINE_TOPICSOVERVIEW,
                          'HEADLINE_FORM' => $HEADLINE_FORM,
                          'DESCR_TOPICTYPE' => $DESCR_TOPICTYPE,
                          'DESCR_STICKYON' => $DESCR_STICKYON,
                          'DESCR_STICKYOFF' => $DESCR_STICKYOFF,
                          'TOPIC_LOCKED' => $TOPIC_LOCKED,
                          'HEADLINE_VOTING' => $HEADLINE_VOTING,
                          'DESCR_VOTEQ' => $DESCR_VOTEQ,
                          'DESCR_VOTEA' => $DESCR_VOTEA,
                          'DESCR_VOTEA_INFO' => $DESCR_VOTEA_INFO,
                          'DESCR_VOTEA_SUBINFO' => $DESCR_VOTEA_SUBINFO,
                          'DESCR_VOTEMULTIA' => $DESCR_VOTEMULTIA,
                          'DESCR_ENMULTI' => $DESCR_ENMULTI,
                          'DESCR_DURATION' => $DESCR_DURATION,
                          'DESCR_DURATION_INFO' => $DESCR_DURATION_INFO,
                          'HEADLINE_ATTACHMENT' => $HEADLINE_ATTACHMENT,
                          'DESCR_ATTACHMENT_INFO' => $DESCR_ATTACHMENT_INFO,
                          'DESCR_ATT_FILE' => $DESCR_ATT_FILE,
                          'DESCR_ATT_DESCR' => $DESCR_ATT_DESCR,
                          'DESCR_OPTIONAL' => $DESCR_OPTIONAL,
                          'DESCR_DELETE' => $DESCR_DELETE,
                          'FORM_VAL_ATT_DESCR' => $FORM_VAL_ATT_DESCR,
                          'FORM_VAL_ACTION' => $FORM_VAL_ACTION,
                          'DESCR_MORE_SMILIES' => $DESCR_MORE_SMILIES,
                          'HEADLINE_SMILIES' => $HEADLINE_SMILIES,
                          'FORM_VAL_GUEST_NAME' => $FORM_VAL_GUEST_NAME,
                          'DESCR_NICKNAME' => $DESCR_NICKNAME,
                          'FORM_VAL_VOTEQ' => $FORM_VAL_VOTEQ,
                          'FORM_VAL_VOTEA' => $FORM_VAL_VOTEA,
                          'FORM_CHECKED_ENMULTI' => $FORM_CHECKED_ENMULTI,
                          'FORM_VAL_DURATION' => $FORM_VAL_DURATION,
                          'DESCR_CREATE_POLL_INFO' => $DESCR_CREATE_POLL_INFO,
                          'FORM_VAL_TOPIC' => $FORM_VAL_TOPIC,
                          'FORM_VAL_MESSAGE' => $FORM_VAL_MESSAGE,
                          'FORM_VAL_FTS' => $FORM_VAL_FTS,
                          'FORM_CHECKED_STICKYOFF' => $FORM_CHECKED_STICKYOFF,
                          'FORM_CHECKED_STICKYON' => $FORM_CHECKED_STICKYON,
                          'TOPIC_ID' => $TOPIC_ID,
                          'TOPIC_TITLE' => $TOPIC_TITLE,
                          'POSTER_CLASS' => $POSTER_CLASS,
                          'POSTER' => $POSTER,
                          'POST_DATE' => $POST_DATE,
                          'POST_TITLE' => $POST_TITLE,
                          'POST_MESSAGE' => $POST_MESSAGE,
                          'FORUM_ID' => $FORUM_ID,
                          );

      $QuickSkin = new QuickSkin("add_topic.html");
      $QuickSkin->assign(array_merge($addtopicVARS,array("CONF" => $GLOBAL_TEMPLATE_VARS,"POSTS" => $POSTS,"SMILIES" => $SMILIES, "ATTACHMENTS" => $ATTACHMENTS,"FLAGS" => $TEMPLATE_FLAGS)));
      $content .= $QuickSkin->result();
      unset($QuickSkin);
      // echo "<pre>" . print_r($TEMPLATE_FLAGS,true) . "</pre>";

  } //forum avail

?>