<?php
class Obrazekproduktu extends Super{
//ID 	ID_PROD 	ID_PAR 	SOUBOR 	TITULEK 	ALT 	PORADI 	STAV 
  protected $id;
  protected $id_prod;
  protected $id_par = 2;
  protected $soubor;
  protected $titulek;
  protected $alt;
  protected $poradi;
  protected $stav = 1;  // default stav 

  protected $getter = array('id', 'id_prod', 'id_par', 'soubor', 'titulek', 'alt', 'poradi', 'stav');
  protected $setter = array('id_prod', 'id_par', 'soubor', 'titulek', 'alt', 'poradi', 'stav');
  public $validate = array(
  	'id'     => array('digits' => 'true'),
  	'id_prod'=> array('required' => 'true','digits' => 'true'),
  	'id_par' => array('required' => 'true','digits' => 'true'),
  	'soubor' => array('maxlength' => 100),
  	'titulek'=> array('required' => 'true','maxlength' => 100),
  	'alt'		 => array('required' => 'true','maxlength' => 100),
  	'poradi' => array(),
  	'stav'   => array('range' => '[0,1]')
  );
	public $label = array(
  	'id'     => 'Id',
  	'id_prod'=> 'Produkt',
  	'id_par' => 'Rozměry',
  	'soubor' => 'Soubor',
  	'titulek'=> 'Titulek',
  	'alt'		 => 'Alt',
  	'poradi' => 'Pořadí',
  	'stav'   => 'Stav'
  );
  public $valAll = array(
  	'id'     => true,
  	'id_prod'=> true,
  	'id_par' => true,
  	'soubor' => true,
  	'titulek'=> true,
  	'alt'		 => true,
  	'poradi' => true,
  	'stav'   => true
  );
	public $filtry = array();
  public $state_validate = array();

  function __construct() {
    /*
     * stavy:
     * 1 - aktivní
     * 2 - neaktivní
     */
    $this->state_validate[1] = $this->valAll;
    $this->state_validate[0] = $this->valAll;
    
    $this->table = 'OBRAZEK_PRODUKTU';

  }
  
  public function getToSelect() {
    $res = dibi::query('SELECT E.*
    	FROM ['.$this->table.'] AS E
    	WHERE STAV = %i',
         1);
    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();
  }
  
/**
 * INSERT do DB
 * @param $key
 */
  public function insert() {

  	$res = dibi::query('INSERT INTO ['.$this->table.'] SET %a %if', $this->toArrayNN(true),
  		empty($this->poradi) ,
  		', PORADI = (SELECT if(isnull(MAX(PORADI)), "1", (MAX(PORADI) + 1)) AS PORADI FROM OBRAZEK_PRODUKTU OP WHERE OP.ID_PROD = %i)',$this->id_prod);
  	$this->id = dibi::insertId();
  	
  }

}
?>