<?php
class Atribut extends Super{

  protected $id;
  protected $nazev;
  protected $titul;
  protected $popis;
  protected $stav = 1;  // default stav 

  protected $getter = array('id', 'nazev', 'titul', 'popis', 'stav');
  protected $setter = array( 'nazev', 'titul', 'popis', 'stav');
  public $label = array(
  	'id'     => 'Id',
  	'nazev'  => 'Název',
  	'titul'  => 'Titulek',
  	'popis'  => 'Popis',
    'stav'   => 'Stav'
  );
  public $validate = array(
  	'id'     => array('digits'	=> 'true'),
  	'nazev'  => array('required' => 'true', 'maxlength' => 35),
  	'titul'  => array('required' => 'true', 'maxlength' => 35),
    'stav'   => array('required' => 'true','range' => '[0,1]' )
  );

  public $state_validate = array();

  public $valAll = array ( 
      'id'     => true,
      'nazev'  => true,
      'titul'  => true,
      'stav'   => true,
	);

	public $filtry = array(
      'ID' => array(
      	'type' => 'text',
				'key'	 => 'ID',
				'sql'	 => 'num',
    		'size' => 3
    	),
      'NAZEV' => array(
      	'type' => 'text',
    		'key'	 => 'NAZEV',
    		'sql'	 => 'text',
    		'size' => 12
    	),
      'TITUL' => array(
      	'type' => 'text',
    		'key'	 => 'TITUL',
    		'sql'	 => 'text',
    		'size' => 12
    	),
      'S_NAZEV' => array(
      	'type' => 'stav',
    		'key'	 => 'STAV',
    		'sql'	 => 'num'
    	),
      'UPRAVIT' => array(
      	'type' => 'submit'
    	),
      'SMAZAT' => array(
      	'type' => 'all'
    	)
    );

  function __construct() {

  	$this->table = 'ATRIBUT';
  	$this->state_validate[0] = $this->valAll;
  	$this->state_validate[1] = $this->valAll;    

  }
  
  public function getAllQuery() {

  	$query = array();
  	$query[] = 'SELECT E.*, S.NAZEV AS S_NAZEV,
     (SELECT COUNT(*) FROM HODNOTA_ATRIBUTU WHERE ID_ATR = S.ID) AS POCET_HODNOT FROM ['.$this->table.'] E';
  	array_push($query, 'INNER JOIN [STAV] S ON S.STAV_ID = E.STAV AND S.ENTITA = %s',$this->table );

  	return $query;

  }

  public function getAll() {

  	$query = $this->getAllQuery();
    $res = dibi::query($query);
    return $res->fetchAssoc('ID');

  }

  public function loadById($id, $hash = false, $postRestore = false) {
 	
    $result = dibi::query('SELECT  E.*, S.NAZEV AS S_NAZEV, S.POPIS AS S_POPIS FROM ['.$this->table.'] AS E 
    	INNER JOIN STAV AS S ON S.STAV_ID = E.STAV AND ENTITA = %s
    	WHERE E.ID = %i;', $this->table, $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->setFromRow($row);
    $this->my_stav = '<div title="'.$row['S_POPIS'].'" class="block_state"> '.$row['STAV'].' - '.$row['S_NAZEV'].'</div><br />';

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

    $this->myStates();

    if ($postRestore) {
      $this->setFromPost();
    }
    return $this->toArray();

  }

  public function delete($id) {

  	dibi::begin();
  	dibi::query('DELETE FROM [HODNOTA_ATRIBUTU] WHERE ID_ATR = %i',$id);
  	dibi::query('DELETE FROM [ATRIBUT] WHERE ID = %i',$id);
  	dibi::commit();
  }
}
?>