<?php

class Registrace extends Controller {

	function Registrace()
	{
		parent::Controller();
		$this->config->load('katalog_config');
		$this->data = $this->config->item('data');
		$this->load->helper('url');
		$this->load->model('Functions');
		$this->load->model('Dbmysql');
		$this->load->model('Kategorie');
    $this->load->model('Lokality');
    $this->load->model('Emaily');
    $this->load->model('Firma');
		$this->load->plugin('captcha');
		$this->data['title'] = $this->data['words'][33];
		$this->data['description'] = $this->data['words'][33];
		$this->data['keywords'] = $this->data['words'][33];
		$this->data['h1'] = $this->data['words'][33];
		$_POST = $this->Functions->htmlarrayspecialchars($_POST);
	}

	function index()
	{	
		if(empty($_POST))
		{
			$_SESSION['captcha'] = create_captcha($this->data['vals']);
			$this->load->view('registrace', $this->data);
		}
		else {
			$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($_POST['kategorie1'] > 0)unSet($error['kategorie']);
			if($_POST['kategorie2'] > 0)unSet($error['kategorie']);
			if($_POST['kategorie3'] > 0)unSet($error['kategorie']);
			if(empty($_POST['popis']))$error['popis'] = $this->data['words'][135];
			if(empty($_POST['title']))$error['title'] = $this->data['words'][136];
			if(count($error) > 0)
			{
				$_SESSION['captcha'] = create_captcha($this->data['vals']);
				$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'];
				$_POST['popis'] = substr($_POST['popis'], 0, 2000);
				$_POST['title'] = substr($_POST['title'], 0, 90);
				$this->Firma->add($_POST);
				$this->Emaily->sendmail('podpora@'.$this->data['domena'], 'mario.palka@gmail.com', 'Nova registracia', ' ');
				$this->data['ok'] = true;
        unSet($_POST);
			}
		}
    $this->load->view('v1.0/registrace', $this->data);
	}
	
	function addmodalkat()
	{
		$polozka = $this->uri->segment(3);
		echo '<script type="text/javascript" src="/js/functions.js" defer="defer"></script><p><a href="#" onclick="WinOpenerWrite(\'registrace\',\''.$polozka.'\',\'0\');WinOpenerWrite(\'registrace\',\''.$polozka.'name\',\'\');return false">Resetovat kategorii</a></p>';
		echo $this->Kategorie->katTree(0, $NULL = NULL, 0, 'registrace', $polozka);
	}
	
}
?>
