<?php
class Print_forums {

  private $db;

  public function __construct() {
    $this->db = new Db_layer();
  }
  
  //---[CZ]: vypsání dat
  public function print_data($paging_number, $date_format) {
    $i=0;
    
    //---[CZ]: vypsání kategorií
    $print_c=$this->db->query("SELECT id, name FROM kboard_categories ORDER by position");
    while (list($id, $name)=$this->db->fetch_row($print_c)) {
    echo "<div class=\"ul-border\">\n\n<h2>$name</h2>\n\n<ul class=\"forum\">";
    
    //---[CZ]: vypsání fór
    $print_f=$this->db->query("SELECT id, name, description, locked FROM kboard_forums WHERE category=$id ORDER by position");
    while (list($id_f, $name_f, $description_f, $locked_f)=$this->db->fetch_row($print_f)) {
    $i++;

    $count_topics=$this->db->query("SELECT COUNT(*) AS p FROM kboard_topics WHERE forum=$id_f");
    $count_topics=$this->db->fetch_array($count_topics);

    $count_posts=$this->db->query("SELECT COUNT(*) AS p FROM kboard_posts WHERE forum=$id_f");
    $count_posts=$this->db->fetch_array($count_posts);

    $last_post=$this->db->query("SELECT id, author, timestamp, topic FROM kboard_posts WHERE forum=$id_f ORDER by timestamp DESC");
    $last_post=$this->db->fetch_array($last_post);

    if ($last_post["author"]!=0) {
    $last_author=$this->db->query("SELECT login FROM ".TABLE_USERS." WHERE id=".$last_post["author"]);
    $last_author=$this->db->fetch_array($last_author);
    $author_login=$last_author["login"];
    } else { $author_login="Anonym"; }

    echo "\n\n<li>
    <span class=\"forum-icon\"><img src=\"./components/images/icons/";

    if ($locked_f==0) { echo "normal.gif"; }
    if ($locked_f==1) { echo "locked.gif"; }

    echo "\" alt=\"Status\" /></span>
    <span class=\"forum-title\"><a href=\"./index.php?forum=$id_f\"><strong>$name_f</strong></a><span class=\"forum-desc\">$description_f</span></span>
    <span class=\"forum-numbers\"><span class=\"forum-desc\">".LANG_NUMBER_OF_TOPICS.":</span>".$count_topics["p"]."</span>
    <span class=\"forum-numbers\"><span class=\"forum-desc\">".LANG_NUMBER_OF_POSTS.":</span>".$count_posts["p"]."</span>
    <span class=\"forum-last-post\"><span class=\"forum-desc\">".LANG_LAST_POST.":</span>";
    
    if ($last_post["author"]=="") { echo "-"; } else { if ($last_post["author"]!=0) { echo "<a href=\"./index.php?profile=".$last_post["author"]."\">$author_login</a>"; } else { echo "$author_login"; } echo ", ".date($date_format, $last_post["timestamp"]).""; }
    
    echo "</span>\n<span class=\"forum-icon2\">";

    if ($last_post["author"]!="") {
    $current_page=$this->db->query("SELECT COUNT(*) AS posts FROM kboard_posts WHERE topic=".$last_post["topic"]);
    $current_page=$this->db->fetch_array($current_page);

    $current_page=ceil($current_page["posts"]/$paging_number);

    echo "<a href=\"./index.php?topic=".$last_post["topic"]."&amp;page=$current_page#".$last_post["id"]."\"><img src=\"./components/images/icons/goto.gif\" alt=\"->\" /></a>";
    } else {
    echo "<img src=\"./components/images/icons/goto_empty.gif\" alt=\"->\" />";
    }

    echo "</span>
    <span class=\"float-ending\"><!-- --></span>
    </li>";
    }
    
    //---[CZ]: pokud je kategorie prázdná, vypíšeme zprávu
    if ($i==0) { echo "<li class=\"align-center\">".LANG_THIS_CATEGORY_IS_EMPTY."</li>"; } else { $i=0; }

    echo "</ul>\n\n</div>\n\n<div class=\"ul-bottom-corners\"><!-- --></div>";
    }
  }

}
?>