<?php

class SBBTopics {

  var $count = 0;
  var $error;
  var $success;
  var $fid = 0;
  var $tid = 0;
  var $pid = 0;
  var $p = 1;
  var $forum_watched = false;
  var $topics = array();
  var $posts = array();
     
  function SBBTopics() {
    global $SBBAuth, $board_config;
    $this->auth = $SBBAuth;
    $this->board_config = $board_config;
    $this->per_page = $board_config->topics_per_page;
    $this->pper_page = $board_config->posts_per_page;
    if (intval($_REQUEST["p"])) $this->p = intval($_REQUEST["p"]);    
    $this->new_tids = $SBBAuth->new_tids;
    $this->last_visit = $SBBAuth->last_visit;
    $this->viewed_tids = ($_SESSION["viewed_tids"] ? $_SESSION["viewed_tids"] : array());
  }
  
  function GetTopics($paginate=true) {
    if (!$paginate) $this->per_page = false;
    //get totals
    $sql = "select id from `" . $this->board_config->prefix . "question` where fid = '" . $this->fid . "';";
    $res = dbquery($sql);
    $total = mysql_num_rows($res);
    
    //some pagecalculations
    if ($this->per_page) {
      if (($this->p * $this->per_page) >= ($total+$this->per_page)) $this->p = 1;       //if you are redirecting to page without ..., the page will be set to 1
      $offset = ($this->p * $this->per_page) - $this->per_page;
      $this->pages = ceil($total/$this->per_page);
      $limitsql = " limit $offset, " . $this->per_page;
    }
    
    //get watched topics
    $sql = "select * from `" . $this->board_config->prefix . "watchlist` where uid = '" . $this->auth->uid . "';";
    $res = dbquery($sql);
    while ($row = mysql_fetch_object($res)) {
      $watched[$row->tid] = true;
    }
    
    //new topics since last visit?
    if ($this->new_tids) {
      $unseen = array_fill_keys($this->new_tids, true);
    }
    //any new topics since recently last visit?
    if ($this->last_visit) {
      $sql = "select id from `" . $this->board_config->prefix . "question` where a_datetime > '" . $this->last_visit . "' and fid = '" . $this->fid . "' and (uid != '" . $this->auth->uid . "' and a_uid = '0')";
      $res = dbquery($sql);
      while ($row = mysql_fetch_object($res)) {
        if (!in_array($row->id,$this->viewed_tids)) {
          $unseen[$row->id] = true;
        }
      }
    }
    
    //the topics  
    $sql = "select * from `" . $this->board_config->prefix . "question` where fid = '" . $this->fid . "' ORDER BY sticky desc, a_datetime desc $limitsql;";
    $res = dbquery($sql);
    while ($row = mysql_fetch_object($res)) {
      $row->watched = $watched[$row->id];
      $row->unseen = $unseen[$row->id];
      $this->topics[] = $row;
    }
    $this->count = mysql_num_rows($res);
  }
  
  //is the current forum watched?
  function forum_watched() {
    $res = dbquery("select id from `" . $this->board_config->prefix . "watchlist` where fid = '" . $this->fid . "' and uid = '" . $this->auth->uid . "';");
    return (mysql_num_rows($res) ? true : false);
  }
  
  //is the current topic watched?
  function topic_watched() {
    $res = dbquery("select * from `" . $this->board_config->prefix . "watchlist` where tid = '" . $this->tid . "' and uid = '" . $this->auth->uid . "';");
    return mysql_num_rows($res);
  }
  
  //is the current topic watched?
  function mark_topic_read() {
    if ($this->tid && $this->auth->uid && $this->new_tids) {
      $new_tids = array_diff($this->new_tids, array($this->tid));
      $new_tids = array_values($new_tids);
      $this->new_tids = $_SESSION["new_tids"] = $new_tids;
    }
  }
  
  //posts of topic
  function GetPosts($paginate=true) {
    if (!$this->tid) return false;
    if (!$paginate) $this->pper_page = false;
    
    //get totals
    $sql = "SELECT a_id FROM `" . $this->board_config->prefix . "answer` WHERE question_id = '" . $this->tid . "';";
    $res = dbquery($sql);
    $total = mysql_num_rows($res) + 1; //because of the maintopic (+1)
    
    //some pagecalculations - only for the replies
    if ($this->pper_page) {
      if (($this->p * $this->pper_page) >= ($total+$this->pper_page)) $this->p = 1;       //if you are redirecting to page without ..., the page will be set to 1
      $offset = ($this->p * $this->pper_page) - $this->pper_page;
      $this->pages = ceil($total/$this->pper_page);
      $limitsql = " limit " . ($this->p > 1 ? $offset-1 : $offset) . ", " . ($this->p == 1 ? ($this->pper_page-1) : $this->pper_page);
    }
    
    $sql = "select * from `" . $this->board_config->prefix . "question` where id = '" . $this->tid . "';";
    $res = dbquery($sql);
    if (mysql_num_rows($res)) {
      $row = mysql_fetch_object($res);
      
      if ($row->poll_title) {
        $sqlt = "select * from `" . $this->board_config->prefix . "poll_options` where tid = '" . $this->tid . "' order by id;";
        $rest = dbquery($sqlt);
        while($rowt = mysql_fetch_object($rest)) {
          $poll_options[] = $rowt;
          $poll_totals += $rowt->votes;
          if ($rowt->votes > $max_votes) $max_votes = $rowt->votes;
        }
        $row->poll_options = $poll_options;
        $row->poll_count = mysql_num_rows($rest);
        $row->poll_totals = $poll_totals;
        $row->max_votes = $max_votes;
      }
      
      if ($row->attachment) {
        $sqlt = "select * from `" . $this->board_config->prefix . "attachments` where tid = '" . $this->tid . "' and pid = '0' order by id;";
        $rest = dbquery($sqlt);
        while ($rowt = mysql_fetch_object($rest)) {
          $attachments[] = $rowt;
        }
        $row->attachments = $attachments;
      }

      //likes
      $ratings["likes"] = $ratings["dislikes"] = array();
      if ($this->board_config->en_likes || $this->board_config->en_dislikes) {
        $sql = "select * from `" . $this->board_config->prefix . "ratings` where tid = '" . $this->tid . "' and pid = '0' order by id desc";
        $res = dbquery($sql);
        while ($ratrow = mysql_fetch_object($res)) {
          $ratings[$ratrow->rating == 1 ? "likes" : "dislikes"][] = $ratrow;
        }
      }
      $row->ratings = $ratings;


      $this->posts["main"] = $row;
      
      
      //replies
      $sql = "SELECT * FROM `" . $this->board_config->prefix . "answer` WHERE question_id = '" . $this->tid . "' order by a_id $limitsql;";
      $res = dbquery($sql);
      while($row = mysql_fetch_object($res)) {
        $attachments = array();
        $sqlt = "select * from `" . $this->board_config->prefix . "attachments` where tid = '" . $this->tid . "' and pid = '" . $row->a_id . "' order by id;";
        $rest = dbquery($sqlt);
        while ($rowt = mysql_fetch_object($rest)) {
          $attachments[] = $rowt;
        }
        $row->attachments = $attachments;

        //likes
        $ratings["likes"] = $ratings["dislikes"] = array();
        if ($this->board_config->en_likes || $this->board_config->en_dislikes) {
          $sqlR = "select * from `" . $this->board_config->prefix . "ratings` where tid = '" . $this->tid . "' and pid = '" . $row->a_id . "' order by id desc";
          $resR = dbquery($sqlR);
          while ($ratrow = mysql_fetch_object($resR)) {
            $ratings[$ratrow->rating == 1 ? "likes" : "dislikes"][] = $ratrow;
          }
        }
        $row->ratings = $ratings;

        $this->posts["replies"][] = $row;
      }
      $this->count = mysql_num_rows($res);
    }
  }
  
  function GetSingleTopic() {
    $sql = "select * from `" . $this->board_config->prefix . "question` where id = '" . $this->tid . "';";
    $res = dbquery($sql);
    if (mysql_num_rows($res)) {
      $row = mysql_fetch_object($res);
      //get poll_options
      $sqlt = "select * from `" . $this->board_config->prefix . "poll_options` where tid = '" . $this->tid . "' order by id;";
      $rest = dbquery($sqlt);
      while ($rowt = mysql_fetch_object($rest)) {
        $poll_options .= $rowt->poll_option . "\n";
      }
      $row->poll_options = $poll_options;
      //get attachments
      $sqlt = "select * from `" . $this->board_config->prefix . "attachments` where tid = '" . $this->tid . "' and pid = '0' order by id;";
      $rest = dbquery($sqlt);
      while ($rowt = mysql_fetch_object($rest)) {
        $attachments[] = $rowt;
      }
      $row->attachments = $attachments;
      return $row;
    } else return false;
  }
  
  function GetSinglePost() {
    $sql = "select * from `" . $this->board_config->prefix . "answer` where a_id = '" . $this->pid . "' and question_id = '" . $this->tid . "';";
    $res = dbquery($sql);
    if (mysql_num_rows($res)) {
      $row = mysql_fetch_object($res);
      //get attachments
      $sqlt = "select * from `" . $this->board_config->prefix . "attachments` where tid = '" . $this->tid . "' and pid = '" . $this->pid . "' order by id;";
      $rest = dbquery($sqlt);
      while ($rowt = mysql_fetch_object($rest)) {
        $attachments[] = $rowt;
      }
      $row->attachments = $attachments;
      return $row;
    } else return false;
  }
  
  //update views
  function updateViews() {
    $sql = "update `" . $this->board_config->prefix . "question` set view = (view + 1) where id = '" . $this->tid . "'";
    dbquery($sql);
  }
  
}

?>