<?php
class Mobjects extends Model
{
	var $ev;
	var $ho;
	var $nap;
	var $most;
	
	function mobjects()
	{
		parent::Model();
		$now = time();
		
		$this->ev = date('Y',$now);
		$this->ho = date('m',$now);
		$this->nap = date('d',$now);
		$this->most = time();
	}
	
	function get_bykat($kat){
		$lang = $this->muser->user['lang'];
		$query = $this->db->from('objects')->where('kategoria',$kat)->where('nyelv',$lang)->get();
		
		if($query->num_rows()==0){
			return false;
		}else{
			return $query->result();
		}
	}
	
	function get_bykategoria($kat){
		$lang = $this->muser->user['lang'];
		$query = $this->db->from('objects')->where('kat_id',$kat)/*->where('nyelv',$lang)*/->get();
		
		if($query->num_rows()==0){
			return false;
		}else{
			return $query->result();
		}
	}
	
	function get_darab($kategoriak){
		$tempkateg = $kategoriak;
		$nyelv = $this->muser->user['lang'];
		
		foreach($tempkateg as $kategoria){
			//$query = " SELECT count(*) db FROM `cadline2`.`objects` WHERE kat_id = '".$kategoria->dl_id."' AND nyelv = '".$nyelv."' ";
			
			$query = " SELECT count(*) db FROM `cadline2`.`objects` WHERE kat_id = '".$kategoria->dl_id."' ";
			
			$darab = $this->db->query($query)->row();
			
			$kategoria->darab = $darab->db;
		}
		
		return $tempkateg;
	}
	
	function get_all(){
		$lang = $this->muser->user['lang'];
		$query = $this->db->from('objects')/*->where('nyelv',$lang)*/->orderby('nyelv, title')->get();
		
		if($query->num_rows()==0){
			return false;
		}else{
			return $query->result();
		}
	}
	
	function get_row($object_id){
		$query = $this->db->from('objects')->where('obj_id',$object_id)->get();
		if($query->num_rows()!=1){
			return false;
		}else{
			$query = $query->result();
			return $query[0];
		}
	}
	
	function newobject($ujobj){
		$query = " INSERT INTO `cadline2`.`objects` VALUES( NULL, '".$ujobj->object."', '".$ujobj->text."', '".$ujobj->kategoria."', '".$ujobj->kep."', '".$ujobj->nyelv."', '".$ujobj->datum."', '".$ujobj->meret."', '".$ujobj->title."', '".$ujobj->email."', '".$ujobj->szerzo."', '".$ujobj->kat_id."', '0' )";
		
		$this->db->query($query);
	}
	
	function editobject($ujobj,$obj_id){
		if($ujobj->kep == ""){
			$query = " UPDATE `cadline2`.`objects` SET `object` = '".$ujobj->object."', `text` = '".$ujobj->text."', `kategoria` = '".$ujobj->kategoria."', `obj_size` = ".$ujobj->meret.", `title` = '".$ujobj->title."', `email` = '".$ujobj->email."', `szerzo` = '".$ujobj->szerzo."', `kat_id` = '".$ujobj->kat_id."'  WHERE `obj_id` = $obj_id";
		}
		else{
			$query = " UPDATE `cadline2`.`objects` SET `object` = '".$ujobj->object."', `text` = '".$ujobj->text."', `kategoria` = '".$ujobj->kategoria."', `kep` = '".$ujobj->kep."', `obj_size` = ".$ujobj->meret.", `title` = '".$ujobj->title."', `email` = '".$ujobj->email."', `szerzo` = '".$ujobj->szerzo."', `kat_id` = '".$ujobj->kat_id."'  WHERE `obj_id` = $obj_id";
		}
		$query = $this->db->query($query);
	}
	
	function del($objid){
		$projectfajl = $this->db->query(" SELECT object, kep FROM `cadline2`.`objects` WHERE obj_id = $objid ")->result();
		$kep = $projectfajl[0]->kep;
		$projectfajl = $projectfajl[0]->object;
		
		$this->db->query(" DELETE FROM `cadline2`.`objects` WHERE obj_id = $objid;");
		chmod($projectfajl, 777);
		unlink($projectfajl);
		if( $kep != "" || $kep != 'NULL'){
			chmod($kep, 777);
			unlink($kep);
		}
	}
}
?>