<?php
class produktController extends baseController {

	function __construct() {
    $this->controller_name = 'Produkt';
    $this->controller_fname = 'produkt';
    $this->controller_info = 'Produkt';
    $this->entita_classname = 'Produkt';
    parent::__construct();
    $this->tpl = 'produkt.tpl';
  }
	
  public function zobrazit($args) {

  	$url = $args[0];
  	$entita = new Produkt();

  	$result = dibi::query('SELECT DISTINCT E.ID, E.KATALOG_CISLO, E.NAZEV, E.REWRITE_LINK, E.META_TITLE, E.META_KEYWORDS, E.META_DESCRIPTION,
  	E.KATALOG_CISLO, E.POPIS_DLOUHY, E.STAV, OP.SOUBOR,
  	if(SLEVA_OD < curdate() AND SLEVA_DO > curdate(), SLEVA_PROCENT * 100, 0) AS SLEVA,
  	if(SLEVA_OD < curdate() AND SLEVA_DO > curdate(), CEIL(E.CENA * D.KOEFICIENT * '.$this->registry->user->koeficient.' *
			(1-SLEVA_PROCENT)), CEIL(E.CENA * D.KOEFICIENT * '.$this->registry->user->koeficient.')) AS CENA,
  	if(SLEVA_OD < curdate() AND SLEVA_DO > curdate(), CEIL(E.CENA * D.KOEFICIENT * '.$this->registry->user->koeficient.' *
			(1-SLEVA_PROCENT) + RECYKLACNI_POPLATEK + AUTORSKY_POPLATEK), CEIL(E.CENA * D.KOEFICIENT * '.$this->registry->user->koeficient.') + RECYKLACNI_POPLATEK + AUTORSKY_POPLATEK) AS CELKOVA_CENA,			
		CEIL(E.CENA * D.KOEFICIENT * '.$this->registry->user->koeficient.' + RECYKLACNI_POPLATEK + AUTORSKY_POPLATEK) AS BEZNA_CENA, S.NAZEV AS S_NAZEV,
  	E.RECYKLACNI_POPLATEK, E.AUTORSKY_POPLATEK,E.DOPRAVA_ZDARMA, S.NAZEV AS S_NAZEV, S.POPIS AS S_POPIS,
  	Z.NAZEV AS ZARUKA,Z.POPIS AS ZARUKA_POPIS,EX.NAZEV AS EXPEDICE,
  	V.NAZEV AS VYROBCE, V.POPIS AS VYROBCE_POPIS, V.URL AS VYROBCE_URL, V.LOGO AS VYROBCE_LOGO
		FROM PRODUKT E
		INNER JOIN KAT_PROD KP ON KP.ID_PROD = E.ID
		INNER JOIN DAN D ON D.ID = E.ID_DAN
		INNER JOIN ZARUKA Z ON Z.ID = E.ID_ZARUKA
		INNER JOIN EXPEDICE EX ON EX.ID = E.ID_EXPEDICE
		INNER JOIN STAV S ON S.STAV_ID = E.STAV AND S.ENTITA = "PRODUKT"
		LEFT JOIN OBRAZEK_PRODUKTU OP ON OP.ID_PROD = E.ID AND OP.PORADI = 1
		LEFT JOIN VYROBCE V ON V.ID = E.ID_VYR
		WHERE E.REWRITE_LINK = %s', $url);
		
  	$template = $this->registry->template;
  	if (count($result) != 1) {
  		$template->set('is_produkt', false, true);
  	}
  	else {
  		$r = $result->fetchAll();
  		$item = $r[0]->getArrayCopy();

  		$item['SOUBOR'] = URL.PATH_PRODUKT.PATH_NAHLED.$item['SOUBOR'];
  		$item['LINK']	 = Tools::productLink($item['REWRITE_LINK']);

  		$item['info_box'] = '<b>Záruka:</b>'.$item['ZARUKA'].'<br />';
  		$item['info_box'] .= '<b>Expedice:</b>'.$item['EXPEDICE'].'<br />';

  		if (!empty($item['KATALOG_CISLO'])) {
  			$item['info_box'] .= '<b>Katalog. č.:</b>'.$item['KATALOG_CISLO'].'<br />';
  		}
  		if (!empty($item['VYROBCE'])) {
  			if (!empty($item['VYROBCE_URL'])) {
  				$item['info_box'] .= '<b>Výrobce:</b><a target="_blank" href="'.$item['VYROBCE_URL'].'">'.$item['VYROBCE'].'</a><br />';
  			}
  			else {
  				$item['info_box'] .= '<b>Výrobce:</b>'.$item['VYROBCE'].'<br />';
  			}
  		}

  		switch($item['STAV']) {
  			case 1:
  			case 7:
  			case 4:
  				$item['info_box'] .= '<br />'.$item['S_POPIS'].'<br />';
  				break;

				case 2:		
  			case 5:		
  			case 6:
  				$item['info_box'] .= '<br />'.$item['S_POPIS'].'<br />';
  			case 3:
      		if ($item['SLEVA'] > 0) {
      			$item['info_box'] .= '<b>Sleva:</b><span class="sleva">'.number_format($item['SLEVA'],0,',',' ').'%</span><br />';
      			$item['info_box'] .= '<b>Původní cena:</b><span class="stara_cena">'.number_format($item['BEZNA_CENA'],0,',',' ').' Kč s DPH</span><br />';
      		}

      		$item['info_box'] .= '<b>Cena:</b><span class="cena">'.number_format($item['CELKOVA_CENA'],0,',',' ').' Kč s DPH</span><br />';
      		$item['info_box'] .= '<form method="post" id="dokosiku" action="'.Tools::controllerlink('kosik', 'pridat').'">
      			<b class="a_r">
      				<input type="hidden" name="id_prod" id="id_prod" value="'.$item['ID'].'">
      				<input type="text" class="mnozstvi" id="mnozstvi" name="mnozstvi" value="1">
      			</b>
      			<input type="submit" class="blue_120" name="pridat" value="Do košíku">
      			<br />';
      		$this->registry->jscript->addScript('$("#dokosiku").submit(function(){
      				var id_prod  = $("#id_prod").val();
      				var mnozstvi = $("#mnozstvi").val();
      				if (mnozstvi > 0) { 
          			$.post("'.URL.'kosik/pridat",{"id_prod": id_prod, "mnozstvi": mnozstvi },
                		function(data){
                  		if (data != "FALSE"){
          						$("#kosik_souhrn").html(data); 
           					}
            			}
            		);
            	}
  						$("#dokosiku").effect("transfer",{to: "#kosik_souhrn"}, 500 );
  						return false;
  					}
  				)
  			', 'document_ready');	
  				break;  				
  		}
  		
			

  		$this->registry->produkt = $item;
  		$template->set('is_produkt', true, true);
  		$template->setHTML('produkt', $item);
  		$this->registry->jscript->addScript('function showMe(name) {
  			$("#aktual_img").attr("src",name);
  		}');
  		
  		$result = dibi::query('SELECT CONCAT("'.URL.PATH_PRODUKT.PATH_NAHLED.'",SOUBOR) AS SOUBOR, OP.TITULEK, OP.ALT, OP.PORADI
  			FROM OBRAZEK_PRODUKTU OP
  			WHERE STAV = 1 AND ID_PROD = %i',$item['ID'],
  			'ORDER BY PORADI ASC');
			$rows = array();
  		foreach($result as $obr) {
  			$rows[] = $obr->getArrayCopy();
  		}
  		$template->setHTML('obrazky', $rows);
  		
  		$result = dibi::query('SELECT A.TITUL, HA.HODNOTA FROM KOMBINACE_HODNOTA KH
			INNER JOIN HODNOTA_ATRIBUTU HA ON HA.ID = KH.ID_HATR
			INNER JOIN ATRIBUT A ON A.ID = HA.ID_ATR
			WHERE KH.ID_PROD = %i',$item['ID']);
			$rows = array();
  		foreach($result as $obr) {
  			$rows[] = $obr->getArrayCopy();
  		}
  		$template->setHTML('atributy', $rows);
  		
  		$result = dibi::query('SELECT V.ID, V.TITUL, KV.HODNOTA, V.JEDNOTKA FROM KOMBINACE_VLASTNOST KV
			INNER JOIN VLASTNOST V ON V.ID = KV.ID_VLA
			WHERE KV.ID_PROD = %i',$item['ID']);
			$rows = array();
  		foreach($result as $obr) {
  			$rows[] = $obr->getArrayCopy();
  		}
  		$template->setHTML('vlastnosti', $rows);
  	}

  	$this->setView();
  }
  
  public function treeNav() {
  	
  	$tree_nav = array();
  	if ($this->registry->last_kategory_id > 0) {
    	$kat_id = $this->registry->last_kategory_id;
  	}
  	else {
			$result = dibi::query('SELECT K.ID
			FROM KATEGORIE K
			INNER JOIN KAT_PROD KP ON KP.ID_KAT = K.ID
			WHERE KP.ID_PROD = %i
			ORDER BY HLOUBKA DESC
			LIMIT 1', $this->registry->produkt['ID']);
			
			if (count($result) > 0) {
				$kat_id = $result->fetchSingle();
			}
  	}
  	
  	if (isset($kat_id)) {
  	 $result = dibi::query('SELECT E.NAZEV, E.REWRITE_LINK
  			FROM KATEGORIE E
  			WHERE LEVA <= (SELECT LEVA FROM KATEGORIE WHERE ID = %i)
  			AND PRAVA >= (SELECT PRAVA FROM KATEGORIE WHERE ID = %i)
  			AND E.STAV = 1
  			ORDER BY LEVA;', $kat_id, $kat_id);

      foreach($result as $row) {
      	$r = $row->getArrayCopy();
      	$r['LINK'] = Tools::categoryLink($r['REWRITE_LINK']);
      	$tree_nav[] = $r;
      }
  	}

    $this->registry->template->set('is_tree_nav', true, true);
    $this->registry->template->set('tree_nav', $tree_nav);
    $this->registry->template->set('akt_prod_nazev', $this->registry->produkt['NAZEV']);
  }
  
}
?>