<?php
class Firma extends Dbmysql {

public $conn = null;

	function __construct() {
		parent::Model();
	}

	function load($id = 0)
	{
		if($id > 0)
		{
			$query = $this->Dbmysql->db->query('SELECT * FROM firma WHERE id = '.$id);
			$firma = $query->result_array();
			if(count($firma) > 0)return $firma[0];
			else return false;
		}else return false;
	}

	function add($data)
	{
		$query = $this->Dbmysql->db->query('SELECT COUNT(*) as pocet FROM firma WHERE uri = "'.$this->Functions->MakeCool(@$data['nazev']).'"');
		$r1 = $query->result_array();
		//Checkneme jedinecnost uri
		if($r1[0]['pocet'] > 0)$uricool = $this->Functions->MakeCool(@$data['nazev']).'-'.$r1[0]['pocet'];
		else $uricool = $this->Functions->MakeCool(@$data['nazev']);
		$key = $this->Functions->nahodny_text(36);
		$query = 'INSERT INTO firma SET nazev = "'.$this->Dbmysql->escape_string(@$data['nazev']).'",
			ico = "'.$this->Dbmysql->escape_string(@$data['ico']).'",
			dic = "'.$this->Dbmysql->escape_string(@$data['dic']).'",
			obec = "'.$this->Dbmysql->escape_string(@$data['obec']).'",
			ulice = "'.$this->Dbmysql->escape_string(@$data['ulice']).'",
			psc = "'.$this->Dbmysql->escape_string(@$data['psc']).'",
			tel = "'.$this->Dbmysql->escape_string(@$data['tel']).'",
			fax = "'.$this->Dbmysql->escape_string(@$data['fax']).'",
			email = "'.$this->Dbmysql->escape_string(@$data['email']).'",
			kontaktemail = "'.$this->Dbmysql->escape_string(@$data['kontaktemail']).'",
			www = "'.$this->Dbmysql->escape_string(@$data['www']).'",
			uri = "'.$uricool.'",
			ulice_post = "'.$this->Dbmysql->escape_string(@$data['ulice_post']).'",
			posta = "'.$this->Dbmysql->escape_string(@$data['posta']).'",
			popis = "'.$this->Dbmysql->escape_string(@$data['popis']).'",
			title = "'.$this->Dbmysql->escape_string(@$data['title']).'",
			verify_key = "'.$key.'",
			verificated = 0,
			status = 0,
			reg_date = '.time().',
			aktualizace = '.time().'
			';//exit;
		$this->Dbmysql->db->query($query);
		$id = $this->Dbmysql->db->insert_id();
		$this->addFirmaKat($id, $data['kat']);
		$this->addFirmaLok($id, $data['lok']);
		//$this->Emaily->verify_registrace($id, $key, $data['email']);
	}
	
	function edit($data)
	{
		$query = 'UPDATE firma SET nazev = "'.$this->Dbmysql->escape_string(@$data['nazev']).'",
			ico = "'.$this->Dbmysql->escape_string(@$data['ico']).'",
			dic = "'.$this->Dbmysql->escape_string(@$data['dic']).'",
			obec = "'.$this->Dbmysql->escape_string(@$data['obec']).'",
			ulice = "'.$this->Dbmysql->escape_string(@$data['ulice']).'",
			psc = "'.$this->Dbmysql->escape_string(@$data['psc']).'",
			tel = "'.$this->Dbmysql->escape_string(@$data['tel']).'",
			fax = "'.$this->Dbmysql->escape_string(@$data['fax']).'",
			email = "'.$this->Dbmysql->escape_string(@$data['email']).'",
			www = "'.$this->Dbmysql->escape_string(@$data['www']).'",
			ulice_post = "'.$this->Dbmysql->escape_string(@$data['ulice_post']).'",
			posta = "'.$this->Dbmysql->escape_string(@$data['posta']).'",
			popis = "'.$this->Dbmysql->escape_string(@$data['popis']).'",
			title = "'.$this->Dbmysql->escape_string(@$data['title']).'",
			status = '.(int)@$data['status'].',
			aktualizace = '.time().' WHERE id = '.$data['id'].'
			';//exit;
		$this->Dbmysql->db->query($query);
		$this->addFirmaKat($data['id'], $data['kat']);
		$this->addFirmaLok($data['id'], $data['lok']);
	}
	
	function addAktualizace($data)
	{
		$query = 'INSERT INTO firmaAktualizace SET nazev = "'.$this->Dbmysql->escape_string(@$data['nazev']).'",
			ico = "'.$this->Dbmysql->escape_string(@$data['ico']).'",
			dic = "'.$this->Dbmysql->escape_string(@$data['dic']).'",
			obec = "'.$this->Dbmysql->escape_string(@$data['obec']).'",
			ulice = "'.$this->Dbmysql->escape_string(@$data['ulice']).'",
			psc = "'.$this->Dbmysql->escape_string(@$data['psc']).'",
			tel = "'.$this->Dbmysql->escape_string(@$data['tel']).'",
			fax = "'.$this->Dbmysql->escape_string(@$data['fax']).'",
			email = "'.$this->Dbmysql->escape_string(@$data['email']).'",
			kontaktemail = "'.$this->Dbmysql->escape_string(@$data['kontaktemail']).'",
			www = "'.$this->Dbmysql->escape_string(@$data['www']).'",
			ulice_post = "'.$this->Dbmysql->escape_string(@$data['ulice_post']).'",
			posta = "'.$this->Dbmysql->escape_string(@$data['posta']).'",
			popis = "'.$this->Dbmysql->escape_string(@$data['popis']).'",
			title = "'.$this->Dbmysql->escape_string(@$data['title']).'",
			firma_id = '.$data['firma_id'].',
			reg_date = '.time().',
			aktualizace = '.time().'
			';
		$this->Dbmysql->db->query($query);
		$id = $this->Dbmysql->db->insert_id();
		$this->addFirmaAktKat($id, $data['kat']);
		$this->addFirmaAktLok($id, $data['lok']);
		//$this->Emaily->verify_registrace($id, $key, $data['email']);
	}
	
	function delete($id)
	{
		$this->delFirmaLok($id);
		$this->delFirmaKat($id);
		$this->Dbmysql->db->query('DELETE FROM firma WHERE id = '.$id);
	}
	
	function addFirmaKat($firma_id = 0, $kats = array())
	{
		if($firma_id > 0 and count($kats) > 0)
		{
			$this->delFirmaKat($firma_id);
			foreach($kats as $v)
			{
				if($v > 0)
				{
					$this->Dbmysql->db->query('INSERT INTO firmakategoria SET firma_id = '.$firma_id.', kategoria_id = '.$v);
				}
			}
		}
	}
	
	function addFirmaLok($firma_id = 0, $loks = array())
	{
		if($firma_id > 0 and count($loks) > 0)
		{
			$this->delFirmaLok($firma_id);
			foreach($loks as $v)
			{
				if($v > 0)
				{
					$this->Dbmysql->db->query('INSERT INTO firmalokalita SET firma_id = '.$firma_id.', lokalita_id = '.$v);
				}
			}
		}
	}
	
	function delFirmaKat($firma_id = 0)
	{
		if($firma_id > 0)
		{
			$this->Dbmysql->db->query('DELETE FROM firmakategoria WHERE firma_id = '.$firma_id);
		}
	}
	
	function delFirmaLok($firma_id = 0)
	{
		if($firma_id > 0)
		{
			$this->Dbmysql->db->query('DELETE FROM firmalokalita WHERE firma_id = '.$firma_id);
		}
	}
	
	function addFirmaAktKat($id = 0, $kats = array())
	{
		if($id > 0 and count($kats) > 0)
		{
			$this->delFirmaAktKat($id);
			foreach($kats as $v)
			{
				if($v > 0)
				{
					$this->Dbmysql->db->query('INSERT INTO firmakategoriaAkt SET id = '.$id.', kategoria_id = '.$v);
				}
			}
		}
	}
	
	function addFirmaAktLok($id = 0, $loks = array())
	{
		if($id > 0 and count($loks) > 0)
		{
			$this->delFirmaAktLok($id);
			foreach($loks as $v)
			{
				if($v > 0)
				{
					$this->Dbmysql->db->query('INSERT INTO firmalokalitaAkt SET id = '.$id.', lokalita_id = '.$v);
				}
			}
		}
	}
	
	function delFirmaAktKat($id = 0)
	{
		if($id > 0)
		{
			$this->Dbmysql->db->query('DELETE FROM firmakategoriaAkt WHERE id = '.$id);
		}
	}
	
	function delFirmaAktLok($id = 0)
	{
		if($id > 0)
		{
			$this->Dbmysql->db->query('DELETE FROM firmalokalitaAkt WHERE id = '.$id);
		}
	}
	
	function addPrispevek($data)
	{
		$key = $this->Functions->nahodny_text(36);
		$query = 'INSERT INTO diskuze SET 
			firma_id = '.(int)$data['firma_id'].',
			autor = "'.$this->Dbmysql->escape_string(@$data['autor']).'",
			email = "'.$this->Dbmysql->escape_string(@$data['email']).'",
			text = "'.$this->Dbmysql->escape_string(@$data['text']).'",
			datum = '.time().',
			verificated = 0,
			verify_key = "'.$key.'"';
		$this->Dbmysql->db->query($query);
		$id = $this->Dbmysql->db->insert_id();
		$this->Emaily->verify_prispevok($id, $key, $data['email']);
	}
	
	function addHodnoceni($data)
	{
		$query = $this->Dbmysql->db->query('SELECT COUNT(*) as pocet FROM firmahodnoceni WHERE firma_id = '.$data['firma_id'].' AND email = "'.$this->Dbmysql->escape_string(@$data['email']).'"');
		$hodnotil = $query->result_array();
		if($hodnotil[0]['pocet'] == 0)
		{
			$key = $this->Functions->nahodny_text(36);
			$query = 'INSERT INTO firmahodnoceni SET 
				firma_id = '.(int)$data['firma_id'].',
				hodnoceni = "'.$this->Dbmysql->escape_string(@$data['hodnoceni']).'",
				email = "'.$this->Dbmysql->escape_string(@$data['email']).'",
				datum = '.time().',
				verificated = 0,
				verify_key = "'.$key.'"';
			$this->Dbmysql->db->query($query);
			$id = $this->Dbmysql->db->insert_id();
			$this->Emaily->verify_hodnoceni($id, $key, $data['email']);
			return true;
		}else return false;
	}
	
}
?>