<?php
class Forums {

  private $db;
  private $status;

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

  //---[CZ]: přidání
  public function add() {
    $position=$this->db->query("SELECT position FROM kboard_forums ORDER by position DESC");
    $position=$this->db->fetch_array($position);
    $position=$position["position"]+1;
    $sql=$this->db->query("INSERT INTO kboard_forums (name, category, position) values ('".$this->db->escape($_POST["name"])."', ".intval($_POST["category"]).", $position)");
    if ($sql) { $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED); } else { $this->status->print_error(LANG_AN_ERROR_OCCURED); }
  }

  //---[CZ]: výběr kategorie
  public function select_category() {
    $sql=$this->db->query("SELECT id, name FROM kboard_categories ORDER by position");
    while (list($id, $name)=$this->db->fetch_row($sql)) {
    echo "<option value=\"$id\">".$this->db->unescape($name)."</option>\n";
    }
  }
  
  //---[CZ]: vypsání práv
  public function print_rights($type) {
    for ($i=0; $i<4; $i++) {
    echo "<option value=\"$i\"";
    if ($this->forum_details("$type"."_rights")==$i) { echo " selected=\"selected\""; }
    echo ">$i</option>\n";
    }
  }

  //---[CZ]: vypsání fór  
  public function print_forums() {
    $sql=$this->db->query("SELECT id, name FROM kboard_categories ORDER by position");
    while (list($category_id, $name)=$this->db->fetch_row($sql)) {
    echo "<table>
    <tr>
    <th class=\"table-title\" colspan=\"5\">".$this->db->unescape($name)."</th>
    </tr>
    <tr>
    <th style=\"width: 65%;\">".LANG_NAME."</th>
    <th style=\"width: 10%;\">".LANG_POSITION."</th>
    <th style=\"width: 25%;\">".LANG_OPTIONS."</th>
    </tr>";
    $i=0;
    $sql2=$this->db->query("SELECT id, name, position FROM kboard_forums WHERE category=$category_id ORDER by position");
    while (list($id, $name, $position)=$this->db->fetch_row($sql2)) {
    $i++;
    echo "<tr>
    <td>".$this->db->unescape($name)."</td>
    <td class=\"align-center\">";
    if ($i!=1) { echo "<a href=\"./admin.php?f=forums&amp;action=move_up&amp;category=$category_id&amp;position=$position\" style=\"text-decoration: none;\">&uarr;</a> "; }
    if ($i<$this->db->num_rows($sql2)) { echo " <a href=\"./admin.php?f=forums&amp;action=move_down&amp;category=$category_id&amp;position=$position\" style=\"text-decoration: none;\">&darr;</a>"; }
    echo "</td>
  <td class=\"align-center\"><input type=\"button\" value=\"".LANG_EDIT."\" onclick=\"parent.location='./admin.php?f=forums&amp;f2=edit_forum&amp;id=$id';\" /> <input type=\"button\" value=\"".LANG_DELETE."\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false; parent.location='./admin.php?f=forums&amp;action=delete&amp;id=$id';\" /></td>
    </tr>";
    }
    echo "</table>";
    }
  }

  //---[CZ]: posunutí nahoru
  public function move_up() {
    $sql=$this->db->query("UPDATE kboard_forums SET position=".(2*intval($_GET["position"])-1)." - position WHERE position IN ('".intval($_GET["position"])."', ".(intval($_GET["position"])-1).")");
    if ($sql) { $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED); } else { $this->status->print_error(LANG_AN_ERROR_OCCURED); }
  }
  
  //---[CZ]: posunutí dolů
  public function move_down() {
    $sql=$this->db->query("UPDATE kboard_forums SET position=".(2*intval($_GET["position"])+1)." - position WHERE position IN ('".intval($_GET["position"])."', ".(intval($_GET["position"])+1).")");
    if ($sql) { $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED); } else { $this->status->print_error(LANG_AN_ERROR_OCCURED); }
  }
  
  //---[CZ]: smazání
  public function delete() {
    $sql=$this->db->query("DELETE FROM kboard_forums WHERE id=".intval($_GET["id"]));
    $sql2=$this->db->query("DELETE FROM kboard_topics WHERE forum=".intval($_GET["id"]));
    $sql3=$this->db->query("DELETE FROM kboard_posts WHERE forum=".intval($_GET["id"]));
    if ($sql AND $sql2 AND $sql3) { $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED); } else { $this->status->print_error(LANG_AN_ERROR_OCCURED); }
  }

  //---[CZ]: editace
  public function update() {
    $sql=$this->db->query("UPDATE kboard_forums SET name='".$this->db->escape($_POST["name"])."', description='".$this->db->escape($_POST["description"])."', read_ntbl=".intval($_POST["read_ntbl"]).", write_ntbl=".intval($_POST["write_ntbl"]).", locked=".intval($_POST["locked"]).", read_rights=".intval($_POST["read_rights"]).", write_rights=".intval($_POST["write_rights"])." WHERE id=".intval($_GET["id"]));
    if ($sql) { $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED); } else { $this->status->print_error(LANG_AN_ERROR_OCCURED); }
  }
  
  //---[CZ]: detaily fóra
  public function forum_details($column) {
    $sql=$this->db->query("SELECT $column FROM kboard_forums WHERE id=".intval($_GET["id"]));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
  }

  //---[CZ]: zamknutí/odemknutí
  public function change_topic_status() {
    $sql=$this->db->query("UPDATE kboard_topics SET locked=".intval($_GET["status"])." WHERE id=".intval($_GET["t_id"]));
    header("Location: ".$_SERVER["HTTP_REFERER"]."&mt=1");
  }

}
?>