<?php

class Admin extends Controller {

	function Admin()
	{
		parent::Controller();
		$this->load->model('Dbmysql');
		$this->load->model('Functions');
		$this->load->model('Kategorie');
		$this->load->model('Lokality');
		$this->load->model('Firma');
		$this->load->helper('url');
		$this->config->load('katalog_config');
		$this->data = $this->config->item('data');
		if(!empty($_POST))$_POST = $this->Functions->htmlarrayspecialchars($_POST);
		if(empty($_SESSION['admin']))
		{
			if($this->uri->segment(2) != "login")
			{
				redirect("admin/login/");
				exit;
			}
      }
	}
	
	function index()
	{
		if(!empty($_POST))$this->actions();
		$query = $this->Dbmysql->db->query('SELECT * FROM firma WHERE status = 0 AND reg_date > 0 ORDER BY reg_date DESC LIMIT 20');
		$this->data['list'] = $query->result_array();
		$this->data['nadpis'] = 'Posledne zadane firmy(nepovolene)';
		$this->load->view('admin/index', $this->data);
	}

	function login()
	{
		if(empty($_SESSION['admin']) and empty($_POST))$this->load->view('admin/login');
		elseif(!empty($_POST))
		{
			if(!empty($_POST['login']) and !empty($_POST['pwd']))
			{
				if($_POST['login'] == 'admin' and $_POST['pwd'] == 'dajheslo')
				{
					$_SESSION['admin'] = 'admin';
					redirect("admin");
				}else sleep(1);
			}
		}else $this->load->view('admin/index');
	}
	
	function logout()
	{
		unset($_SESSION['admin']);
		redirect("admin");
	}
	
	function seznam()
	{
		if(!empty($_POST))$this->actions();
		$query = $this->Dbmysql->db->query('SELECT * FROM firma ORDER BY id DESC LIMIT 10');
		$this->data['list'] = $query->result_array();
		$this->data['nadpis'] = 'Seznam firem';
		$this->load->view('admin/seznam', $this->data);
	}
	
	function actions()
	{
		if(!empty($_POST['vyber']) and count($_POST['vyber']) > 0)
		{
			if($_POST['action'] == 1)
			{
				foreach($_POST['vyber'] as $v)
				{
					if($v > 0)$this->Dbmysql->db->query('UPDATE firma SET status = 1 WHERE id = '.$v);
				}
			}
			if($_POST['action'] == 2)
			{
				foreach($_POST['vyber'] as $v)
				{
					if($v > 0)$this->Dbmysql->db->query('UPDATE firma SET status = 0 WHERE id = '.$v);
				}
			}
			if($_POST['action'] == 3)
			{
				foreach($_POST['vyber'] as $v)
				{
					if($v > 0)$this->Dbmysql->db->query('DELETE FROM firma WHERE id = '.$v);
				}
			}
		}
	}
	
	function detail()
	{
		$id = $this->uri->segment(3);
		if(!empty($id) and $id > 0)
		{
			//Nacitanie lokalit
			$query = $this->Dbmysql->db->query('SELECT * FROM lokality WHERE id IN (SELECT lokalita_id FROM firmalokalita WHERE firma_id = '.$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 = '.$id.')');
			$this->data['kategorie'] = $query->result_array();
		
		if(empty($_POST))
		{
			if(empty($this->data['lokality'][0]['id']))$this->data['lokality'][0]['id'] = 0;
			if(empty($this->data['lokality'][1]['id']))$this->data['lokality'][1]['id'] = 0;
			$this->data['lokality1'] = $this->Lokality->getHTMLoptions(0, $this->data['NULL'] = '', $this->data['lokality'][0]['id']);
			$this->data['lokality2'] = $this->Lokality->getHTMLoptions(0, $this->data['NULL'] = '', $this->data['lokality'][1]['id']);
			//Nacitanie kategorie
			if(empty($this->data['kategorie'][0]['id']))$this->data['kategorie'][0]['id'] = 0;
			if(empty($this->data['kategorie'][1]['id']))$this->data['kategorie'][1]['id'] = 0;
			if(empty($this->data['kategorie'][2]['id']))$this->data['kategorie'][2]['id'] = 0;
			$this->data['kategorie1'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', $this->data['kategorie'][0]['id']);
			$this->data['kategorie2'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', $this->data['kategorie'][1]['id']);
			$this->data['kategorie3'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', $this->data['kategorie'][2]['id']);
		}else {
			$error = array();
			/*if(empty($_POST['nazev']))$error['nazev'] = true;
			if(empty($_POST['ulice']))$error['ulice'] = true;
			if(empty($_POST['obec']))$error['obec'] = true;
			if(empty($_POST['psc']))$error['psc'] = true;
			if(empty($_POST['tel']))$error['tel'] = true;
			if(empty($_POST['email']))$error['email'] = true;*/
			$error['kategorie'] = true;
			if($_POST['kategorie1'] > 0)unSet($error['kategorie']);
			if($_POST['kategorie2'] > 0)unSet($error['kategorie']);
			if($_POST['kategorie3'] > 0)unSet($error['kategorie']);
			$error['lokality'] = true;
			if($_POST['lokality1'] > 0)unSet($error['lokality']);
			if($_POST['lokality2'] > 0)unSet($error['lokality']);
			if(empty($_POST['popis']))$error['popis'] = true;
			if(empty($_POST['title']))$error['title'] = true;
			//Nacitanie lokality
			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'];
			$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']);
			//Nacitanie kategorie
			if(empty($_POST['kategorie1']))$this->data['kategorie'][0]['id'] = 0;
			else $this->data['kategorie'][0]['id'] = (int)$_POST['kategorie1'];
			if(empty($_POST['kategorie2']))$this->data['kategorie'][1]['id'] = 0;
			else $this->data['kategorie'][1]['id'] = (int)$_POST['kategorie2'];
			if(empty($_POST['kategorie3']))$this->data['kategorie'][2]['id'] = 0;
			else $this->data['kategorie'][2]['id'] = (int)$_POST['kategorie3'];
			$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(count($error) > 0)
			{
				$this->data['error'] = $error;
				$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['id'] = $id;
				if(!empty($_POST['status']))$_POST['status'] = 1;
				$this->Firma->edit($_POST);
			}
		}
			$query = $this->Dbmysql->db->query('SELECT * FROM firma WHERE id = '.$id);
			$this->data['user_data'] = $query->result_array();
			$this->data['user_data'] = $this->data['user_data'][0];
		}
		$this->load->view('admin/detail', $this->data);
	}
	
	function kategorie()
	{
		$id = (int)$this->uri->segment(3);
		//if(!empty($_POST)){print_r($_POST);exit;}
		if(!empty($_POST) and empty($_POST['nova']))foreach($_POST['kat'] as $k => $v)
		{
			$this->Dbmysql->db->query('UPDATE okecStrom3 SET new_name = "'.$v['new_name'].'",
				title = "'.$v['title'].'",
				description = "'.$v['description'].'",
				parent_id = '.$v['parent_id'].',
				cool_name = "'.$this->Functions->MakeCool($v['new_name']).'",
				name = "'.$v['name'].'" WHERE id = '.$k);
		}
		if(!empty($_POST) and !empty($_POST['nova']))
		{
			$this->Dbmysql->db->query('INSERT INTO okecStrom3 SET new_name = "'.$_POST['new_name'].'",
				title = "'.$_POST['title'].'",
				description = "'.$_POST['description'].'",
				parent_id = '.$_POST['parent_id'].',
				cool_name = "'.$this->Functions->MakeCool($_POST['new_name']).'",
				name = "'.$_POST['name'].'"');
		}
		$this->data['allkategorie'] = $this->Kategorie->getAllKategorie();
		$this->data['kategorie'] = $this->Kategorie->getPodkategorie($id);
		$this->data['kategoria'] = $this->Kategorie->loadById($id);
		$this->load->view('admin/kategorie', $this->data);
	}
	
	function deletekat()
	{
		$id = (int)$this->uri->segment(3);
		if($id > 0)$this->Dbmysql->db->query('DELETE FROM okecStrom3 WHERE id = '.$id);
		$this->kategorie();
	}
	
	function lokality()
	{
		$id = (int)$this->uri->segment(3);
		if(!empty($_POST))foreach($_POST['lok'] as $k => $v)
		{
			$this->Dbmysql->db->query('UPDATE lokality SET lokalita = "'.$v['lokalita'].'" WHERE id = '.$k);
		}
		$this->data['alllokality'] = $this->Lokality->getAllPodlokality();
		$this->data['lokality'] = $this->Lokality->getPodlokality($id);
		$this->data['lokalita'] = $this->Lokality->loadById($id);
		$this->load->view('admin/lokality', $this->data);
	}
	
	function aktualizace()
	{
		if(!empty($_POST))$this->aktualizaceactions();
		$query = $this->Dbmysql->db->query('SELECT * FROM firmaAktualizace ORDER BY id DESC LIMIT 30');
		$this->data['list'] = $query->result_array();
		$this->data['nadpis'] = 'Aktualizace';
		$this->load->view('admin/aktualizace', $this->data);
	}
	
	function aktualizaceactions()
	{
		if(!empty($_POST['vyber']) and count($_POST['vyber']) > 0)
		{
			if($_POST['action'] == 3)
			{
				if(!empty($_POST['vyber']))foreach($_POST['vyber'] as $v)
				{
					if($v > 0)$this->Dbmysql->db->query('DELETE FROM firmaAktualizace WHERE id = '.$v);
				}
			}
		}
		if(!empty($_POST['makechanges']) and !empty($_POST['id']) and !empty($_POST['firma_id']))
		{
			$q = 'UPDATE firma SET ';
			$i = 0;
			foreach($_POST as $k => $v)
			{
				if(strpos($k, '_ok') !== false)
				{
					$q .= substr($k, 0, -3).' = "'.$_POST[substr($k, 0, -3)].'",';
					$i++;
				}
			}
			$q .= 'aktualizace = '.time();
			$q .= ' WHERE id = '.$_POST['firma_id'];
			if($i > 0)$this->Dbmysql->db->query($q);
			if(!empty($_POST['lokalita']) and count($_POST['lokality']) > 0)$this->Firma->addFirmaLok($_POST['firma_id'], $_POST['lokality']);
			if(!empty($_POST['kategoria']) and count($_POST['kategorie']) > 0)$this->Firma->addFirmaKat($_POST['firma_id'], $_POST['kategorie']);
			if(!empty($_POST['deleteakt']))
			{
				$this->Dbmysql->db->query('DELETE FROM firmaAktualizace WHERE id = '.(int)$_POST['id']);
				redirect('admin/aktualizace');
			}
		}
	}
	
	function aktualizuj()
	{
		if(!empty($_POST))$this->aktualizaceactions();
		$this->data['nadpis'] = 'Aktualizuj';
		$id = (int)$this->uri->segment(3);
		if(!empty($id))
		{
			$query = $this->Dbmysql->db->query('SELECT * FROM firmaAktualizace WHERE id = '.$id);
			$this->data['akt'] = $query->result_array();
			if(count($this->data['akt']) > 0)
			{
				$query = $this->Dbmysql->db->query('SELECT * FROM firma WHERE id = '.$this->data['akt'][0]['firma_id']);
				$this->data['firma'] = $query->result_array();
				if(count($this->data['firma']) > 0)
				{
					//naloadujeme kategorie
					$query = $this->Dbmysql->db->query('SELECT * FROM lokality WHERE id IN (SELECT lokalita_id FROM firmalokalitaAkt WHERE id = '.$id.')');
					$this->data['akt']['lokality'] = $query->result_array();
					if(!empty($this->data['akt']['lokality'][0]['id']))$this->data['akt']['lokality1'] = $this->Lokality->getHTMLoptions(0, $this->data['NULL'] = '', (int)$this->data['akt']['lokality'][0]['id']);
					else $this->data['akt']['lokality1'] = $this->Lokality->getHTMLoptions(0, $this->data['NULL'] = '', 0);
					if(!empty($this->data['akt']['lokality'][1]['id']))$this->data['akt']['lokality2'] = $this->Lokality->getHTMLoptions(0, $this->data['NULL'] = '', (int)$this->data['akt']['lokality'][1]['id']);
					else $this->data['akt']['lokality2'] = $this->Lokality->getHTMLoptions(0, $this->data['NULL'] = '', 0);
					$query = $this->Dbmysql->db->query('SELECT * FROM lokality WHERE id IN (SELECT lokalita_id FROM firmalokalita WHERE firma_id = '.$this->data['firma'][0]['id'].')');
					$this->data['firma']['lokality'] = $query->result_array();
					//naloadujeme lokality
					$query = $this->Dbmysql->db->query('SELECT * FROM okecStrom3 WHERE id IN (SELECT kategoria_id FROM firmakategoriaAkt WHERE id = '.$id.')');
					$this->data['akt']['kategorie'] = $query->result_array();
					if(!empty($this->data['akt']['kategorie'][0]['id']))$this->data['akt']['kategorie1'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', (int)$this->data['akt']['kategorie'][0]['id']);
					else $this->data['akt']['kategorie1'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', 0);
					if(!empty($this->data['akt']['kategorie'][1]['id']))$this->data['akt']['kategorie2'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', (int)$this->data['akt']['kategorie'][1]['id']);
					else $this->data['akt']['kategorie2'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', 0);
					if(!empty($this->data['akt']['kategorie'][2]['id']))$this->data['akt']['kategorie3'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', (int)$this->data['akt']['kategorie'][2]['id']);
					else $this->data['akt']['kategorie3'] = $this->Kategorie->getHTMLoptions(0, $this->data['NULL'] = '', 0);
					$query = $this->Dbmysql->db->query('SELECT * FROM okecStrom3 WHERE id IN (SELECT kategoria_id FROM firmakategoria WHERE firma_id = '.$this->data['firma'][0]['id'].')');
					$this->data['firma']['kategorie'] = $query->result_array();
					$this->load->view('admin/aktualizuj', $this->data);
				}
			}
		}
	}
}
?>
