<?php
class Print_posts {

  private $db;
  private $paging;
  private $login;
  private $index;
  private $formating;

  public function __construct() {
    $this->db = new Db_layer();
    $this->paging = new Paging();
    $this->login = new Login();
    $this->index = new Index();
    $this->formating = new Formating();
  }
  
  public function print_data($paging_number, $date_format, $locked_forums_writing_rights, $locked_topics_writing_rights, $items_replace_rights, $post_delete_rights) {
  
    //---[CZ]: zjistíme stránku, případně přiřadíme 0
    if (!isset($_GET["page"])) {
    $page=1;
    $skip=0;
    }
    else {
    $page=$_GET["page"];
    $skip=$page*$paging_number-$paging_number;
    }

    //---[CZ]: spočítáme příspěvky v tomto topicu
    $select=$this->db->query("SELECT COUNT(*) AS c FROM kboard_posts WHERE topic=".intval($_GET["topic"]));
    $select=$this->db->fetch_array($select);

    //---[CZ]: spočítáme počet stran
    $pages=ceil($select["c"]/$paging_number);
    
    //---[CZ]: případně vypíšeme stránkování
    if ($pages>0) { $this->paging->index_paging($pages, $page); }
    
    //---[CZ]: vypíšeme příspěvky patřící do tohoto topicu
    $i=0;
    $print=$this->db->query("SELECT id, author, response, text, timestamp, ip FROM kboard_posts WHERE topic=".intval($_GET["topic"])." ORDER by timestamp LIMIT $skip, $paging_number");
    while (list($id, $author, $response, $text, $timestamp, $ip)=$this->db->fetch_row($print)) {
    
    //---[CZ]: pokud není autor anonym
    if ($author!=0) {
    
    // tabulka K:CMS nemá siqn...
    if (TABLE_USERS=="kcms_users") { $sign_if=""; }
    if (TABLE_USERS=="kboard_users") { $sign_if=", sign"; }
    $author=$this->db->query("SELECT id, login, avatar, rights$sign_if FROM ".TABLE_USERS." WHERE id=$author");
    $author=$this->db->fetch_array($author);
    $author_login=$author["login"];
    
    //---[CZ]: pokud je anonym
    } else { $author_login="Anonym"; }

    //---[CZ]: pokud se jedná o reakci, zjistíme text originálu
    if ($response!=0) {
    $original=$this->db->query("SELECT text FROM kboard_posts WHERE id=$response");
    $original=$this->db->fetch_array($original);
    }

    //---[CZ]: spočítáme posty v tomto topicu
    $count_posts=$this->db->query("SELECT COUNT(*) AS c FROM kboard_posts WHERE author=".$author["id"]);
    $count_posts=$this->db->fetch_array($count_posts);

    $i++;

    echo "\n\n<script type=\"text/javascript\">
    /* <![CDATA[ */
    document.write('<a name=\"$id\"><!-- --></a>');
    /* ]]> */
    </script>

    <div class=\"post-$i\">

    <div class=\"topic-left-column\">
    <p><strong>$author_login</strong></p>";

    if ($author!=0) {

    if ($author["avatar"]!="") {
    echo "<p><img src=\"./avatars/".$author["id"].".".$author["avatar"]."\" alt=\"".$author["login"]."\" /></p>";
    }

    echo "<p class=\"topic-desc\">".LANG_NUMBER_OF_POSTS.": ".$count_posts["c"]."<br />".LANG_RANK.": ";

    $rank=$this->db->query("SELECT name FROM kboard_ranks WHERE start<=".$count_posts["c"]." AND end>=".$count_posts["c"]);
    $rank=$this->db->fetch_array($rank);

    if ($author["rights"]==0) { if ($rank["name"]=="") { echo LANG_USER; } else { echo($rank["name"]); } } else { echo "admin "; $i2=1; while ($i2<=$author["rights"]) { $i2++; echo "&bull;"; } }

    echo "</p>";

    }

    echo "<p class=\"topic-desc\">";

    if ($author!=0) {
    echo "<a href=\"./index.php?profile=".$author["id"]."\">".LANG_USER_PROFILE."</a>";
    }

    if ($_GET["action"]!="response") {

    if ($this->index->topic_forum_details("locked")==0 OR $this->login->check_rights()>=$locked_forums_writing_rights) {
    if ($this->index->topic_details("locked")==0 OR $this->login->check_rights()>=$locked_topics_writing_rights) {
    
    if ($this->index->topic_forum_details("write_ntbl")==0 OR isset($_SESSION["user_id"])) {

    echo "<br /><a href=\"./index.php?action=response&amp;p_id=$id&amp;t_id=".$_GET["topic"]."&amp;f_id=".$this->index->topic_forum_details("id")."\">".LANG_RESPONSE."</a>";
    
    }

    }
    }

    echo "</p>";

    if ($this->login->check_rights()>=$post_delete_rights) { echo "\n\n<p class=\"topic-desc\">IP: $ip<br /><a href=\"./index.php?action=delete_post&amp;p_id=$id&amp;t_id=".$_GET["topic"]."\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false;\">".LANG_DELETE."</a></p>"; }

    } else { echo "</p>"; }

    echo "</div>

    <div class=\"topic-right-column\">
    <p class=\"topic-desc\">Odesláno: ".date($date_format, $timestamp)."";

    if (isset($_SESSION["user_id"]) AND $_GET["action"]!="response") { if ($_SESSION["user_id"]==$author["id"] AND $this->index->topic_details("locked")==0 AND $this->index->topic_forum_details("locked")==0 OR $this->login->check_rights()>0) { echo " [<a href=\"./index.php?action=edit_post&amp;p_id=$id\">".strtolower(LANG_EDIT)."</a>]"; } }

    if ($this->login->check_rights()>=$items_replace_rights AND $_GET["action"]!="response") { echo " [<a href=\"./index.php?action=replace_post&amp;p_id=$id&amp;t_id=".$_GET["topic"]."\">".LANG_REPLACE."</a>]"; }

    echo "</p>";

    $response_topic=$this->db->query("SELECT topic FROM kboard_posts WHERE id=$response");
    $response_topic=$this->db->fetch_array($response_topic);

    if ($_GET["action"]!="response" AND $response!=0) { echo "<div class=\"response\">\n<p><strong>".LANG_THIS_IS_A_RESPONSE_TO_FOLLOWING." <a href=\"";

    if ($response_topic["topic"]!=$_GET["topic"]) { echo "./index.php?topic=".$response_topic["topic"]."&amp;page=$page"; }

    echo "#$response\">".LANG_POST."</a>:</strong></p>\n\n<p class=\"topic-desc\">".$this->db->unescape($this->formating->apply_on($original["text"]))."</p>\n</div>"; }

    echo "<p>".$this->db->unescape($this->formating->apply_on($text))."</p>\n";

    if ($author["sign"]!="" AND $author!=0) { echo "\n<p class=\"topic-desc\">-----<br />".$this->db->unescape($author["sign"])."</p>\n"; }

    echo "</div>

    <div class=\"float-ending\"><!-- --></div>

    </div>

    <div class=\"post-bottom-corners-$i\"><!-- --></div>";

    if ($i==2) { $i=0; }
    }

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

}
?>