<?php
class Mrender extends Model
{
	/*
	Render ÁLLAPOTOK:
	
	0 - semmi nem törént vele, elkezdendő
	1 - kész van
	2 - renderelés folyamatban
	30 - megszakítandó
	31 - megszakítás alatt
	32 - megszakítva
	
	
	*/
	
	function mrender()
	{
		parent::Model();
	}
	
	function get_nemkeszek(){
		$lang = $this->muser->user['lang'];
		$query = $this->db->from('render_img')->where('deleted','0')->where('allapot','0')->orderby('render_id')->get();
		
		if($query->num_rows()==0){
			return false;
		}else{
			return $query->result();
		}
	}
	
	function get_byallapot($allapot){
		$query = $this->db->from('render_img')->where('deleted','0')->where('allapot',$allapot)->get();
		
		if($query->num_rows()==0){
			return false;
		}else{
			return $query->result();
		}
	}
	
	function get_allspec(){
		$query = $this->db->from('render_img')->where("datum > '".(time()-179200)."'")->where('allapot != 1')->where('allapot != 2')->get();
		
		if($query->num_rows()==0){
			return false;
		}else{
			return $query->result();
		}
	}
	
	function get_byuserid($uid){
	//	$query = $this->db->from('render_img')->where('deleted','0')->where('user_id',$uid)->get();
		
		$query = "SELECT * FROM `render_img` WHERE user_id = '".$uid."' AND deleted = 0 ORDER BY (Case When allapot = 0 Then 10 When allapot = 4 Then 20 When allapot = 2 Then 30 When allapot = 1 Then 40 When substr(allapot,1,1) = 3 Then 50  End), teszt DESC, datum DESC";
		$query = $this->db->query($query)->result();
		
		if(count($query)==0){
			return false;
		}else{
			return $query;
		}
	}
	
	function get_all($rend="", $ir=""){
		$query = $this->db->from('render_img')->where('deleted','0')/*->orderby('datum DESC')->get()*/;
		
		if($rend == "")
			$rendezes = "datum";
		else
			$rendezes = $rend;
		
		if($ir == "")
			$rendezes .= " DESC";
		else
			$rendezes .= " ".$ir;
		
		$query = $query->orderby($rendezes);
		
		$query = $query->get();
		
		if($query->num_rows()==0)
			return false;
		else
			return $query->result();
	}
	
	function get_alldeleted(){
		$query = $this->db->from('render_img')->where('deleted','1')->where("datum > '".(time()-(60*60*24*5))."'")->get();
		
		if($query->num_rows()==0)
			return false;
		else
			return $query->result();
	}
	
	function get_row($id){
		if(!is_numeric($id))
			return false;
		
		//$query = $this->db->from('render_img')->where('render_id',$id)->get();
		$query = " SELECT * FROM `cadline2`.`render_img` WHERE render_id = ".$id;
		$query = $this->db->query($query);
		if($query->num_rows()!=1)
			return false;
		else
			return $query->row();
	}
	
	function get_rowByguid($guid){
		if($guid=="")
			return false;
		
		$query = " SELECT * FROM `cadline2`.`render_img` WHERE guid = '".$guid."'";
		$query = $this->db->query($query);
		if($query->num_rows()!=1)
			return false;
		else
			return $query->row();
	}
	
	function get_rowBygforumgalid($id){
		$query = " SELECT * FROM `cadline2`.`render_img` WHERE forumgal_id = '".$id."'";
		$query = $this->db->query($query);
		if($query->num_rows()==0)
			return false;
		else
			return $query->result();
	}
	
	function keszrender($id,$kep){
		if( file_exists($kep) )
			$this->db->query(" UPDATE `cadline2`.`render_img` SET `allapot` = '1', `keszkep` = '".$kep."' WHERE render_id = $id");
	}
	
	function newrender($ujrender){
		$vane = $this->get_rowByguid($ujrender->guid);
		
		if($vane){
			$record->osztaly = "mrender";
			$record->fv = "newrender";
			$record->hiba = "DUPLA guid: ".$ujrender->guid;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
		}
		else{
			$query = " INSERT INTO `cadline2`.`render_img` (`render_id`,`user_id`,`guid`,`kiskep`,`nyelv`,`render_name`,`allapot`,`datum`,`teszt`,`szamla`,`deleted`) VALUES( NULL,'".$ujrender->user_id."','".$ujrender->guid."','".$ujrender->kiskep."', '".$ujrender->nyelv."','".$ujrender->render_name."','0','".$ujrender->datum."','".$ujrender->teszt."','".$ujrender->szamla."','0' )";
		
			$this->db->query($query);
		}
	}
	
	function frissit($mit,$allapot,$statusz){
		$query = " UPDATE `cadline2`.`render_img` SET `allapot` = '".$allapot."', `statusz` = '".$statusz."' WHERE `render_id` = ".$mit;
		
		$this->db->query($query);
	}
	
	function editrender($ujrender,$id){
		$query = " UPDATE `cadline2`.`render_img` SET `render_name` = '".$ujrender->hely."', `allapot` = '".$ujrender->allapot."', `datum` = '".$ujrender->datum."', `keszkep` = '".$ujrender->keszkep."', `deleted` = '".$ujrender->deleted."' WHERE `render_id` = '".$id."'";
		
		$query = $this->db->query($query);
	}
	
	function del($id){
		//$render = $this->db->query(" SELECT * FROM `cadline2`.`render_img` WHERE render_id = $id ")->row();
		//$kep = $render->keszkep;
		
		if(is_numeric($id))
			$this->db->query(" UPDATE `cadline2`.`render_img` SET `deleted` = '1' WHERE render_id = $id");
		
		/*
		if( $kep != "" || $kep != 'NULL'){
			chmod($kep, 777);
			unlink($kep);
		}
		*/
	}
	
	
	function get_hibak(){
		$query = " SELECT * FROM `cadline2`.`a_hibak` WHERE (fv = 'hozzaad' or fv = 'submit') ";
		$re = $this->db->query($query)->result();
		return $re;
	}
	
}
?>