<?php
class Kategorie extends Dbmysql {

public $conn = null;
public $tabulka = 'kategorie';
public $id = 'id';
public $parent_id = 'parent_id';
public $nazov = 'nazov';
public $cool = 'cool';

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

	function loadById($id = '')
	{
		$query = $this->Dbmysql->db->query('SELECT * FROM '.$this->tabulka.' WHERE '.$this->id.' = '.$id);
		$locs = $query->result_array();
		if(count($locs) > 0)return $locs;
		else return false;
	}
	
	function getPodkategorie($id = 0)
	{
		$query = $this->Dbmysql->db->query('SELECT * FROM '.$this->tabulka.' WHERE '.$this->parent_id.' = '.$id.' ORDER BY '.$this->cool.' ASC');
		return $query->result_array();
	}
	
	function getAllPodkategorie($id = 0, &$res = array())
	{
		$query = $this->Dbmysql->db->query('SELECT * FROM '.$this->tabulka.' WHERE '.$this->parent_id.' = '.$id.' ORDER BY '.$this->nazov);
		$tmp = $query->result_array();
		if(count($tmp)>0)
		{
			foreach($tmp as $v)
			{
				$res[] = $v;
				$tmpRes = $this->getAllPodkategorie($v['id'], $res);
			}
		}
		return $res;
	}
	
	function getAllKategorie($id = 0, &$res = array(), $level = 0)
	{
		$query = $this->Dbmysql->db->query('SELECT * FROM '.$this->tabulka.' WHERE '.$this->parent_id.' = '.$id.' ORDER BY '.$this->nazov);
		$tmp = $query->result_array();
		if(count($tmp)>0)
		{
			foreach($tmp as $v)
			{
				$res[$v['id']] = $v;
				$res[$v['id']]['level'] = $level;
				$tmpRes = $this->getAllKategorie($v['id'], $res, $level +1);
			}
		}
		return $res;
	}
	
	function getNadkategorie($id = 0, &$res = array())
	{
		$query = $this->Dbmysql->db->query('SELECT * FROM '.$this->tabulka.' WHERE '.$this->id.' = '.$id);
		$tmp = $query->result_array();
		if(count($tmp)>0)
		{
			foreach($tmp as $v)
			{
				$res[] = $v;
				$tmpRes = $this->getNadkategorie($v['parent_id'], $res);
			}
		}
		return $res;
	}

	function getAllPodkategorieIds($id = 0, &$res = array())
	{
		$query = $this->Dbmysql->db->query('SELECT '.$this->id.' FROM '.$this->tabulka.' WHERE '.$this->parent_id.' = '.$id);
		$tmp = $query->result_array();
		if(count($tmp)>0)
		{
			foreach($tmp as $v)
			{
				$res[] = $v['id'];
				$tmpRes = $this->getPodkategorie($v['id'], $res);
			}
		}
		return $res;
	}
	
	function getCountPodkategorii($id = 0)
	{
		$query = $this->Dbmysql->db->query('SELECT COUNT(id) as pocet FROM '.$this->tabulka.' WHERE '.$this->parent_id.' = '.$id);
		$count = $query->result_array();
		return $count[0]['pocet'];
	}
	
	function getHTMLoptions($id = 0, &$res = '', $selected = 0)
	{
		$kategorie = $this->getPodkategorie($id);
		foreach($kategorie as $v)
		{
			$pocPod = $this->getCountPodkategorii($v['id']);
			if($pocPod == 0)
			{
				$res .= '<option style="padding-left: 20px;" value="'.$v['id'].'"';
				if($v['id'] == $selected)$res .= ' selected="selected"';
				$res .= '>'.$v['new_name'].'</option>';
			}
			else {
				$res .= '<optgroup ';
				if($v['parent_id'] != 0)$res .= 'style="padding-left: 20px;"';
				/*else $res .= 'style="padding-left: 20px; font-weight: normal;"';*/
				$res .= ' label="'.$v['new_name'].'">';
			}
			$this->getHTMLoptions($v['id'], $res, $selected);
			if($pocPod > 0)$res .= '</optgroup>';
		}
		return $res;
	}
	
	function getAdminHTMLoptions($id = 0, &$res = '', $level = 0)
	{
		$kategorie = $this->getPodkategorie($id);
		foreach($kategorie as $v)
		{
			$pocPod = $this->getCountPodkategorii($v['id']);
			$res .= '<option style="padding-left: '.($level*2).'0px;" value="'.$v['id'].'">'.$v['new_name'].'</option>';
			$this->getAdminHTMLoptions($v['id'], $res, $level + 1);
		}
		return $res;
	}
	
	function katTree($parent = 0, &$res = '', $level = 0, $form = 'form', $polozka = 'kategorie1')
	{
		//if($parent == 780 or $parent == 0)$limit = 'LIMIT 1';
		$limit = '';
		$query = $this->Dbmysql->db->query('SELECT * FROM '.$this->tabulka.' WHERE '.$this->parent_id.' = '.$parent.' ORDER BY '.$this->nazov.' '.$limit);
		$kats = $query->result_array();
		if($level == 0)$res .= '<div id="'.$parent.'" style="display: block;">';
		else $res .= '<div id="'.$parent.'" style="display: none;">';
		foreach($kats as $k => $v)
		{
			for($i = 0; $i < $level * 6; $i++)$res .= '&nbsp;';
			$query = $this->Dbmysql->db->query('SELECT COUNT(*) as podkats FROM '.$this->tabulka.' WHERE parent_id = '.$v['id']);
			$pocet = $query->result_array();
			if($pocet[0]['podkats'] == 0)
			{
				$res .= '-';
				$style = 'color: #000000';
			}
			else {
				$res .= '+';
				$style = 'color: #777777';
			}
			$res .= '&nbsp;<a href="#" id="link'.$v['id'].'" style="'.$style.'" onclick="';
			if($pocet[0]['podkats'] > 0)$res .= 'showdiv('.$v['id'].')';
			else $res .= 'javascript:WinOpenerWrite(\''.$form.'\',\''.$polozka.'\',\''.$v['id'].'\');WinOpenerWrite(\''.$form.'\',\''.$polozka.'name\',\''.$v['new_name'].'\');return false';
			$res .= '">'.$v['new_name'].'('.$pocet[0]['podkats'].')</a><br>';
			$this->katTree($v['id'], $res, $level + 1, $form, $polozka);
		}
		$res .= '</div>';
		return $res;	
	}
	
}
?>
