<?php
class Atributhodnota extends Super{

  protected $id;
  protected $id_atr;
  protected $hodnota;
  protected $stav = 1;  // default stav 

  protected $getter = array('id', 'id_atr', 'hodnota', 'stav');
  protected $setter = array( 'id_atr', 'hodnota', 'stav');
  public $validate = array(
  	'id'      => array('digits' => 'true'),
  	'id_atr'  => array('required' => 'true', 'digits' => 'true'),
  	'hodnota' => array('required' => 'true'),
    'stav'    => array('range' => '[0,1]')
  );
  public $valAll = array(
  	'id'      => true,
  	'id_atr'  => true,
  	'hodnota' => true,
    'stav'    => true
  );
	public $filtry = array(
      'ID' => array(
      	'type' => 'text',
				'key'	 => 'ID',
				'sql'	 => 'num',
    		'size' => 3
    	),
      'A_NAZEV' => array(
      	'type' => 'select',
    		'sql'	 => 'num',
    		'key'	 => 'ID_ATR',
    		'table'=> 'ATRIBUT',
    		'opt'  => 'NAZEV'
    	),
      'HODNOTA' => array(
      	'type' => 'text',
    		'key'	 => 'HODNOTA',
    		'sql'	 => 'text',
    		'size' => 12
    	),
      'S_NAZEV' => array(
      	'type' => 'stav',
    		'key'	 => 'STAV',
    		'sql'	 => 'num',
    		'table' => 'HODNOTA_ATRIBUTU'
    	),
      'UPRAVIT' => array(
      	'type' => 'submit',
    		'size' => 12
    	),
      'SMAZAT' => array(
      	'type' => 'all'
    	)
    );
  public $state_validate = array();

  function __construct() {

    $this->state_validate[1] = $this->valAll;
    $this->state_validate[0] = $this->valAll;
    $this->table = 'HODNOTA_ATRIBUTU';
    $registry    = Registry::getInstance();

  }

  public function getAllQuery() {

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

  }

  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();
  }

}
?>