<?php
class Users {

  private $db;
  private $status;

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

  //---[CZ]: změna práv
  public function change_rights() {
    $sql=$this->db->query("UPDATE ".TABLE_USERS." SET rights=".intval($_POST["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]: smazání
  public function delete() {
    $sql=$this->db->query("DELETE FROM ".TABLE_USERS." 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, login, rights, regdate, lastvisit FROM ".TABLE_USERS." ORDER by rights DESC");
    while (list($id, $login, $rights, $regdate, $lastvisit)=$this->db->fetch_row($sql)) {
    echo "<form action=\"./admin.php?f=users&amp;action=change_rights&amp;id=$id\" method=\"post\">
    <tr>
    <td><a href=\"./index.php?profile=$id\" onclick=\"return !openWindow(this.href);\">$login</a></td>
    <td class=\"align-center\">".date("d.m.Y, H:i", $regdate)."</td>
    <td class=\"align-center\">";
    if ($lastvisit==0) { echo "n/a"; } else { echo (date("d.m.Y, H:i", $lastvisit)); }
    echo "</td>
    <td class=\"align-center\"><select name=\"rights\">";
    for ($i=0; $i<4; $i++) {
    echo "<option value=\"$i\"";
    if ($rights==$i) { echo " selected=\"selected\""; }
    echo ">$i</option>\n";
    }
    echo "</select></td>
    <td class=\"align-center\"><input type=\"submit\" value=\"".LANG_SAVE."\" /> <input type=\"button\" value=\"".LANG_DELETE."\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false; parent.location='./admin.php?f=users&amp;action=delete_account&amp;id=$id';\" /></td>
    </tr>
    </form>\n";
    }
  }

}
?>