<?php

class SBBForums {

  var $count = 0;
  var $error;
  var $success;
  var $cat_count = array();
     
  function SBBForums() {
    global $SBBAuth, $board_config;
    $this->auth = $SBBAuth;
    $this->board_config = $board_config;
  }
  
  //forum informations
  function GetForums() {
    //get categories order
    $cat_ids[] = 0;
    $res = dbquery("select * from `" . $this->board_config->prefix . "categories` order by pos;");
    while ($row = mysql_fetch_object($res)) {
      $cat_ids[] = $row->id;
    }
    //permission addon
    if (!$this->auth->auth) $minimum_perm = 0;
    else $minimum_perm = 1;
    if ($this->auth->is_mod) $minimum_perm = 2;
    if ($this->auth->is_admin) $minimum_perm = 3;
    
    $sql = "select * from `" . $this->board_config->prefix . "forums` where viewing_permission <= $minimum_perm order by find_in_set(cat_id,'" . implode(",",$cat_ids) . "'), pos;";
    $res = dbquery($sql);
    while ($row = mysql_fetch_object($res)) {
      $forum[] = $row;
      $this->cat_count[$row->cat_id]++;
    }
    $this->count = mysql_num_rows($res);
    return $forum;
  }
  
  //just a forum list (old style adapted)
  function GetForumList() {
    $forums = $forum = array();
    //permission addon
    if (!$this->auth->auth) $minimum_perm = 0;
    else $minimum_perm = 1;
    if ($this->auth->is_mod) $minimum_perm = 2;
    if ($this->auth->is_admin) $minimum_perm = 3;
    
    $sql = "select * from `" . $this->board_config->prefix . "forums` where viewing_permission <= $minimum_perm order by pos;";
    $res = dbquery($sql);
    while ($row = mysql_fetch_object($res)) {
      $row->allowed2post = $row->allowed2reply = false;
      //posting and reply permissions
      if ($row->posting_permission <= $minimum_perm) $row->allowed2post = true;
      if ($row->reply_permission <= $minimum_perm) $row->allowed2reply = true;
      $forums[] = $forum[$row->id] = $row;
    }
    return array($forum,$forums);
  }
  //just a category list (old style adapted)
  function GetCatList() {
    $categories = $category = array();
    $sql = "select * from `" . $this->board_config->prefix . "categories` order by pos;";
    $res = dbquery($sql);
    while ($row = mysql_fetch_object($res)) {
      $categories[] = $category[$row->id] = $row;
    }
    return array($category,$categories);
  }
  
  //category informations
  function GetCategories() {
    $sql = "select * from `" . $this->board_config->prefix . "categories` order by pos;";
    $res = dbquery($sql);
    while ($row = mysql_fetch_object($res)) {
      $categories[] = $row;
    }
    $this->count = mysql_num_rows($res);
    return $categories;
  }
  
  //get forumid from topic
  function GetFid($tid) {
    $sql = "select fid from `" . $this->board_config->prefix . "question` where id = '$tid'";
    $res = dbquery($sql);
    $row = mysql_fetch_object($res);
    return intval($row->fid);
  }  
  
  //some statistics
  function GetStats() {
    //topics
    $res = dbquery("select fid, count(id) as cnt from `" . $this->board_config->prefix . "question` group by fid;");
    while ($row = mysql_fetch_object($res)) {
      $topics[$row->fid] = $row->cnt;
      $cntposts += $row->cnt;
      $cnttopics += $row->cnt;
    }
    //posts
    $res = dbquery("select count(`" . $this->board_config->prefix . "answer`.a_id) as cnt, `" . $this->board_config->prefix . "question`.fid as fid from `" . $this->board_config->prefix . "answer` inner join `" . $this->board_config->prefix . "question` on (`" . $this->board_config->prefix . "question`.id = `" . $this->board_config->prefix . "answer`.question_id) group by `" . $this->board_config->prefix . "question`.fid;");
    while ($row = mysql_fetch_object($res)) {
      $posts[$row->fid] = $row->cnt;
      $cntposts += $row->cnt;
    }
    //lastpost
    //performancefix for
    //$res = dbquery("select * from `" . $this->board_config->prefix . "question` where a_datetime in (select max(a_datetime) from `" . $this->board_config->prefix . "question` group by fid);"); 
    $a_times = array();
    $sql = "select max(a_datetime) max_atime from `" . $this->board_config->prefix . "question` group by fid";
    $res = dbquery($sql);
    while($row = mysql_fetch_object($res)) {
      $a_times[] = $row->max_atime;
    }
    if ($a_times) {
      $res = dbquery("select * from `" . $this->board_config->prefix . "question` where a_datetime in ('" . implode("','",$a_times) . "')"); 
      while ($row = mysql_fetch_object($res)) {
        $last[$row->fid] = $row;
      }
    }
    //latest member
    $res = dbquery("select * from `" . $this->board_config->prefix . "users` where status = '1' order by id desc limit 1;"); 
    $latest_member = mysql_fetch_object($res);
    //count active members
    $res = dbquery("select id from `" . $this->board_config->prefix . "users` where status = '1';"); 
    $cntmembers = mysql_num_rows($res);
    //whoisonline
    $res = dbquery("select uid, count(id) as counted from `" . $this->board_config->prefix . "whoisonline` group by uid;");
    while ($row = mysql_fetch_object($res)) {
      if ($row->uid == 0) $onlineguests = $row->counted;
      else $whoisonline_ids[] = $row->uid;
    }
    //get whoisonline usernames
    if ($whoisonline_ids) {
      $res = dbquery("select id, user from `" . $this->board_config->prefix . "users` where id in (". implode(",",$whoisonline_ids) . ");");
      while ($row = mysql_fetch_object($res)) {
        $whoisonline[] = $row->user;
      }
    }
    return array("topics" => $topics,
                 "posts" => $posts,
                 "last" => $last,
                 "latest_member" => $latest_member,
                 "cntposts" => intval($cntposts),
                 "cntmembers" => intval($cntmembers),
                 "cnttopics" => intval($cnttopics),
                 "whoisonline" => $whoisonline,
                 "onlineguests" => $onlineguests
                 );
  }
  
}

?>