<?php
class Categories {

  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_categories ORDER by position DESC");
    $position=$this->db->fetch_array($position);
    $position=$position["position"]+1;
    $sql=$this->db->query("INSERT INTO kboard_categories (name, position) values ('".$this->db->escape($_POST["name"])."', $position)");
    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_categories() {
    $i=0;
    $sql=$this->db->query("SELECT id, name, position FROM kboard_categories ORDER by position");
    while (list($id, $name, $position)=$this->db->fetch_row($sql)) {
    $i++;
    echo "<tr>
    <td>".$this->db->unescape($name)."</td>
    <td class=\"align-center\">";
    if ($i!=1) { echo "<a href=\"./admin.php?f=categories&amp;action=move_up&amp;position=".($position)."\" style=\"text-decoration: none;\">&uarr;</a> "; }
    if ($i<$this->db->num_rows($sql)) { echo " <a href=\"./admin.php?f=categories&amp;action=move_down&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=categories&amp;f2=edit_category&amp;id=$id';\" /> <input type=\"button\" value=\"".LANG_DELETE."\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false; parent.location='./admin.php?f=categories&amp;action=delete&amp;id=$id';\" /></td>
    </tr>";
    }
  }

  //---[CZ]: posunutí nahoru
  public function move_up() {
    $sql=$this->db->query("UPDATE kboard_categories 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_categories 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_categories 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]: editace
  public function update() {
    $sql=$this->db->query("UPDATE kboard_categories SET name='".$this->db->escape($_POST["name"])."' 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 kategorie
  public function category_details($column) {
    $sql=$this->db->query("SELECT $column FROM kboard_categories WHERE id=".intval($_GET["id"]));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
  }

}
?>