<?php
class User extends Super {

  protected $id;
  protected $id_skup; // zrušit -> nerušit 
  protected $mail;
  protected $heslo_hash;
  protected $heslo;
  protected $jmeno;
  protected $prijmeni;
  protected $koeficient = 1;
  protected $stav;
  protected $dt_vytvoren;
  protected $dt_zmenen;
  protected $dt_aktivni;

  protected $login;

  protected $getter = array('id', 'id_skup', 'mail', 'jmeno', 'heslo_hash', 'prijmeni', 'koeficient' , 'stav', 'dt_vytvoren', 'dt_zmenen', 'dt_zmenen', 'dt_aktivni', 'login');
  protected $setter = array( 'mail', 'id_skup', 'heslo', 'heslo_hash', 'jmeno', 'prijmeni', 'stav');
  
  public $validate = array(
  	'id' => array('digits' => 'true'),
  	'id_skup' => array('required' => 'true', 'digits' => 'true'),
  	'mail' => array('required' => 'true', 'email' => 'true'),
  	'heslo' => array('required' => 'true'),
  	'heslo2' => array('required' => 'true','equalTo' => 'heslo'),
  	'jmeno' => array('required' => 'true', 'maxlength' => 24),
  	'prijmeni' => array('required' => 'true', 'maxlength' => 35),
  	'stav' => array('required' => 'true', 'digits' => 'true'),
  );

  public $valAll = array(
  	'id' => true,
  	'id_skup' => true,
  	'mail' => true,
  	'heslo' => true,
  	'jmeno' => true,
  	'prijmeni' => true,
  	'stav' =>  true
  );
  public $state_validate = array();

  function __construct() {
    /*
     * stavy:
     * 1 - neautorizovaný
     * 2 - autorizovaný
     * 3 - blokovaný
     * 4 - archiv
     */
    $this->state_validate[1] = array(
    	'id' => true,
    	'id_skup' => true,
    	'mail' => true,
    	'heslo' => true,
    	'heslo2' => true,
    	'jmeno' => true,
    	'prijmeni' => true,
    	'stav' =>  true
    );
    $this->state_validate[2] = $this->valAll;
    $this->state_validate[3] = $this->valAll;
    $this->state_validate[4] = $this->valAll;

    $this->table = 'UZIVATEL';
    $this->neregistrovany();
  }

  public function neregistrovany() {
    $this->id         = 1; // 1 - neregistrovaný uživatel
    $this->id_skup 		= 1;
    $this->koeficient = 1;
    $this->jmeno      = 'Neregistrovaný';
    $this->prijmeni   = 'uzivatel';
    $this->login      = false;

  }

  public function login($email, $heslo) {

    $this->mail   = $email;
    $this->heslo  = $heslo;

    return $this->load();

  }

  public function load() {

    $this->validate('mail','heslo');
    $result = dibi::query('SELECT U.ID, U.MAIL, U.JMENO, U.PRIJMENI, U.DT_VYTVOREN, U.DT_ZMENEN, U.DT_AKTIVNI, U.STAV,
		SU.KOEFICIENT
		FROM UZIVATEL U
		INNER JOIN SKUPINA_UZIVATEL SU ON SU.ID = U.ID_SKUP
		WHERE U.MAIL = %s AND U.HESLO_HASH = SHA1(%s)',$this->mail, $this->heslo);
    if ($result->count() == 1) {
    	$res = $result->fetchAll();
    	$res = $res[0];
    	if ($res['STAV'] == 2) {
        $this->id          = $res['ID'];
        $this->jmeno       = $res['JMENO'];
        $this->prijmeni    = $res['PRIJMENI'];
        $this->mail        = $res['MAIL'];
        $this->koeficient  = $res['KOEFICIENT'];
        $this->stav        = $res['STAV'];
        $this->dt_vytvoren = $res['DT_VYTVOREN'];
        $this->dt_zmenen   = $res['DT_ZMENEN'];
        $this->dt_aktivni  = $res['DT_AKTIVNI'];
        $this->login       = true;
  			$this->heslo  		 = '';
        dibi::query('UPDATE [UZIVATEL] SET DT_AKTIVNI = NOW() WHERE ID = %i', $this->id);
        return true;
    	}
    	else {
 				Tools::redirect(Tools::controllerlink('uzivatel', 'stav', array($res['STAV'])));
    	}
    }
    return false;
  }

  public function loadById($id) {
    $this->validate('id');
    $result = dibi::query('SELECT U.ID, U.MAIL, U.JMENO, U.PRIJMENI, U.DT_VYTVOREN, U.DT_ZMENEN, U.DT_AKTIVNI, U.STAV,
		SU.KOEFICIENT
		FROM UZIVATEL U
		INNER JOIN SKUPINA_UZIVATEL SU ON SU.ID = U.ID_SKUP
		WHERE U.ID = %i AND U.STAV = 1',$this->id);
    if ($result->count() == 1) {
      $res = $result->fetchAll();
      $res = $res[0];
      $this->jmeno       = $res['JMENO'];
      $this->prijmeni    = $res['PRIJMENI'];
      $this->mail        = $res['MAIL'];
      $this->koeficient  = $res['KOEFICIENT'];
      $this->stav        = $res['STAV'];
      $this->dt_vytvoren = $res['DT_VYTVOREN'];
      $this->dt_zmenen   = $res['DT_ZMENEN'];
      $this->dt_aktivni  = $res['DT_AKTIVNI'];
      return true;
    }
    return false;
  }

  function save() {

  }

  function __sleep() {

    //vytvoříme pole vlastností objektu
    $vlastnosti = get_object_vars($this);
    foreach ($vlastnosti as $klic => $hodnota) {
      $pole[] = $klic;
    }
    return $pole;

  }

  function __wakeup() {
    Debug::addPoint( __CLASS__.' - '.__METHOD__, $this);
  }

  function getName() {
    return $this->jmeno.' '.$this->prijmeni;
  }

  public function isLogin() {
    return $this->login;
  }

  public function __destruct() {
    $_SESSION['user'] = serialize($this);
  }

/**
 * INSERT do DB
 * @param $key
 */
  public function insert() {
  	if (method_exists($this, 'beforeInsert')) {
  		$this->beforeInsert();
  	}
  	$res = dibi::query('INSERT INTO ['.$this->table.']', $this->toArray(true,array('mail', 'id_skup', 'heslo_hash', 'jmeno', 'prijmeni', 'stav')) );
  	$this->id = dibi::insertId();
  }

  public function getHash() {

  	$res = dibi::query('INSERT INTO ['.$this->table.']', $this->toArray(true) );
  	$this->id = dibi::insertId();
  }
}
?>