<?php
class Adresa extends Super {

  protected $id;
  protected $id_uziv;
  protected $id_vyr;
  protected $id_dod;
  protected $titul;
  protected $jmeno;
  protected $prijmeni;
  protected $nazev_firmy;
  protected $ico;
  protected $dic;
  protected $kontakt_mail;
  protected $kontakt_telefon;
  protected $ulice;
  protected $cislo_popisne;
  protected $obec;
  protected $psc;  
  protected $stav;

  protected $getter = array('id', 'id_uziv', 'id_vyr', 'id_dod', 'titul', 'jmeno', 'prijmeni', 'nazev_firmy',
	'ico', 'dic', 'kontakt_mail', 'kontakt_telefon', 'ulice', 'cislo_popisne', 'obec', 'psc', 'stav');
  protected $setter = array( 'id_uziv', 'id_vyr', 'id_dod', 'titul', 'jmeno', 'prijmeni', 'nazev_firmy',
	'ico', 'dic', 'kontakt_mail', 'kontakt_telefon', 'ulice', 'cislo_popisne', 'obec', 'psc', 'stav');
  public $valAll = array(
  	'id'      => true,
		'id_uziv' => true,
    'id_vyr'  => true,
    'id_dod'  => true,
    'titul'	  => true,
    'jmeno'   => true,
    'prijmeni'	  => true,
    'nazev_firmy' => true,
    'ico'	=> true,
    'dic'	=> true,
    'kontakt_mail'    => true,
    'kontakt_telefon' => true,
    'ulice'  => true,
    'cislo_popisne' => true,
    'obec'   => true,
    'psc'    => true,
    'stav'   => true
  );
  public $validate = array(
  	'id'      => array('digits' => 'true'),
		'id_uziv' => array('digits' => 'true'),
    'id_vyr'  => array('digits' => 'true'),
    'id_dod'  => array('digits' => 'true'),
    'titul'	  => array('maxlength' => 35),
    'jmeno'   => array('required' => 'true', 'rangelength' => '[1, 24]'),
    'prijmeni'	  => array('required' => 'true', 'rangelength' => '[1, 35]'),
    'nazev_firmy' => array('rangelength' => '[0, 255]'),
    'ico'	=> array('digits' => 'true'),
    'dic'	=> array('digits' => 'true'),
    'kontakt_mail'    => array('required' => 'true', 'email' => 'true', 'maxlength' => 320),
    'kontakt_telefon' => array('required' => 'true','rangelength' => '[9,14]', 'telefon' => 'true'),
    'ulice'  => array('required' => 'true', 'rangelength' => '[1, 48]'),
    'cislo_popisne'     => array('required' => 'true', 'rangelength' => '[1, 10]'),
    'obec'   => array('required' => 'true', 'rangelength' => '[2, 48]'),
    'psc'    => array('required' => 'true', 'rangelength' => '[5,5]'),
    'stav'   => array('required' => 'true', 'range' => '[0,1]')
  );

  public $state_validate = array();

  public function __construct() {
    /*
     * stavy:
     * 1 - aktivní
     * 2 - neaktivní
     */
    $this->table = 'ADRESA';
    $this->state_validate[0] = $this->valAll;
    $this->state_validate[1] = $this->valAll;
  }
  
  public function getAll() {
    $res = dibi::query('SELECT A.ID, A.TITUL, A.JMENO, A.PRIJMENI, A.NAZEV_FIRMY, A.KONTAKT_MAIL, A.KONTAKT_TELEFON, 
    	A.ULICE, A.CISLO_POPISNE, A.OBEC, A.PSC, S.NAZEV AS STAV FROM ['.$this->table.'] AS A
    	INNER JOIN [STAV] AS S ON S.STAV_ID = A.STAV AND S.ENTITA = "'.$this->table.'"
    	');
    return $res->fetchAssoc('ID');
  }

  public function loadById($id, $hash = false) {
    $result = dibi::query('SELECT  A.* FROM ['.$this->table.'] AS A
    	WHERE A.ID = %i;', $id);
    if (count($result) == 0) {
      throw new Appliexception(1, 'Požadovaný záznam neexistuje', 'seznam', __METHOD__.' Neexistující záznam '.$id );
    }

    $row = $result->fetch()->getArrayCopy();
    $this->id                 = $row['ID'];
    $this->id_uziv            = $row['ID_UZIV'];
    $this->id_vyr             = $row['ID_VYR'];
    $this->id_dod             = $row['ID_DOD'];
    $this->titul              = $row['TITUL'];
    $this->jmeno              = $row['JMENO'];
    $this->prijmeni           = $row['PRIJMENI'];
    $this->nazev_firmy        = $row['NAZEV_FIRMY'];
    $this->ico                = $row['ICO'];
    $this->dic                = $row['DIC'];
    $this->kontakt_mail       = $row['KONTAKT_MAIL'];
    $this->kontakt_telefon    = $row['KONTAKT_TELEFON'];
    $this->ulice              = $row['ULICE'];
    $this->cislo_popisne      = $row['CISLO_POPISNE'];
    $this->obec               = $row['OBEC'];
    $this->psc                = $row['PSC'];
    $this->stav               = $row['STAV'];

    if ($hash) {
      $this->hash  = $this->getHash();
    } 

    return $this->toArray();
  }

  public function checkBlock($id) {
  	$result = dibi::query('SELECT COUNT(*) FROM OBJEDNAVKA O 
			WHERE O.ID_ADR_DORUC = %i OR O.ID_ADR_FAKT = %i', $id, $id);
  	if ($result->fetchSingle() == 0) {
  		return false;
  	}
  	else {
  		return true;
  	}
  }

  public function saveAsNew() {
  	$this->id = null;
    $this->stav = 1;
    $this->insert();
  }

}
?>