<?php
class Produkt extends Super{

  protected $id;
  protected $id_dan;
  protected $id_vyr;
  protected $name_vyr;
  protected $id_zaruka;
  protected $id_expedice;
  protected $nazev;
  protected $rewrite_link;
  protected $katalog_cislo;
  protected $cena;
  protected $recyklacni_poplatek;
  protected $autorsky_poplatek;
  protected $neomezeno;
  protected $pocet_kusu;
  protected $popis_kratky;
  protected $popis_dlouhy;
  protected $sleva_procent = 0;
  protected $sleva_pocet;
  protected $sleva_od;
  protected $sleva_do;
  protected $meta_title;
  protected $meta_keywords;
  protected $meta_description;
  protected $doprava_zdarma = 0;
  protected $priorita;
  protected $stav = 2;  // default stav 

  protected $getter = array('id', 'id_dan', 'id_vyr', 'name_vyr', 'id_zaruka', 'id_expedice', 'nazev', 'rewrite_link', 
	'katalog_cislo', 'cena', 'recyklacni_poplatek', 'autorsky_poplatek', 'neomezeno', 'pocet_kusu','popis_kratky', 
	'popis_dlouhy', 'sleva_procent', 'sleva_pocet', 'sleva_od', 'sleva_do', 'meta_title', 'meta_keywords', 
	'meta_description', 'doprava_zdarma', 'priorita', 'stav');

  protected $setter = array('id_dan', 'id_vyr', 'id_zaruka', 'id_expedice', 'nazev', 'rewrite_link', 
	'katalog_cislo', 'cena', 'recyklacni_poplatek', 'autorsky_poplatek', 'neomezeno', 'pocet_kusu','popis_kratky', 
	'popis_dlouhy', 'sleva_procent', 'sleva_pocet', 'sleva_od', 'sleva_do', 'meta_title', 'meta_keywords', 
	'meta_description', 'doprava_zdarma', 'priorita', 'stav');

  public $validate = array(
	'id'	      	=> array('digits' => 'true'),
	'id_dan'      => array('required' => 'true','digits' => 'true'),
  'id_vyr'      => array('digits' => 'true'),
  'id_zaruka'   => array('required' => 'true','digits' => 'true'),
  'id_expedice' => array('required' => 'true','digits' => 'true'),
  'nazev'		  	=> array('required' => 'true','maxlength' => 150),
  'rewrite_link'      => array('required' => 'true', 'maxlength' => 160),
  'meta_title'        => array('required' => 'true', 'maxlength' => 250),
  'meta_keywords'     => array('maxlength' => 250), 
	'meta_description'  => array('maxlength' => 250),
	'katalog_cislo'	    => array('maxlength' => 65),
  'cena'              => array('required' => 'true','numberDE' => 'true'),
  'recyklacni_poplatek' => array('numberDE' => 'true'),
  'autorsky_poplatek'   => array('numberDE' => 'true'),
  'neomezeno'		  	=> array('range' => '[0,1]'),
  'pocet_kusu'      => array('digits' => 'true'),
  'popis_kratky'    => array('required' => 'true'), 
	'popis_dlouhy'    => array('required' => 'true'),
	'sleva_procent'   => array('numberDE' => 'true'),
  'sleva_pocet'     => array('digits' => 'true'),
  'sleva_od'        => array('dateDE' => 'true'),
  'sleva_do'        => array('dateDE' => 'true'),
  'doprava_zdarma'  => array('range' => '[0,1]'),
  'priorita'        => array('range' => '[-10,+10]'),
  'stav'			  		=> array('range' => '[1,7]')
	);
	public $label = array(
	'id'	      	=> 'Id',
	'id_dan'      => 'Daň',
  'id_vyr'      => 'Výrobce',
  'id_zaruka'   => 'Záruka',
  'id_expedice' => 'Expedice',
  'nazev'		  	=> 'Název',
  'rewrite_link'      => 'Link',
  'meta_title'        => 'Titulek',
  'meta_keywords'     => 'Klíč. slova', 
	'meta_description'  => 'Popis',
	'katalog_cislo'	    => 'Katalog. č.',
  'cena'              => 'Cena bez DPH',
  'recyklacni_poplatek' => 'Recyklační poplatek',
  'autorsky_poplatek'   => 'Autorský poplatek',
  'neomezeno'		  	=> 'Neuvádět počet',
  'pocet_kusu'      => 'Počet',
  'popis_kratky'    => 'Krátký popis', 
	'popis_dlouhy'    => 'Delší popis',
	'sleva_procent'   => 'Sleva v %',
  'sleva_pocet'     => 'Kusů',
  'sleva_od'        => 'Sleva od',
  'sleva_do'        => 'Sleva do',
  'doprava_zdarma'  => 'Doprava zdarma',
  'priorita'        => 'Priorita',
  'stav'			  		=> 'Stav'
	);
/* STAVY
1	Připravujeme
2	Novinka
3	V prodeji
4	Dočasně nedostupný
5	Znovu v prodeji
6	Výprodej
7	Prodej ukončen

 */
  public $valAll = array(
    	'id_dan'      => true,
      'id_vyr'      => true,
      'id_zaruka'   => true,
      'id_expedice' => true,
      'nazev'		  => true,
      'rewrite_link'      => true,
      'meta_title'        => true,
      'meta_keywords'     => true, 
    	'meta_description'  => true,
    	'katalog_cislo'	    => true,
      'cena'              => true,
      'recyklacni_poplatek' => true,
      'autorsky_poplatek'   => true,
      'neomezeno'		  	=> true,
      'pocet_kusu'      => true,
      'popis_kratky'    => true, 
    	'popis_dlouhy'    => true,
    	'sleva_procent'   => true,
      'sleva_pocet'     => true,
      'sleva_od'        => true,
      'sleva_do'        => true,
      'doprava_zdarma'  => true,
      'priorita'        => true,
  		'id_zaruka'				=> 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
    	),
      'KATALOG_CISLO' => array(
      	'type' => 'text',
    		'key'	 => 'KATALOG_CISLO',
    		'sql'	 => 'text',
    		'size' => 7
    	),    	
      'S_NAZEV' => array(
      	'type' => 'stav',
    		'key'	 => 'STAV',
    		'sql'	 => 'num'
    	),
      'UPRAVIT' => array(
      	'type' => 'submit'
    	),
      'NAHLED' => array(
      	'type' => 'all'
    	)
    );

  public $state_validate = array();

  function __construct() {

    $this->state_validate[1] = array(
      'nazev'		  				=> true,
      'rewrite_link'      => true,
      'meta_title'        => true,
      'meta_keywords'     => true, 
    	'meta_description'  => true,
    	'katalog_cislo'	    => true,
      'popis_kratky'    => true, 
    	'popis_dlouhy'    => true,
      'priorita'        => true,
      'stav'			  => true
    );

    $this->state_validate[2] = $this->valAll;
    $this->state_validate[3] = $this->valAll;
    $this->state_validate[4] = $this->valAll;
    $this->state_validate[5] = $this->valAll;
    $this->state_validate[6] = $this->valAll;
    $this->state_validate[7] = array(
      'nazev'		  				=> true,
      'rewrite_link'      => true,
      'meta_title'        => true,
      'meta_keywords'     => true, 
    	'meta_description'  => true,
    	'katalog_cislo'	    => true,
      'popis_kratky'    => true, 
    	'popis_dlouhy'    => true,
      'priorita'        => true,
      'stav'			  		=> true
    );

    $this->table = 'PRODUKT';

  }

  public function loadById($id, $hash = false, $postRestore = false) {
    $result = dibi::query('SELECT  E.*, V.NAZEV AS NAME_VYR, 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
    	LEFT JOIN VYROBCE V ON V.ID = E.ID_VYR
    	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();
  }

/**
* vrátí url podle názvu
* @param  $name
* @return string
*/ 
  public static function getUrl($name) {

    if (empty($name)) {
      die('FALSE');
    }

    $url = Tools::toURL($name);

    $result = dibi::query('SELECT REWRITE_LINK FROM [PRODUKT] WHERE REWRITE_LINK LIKE %sql OR REWRITE_LINK LIKE %s 
    	ORDER BY REWRITE_LINK DESC LIMIT 1', '"'.mysql_escape_string($url).'\__"', $url);

    if (count($result) == 0) {
      return $url;
    }
    elseif (count($result) == 1) {
      $db_url = $result->fetchSingle();
      $numb = explode('_', $db_url);

      return $url.'_'.($numb[count($numb)-1] + 1);

    }

  }

  public function getAllQuery() {

  	$query = array();
  	$query[] = 'SELECT E.ID, E.REWRITE_LINK, E.KATALOG_CISLO, E.NAZEV, D.NAZEV AS D_NAZEV, E.CENA,
  	 ROUND(D.KOEFICIENT*E.CENA) AS CENA_S, E.ID_VYR, S.NAZEV AS S_NAZEV, IF(NEOMEZENO = 1, " inf ", POCET_KUSU ) AS KUSU
			FROM ['.$this->table.'] AS E
			INNER JOIN DAN D ON D.ID = E.ID_DAN';
  	array_push($query, 'INNER JOIN [STAV] S ON S.STAV_ID = E.STAV AND S.ENTITA = %s',$this->table );
  	return $query;

  }

	public function beforeInsert() {

		$this->sleva_procent = Tools::numberFromDE($this->sleva_procent);
  	if (!empty($this->sleva_procent) AND intval($this->sleva_procent) > 0) {
    	$this->sleva_procent = ($this->sleva_procent/100);
    }
    else {
    	$this->sleva_procent = 0;
    }

    if ($this->pocet_kusu == null) {
    	$this->neomezeno = true;
    }
    else {
    	$this->neomezeno = false;
    }
		$this->sleva_od = Tools::dateToENG($this->sleva_od);
		$this->sleva_do = Tools::dateToENG($this->sleva_do);
		$this->cena = Tools::numberFromDE($this->cena);
		$this->recyklacni_poplatek = Tools::numberFromDE($this->recyklacni_poplatek);
		$this->autorsky_poplatek = Tools::numberFromDE($this->autorsky_poplatek);
	}

	public function beforeUpdate() {

		$this->sleva_procent = Tools::numberFromDE($this->sleva_procent);
  	if (!empty($this->sleva_procent) AND intval($this->sleva_procent) > 0) {
    	$this->sleva_procent = ($this->sleva_procent/100);
    }
    else {
    	$this->sleva_procent = 0;
    }

    if ($this->pocet_kusu == null) {
    	$this->neomezeno = true;
    }
    else {
    	$this->neomezeno = false;
    }
		$this->sleva_od = Tools::dateToENG($this->sleva_od);
		$this->sleva_do = Tools::dateToENG($this->sleva_do);
		$this->cena = Tools::numberFromDE($this->cena);

		$this->recyklacni_poplatek = Tools::numberFromDE($this->recyklacni_poplatek);
		$this->autorsky_poplatek = Tools::numberFromDE($this->autorsky_poplatek);

	}
	
}
?>