<?php
class Mprojects extends Model
{
	var $ev;
	var $ho;
	var $nap;
	var $most;
	
	function mprojects()
	{
		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('projects')->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('projects')->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`.`projects` WHERE kat_id = '".$kategoria->dl_id."' AND nyelv = '".$nyelv."' ";
			
			$query = " SELECT count(*) db FROM `cadline2`.`projects` 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('projects')/*->where('nyelv',$lang)*/->orderby('nyelv, title')->get();
		
		if($query->num_rows()==0)
		{
			return false;
		}
		else
		{
			return $query->result();
		}
	}
	
	function get_row($project_id){
		$query = $this->db->from('projects')->where('pr_id',$project_id)->get();
		if($query->num_rows()!=1)
		{
			return false;
		}
		else
		{
			$query = $query->result();
			return $query[0];
		}
	}
	
	function newproject($ujpr){
		$query = " INSERT INTO `cadline2`.`projects` VALUES( NULL, '".$ujpr->project."','".$ujpr->eredeti."', '".$ujpr->text."', '".$ujpr->kategoria."', '".$ujpr->kep."', '".$ujpr->nyelv."', '".$ujpr->datum."', '".$ujpr->meret."', '".$ujpr->title."', '".$ujpr->email."', '".$ujpr->szerzo."', '".$ujpr->kat_id."' )";
		
		$this->db->query($query);
	}
	
	function editproject($ujpr,$pr_id){
		if($ujpr->kep == ""){
			$query = " UPDATE `cadline2`.`projects` SET `project` = '".$ujpr->project."', `text` = '".$ujpr->text."', `kategoria` = '".$ujpr->kategoria."', `pr_size` = ".$ujpr->meret.", `title` = '".$ujpr->title."', `email` = '".$ujpr->email."', `szerzo` = '".$ujpr->szerzo."', `kat_id` = '".$ujpr->kat_id."'  WHERE `pr_id` = $pr_id";
		}
		else{
			$query = " UPDATE `cadline2`.`projects` SET `project` = '".$ujpr->project."', `text` = '".$ujpr->text."', `kategoria` = '".$ujpr->kategoria."', `kep` = '".$ujpr->kep."', `pr_size` = ".$ujpr->meret.", `title` = '".$ujpr->title."', `email` = '".$ujpr->email."', `szerzo` = '".$ujpr->szerzo."', `kat_id` = '".$ujpr->kat_id."'  WHERE `pr_id` = $pr_id";
		}
		$query = $this->db->query($query);
	}
	
	function del($prid){
		$projectfajl = $this->db->query(" SELECT project, kep FROM `cadline2`.`projects` WHERE pr_id = $prid ")->result();
		$kep = $projectfajl[0]->kep;
		$projectfajl = $projectfajl[0]->project;
		
		$this->db->query(" DELETE FROM `cadline2`.`projects` WHERE pr_id = $prid;");
		chmod($projectfajl, 777);
		unlink($projectfajl);
		if( $kep != "" || $kep != 'NULL'){
			chmod($kep, 777);
			unlink($kep);
		}
	}
	
}
?>