<?php
class Replace_items {

  private $db;

  public function __construct() {
    $this->db = new Db_layer();
  }

  //---[CZ]: vypsání témat
  public function print_topics() {
    $print=$this->db->query("SELECT id, name FROM kboard_topics ORDER by lastpost_timestamp DESC");
    while (list($id, $name)=$this->db->fetch_row($print)) {
    echo "<option value=\"$id\">$name</option>";
    }
  }
  
  //---[CZ]: vypsání fór
  public function print_forums() {
    $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 "<optgroup label=\"$name\">";
    $print_f=$this->db->query("SELECT id, name FROM kboard_forums WHERE category=$id ORDER by position");
    while (list($id_f, $name_f)=$this->db->fetch_row($print_f)) {
    echo "<option value=\"$id_f\">$name_f</option>";
    }
    echo "</optgroup>";
    }
  }
  
  //---[CZ]: přesunutí topicu
  public function move_topic() {
    $sql=$this->db->query("UPDATE kboard_topics SET forum=".intval($_POST["new_forum"])." WHERE id=".intval($_GET["t_id"]));
    $sql=$this->db->query("UPDATE kboard_posts SET forum=".intval($_POST["new_forum"])." WHERE topic=".intval($_GET["t_id"]));
    header("Location: ./index.php?forum=".$_GET["f_id"]."&mt=1");
  }
  
  //---[CZ]: přesunutí příspěvku
  public function move_post() {
    $sql=$this->db->query("UPDATE kboard_posts SET topic=".intval($_POST["new_topic"])." WHERE id=".intval($_GET["p_id"]));
    
    $last_id=$this->db->query("SELECT id, timestamp FROM kboard_posts WHERE topic=".$_GET["t_id"]." ORDER BY id DESC");
    $last_id=$this->db->fetch_array($last_id);

    if ($last_id["id"]=="") {
    $sql=$this->db->query("UPDATE kboard_topics SET lastpost_id=0, lastpost_timestamp=0 WHERE id=".$_GET["t_id"]);
    } else { $sql=$this->db->query("UPDATE kboard_topics SET lastpost_id=".$last_id["id"].", lastpost_timestamp=".$last_id["timestamp"]." WHERE id=".$_GET["t_id"]); }

    $last_id=$this->db->query("SELECT id, timestamp FROM kboard_posts WHERE topic=".$_POST["new_topic"]." ORDER BY id DESC");
    $last_id=$this->db->fetch_array($last_id);

    if ($last_id["id"]=="") {
    $sql=$this->db->query("UPDATE kboard_topics SET lastpost_id=0, lastpost_timestamp=0 WHERE id=".$_POST["new_topic"]);
    } else { $sql=$this->db->query("UPDATE kboard_topics SET lastpost_id=".$last_id["id"].", lastpost_timestamp=".$last_id["timestamp"]." WHERE id=".$_POST["new_topic"]); };
    
    header("Location: ./index.php?topic=".$_GET["t_id"]."&mt=1");
  }

}
?>