<?php

/****
 * for the first just the admin is able to edit
 ****/ 

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

$tid = intval($_REQUEST["tid"]);
$pid = intval($_REQUEST["pid"]);

$SBBTopics->tid = $tid;
$SBBTopics->pid = $pid;

$TEMPLATE_FLAGS["EDITTOPICTITLE"] = $TEMPLATE_FLAGS["TOPICNOTLOCKED"] = true;
if ($board_config->en_attachments) $TEMPLATE_FLAGS["ATTACHMENT_PART"] = true;

$FORM_VAL_ACTION = "edit_post";

$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");
$FORM_VAL_FTS = "";

$HEADLINE_ATTACHMENT = lang("add_attachment");
$DESCR_ATTACHMENT_INFO = sprintf(lang("descr_attachment_info"),$board_config->max_attachment_size);
$DESCR_ATT_FILE = lang("descr_att_file");
$DESCR_ATT_DESCR = lang("descr_att_descr");
$DESCR_OPTIONAL = lang("optional");
$DESCR_DELETE = lang("delete");

$CONFIRM_DELATTACHMENT = lang("confirm_delattachment");

$DESCR_MORE_SMILIES = lang("more_smilies");
$HEADLINE_SMILIES = lang("hl_more_smilies");

  //topicid is avail in every case
  $row = $SBBTopics->GetSingleTopic();
  
  if ($row->topic) {
    $breadcrumps[] = array("link" => $board_config->seo ? "$main_path/sbbf" . $row->fid . adddescr2link($forum[$row->fid]->title) . ".html" : "$main_script?todo=show_topics&amp;fid=" . $row->fid, 
                           "descr" => $forum[$row->fid]->title);
    $breadcrumps[] = array("link" => $board_config->seo ? "$main_path/sbbt" . $tid . adddescr2link($row->topic) . ".html" : "$main_script?todo=viewtopic&amp;tid=$tid", 
                           "descr" => $row->topic);
    $sbb_add2title .= " - " . he($row->topic);
  }
  
  $TOPIC_TITLE = $row->topic;
  $TOPIC_ID = $tid;
  $FORUM_ID = $row->fid;
  
  if (!$pid && $tid) {
    if (mysql_num_rows($res)) $postfound = true;
    if (!$action) {
      $topic = $row->topic;
      $message = $row->detail;
      $sticky = $row->sticky;
      $poll_title = $row->poll_title;
      $poll_options = $row->poll_options;
      $poll_multiple = $row->poll_multiple;
      $poll_duration = $row->poll_duration;
    }
    $is_topic = true;
    //check attachments
    if ($row->attachments) {
      $attcnt = count($row->attachments);
      for ($i=0;$i<$attcnt;$i++) {
        $thisatt = $row->attachments[$i];
        $ext = strtolower(substr(strrchr($thisatt->file_name,"."),1));
        $ATTACHMENTS[] = array("ATT_LINK" => ($board_config->seo ? "$main_path/sbbdl" . $thisatt->id . "t" . $thisatt->tid . ".$ext" : "$rel_path/d.php?id=" . $thisatt->id . "&amp;tid=" . $thisatt->tid),
                               "ATT_ID" => $thisatt->id,
                               "ATT_DESCR" => $thisatt->file_name,
                               "ATT_INFO" => $thisatt->descr . " " . fsize(filesize($www_data_dir . "/attachments/" . $thisatt->id . "/" . $thisatt->file_name)));
      }
    }
    
    $TEMPLATE_FLAGS["VOTING_PART"] = true;
  } elseif ($pid && $tid) {
    //the single post
    $row = $SBBTopics->GetSinglePost();

    if (mysql_num_rows($res)) $postfound = true;
        
    $POST_ID = $pid;
    
    if (!$action) {
      $topic = $row->a_topic;
      $message = $row->a_answer;
    }    
    //check attachments
    if ($row->attachments) {
      $attcnt = count($row->attachments);
      for ($i=0;$i<$attcnt;$i++) {
        $thisatt = $row->attachments[$i];
        $ext = strtolower(substr(strrchr($thisatt->file_name,"."),1));
        $ATTACHMENTS[] = array("ATT_LINK" => ($board_config->seo ? "$main_path/sbbdl" . $thisatt->id . "t" . $thisatt->tid . ".$ext" : "$rel_path/d.php?id=" . $thisatt->id . "&amp;tid=" . $thisatt->tid),
                               "ATT_ID" => $thisatt->id,
                               "ATT_DESCR" => $thisatt->file_name,
                               "ATT_INFO" => $thisatt->descr . " " . fsize(filesize($www_data_dir . "/attachments/" . $thisatt->id . "/" . $thisatt->file_name)));
      }
    }
  } 
  
  $HEADLINE_FORM = lang("editpost");

 
  //checkout external smilies
  $smilies = smilies();
  if ($smilies["count"]) {
    $TEMPLATE_FLAGS["EXTERNAL_SMILIES"] = true;
    $SMILIES = $smilies["files"]; //template vars will be set within smilies() -> functions.php
  }


  if (!$f_auth_uid) $TEMPLATE_FLAGS["GUEST_NAME"] = 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");
  $BB_LISTENTRY_TITLE = lang("listitem");
  
  $FORM_VAL_VOTEQ = $poll_title;
  $FORM_VAL_VOTEA = stripslashes($poll_options);
  if ($poll_multiple) $FORM_CHECKED_ENMULTI = "checked";
  $FORM_VAL_DURATION = $poll_duration;
  
  $FORM_CHECKED_STICKYOFF = (!$sticky ? "checked" : "");
  $FORM_CHECKED_STICKYON = ($sticky ? "checked" : "");
  
  $FORM_VAL_TOPIC = he($topic);
  $FORM_VAL_MESSAGE = $message;
  
  $TEMPLATE_FLAGS["DEFINE_TOPIC_TYPE"] = true;
  if (!$is_mod) $TEMPLATE_FLAGS["DEFINE_TOPIC_TYPE"] = false;
  elseif (!$is_topic) $TEMPLATE_FLAGS["DEFINE_TOPIC_TYPE"] = false;
  

  /************
   * initiate template and assign variables
   ************/         
  $editpostVARS = array('DESCR_TITLE' => $DESCR_TITLE,
                        'DESCR_MESSAGE' => $DESCR_MESSAGE,
                        'BB_LISTENTRY_TITLE' => $BB_LISTENTRY_TITLE,
                        'DESCR_SUBMIT' => $DESCR_SUBMIT,
                        'HEADLINE_FORM' => $HEADLINE_FORM,
                        'DESCR_TOPICTYPE' => $DESCR_TOPICTYPE,
                        'DESCR_STICKYON' => $DESCR_STICKYON,
                        'DESCR_STICKYOFF' => $DESCR_STICKYOFF,
                        'FORM_VAL_TOPIC' => $FORM_VAL_TOPIC,
                        'FORM_VAL_MESSAGE' => $FORM_VAL_MESSAGE,
                        'POST_NOT_FOUND' => $POST_NOT_FOUND,
                        'FORM_CHECKED_STICKYOFF' => $FORM_CHECKED_STICKYOFF,
                        'FORM_CHECKED_STICKYON' => $FORM_CHECKED_STICKYON,
                        'POST_ID' => $POST_ID,
                        'TOPIC_ID' => $TOPIC_ID,
                        'TOPIC_TITLE' => $TOPIC_TITLE,
                        'FORUM_ID' => $FORUM_ID,
                        'FORM_VAL_ACTION' => $FORM_VAL_ACTION,
                        'CONFIRM_DELATTACHMENT' => $CONFIRM_DELATTACHMENT,
                        'DESCR_MORE_SMILIES' => $DESCR_MORE_SMILIES,
                        'HEADLINE_SMILIES' => $HEADLINE_SMILIES,
                        '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_VOTEQ' => $FORM_VAL_VOTEQ,
                        'FORM_VAL_VOTEA' => $FORM_VAL_VOTEA,
                        'FORM_CHECKED_ENMULTI' => $FORM_CHECKED_ENMULTI,
                        'FORM_VAL_DURATION' => $FORM_VAL_DURATION,
                        'FORM_VAL_FTS' => $FORM_VAL_FTS,
                        'DESCR_CREATE_POLL_INFO' => $DESCR_CREATE_POLL_INFO,
                        '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);
  $QuickSkin = new QuickSkin("add_topic.html");
  $QuickSkin->assign(array_merge($editpostVARS,array("CONF" => $GLOBAL_TEMPLATE_VARS,"POSTS" => $POSTS,"SMILIES" => $SMILIES, "ATTACHMENTS" => $ATTACHMENTS,"FLAGS" => $TEMPLATE_FLAGS)));
  $content .= $QuickSkin->result();
  unset($QuickSkin);

?>