<?php
class Mdownloads extends Model
{
	var $downloads_dir = 'public/downloads/';
	var $downloads_dir2 = 'public/downloads/';
	var $downloads_dir_url = false;
	
	var $categories = array(
		'drivers'=>array('hun'=>'Driverek','eng'=>'Drivers','cr'=>'Drivers','jap'=>'ドライバー','gr'=>'Drivers'),
		'updates'=>array('hun'=>'Frissítések','eng'=>'Updates','cr'=>'Updates','jap'=>'アップデート','gr'=>'Updates'),
		'support'=>array('hun'=>'Segítség','eng'=>'Support','cr'=>'Support','jap'=>'サポート','gr'=>'Support'),
		'workshop'=>array('hun'=>'Workshop','eng'=>'Workshop','cr'=>'Workshop','jap'=>'Workshop','gr'=>'Workshop'),
		'misc'=>array('hun'=>'Egyéb','eng'=>'Other','cr'=>'Other','jap'=>'Other','gr'=>'Other'),
	);
	
	var $types = array
	(
		'misc'=>array('hun'=>'Egyéb fájltípus', 'eng'=>'Generic file','cr'=>'Generic file', 'gr'=>'Generic file',  'jap'=>'Generic file', 'icon'=>'/public/img/filetype/misc.png'),
		'exe'=>array('hun'=>'EXE Program', 'eng'=>'Executable','cr'=>'Executable', 'gr'=>'Executable', 'jap'=>'Executable', 'icon'=>'/public/img/filetype/exe.png'),
		'zip'=>array('hun'=>'Tömörített fájl', 'eng'=>'Compressed Archive','cr'=>'Compressed Archive','gr'=>'Compressed Archive',  'jap'=>'Compressed Archive', 'icon'=>'/public/img/filetype/zip.png'),
		'pdf'=>array('hun'=>'PDF dokumentum', 'eng'=>'PDF document','cr'=>'PDF document','gr'=>'PDF document', 'jap'=>'PDF document', 'icon'=>'/public/img/filetype/pdf.png'),
		'media'=>array('hun'=>'Médiafile', 'eng'=>'Media file','cr'=>'Media file','gr'=>'Media file',  'jap'=>'Media file','icon'=>'/public/img/filetype/media.png'),
	);
	
	
	function Mdownloads()
	{
		parent::Model();
		
		if(isset($this->muser->user['admin_lang']))
		{
			$this->downloads_dir = $this->downloads_dir.$this->muser->user['admin_lang'].'/';
		}
		
		$this->downloads_dir_url = '/'.$this->downloads_dir;
	}


	function get_heading($category)
	{
		return $this->categories[$category][$this->muser->user['lang']];
	}
	
	function get_category($category,$lang)
	{
		$query = $this->db->where('download_category',$category)->where('download_lang',$lang)->orderby('download_date','desc')->get('downloads');
		if($query->num_rows()==0)
		{
			return false;
		}
		else
		{
			return $query->result();
		}
		
	}

	function get_latest($num=10,$lang=false)
	{
		if(!$lang)
		{
			$query = $this->db->orderby('download_date','desc')->limit($num)->get('downloads');
		}
		else
		{
			$query = $this->db->where('download_lang',$lang)->orderby('download_date','desc')->limit($num)->get('downloads');
		}
	
		if($query->num_rows()==0)
		{
			return false;
		}
		else
		{
			return $query->result();
		}
	}


	function get_row($id)
	{
		$query = $this->db->where('download_id',$id)->get('downloads');
		if($query->num_rows()==0)
		{
			return false;
		}
		else
		{
			return $query->row();
		}
	}
	
	
	function generate_subfolder_dropdown($sub)
	{
		$this->load->helper('directory');
		
		$map = directory_map($this->downloads_dir.$sub);
		
		if(count($map)==0)
		{
			return false;
		}
		
		$tomb = array();
		
		foreach ($map as $kulcs => $elem)
		{
			// ha alkönyvtár
			if(is_array($elem))
			{
				if(count($elem)!=0)
				{
					foreach($elem as $file)
					{
						$tomb[$this->downloads_dir_url.$sub.'/'.$kulcs.'/'.$file]=$kulcs.'/'.$file;
					}
				}
			}
			//ha fájl
			else
			{	
				$tomb[$this->downloads_dir_url.$sub.'/'.$elem]=$elem;
			}
		}
		
		return $tomb;
	}
	
	function generate_file_dropdown()
	{
		$this->load->helper('directory');
		
		$map = directory_map($this->downloads_dir);
		
		if(count($map)==0)
		{
			return false;
		}
		
		$tomb = array();
		$tomb[0] = $this->lang->line('download_select_file');
		
		foreach ($map as $kulcs => $elem)
		{
			// ha alkönyvtár
			if(is_array($elem))
			{
				if(count($elem)!=0)
				{
					foreach($elem as $file)
					{
						$tomb[$this->downloads_dir_url.$kulcs.'/'.$file]=$kulcs.'/'.$file;
					}
				}
			}
			//ha fájl
			else
			{	
				$tomb[$this->downloads_dir_url.$elem]=$elem;
			}
		}
		
		return $tomb;
		
	}
	
	function generate_category_dropdown()
	{
		$tomb = array();
		
		foreach($this->categories as $k => $v)
		{
			$tomb[$k] = $v[$this->muser->user['lang']];
		}
		
		return $tomb;
	
	}
	function generate_type_dropdown()
	{
		$tomb = array();
		
		foreach($this->types as $k => $v)
		{
			$tomb[$k] = $v[$this->muser->user['lang']];
		}
		
		return $tomb;
	
	}
	
	
	
	
	
	
	
	function generate_file2_dropdown($kit="exe")
	{
		$this->load->helper('directory');
		
		$map = directory_map($this->downloads_dir2);
		
		if(count($map)==0)
		{
			return false;
		}
		
		$tomb = array();
		$tomb[0] = $this->lang->line('download_select_file');
		
		foreach ($map as $kulcs => $elem)
		{
			// ha alkönyvtár
			if(is_array($elem))
			{
				if(count($elem)!=0 && strlen($kulcs) <= 3 )
				{
					foreach($elem as $kulcs2=>$file)
					{
						if(is_array($file))
						{
							if(count($file)!=0)
							{
								foreach($file as $file2)
								{
									if(substr($file2,-4)==".".$kit)
										$tomb[$this->downloads_dir_url.$kulcs.'/'.$kulcs2.'/'.$file2]=$kulcs.'/'.$kulcs2.'/'.$file2;
								}
							}
						}
						elseif(substr($file,-4)==".".$kit){
							$tomb[$this->downloads_dir_url.$kulcs.'/'.$file]=$kulcs.'/'.$file;
						}
					}
				}
			}
			//ha fájl
			else
			{	
				$tomb[$this->downloads_dir_url.$elem]=$elem;
			}
		}
		
		return $tomb;
		
	}
	
	
	
}
?>