<?php
class Mgallery extends Model
{
	var $img_dir = 'public/gallery/';

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

	function checkFolder($folder)
	{
		if(strstr($folder,'..')!==false || strstr($folder,'~')!==false || strstr($folder,'/')!==false)
		{
			return false;
		}

		$path = $this->img_dir . $folder;

		if(!is_dir($path))
		{
			return false;
		}
		
		return true;
	}
	
	function getImages($folder)
	{
        $this->load->helper('directory');
        
        $path = $this->img_dir . $folder;
        
        $files = directory_map($path, true);
        
        $files = $this->stripFolders($path, $files);
        
        return $files;
	}
	
	function stripFolders($path, $files)
	{
		sort($files);
		//print "<pre>"; print_r($files); print "</pre>";
		foreach($files as $k => $f)
		{
			if(is_dir($path . '/' . $f))
				unset($files[$k]);
		}

		return $files;
	}

	function checkHomeFolder($folder)
	{
		if(strstr($folder,'..')!==false || strstr($folder,'~')!==false || strstr($folder,'/')!==false)
		{
			return false;
		}

		$path = 'public/img/home/' . $folder;

		if(!is_dir($path))
		{
			return false;
		}

		return true;
	}

	function getHomeImages($folder, $random = false)
	{
        $this->load->helper('directory');

        $path = $random ? 'public/img/home/' . $folder . '/thumbs' : 'public/img/home/' . $folder;

        $files = directory_map($path, true);

        $files = $this->stripFolders($path, $files);
        
        if($random)
            return $files[rand(0, count($files) - 1)];

        return $files;
	}

	function prepHomeImages($folder)
	{
        $path = 'public/img/home/' . $folder;

        if($this->checkHomeFolder($folder))
        {
			$files = $this->getHomeImages($folder);

			foreach($files as $k => $f)
			{
				if(!is_dir($path . '/thumbs'))
				{
	                mkdir($path . '/thumbs');
	                chmod($path. '/thumbs', 0777);
				}
				
				if(!is_file($path . '/thumbs/' . $f))
					$this->createHomeThumb($path, $f);
			}
        }
        else
        {
			return false;
        }
	}
	
	function createHomeThumb($path, $kep)
	{
        $config['image_library'] = 'gd2';
		$config['source_image'] = $path . '/' . $kep;
		$config['new_image'] = $path . '/thumbs/' . $kep;
		$config['maintain_ratio'] = true;
		$config['quality'] = '95%';
		$config['width'] = '300';
		$config['height'] = '210';

		$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 prepImages($folder)
	{
		$path = $this->img_dir . $folder;
		
        if($this->checkFolder($folder))
        {
			$files = $this->getImages($folder);
			
			if(!is_dir($path . '/thumbs'))
			{
                mkdir($path . '/thumbs');
                chmod($path. '/thumbs', 0777);
			}
			
			foreach($files as $k => $f)
			{
				//get the info of the image
				$info = getimagesize($path . '/' . $f);
				
				//check if the width is 800 pixels
				if($info[0]!=800 && $info[1]!=600)
				    $this->resizeImage($path . '/' . $f, '800', '600');
				
				if(!is_file($path . '/thumbs/' . $f))
				    $this->createThumb($path, $f);
			}
        }
        else
        {
			return false;
        }
	}
	
	function resizeImage($filepath, $x, $y)
	{
		$config['image_library'] = 'gd2';
		$config['source_image'] = $filepath;
		$config['maintain_ratio'] = true;
		$config['quality'] = '95%';
		$config['width'] = $x;
		$config['height'] = $y;

		$this->load->library('image_lib');
		$this->image_lib->initialize($config);
		if(!$this->image_lib->resize())
		{
			echo $this->image_lib->display_errors();
			echo $filepath;
		}

		unset($config);

		$this->image_lib->clear();
	}
	
	function createThumb($path, $kep, $x, $y)
	{
        $config['image_library'] = 'gd2';
		$config['source_image'] = $path . '/' . $kep;
		$config['new_image'] = $path . '/thumbs/' . $kep;
		$config['maintain_ratio'] = true;
		$config['quality'] = '95%';
		$config['width'] = '150';
		$config['height'] = '113';

		$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,$uj="0")
	{
		if( $this->uri->segment(1) == "interior" || $this->uri->segment(1) == "photo" || $this->uri->segment(1) == "multimedia" ){
			if( !$kiskep || !file_exists('public/'.$mappa_nev.'/thumbs/'.$kiskep) ){
				$data['kiskep'] = $this->getHomeImages('/../../'.$mappa_nev,true);
			}
			else{
				$data['kiskep']=$kiskep;
			}
	
			if(!$link){
				$data['url'] = '/interior/'.$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')";
	
			}
			
			
		}else{
		
			if(!$kiskep)
			{
				$data['kiskep'] = $this->RandomFile('public/gallery/'.$mappa_nev.'/thumbs/');
			}
			else
			{
				$data['kiskep']=$kiskep;
			}
	
			if(!$link)
			{
				$data['url'] = '/interior/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;
		$data['uj']=$uj;


		return $this->load->view('media/images_row',$data,true);
	}
	
	function get_all($nyelv,$terulet=false){
		$query = " SELECT * FROM `cadline2`.`gallerys` WHERE nyelv = '".$nyelv."' AND deleted = 0";
		if($terulet)
			$query .= " AND terulet = '".$terulet."'";
		$query .= " ORDER BY sorszam";
		
		$re = $this->db->query($query)->result();
		
		if($re)
			return $re;
		else
			return false;
	}
	
	function get_rowMappa($mappa, $nyelv){
		$query = " SELECT * FROM `cadline2`.`gallerys` WHERE mappa = '".$mappa."' AND nyelv = '".$nyelv."'";
		
		$re = $this->db->query($query)->row();
		if($re)
			return $re;
		else
			return false;
	}
	
	function get_rowHonapSorszam($honap, $nyelv){
		$query = " SELECT * FROM `cadline2`.`gallerys` WHERE terulet = 'honapkepe' AND nyelv = '".$nyelv."' AND sorszam = '".$honap."'";
		
		$re = $this->db->query($query)->row();
		if($re)
			return $re;
		else
			return false;
	}
	
}
?>