<?php
class Print_topics {

  private $db;
  private $paging;
  private $login;

  public function __construct() {
    $this->db = new Db_layer();
    $this->paging = new Paging();
    $this->login = new Login();
  }
  
  public function print_data($paging_number, $date_format, $topic_lock_and_unlock_rights, $topic_delete_rights, $items_replace_rights) {
  
    //---[CZ]: aktuální strana, kolik vynechat
    if (!isset($_GET["page"])) {
    $page=1;
    $skip=0;
    }
    else {
    $page=$_GET["page"];
    $skip=$page*$paging_number-$paging_number;
    }
    
    //---[CZ]: pokud vyhledáváme
    if (isset($_GET["w"])) {
    $select=$this->db->query("SELECT kboard_topics.*, kboard_posts.* FROM kboard_topics join kboard_posts on kboard_topics.id = kboard_posts.id where kboard_topics.name LIKE '%".$this->db->escape($_GET["w"])."%' OR kboard_posts.text LIKE '%".$this->db->escape(rawurldecode($_GET["w"]))."%' group by kboard_topics.id");
    }
    
    //---[CZ]: pokud ne
    if (isset($_GET["forum"])) {
    $select=$this->db->query("SELECT id FROM kboard_topics WHERE forum=".intval($_GET["forum"]));
    }

    //---[CZ]: spočítáme počet stran
    $pages=ceil($this->db->num_rows($select)/$paging_number);
    
    echo $select["c"];
    
    //---[CZ]: případně vypíšeme stránkování
    if ($pages>0) { $this->paging->index_paging($pages, $page); }

    echo "<div class=\"ul-border\">\n\n<ul class=\"forum\">";
    
    //---[CZ]: pokud vyhledáváme
    if (isset($_GET["w"])) {
    $print=$this->db->query("SELECT kboard_topics.id, kboard_topics.name, kboard_topics.author, kboard_topics.locked, kboard_topics.timestamp, kboard_topics.views, kboard_topics.forum, kboard_topics.lastpost_id, kboard_topics.lastpost_timestamp, kboard_topics.status, kboard_posts.topic, kboard_posts.text FROM kboard_topics join kboard_posts on kboard_topics.id = kboard_posts.topic where kboard_topics.name LIKE '%".$this->db->escape(rawurldecode($_GET["w"]))."%' or kboard_posts.text LIKE '%".$this->db->escape(rawurldecode($_GET["w"]))."%' group by kboard_topics.id ORDER by kboard_topics.status DESC, kboard_topics.lastpost_timestamp DESC LIMIT $skip, $paging_number");
    }
    
    //---[CZ]: pokud ne
    if (isset($_GET["forum"])) {
    $print=$this->db->query("SELECT id, name, author, locked, timestamp, views, forum, lastpost_id, lastpost_timestamp, status FROM kboard_topics WHERE forum=".intval($_GET["forum"])." ORDER by status DESC, lastpost_timestamp DESC LIMIT $skip, $paging_number");
    }
    
    //---[CZ]: vypíšeme témata
    $i=0;
    while (list($id, $name, $author, $locked, $timestamp, $views, $forum, $lastpost_id, $lastpost_timestamp, $status)=$this->db->fetch_row($print)) {
    $i++;
    
    //---[CZ]: spočítáme příspěvky v tomto tématu
    $count_posts=$this->db->query("SELECT COUNT(*) AS c FROM kboard_posts WHERE topic=$id");
    $count_posts=$this->db->fetch_array($count_posts);

    //---[CZ]: pokud topic není prázdný, zjistíme údaje o poslední příspěvku a jeho autoru
    if ($lastpost_id!=0) {

    $last_post=$this->db->query("SELECT author, topic FROM kboard_posts WHERE id=$lastpost_id");
    $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);
    }

    }

    //---[CZ]: údaje o autorovi topicu
    $author=$this->db->query("SELECT id, login FROM ".TABLE_USERS." WHERE id=$author");
    $author=$this->db->fetch_array($author);

    echo "\n\n<li";

    if ($locked==1) { echo " class=\"locked-topic\""; }

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

    //---[CZ]: ikonky stavu
    if ($status==1) { echo "important.gif"; } else {
    if ($locked==0) { echo "normal.gif"; }
    if ($locked==1) { echo "locked.gif"; }
    }

    echo "\" alt=\"Status\" /></span>\n\n<span class=\"forum-title\"><a href=\"./index.php?topic=$id\"><strong>".$this->db->unescape($name)."</strong></a><span class=\"forum-desc\">".LANG_CREATED_BY." ";

    //---[CZ]: vypíšeme jméno autora
    if ($author["id"]!=0) { echo "<a href=\"./index.php?profile=".$author["id"]."\">".$author["login"]."</a>"; } else { echo "Anonym"; }

    //---[CZ]: vypíšeme datum
    echo ", ".date($date_format, $timestamp)."";
    
    //---[CZ]: vypíšeme jednotlivé sloupce
    echo "<span class=\"options\">[<a href=\"#\" onclick=\"getElementById('t-$id').style.display='block';\">".LANG_SHOW_OPTIONS."</a>]</span></span></span>
    
    <span class=\"forum-numbers\"><span class=\"forum-desc\">".LANG_NUMBER_OF_VIEWS.":</span>$views</span>
    
    <span class=\"forum-numbers\"><span class=\"forum-desc\">".LANG_NUMBER_OF_POSTS.":</span>".$count_posts["c"]."</span>
    
    <span class=\"forum-last-post\"><span class=\"forum-desc\">".LANG_LAST_POST.":</span>";

    //---[CZ]: autor a datum posledního příspěvku
    if ($lastpost_id==0) { echo "-"; } else {

    if ($last_post["author"]!=0) { echo "<a href=\"./index.php?profile=".$last_post["author"]."\">".$last_author["login"]."</a>"; } else { echo "Anonym"; }

    echo ", ".date($date_format, $lastpost_timestamp).""; }
    echo "</span>\n\n<span class=\"forum-icon2\">";
    
    //---[CZ]: pokud topic není prázdný, vypíšeme odkaz (šipku) na poslední příspěvek
    if ($lastpost_id!=0) {
    
    //---[CZ]: zjistíme počet stran v tomto topicu
    $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#$lastpost_id\"><img src=\"./components/images/icons/goto.gif\" alt=\"->\" /></a>";
    
    //---[CZ]: pokud je prázdný, vypíšeme vysvícenou šipku
    } else { echo "<img src=\"./components/images/icons/goto_empty.gif\" alt=\"->\" />"; }

    echo "</span>\n\n<span class=\"float-ending\"><!-- --></span>";

    echo "\n\n<span class=\"popup-topic\" id=\"t-$id\">";

    //---[CZ]: přepínač zamknuto/odemknuto
    if ($locked==0) { if ($this->login->check_rights()>=$topic_lock_and_unlock_rights) { echo "[<a href=\"./index.php?action=topic_lock&amp;status=1&amp;t_id=$id\">".LANG_UNLOCKED."</a>]"; } else { echo "[".LANG_UNLOCKED."]"; } } else { if ($this->login->check_rights()>=$topic_lock_and_unlock_rights) { echo "[<a href=\"./index.php?action=topic_lock&amp;status=0&amp;t_id=$id\">".LANG_LOCKED."</a>]"; } else { echo "[".LANG_LOCKED."]"; } }
    
    echo " ";
    
    //---[CZ]: přepínač statusu
    if ($status==0) { if ($this->login->check_rights()>=$topic_lock_and_unlock_rights) { echo "[<a href=\"./index.php?action=topic_status&amp;status=1&amp;t_id=$id\">".LANG_NORMAL."</a>]"; } else { echo "[".LANG_NORMAL."]"; } } else { if ($this->login->check_rights()>=$topic_lock_and_unlock_rights) { echo "[<a href=\"./index.php?action=topic_status&amp;status=0&amp;t_id=$id\">".LANG_IMPORTANT."</a>]"; } else { echo "[".LANG_IMPORTANT."]"; } }
    
    if (isset($_SESSION["user_id"])) {
    
    //---[CZ]: tlačítka pro upravení a smazání topicu
    if ($this->login->check_rights()>=$topic_delete_rights OR $_SESSION["user_id"]==$author["id"]) {
    
    echo " [<a href=\"./index.php?action=edit_topic&amp;t_id=$id\">".strtolower(LANG_EDIT)."</a>] [<a href=\"./index.php?action=delete_topic&amp;t_id=$id&amp;f_id=$forum\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false;\">".strtolower(LANG_DELETE)."</a>]";
    
    }
    
    //---[CZ]: tlačítko pro přesunutí topicu
    if ($this->login->check_rights()>=$items_replace_rights) { echo " [<a href=\"./index.php?action=replace_topic&amp;t_id=$id&amp;f_id=$forum\">".LANG_REPLACE."</a>]"; }
    
    }
    
    //---[CZ]: tlačítko pro zavření okna
    echo " [<a href=\"#\" onclick=\"getElementById('t-$id').style.display='none';\">".LANG_CLOSE."</a>]</span>\n</li>";
    
    }
    
    //---[CZ]: pokud je fórum prázdné/vyhledávání nic nenašlo
    if ($i==0) { echo "<li class=\"align-center\">";
    if ($_GET["action"]=="search") { echo LANG_NO_RESULTS; }
    else { echo LANG_THIS_FORUM_IS_EMPTY; }
    echo "</li>"; }
    
    echo "</ul>\n\n</div>\n\n<div class=\"ul-bottom-corners\"><!-- --></div>";

    //---[CZ]: případně vypíšeme stránkování
    if ($pages>0) { $this->paging->index_paging($pages, $page); }
  }

}
?>