<?php
class Mmedia extends Model
{
	var $img_dir = 'public/galeria/';
	
	var $img_settings = array
	(
		'kicsi'=>array('x'=>133,'y'=>100),
		'kozepes'=>array('x'=>180,'y'=>150),
		'nagy'=>array('x'=>640,'y'=>480),
	);
	
	function Mmedia()
	{
		parent::Model();
	}
	
	function prep_img_dir($galeria)
	{
		if(strstr($galeria,'..')!==false || strstr($galeria,'~')!==false || strstr($galeria,'/')!==false)
		{
			show_error('Nincs ilyen galéria: '.$galeria);
		}
		
		$path = $this->img_dir.$galeria;
		
		if(!is_dir($path))
		{
			show_error('Nincs ilyen galéria: '.$galeria);
		}
		
		$this->load->helper('directory');
		
		$files = directory_map($path, true);
		
		foreach($this->img_settings as $k => $v)
		{
			if(!is_dir($path.'/'.$k))
			{
				@mkdir($path.'/'.$k);
			}
		}
		
		unset($k,$v);
		
		if(count($files)==0)
		{
			return false;
		}
		
		
		foreach($files as $key => $file)
		{
			if(!array_key_exists($file,$this->img_settings))
			{
				foreach($this->img_settings as $k => $v)
				{
					if(!is_file($path.'/'.$k.'/'.$file))
					{
						$this->img_resize($galeria,$file,$k);
					}
				}
			}
			else
			{
				unset($files[$key]);
			}
		}
		
		return $files;
	}
	
	function img_resize($galeria,$kep,$meret)
	{
		$config['image_library'] = 'gd2';
		$config['source_image'] = $this->img_dir.$galeria.'/'.$kep;
		$config['new_image']= $this->img_dir.$galeria.'/'.$meret.'/'.$kep;
		$config['maintain_ratio'] = TRUE;
		$config['quality']='95%';
		$config['width'] = $this->img_settings[$meret]['x'];
		$config['height'] = $this->img_settings[$meret]['y'];
		
		$this->load->library('image_lib');
		$this->image_lib->initialize($config); 
		if(!$this->image_lib->resize())
		{
			echo $this->image_lib->display_errors();
		}
		
		unset($config);
		
		$this->image_lib->clear();
	}
	
	function image_gallery_row($mappa_nev,$cim,$szoveg,$kiskep=false,$link=false)
	{
		if(!$kiskep)
		{
			$data['kiskep'] = $this->RandomFile('public/gallery/'.$mappa_nev.'/thumbs/');
		}
		else
		{
			$data['kiskep']=$kiskep;
		}
		
		
		if(!$link)
		{
			$data['url'] = '/multimedia/gallery/'.$mappa_nev;
			$data['js_url']='window.location.href=\''.$data['url'].'\'';
		}
		else
		{
			$data['url']= $link;
			$data['js_url']="window.open($link,'','resizable=yes,width=800,height=600, top=10, left=10')";
			
		}
		
		$data['mappa']=$mappa_nev;
		$data['cim']=$cim;
		$data['szoveg']=$szoveg;
		
		
		return $this->load->view('media/images_row',$data,true);
	}
	
	function video_gallery_row($cim,$leiras,$camtasia_url,$letoltes_file=false,$kiskep=false,$meret=false)
	{
		$data['lang']=$this->muser->user['lang'];
		
		
		if($kiskep==false || !file_exists('public/videogaleria/'.$kiskep))
		{
			$data['kiskep'] = '/public/videogaleria/none.png';
		}
		else
		{
			$data['kiskep'] = '/public/videogaleria/'.$kiskep;
		}
		
		if(!$letoltes_file)
		{
			$data['letolt'] = false;
		}
		else
		{
			$data['letolt']=$letoltes_file;
			
			if(!is_numeric($meret))
			{
				$data['meret']=false;
			}
			else
			{
				$data['meret']=$meret;
			}
			
			//$data['letolt'] = '<span class="gal-down"><a href="/videogaleria/'.$letoltes_file.'">&nbsp;&nbsp;&nbsp; '.$down_text.'</a></span>'.$mb;
		}
		
		$data['cim']=$cim;
		$data['leiras']=$leiras;	
		$data['camtasia_url']=$camtasia_url;
		
		return $this->load->view('media/videos_row',$data,true);
		
		$lol =
		'
			<div class="gal-sor">
				<a href="javascript:openSwf(\''.$camtasia_url.'\',\'a\',1019,676)">
					<img src="'.$kiskep.'">
				</a>
				
				<div class="gal-cim">
						<a href="javascript:openSwf(\''.$camtasia_url.'\',\'a\',1019,676)">
						<span>'.$cim.'</span>
					</a>
				</div>
				
				<span class="gal-play"><a href="javascript:openSwf(\''.$camtasia_url.'\',\'a\',1019,676)">&nbsp;&nbsp;&nbsp; '.$play_text.'</a></span>	
				'.$letoltes.'		
				<div class="gal-video-szoveg">
					<span>'.$leiras.'</span>
				</div>
				<div class="clear"></div>
			</div>
		';
	}

	
	
	function RandomFile($folder='', $extensions='.*'){
 
    // fix path:
    $folder = trim($folder);
    $folder = ($folder == '') ? './' : $folder;
 
    // check folder:
    if (!is_dir($folder)){ return false; }
 
    // create files array
    $files = array();
 
    // open directory
    if ($dir = @opendir($folder)){
 
        // go trough all files:
        while($file = readdir($dir)){
 
            if (!preg_match('/^\.+$/', $file) and 
                preg_match('/\.('.$extensions.')$/', $file)){
 
                // feed the array:
                $files[] = $file;                
            }            
        }        
        // close directory
        closedir($dir);    
    }
    else {
        return false;
    }
 
    if (count($files) == 0){
        return false;
    }
 
    // seed random function:
    mt_srand((double)microtime()*1000000);
 
    // get an random index:
    $rand = mt_rand(0, count($files)-1);
 
    // check again:
    if (!isset($files[$rand])){
        return false;
    }
 
    // return the random file:
    return $files[$rand];
 
}

	
	
}
?>