<?php

class Spravovat extends Controller {

	function Spravovat()
	{
		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('Emaily');
		$this->load->model('Firma');
		$this->load->plugin('captcha');
		$this->data['title'] = $this->data['words'][33];
		$this->data['description'] = $this->data['words'][34];
		$this->data['keywords'] = $this->data['words'][33];
		$this->data['h1'] = $this->data['words'][34];
		if($this->data['URI']['pocet_domen']<3 or $this->data['domains'][0] != 'www')
		{
			header('HTTP/1.0 301 Moved Permanently');
			header('Location:'.substr(base_url(),0,-1).$_SERVER['REQUEST_URI']);
		}
		$this->data['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

				);
	}

	function index()
	{
		
	}
	
	function editovat()
	{
		$id = $this->uri->segment(3);
		if(empty($id) or !is_numeric($id))redirect();
		else {
			$this->data['user_data'] = $this->Firma->load($id);
			$this->load->view('editace', $this->data);
		}
	}
	
	function novy_prispevek()
	{
		$id = $this->uri->segment(3);
		if(!empty($id))
		{
			$this->data['firma'] = $this->Firma->load($id);
			if($this->data['firma']['id'] > 0)
			{
				if(!empty($_POST))
				{
					$_POST = $this->Functions->htmlarrayspecialchars($_POST);
					$error = array();
					if(empty($_POST['email']))$error['email'] = true;
					if(empty($_POST['text']))$error['text'] = true;
					if(empty($_POST['word']) or empty($_SESSION['captcha']['word']) or strtolower($_POST['word']) != strtolower($_SESSION['captcha']['word']))$error['captcha'] = true;
					if(count($error) > 0)
					{
						$this->data['error'] = $error;
						$this->data['user_data'] = $_POST;
					}else {
						$this->data['ok'] = true;
						$_POST['firma_id'] = $id;
						$this->Firma->addPrispevek($_POST);
					}
				}
				$_SESSION['captcha'] = create_captcha($this->data['vals']);
				$this->data['title'] = $this->data['words'][62].' - '.$this->data['firma']['nazev'];
				$this->data['description'] = $this->data['words'][62];
				$this->data['keywords'] = $this->data['words'][62];
				$this->data['h1'] = $this->data['words'][62].' :: '.$this->data['firma']['nazev'];
				$this->load->view('spravovat_novy_prispevek', $this->data);
			}else show_404();
		}else show_404();
	}
	
	function hodnoceni()
	{
		$id = $this->uri->segment(3);
		if(!empty($id))
		{
			$this->data['firma'] = $this->Firma->load($id);
			if($this->data['firma']['id'] > 0)
			{
				if(!empty($_POST))
				{
					$_POST = $this->Functions->htmlarrayspecialchars($_POST);
					$error = array();
					if(empty($_POST['email']))$error['email'] = true;
					if(empty($_POST['hodnoceni']) and ($_POST['hodnoceni'] > 0 and $_POST['hodnoceni'] <= 10))$error['hodnoceni'] = true;
					if(empty($_POST['word']) or empty($_SESSION['captcha']['word']) or strtolower($_POST['word']) != strtolower($_SESSION['captcha']['word']))$error['captcha'] = true;
					if(count($error) > 0)
					{
						$this->data['error'] = $error;
						$this->data['user_data'] = $_POST;
					}else {
						$_POST['firma_id'] = $id;
						$this->data['ok'] = true;
						$this->data['stav'] = $this->Firma->addHodnoceni($_POST);
					}
				}
				$_SESSION['captcha'] = create_captcha($this->data['vals']);
				$this->data['title'] = $this->data['words'][65];
				$this->data['description'] = $this->data['words'][65];
				$this->data['keywords'] = $this->data['words'][65];
				$this->data['h1'] = $this->data['words'][65].' :: '.$this->data['firma']['nazev'];
				$this->load->view('spravovat_hodnoceni', $this->data);
			}else show_404();
		}else show_404();
	}
}
?>
