<?php

class Katalog extends Controller {

	function Katalog()
	{
		parent::Controller();
		$this->config->load('katalog_config');
		$this->data = $this->config->item('data');
		//$this->output->cache((24-date("H"))*60 -( 60-date("i")));
    $this->load->model('Dbmysql');
    $this->load->model('Functions');
    $this->load->amodel('Kategorie');
    $this->load->model('Lokality');
    $this->load->model('Timer');
		$this->load->helper('url');
		$this->data['itemsPerPage'] = 10;
	}

	function index()
	{
		$this->pages();
	}

	function pages()
	{
		//Ak sa jedna o homepage
		if($this->uri->total_segments() == 1)
		{
      $this->data['podkategorie'] = $this->Kategorie->getPodkategorie(0);
      $this->data['title'] = $this->data['words'][30];
      $this->data['description'] = $this->data['words'][30];
      $this->data['keywords'] = $this->data['words'][30];
      $this->data['h1'] = $this->data['words'][30];
      $query = $this->Dbmysql->db->query('SELECT * FROM firma WHERE status <> 0 ORDER BY reg_date DESC LIMIT '.$this->data['itemsPerPage']);
			$this->data['result'] = $query->result();
			$this->data['pocetStranok'] = 0;
      $this->load->view('v1.0/katalog', $this->data);
		}else
		{
			$this->data['aktPage'] = 0;
			if(empty($this->data['aktPage']) or $this->data['aktPage'] < 0 or !is_numeric((int)$this->data['aktPage']))
      {
        $this->data['aktPage'] = 0;
      }

			$od = ($this->data['aktPage'] * $this->data['itemsPerPage']);

			//Nacitame kategoriu
			$this->data['kategoria'] = $this->Kategorie->loadByCool($this->uri->segment(2));

      //Ak bolo najdenych viac ako 0 kategorii
			if(count($this->data['kategoria']) > 0)
			{
				//nacitame podkategorie a nadkategorie
				$this->data['nadkategorie'] = $this->Kategorie->getNadkategorie($this->data['kategoria'][0]['id']);
				$this->data['nadkategorie'] = array_reverse($this->data['nadkategorie']);

        //Title, description, keywords, H1 kategorie
				$this->data['title'] = $this->data['kategoria'][0]['nazov'];
	 	    $this->data['description'] = $this->data['kategoria'][0]['nazov'];

        if(empty($this->data['description']))$this->data['description'] = $this->data['title'];
	 	    else $this->data['keywords'] = str_replace(' ', ',', $this->data['description']);
        $this->data['h1'] = $this->data['kategoria'][0]['nazov'];

        //Vyberieme ID firiem z indexu
	 	    $query = $this->Dbmysql->db->query('SELECT * FROM katalogindex WHERE sekcia = "'.$this->data['kategoria'][0]['id'].'_0"');
	 	    $this->data['kat_ids'] = $query->result();

        if(count($this->data['kat_ids']) > 0)$kat_ids = trim($this->data['kat_ids'][0]->ids);
	 	    else $kat_ids = '';

        if($kat_ids != '')$kat_idsArray = explode(',', $kat_ids);
	 	    else $kat_idsArray = array();
	 	    $posun = 1;
			}

			//Ak je v url nezmysel presmerujem na 404
			if(count($this->data['kategoria']) == 0 and $this->uri->total_segments() > 1)show_404();

			//Vyberieme ids z kategorii
			if(!empty($kat_idsArray))$idsArray = $kat_idsArray;
			else $idsArray = array();

			//Vytvorime retazec finalnych ids
			$idsArray = array_unique($idsArray);$ids = '';
			if(!empty($idsArray))foreach($idsArray as $v)
		  {
		   	if((int)$v > 0)$ids .= $v.',';
		  }
		  $ids = substr(trim($ids), 0, -1);

		   //Nacitanie firiem
			//$this->data['celkPocet'] = count($idsArray);
			if(strlen($ids) > 0)
			{
				//Vyberieme sort
				if(isSet($this->data['s1']) and isSet($this->data['s2']))
				{
					if($this->data['s1'] == 0)$orderBy = 'rating';
					elseif($this->data['s1'] == 1)$orderBy = 'nazev';
					elseif($this->data['s1'] == 2)$orderBy = 'pocet_hodnoceni';
					else show_404();
					if($this->data['s2'] == 1)$order = ' ASC';
					elseif($this->data['s2'] == 0)$order = ' DESC';
					else show_404();
					$orderBy .= $order;
				}else {
					$orderBy = 'rating DESC';
					$order = ' DESC';
				}

				$query = $this->Dbmysql->db->query('SELECT SQL_CALC_FOUND_ROWS(firma.id), firma.* FROM firma WHERE id IN ('.$ids.') AND status <> 0 ORDER BY reg_date '.$order.' LIMIT '.$od.', '.$this->data['itemsPerPage']);
				$this->data['result'] = $query->result();
				$query = $this->Dbmysql->db->query('SELECT FOUND_ROWS() as pocet');
        $this->data['pocet'] = $query->result_array();
        $this->data['celkPocet'] = $this->data['pocet'][0]['pocet'];
			}else {
        $this->data['result'] = array();
        $this->data['celkPocet'] = 0;
      }

		  //Nacitanie podkategorii
		  if(!empty($this->data['kategoria'][0]['id']))$kategoria_id = $this->data['kategoria'][0]['id'];
		  else $kategoria_id = 0;
		  $this->data['podkategorie'] = $this->Kategorie->getPodkategorie($kategoria_id);

			//Strankovanie
			$this->data['start'] = $this->data['aktPage'] - 5;
			if($this->data['start'] < 0)$this->data['start'] = 0;
			$this->data['pocetStranok'] = ceil($this->data['celkPocet'] / $this->data['itemsPerPage']);
			$this->data['do'] = $this->data['itemsPerPage'] + $this->data['start'];
			if($this->data['do'] > $this->data['pocetStranok'])$this->data['do'] = $this->data['pocetStranok'];

			$this->load->view('v1.0/katalog', $this->data);
		}
	}

	function home()
	{
		$this->load->view('kataloghome', $this->data);
	}

	function makeCool()
	{
		$query = $this->Dbmysql->db->query('SELECT * FROM okecStrom3');
		foreach($query->result_array() as $v)
		{
			$this->Dbmysql->db->query('UPDATE okecStrom3 SET cool_name = "'.$this->Functions->MakeCool($v['new_name']).'" WHERE id = '.$v['id']);
		}
	}

	function testy()
	{

	}

	function __destruct()
	{

	}
}
?>

