<?php
class Ranks {

  private $db;
  private $status;

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

  //---[CZ]: přidání
  public function add() {
    $sql=$this->db->query("INSERT INTO kboard_ranks (name, start, end) values ('".$this->db->escape($_POST["name"])."', ".intval($_POST["start"]).", ".intval($_POST["end"]).")");
    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 remove() {
    $sql=$this->db->query("DELETE FROM kboard_ranks 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]: vypsání
  public function print_data() {
    $sql=$this->db->query("SELECT id, name, start, end FROM kboard_ranks ORDER by id");
    while (list($id, $name, $start, $end)=$this->db->fetch_row($sql)) {
    echo "<tr>
    <td>".$this->db->unescape($name)."</td>
    <td class=\"align-center\">$start &mdash; $end</td>
    <td class=\"align-center\"><input type=\"button\" value=\"".LANG_EDIT."\" onclick=\"parent.location='./admin.php?f=ranks&amp;f2=edit_rank&amp;id=$id';\" /> <input type=\"button\" value=\"".LANG_DELETE."\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false; parent.location='./admin.php?f=ranks&amp;action=delete&amp;id=$id';\" /></td>
    </tr>";
    }
  }
  
  //---[CZ]: editace
  public function update() {
    $sql=$this->db->query("UPDATE kboard_ranks SET name='".$this->db->escape($_POST["name"])."', start=".intval($_POST["start"]).", end=".intval($_POST["end"])." 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 hodnosti
  public function rank_details($column) {
    $sql=$this->db->query("SELECT name, start, end FROM kboard_ranks WHERE id=".intval($_GET["id"]));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
  }

}
?>