<?php

class Detail2 extends Controller {

	function Detail2()
	{
		parent::Controller();
		$this->config->load('katalog_config');
		$this->data = $this->config->item('data');
		if($this->data['URI']['pocet_domen'] < 3 or $this->data['domains'][0] != 'firma')
		{
			header('HTTP/1.0 301 Moved Permanently');
			header('Location: http://firma.'.$this->data['domena'].$_SERVER['REQUEST_URI']);
			exit;
		}
    $this->load->model('Dbmysql');
    $this->load->model('Functions');
		$this->load->model('Firma');
		$this->load->model('Lokality');
		$this->load->model('Kategorie');
		$this->load->model('Emaily');
		$this->load->helper('url');
		$this->load->plugin('captcha');
		/*
		if(strlen($this->uri->segment(2)) > 0)
		{
			header('HTTP/1.0 301 Moved Permanently');
			header('Location: http://'.$this->uri->segment(2).'.'.$this->data['domena'].'/'.$this->uri->segment(1).'/');
			exit;
		}else {
			$query = $this->Dbmysql->db->query('SELECT * FROM firma WHERE status <> 0 AND uri = "'.$this->data['data']['domains'][0].'"');
			$this->data['detail'] = $query->result_array();
		}//*/
		//*
		$query = $this->Dbmysql->db->query('SELECT * FROM firma WHERE status <> 0 AND uri = "'.$this->uri->segment(2).'"');
		$this->data['detail'] = $query->result_array();
		//*/
		//$this->data['noAds'] = 0;
	}
	
	function index()
	{
		if(count($this->uri->segment_array()) > 1)$this->firma();
		else header('Location: /');
	}
	
	function firma()
	{
		if(count($this->data['detail']) > 0)
		{
			//Nacitanie lokality
			$query = $this->Dbmysql->db->query('SELECT * FROM lokality WHERE id IN (SELECT lokalita_id FROM firmalokalita WHERE firma_id = '.$this->data['detail'][0]['id'].')');
			$this->data['lokality'] = $query->result_array();
			//Nacitanie kategorie
			$query = $this->Dbmysql->db->query('SELECT * FROM okecStrom3 WHERE id IN (SELECT kategoria_id FROM firmakategoria WHERE firma_id = '.$this->data['detail'][0]['id'].')');
			$this->data['kategorie'] = $query->result_array();
			//Title
			if(!empty($this->data['detail'][0]['title']))$this->data['title'] = $this->data['detail'][0]['title'].' - '.$this->data['detail'][0]['obec'];
			else $this->data['title'] = $this->data['detail'][0]['nazev'].' - '.$this->data['detail'][0]['obec'];
			//Description
			$this->data['description'] = $this->data['detail'][0]['nazev'];
			if(!empty($this->data['detail'][0]['popis']))
			{
				$popis = str_replace('|', ', ', $this->data['detail'][0]['popis']);
				$this->data['description'] .= ' '.substr($popis , 0, 250);
			}else $this->data['description'] .= ' '.$this->data['detail'][0]['title'];
			$this->data['description'] .= ' - '.$this->data['detail'][0]['obec'];
			//Keywords
			$this->data['keywords'] = $this->data['detail'][0]['nazev'];
			//H1
			$this->data['h1'] = $this->data['detail'][0]['nazev'];
			$this->load->view('firma_detail2', $this->data);
		}else show_404();
	}
	
	function hodnoceni()
	{
		if(count($this->data['detail']) > 0)
		{
			//Nacitame hlasovanie
			$query = $this->Dbmysql->db->query('SELECT COUNT(*) as pocet, SUM(hodnoceni) as suma FROM firmahodnoceni WHERE verificated <> 0 AND firma_id = '.$this->data['detail'][0]['id']);
			$this->data['hodnoceni'] = $query->result_array();
			//Title
			if(!empty($this->data['detail'][0]['title']))$this->data['title'] = $this->data['detail'][0]['title'].' - '.$this->data['detail'][0]['obec'];
			else $this->data['title'] = $this->data['detail'][0]['nazev'].' - '.$this->data['detail'][0]['obec'];
			//Description
			$this->data['description'] = $this->data['detail'][0]['nazev'];
			if(!empty($this->data['detail'][0]['popis']))
			{
				$popis = str_replace('|', ', ', $this->data['detail'][0]['popis']);
				$this->data['description'] .= ' '.substr($popis , 0, 250);
			}else $this->data['description'] .= ' '.$this->data['detail'][0]['title'];
			$this->data['description'] .= ' - '.$this->data['detail'][0]['obec'];
			//Keywords
			$this->data['keywords'] = $this->data['detail'][0]['nazev'];
			//H1
			$this->data['h1'] = $this->data['words']['65'].' :: '.$this->data['detail'][0]['nazev'];
			$this->load->view('firma_hodnoceni', $this->data);
		}else show_404();
	}
	
	function diskuse()
	{
		if(count($this->data['detail']) > 0)
		{
			//Nacitame diskusiu
			$query = $this->Dbmysql->db->query('SELECT * FROM diskuze WHERE verificated <> 0 AND firma_id = '.$this->data['detail'][0]['id'].' ORDER BY datum DESC');
			$this->data['diskuse'] = $query->result_array();
			//Title
			if(!empty($this->data['detail'][0]['title']))$this->data['title'] = $this->data['detail'][0]['title'].' - '.$this->data['detail'][0]['obec'];
			else $this->data['title'] = $this->data['detail'][0]['nazev'].' - '.$this->data['detail'][0]['obec'];
			//Description
			$this->data['description'] = $this->data['detail'][0]['nazev'];
			if(!empty($this->data['detail'][0]['popis']))
			{
				$popis = str_replace('|', ', ', $this->data['detail'][0]['popis']);
				$this->data['description'] .= ' '.substr($popis , 0, 250);
			}else $this->data['description'] .= ' '.$this->data['detail'][0]['title'];
			$this->data['description'] .= ' - '.$this->data['detail'][0]['obec'];
			//Keywords
			$this->data['keywords'] = $this->data['detail'][0]['nazev'];
			//H1
			$this->data['h1'] = $this->data['words']['60'].' :: '.$this->data['detail'][0]['nazev'];
			$this->load->view('firma_diskuse', $this->data);
		}else show_404();
	}
	
	function mapa()
	{
		if(count($this->data['detail']) > 0)
		{
			//Title
			if(!empty($this->data['detail'][0]['title']))$this->data['title'] = $this->data['detail'][0]['title'].' - '.$this->data['detail'][0]['obec'];
			else $this->data['title'] = $this->data['detail'][0]['nazev'].' - '.$this->data['detail'][0]['obec'];
			//Description
			$this->data['description'] = $this->data['detail'][0]['nazev'];
			if(!empty($this->data['detail'][0]['obec']) and !empty($this->data['detail'][0]['ulice']))
			{
				$this->data['description'] .= $this->data['detail'][0]['ulice'].' '.$this->data['detail'][0]['obec'];
				$this->data['title'] .= ' :: '.$this->data['detail'][0]['ulice'].' '.$this->data['detail'][0]['obec'];
			}else $this->data['description'] .= ' '.$this->data['detail'][0]['nazev'];
			//Keywords
			$this->data['keywords'] = $this->data['detail'][0]['nazev'];
			//H1
			$this->data['h1'] = $this->data['words']['86'].' :: '.$this->data['detail'][0]['nazev'];
			$this->load->view('firma_mapa', $this->data);
		}else show_404();
	}

	function aktualizovat()
	{
		$vals = array(

					'word'		 => '',

					'img_path'	 => './captcha/',

					'img_url'	 => '/captcha/',

					//'font_path'	 => './system/texb.ttf',
					'img_width'	 => '130',

					'img_height' => '40',
					'strlen'		 => 6,
					'font-size'	 => 55,

					'expiration' => 7200

				);
		if(count($this->data['detail']) > 0)
		{
			//Nacitanie lokality
			$query = $this->Dbmysql->db->query('SELECT * FROM lokality WHERE id IN (SELECT lokalita_id FROM firmalokalita WHERE firma_id = '.$this->data['detail'][0]['id'].')');
			$this->data['lokality'] = $query->result_array();
			//Nacitanie kategorie
			$query = $this->Dbmysql->db->query('SELECT * FROM okecStrom3 WHERE id IN (SELECT kategoria_id FROM firmakategoria WHERE firma_id = '.$this->data['detail'][0]['id'].')');
			$this->data['kategorie'] = $query->result_array();
			//Title
			if(!empty($this->data['detail'][0]['title']))$this->data['title'] = $this->data['detail'][0]['title'].' - '.$this->data['detail'][0]['obec'];
			else $this->data['title'] = $this->data['detail'][0]['nazev'];
			//Description
			$this->data['description'] = $this->data['detail'][0]['nazev'];
			if(!empty($this->data['detail'][0]['popis']))
			{
				$popis = str_replace('|', ', ', $this->data['detail'][0]['popis']);
				$this->data['description'] .= ' '.substr($popis , 0, 250);
			}else $this->data['description'] .= ' '.$this->data['detail'][0]['title'];
			$this->data['description'] .= ' - '.$this->data['detail'][0]['obec'];
			//Keywords
			$this->data['keywords'] = $this->data['detail'][0]['nazev'];
			//H1
			$this->data['h1'] = $this->data['words'][111].' :: '.$this->data['detail'][0]['nazev'];
			$query = $this->Dbmysql->db->query('SELECT * FROM firma WHERE id = '.$this->data['detail'][0]['id']);
			$this->data['user_data'] = $query->result_array();
			$this->data['user_data'] = $this->data['user_data'][0];
		}else show_404();
		if(!empty($_POST))
		{
			$_POST = $this->Functions->htmlarrayspecialchars($_POST);
			$error = array();
			if(empty($_POST['ico']) or strlen(trim($_POST['ico'])) != 8 or !is_numeric(trim($_POST['ico'])))$error['ico'] = $this->data['words'][124];
			if(empty($_POST['nazev']))$error['nazev'] = $this->data['words'][125];
			if(empty($_POST['ulice']))$error['ulice'] = $this->data['words'][126];
			if(empty($_POST['obec']))$error['obec'] = $this->data['words'][127];
			if(empty($_POST['psc']))$error['psc'] = $this->data['words'][128];
			if(empty($_POST['tel']))$error['tel'] = $this->data['words'][129];
			if(empty($_POST['email']))$error['email'] = $this->data['words'][130];
			//if (preg_match('/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[A-Za-z]{2,5}$/', $address))
			//if(empty($_POST['kontaktemail']))$error['kontaktemail'] = $this->data['words'][131];
			if(empty($_POST['word']) or empty($_SESSION['captcha']['word']) or strtolower($_POST['word']) != strtolower($_SESSION['captcha']['word']))$error['captcha'] = $this->data['words'][132];
			$error['kategorie'] = $this->data['words'][133];
			if(!empty($_POST['kategorie1']))unSet($error['kategorie']);
			if(!empty($_POST['kategorie2']))unSet($error['kategorie']);
			if(!empty($_POST['kategorie3']))unSet($error['kategorie']);
			$error['lokality'] = $this->data['words'][134];
			if($_POST['lokality1'] > 0)unSet($error['lokality']);
			if($_POST['lokality2'] > 0)unSet($error['lokality']);
			if(empty($_POST['popis']))$error['popis'] = $this->data['words'][135];
			if(empty($_POST['title']))$error['title'] = $this->data['words'][136];
			if(count($error) > 0)
			{
				$this->data['error'] = $error;
				$this->data['errortext'] = '';
				foreach($error as $v)$this->data['errortext'] .= $v.'<br>';
				$this->data['user_data'] = $_POST;
			}else {
				if($_POST['kategorie1'] > 0)$_POST['kat'][] = $_POST['kategorie1'];
				if($_POST['kategorie2'] > 0)$_POST['kat'][] = $_POST['kategorie2'];
				if($_POST['kategorie3'] > 0)$_POST['kat'][] = $_POST['kategorie3'];
				if($_POST['lokality1'] > 0)$_POST['lok'][] = $_POST['lokality1'];
				if($_POST['lokality2'] > 0)$_POST['lok'][] = $_POST['lokality2'];
				$_POST['firma_id'] = $this->data['detail'][0]['id'];
				$_POST['popis'] = substr($_POST['popis'], 0, 2000);
				$_POST['title'] = substr($_POST['title'], 0, 90);
				$this->Firma->addAktualizace($_POST);
				$this->Emaily->sendmail('podpora@'.$this->data['domena'], 'mario.palka@gmail.com', 'Nova aktualizacia', ' ');
				$this->data['ok'] = true;
			}
		}
		$_SESSION['captcha'] = create_captcha($vals);
		if(count($this->data['lokality']) == 0)$this->data['lokality'][0]['id'] = 0;
		if(count($this->data['lokality']) == 1)$this->data['lokality'][1]['id'] = 0;
		if(!empty($_POST))
		{
			if(empty($_POST['lokality1']))$this->data['lokality'][0]['id'] = 0;
			else $this->data['lokality'][0]['id'] = (int)$_POST['lokality1'];
			if(empty($_POST['lokality2']))$this->data['lokality'][1]['id'] = 0;
			else $this->data['lokality'][1]['id'] = (int)$_POST['lokality2'];
			if(empty($_POST['kategorie1']))$this->data['kategorie1'] = 0;
			if(empty($_POST['kategorie2']))$this->data['kategorie2'] = 0;
			if(empty($_POST['kategorie3']))$this->data['kategorie3'] = 0;
		}
		$this->data['lokality1'] = $this->Lokality->getHTMLoptions(0, $this->data['NULL'] = '', (int)$this->data['lokality'][0]['id']);
		$this->data['lokality2'] = $this->Lokality->getHTMLoptions(0, $this->data['NULL'] = '', (int)$this->data['lokality'][1]['id']);
		//xxx
		/*
    if(count($this->data['kategorie1']) == 0)$this->data['kategorie'][0]['id'] = 0;
		if(count($this->data['kategorie']) == 1)$this->data['kategorie'][1]['id'] = 0;
		if(count($this->data['kategorie']) == 2)$this->data['kategorie'][2]['id'] = 0;
		$this->data['kategorie1'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', (int)$this->data['kategorie'][0]['id']);
		$this->data['kategorie2'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', (int)$this->data['kategorie'][1]['id']);
		$this->data['kategorie3'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', (int)$this->data['kategorie'][2]['id']);
		*/
		if(!empty($this->data['kategorie'][0]))
		{
  		$this->data['user_data']['kategorie1'] = $this->data['kategorie'][0]['id'];
  		$this->data['user_data']['kategorie1name'] = $this->data['kategorie'][0]['new_name'];
		}
		if(!empty($this->data['kategorie'][1]))
		{
  		$this->data['user_data']['kategorie2'] = $this->data['kategorie'][1]['id'];
  		$this->data['user_data']['kategorie2name'] = $this->data['kategorie'][1]['new_name'];
		}
		if(!empty($this->data['kategorie'][2]))
		{
  		$this->data['user_data']['kategorie3'] = $this->data['kategorie'][2]['id'];
  		$this->data['user_data']['kategorie3name'] = $this->data['kategorie'][2]['new_name'];
		}
		$this->load->view('firma_editace', $this->data);
	}
	
	function addmodalkat()
	{
		$polozka = $this->uri->segment(3);
		$form = $this->uri->segment(4);
		echo '<script type="text/javascript" src="/js/functions.js" defer="defer"></script><p><a href="#" onclick="WinOpenerWrite(\''.$form.'\',\''.$polozka.'\',\'0\');WinOpenerWrite(\''.$form.'\',\''.$polozka.'name\',\'\');return false">Resetovat kategorii</a></p>';
		echo $this->Kategorie->katTree(0, $NULL = NULL, 0, $form, $polozka);
	}
}
?>
