<?php
class kategorieController extends baseController {

  function __construct() {
    $this->controller_name = 'Kategorie';
    $this->controller_fname = 'kategorie';
    $this->controller_info = 'Kategorie';
    $this->entita_classname = 'Kategorie';
    parent::__construct();
    $this->tpl = 'kategorie.tpl';
  }

  public function zobrazit($args) {

  	$id = @$args[0];
  	$kategorie = new Kategorie();
  	$entita = new Produkt();
  	$this->item_to_page = 6;
  	$filtry = array();
  	if (!empty($id)) {
  		$kategorie->loadByURL($args[0]);
  		$filtr = array(
  			'key' => 'ID_KAT',
  			'sql' => 'num',
  			'tab_name' => 'KP'
  		);
  		$filtr2 = array(
  			'key' => 'STAV',
  			'sql' => 'in',
  			'tab_name' => 'E'
  		);
  		$filtry[] = $filtr;
  		$filtry[] = $filtr2;
  		$_POST['ID_KAT'] = $kategorie->id;
  		$filtr = true;
  	}
  	else {
  		$kategorie->loadByID(1);
  		$order = array();
  		$order[] = array('name' => 'priorita', 'ord' => 'DESC');
  		$this->registry->content_order = $order;
  		$filtr = false;
  	}

  	$this->registry->addPersistent('last_kategory_id', $kategorie->id);
  	$this->registry->meta_title = $kategorie->meta_title;
  	$this->registry->meta_description = $kategorie->meta_description;
  	$this->registry->meta_keywords = $kategorie->meta_keywords;

  	$limit_settings = array(
  		'res_text' => 'V kategorii najdete celkem ',
  		'res_1' => 'produkt',
  		'res_3' => 'produkty',
  		'res_5' => 'produktů',
  		'next_button' => 'Další',
  		'prev_button' => 'Předchozí',
  	
  	);
  	
  	$this->registry->kategorie = $kategorie->toArray(false,array(),true);
  	$query = array();
  	$from = 'FROM PRODUKT E	INNER JOIN KAT_PROD KP ON KP.ID_PROD = E.ID ';

  	$query[] = 'SELECT DISTINCT E.NAZEV, E.REWRITE_LINK, E.POPIS_KRATKY,
 		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) + RECYKLACNI_POPLATEK + AUTORSKY_POPLATEK), CEIL(E.CENA * D.KOEFICIENT * '.$this->registry->user->koeficient.') + RECYKLACNI_POPLATEK + AUTORSKY_POPLATEK) AS CENA,
 		CEIL(E.CENA * D.KOEFICIENT * '.$this->registry->user->koeficient.' + RECYKLACNI_POPLATEK + AUTORSKY_POPLATEK) AS BEZNA_CENA, S.NAZEV AS S_NAZEV
			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 STAV S ON S.STAV_ID = E.STAV AND S.ENTITA = "PRODUKT" AND S.STAV_ID IN (1, 2, 3, 5, 6)
			LEFT JOIN OBRAZEK_PRODUKTU OP ON OP.ID_PROD = E.ID AND OP.PORADI = 1';
    $filtr_query = array();

    if ($filtr == true) {
    	$this->setFilter($filtr_query, $filtry,array('STAV' => array(1, 2, 3, 5, 6)));
   		$this->myOrder($filtr_query);
    	$strankovani = $this->addLimit($query, $filtr_query , $from, true, $limit_settings);
    }
    else {
    	//$this->setFilter($filtr_query, $filtry,array('STAV' => array(1, 2, 3, 5, 6)));
    	$this->myOrder($filtr_query);
    	$this->addLimit($query, $filtr_query, $from, true, $limit_settings);
    	$strankovani = '';
    }

		$query = array_merge($query,$filtr_query);
//dibi::test($query);
    $result = dibi::query($query);
    $template = $this->registry->template;
  
    
    if (count($result) > 0) {

      $zaznamy = $result->fetchAll();
      $items = array();
      foreach($zaznamy as $key=>$item) {
      	$items[$key] = $zaznamy[$key]->getArrayCopy();
  			$items[$key]['SOUBOR'] = URL.PATH_PRODUKT.PATH_NAHLED.$item['SOUBOR'];
  			$items[$key]['CENA']	 = number_format($item['CENA'],0,',',' '). 'Kč s DPH';
  			$items[$key]['LINK']	 = Tools::productLink($item['REWRITE_LINK']);
  			$items[$key]['STAV']	 = '<b>Stav:</b> '.$item['S_NAZEV'];
  			if ($item['SLEVA'] != 0) {
  				$items[$key]['SLEVA']	 = '<span class="sleva"><b>Sleva:</b> '.number_format($item['SLEVA'],2,',','').'%</span><br /><b>Původní cena:</b> <span class="stara_cena"> '.number_format($item['BEZNA_CENA'],0,',',' ').'</span> Kč';
  			}
  			else {
  				$items[$key]['SLEVA'] = '';
  			}

  		}

  		$template->set('produkty', true, true);
  		$template->setHTML('item', $items);
  		$template->setHTML('strankovani', '<div class="strankovani">'.$strankovani.'</div>');

    }
    else {
    	$template->set('produkty', false, true);
    }

    $action = URL.'kategorie'.DIRSEP.@$args[0];
		if (substr($action,-1) != DIRSEP) {
    	$action .= DIRSEP;
    }

    if (!strstr($action,SEND)) {
    	$action .= SEND;
    }
    $template->setHTML('form_action', $action);

/*
    foreach($zaznamy as $item) {
    	$this->html .= '<div class="produkt"><h3><a href="produkt/'.$item['REWRITE_LINK'].'">'.$item['NAZEV'].'</a></h3>
    		<div class="image"><a href="'.URL.'produkt/'.$item['REWRITE_LINK'].'"><img width="120" src="'.URL.PATH_PRODUKT.PATH_NAHLED.$item['SOUBOR'].'"></a></div>
				<div class="popis">'.$item['POPIS_KRATKY'].'</div>
    		<div class="cena">Cena: '.number_format($item['CENA'],0,',',' ').' Kč s DPH</div>
    	</div>';
    }
*/  	

  	$this->setView();
  }

  public function treeNav() {
  	
  	$kategorie = $this->registry->kategorie;
    $result = dibi::query('SELECT E.NAZEV, E.REWRITE_LINK
			FROM KATEGORIE E
			WHERE LEVA < %i
			AND PRAVA > %i
			AND E.STAV = 1
			AND E.ID <> %i
			ORDER BY LEVA;', $kategorie['leva'], $kategorie['prava'], $kategorie['id']);

    $tree_nav = array();
    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_kat_nazev', $kategorie['nazev']);
  }

}
?>