<?php
class Mcollection extends Model
{
	var $ev;
	var $ho;
	var $nap;
	var $most;
	
	function mcollection()
	{
		parent::Model();
		$now = time();
		
		$this->ev = date('Y',$now);
		$this->ho = date('m',$now);
		$this->nap = date('d',$now);
		$this->most = time();
	}
	
	
	// az Archline-ban megjeleno projektek listaja
	function getInArchline($kat,$bizalom,$lang,$mycountry){
		$parancs = " SELECT * FROM `cadline2`.`collection` WHERE deleted = 0 ";
		
		if( $kat && is_numeric($kat) )
			$parancs .= " AND kat_id = " . $kat;
		
		if( $bizalom && is_numeric($bizalom) )
			$parancs .= " AND bizalom >= " . $bizalom;
		
		if( $mycountry === true )
			$parancs .= " AND archline_nyelv = '".$lang."' ";
		
		$parancs .= " ORDER BY archline_sorszam ASC";
		
		
		$eredmeny = $this->db->query($parancs)->result();
		
		if( count($eredmeny) == 0 )
			return false;
		else
			return $eredmeny;
	}
	
	
	
	function get_bykategoria($kat){
		$lang = $this->muser->user['lang'];
		$query = $this->db->from('collection')->where('kat_id',$kat)->where('deleted','0')/*->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`.`collection` WHERE kat_id = '".$kategoria->dl_id."' AND `deleted` = 0";
			$query2 = " SELECT count(*) db FROM `cadline2`.`collection` WHERE kat_id = '".$kategoria->dl_id."' AND `deleted` = 0 AND bizalom >= 10";
			
			$darab = $this->db->query($query)->row();
			$darab2 = $this->db->query($query2)->row();
			
			$kategoria->darab = $darab->db;
			$kategoria->darab_gyari = $darab2->db;
		}
		
		return $tempkateg;
	}
	
	
	function get_darabegykategoria($kat_id){
		$nyelv = $this->muser->user['lang'];
		
		$query = " SELECT count(*) db FROM `cadline2`.`collection` WHERE kat_id = '".$kat_id."' AND `deleted` = 0";
		$darab = $this->db->query($query)->row();
		
		return $darab->db;
	}
	
	function get_darabegytipus($tipus){
		$query = " SELECT count(*) db FROM `cadline2`.`collection` WHERE tipus = '".$tipus."' AND `deleted` = 0";
		$darab = $this->db->query($query)->row();
		
		return $darab->db;
	}
	
	function get_tipus($kat_id){
		$query = $this->db->from('dl_kategoriak')->where('dl_id',$kat_id)->where('deleted','0')->select('tipus')->get();
		
		if($query->num_rows()==0){
			return false;
		}else{
			$query = $query->row();
			return $query->tipus;
		}
	}
	
	
	function get_kategoriak($mit){
		
		$query = " ( SELECT dl_id, tipus,";
		
		if(hun())
			$query .= " nev_hun nev";
		else
			$query .= " nev_eng nev";
		
		$query .= ", nev_hun, nev_eng, deleted FROM `cadline2`.`dl_kategoriak` k WHERE deleted = 0";
		
		if(hun())
			$query .= " AND nev_hun != ''";
		else
			$query .= " AND nev_eng != ''";
		
		if($mit != "mind")
			$query .= " AND k.tipus = '".$mit."'";
		
		
		
		$query .= ") UNION ( SELECT dl_id, tipus,";
		
		if(hun())
			$query .= " nev_eng nev";
		else
			$query .= " nev_hun nev";
		
		$query .= ", nev_hun, nev_eng, deleted FROM `cadline2`.`dl_kategoriak` k WHERE deleted = 0 ";
		
		if(hun())
			$query .= " AND nev_hun = ''";
		else
			$query .= " AND nev_eng = ''";
		
		if($mit != "mind")
			$query .= " AND k.tipus = '".$mit."'";
		
		$query .= ") ";
		
		
		$query .= " ORDER BY tipus";
		
		if(hun())
			$query .= ", nev";
		else
			$query .= ", nev";
		
		$ered = $this->db->query($query)->result();
		return $ered;
	}
	
	function get_all($tipus = ""){
		$lang = $this->muser->user['lang'];
		$query = $this->db->from('collection')->where('deleted','0');
		
		if($tipus)
			$query = $query->where('tipus',$tipus);
		
		$query = $query->orderby('nyelv, title')->get();
		
		if($query->num_rows()==0){
			return false;
		}else{
			return $query->result();
		}
	}
	
	function get_allUserId($user_id){
		$query = $this->db->from('collection')->where('deleted','0')->where('u_id',$user_id);
		
		$query = $query->orderby('nyelv, title')->get();
		
		if($query->num_rows()==0){
			return false;
		}else{
			return $query->result();
		}
	}
	
	function get_type(){
		$query = " SELECT DISTINCT(tipus) FROM `cadline2`.`dl_kategoriak` WHERE deleted = 0 ORDER BY tipus";
		return $this->db->query($query)->result();
	}
	
	function get_row($object_id){
		$query = $this->db->from('collection')->where('c_id',$object_id)->where('deleted','0')->get();
		if($query->num_rows()!=1){
			return false;
		}else{
			$query = $query->result();
			return $query[0];
		}
	}
	
	function newobject($ujobj){
		$query = " INSERT INTO `cadline2`.`collection` (`c_id`, `u_id`, `fajl`, `desc`, `tipus`, `kat_id`, `kep`, `nyelv`, `datum`, `size`, `title`, `szerzo`, `eredeti_nev`, `bizalom`, `archline_sorszam`, `archline_nyelv` )";
		$query .= " VALUES(
			NULL,
			'".$ujobj->user_id."',
			'".$ujobj->object."',
			'".$ujobj->text."',
			'".$ujobj->tipus."',
			'".$ujobj->kat_id."',
			'".$ujobj->kep."',
			'".$ujobj->nyelv."',
			'".$ujobj->datum."',
			'".$ujobj->meret."',
			'".$ujobj->title."',
			'".$ujobj->szerzo."',
			'".$ujobj->eredeti."',
			'".$ujobj->bizalom."',
			'".$ujobj->sorszam."',
			'".$ujobj->archline_nyelv."'
		) ";
		
		$this->db->query($query);
	}
	
	function editobject($ujobj,$c_id){
		
		$query = " UPDATE `cadline2`.`collection` SET `fajl` = '".$ujobj->fajl."', `desc` = '".$ujobj->text."', `kep` = '".$ujobj->kep."', `size` = ".$ujobj->meret.", `title` = '".$ujobj->title."', `szerzo` = '".$ujobj->szerzo."', `kat_id` = '".$ujobj->kat_id."', `datum` = '".$ujobj->datum."', `nyelv` = '".$ujobj->nyelv."', `bizalom` = '".$ujobj->bizalom."', `archline_sorszam` = '".$ujobj->archline_sorszam."' WHERE `c_id` = '$c_id'";
		
		$query = $this->db->query($query);
	}
	
	function del($c_id){
		$projectfajl = $this->db->query(" SELECT fajl, kep FROM `cadline2`.`collection` WHERE c_id = $c_id ")->row();
		$kep = $projectfajl->kep;
		$fajl = $projectfajl->fajl;
		
		$this->db->query(" UPDATE `cadline2`.`collection` SET `deleted` = 1 WHERE c_id = $c_id;");
		chmod($fajl, 777);
		$temptomb = explode("/",$fajl);
		$fajlnev = $temptomb[count($temptomb)-1];
		$ujfajl = "public/collection/zz_trash/".$fajlnev;
		if( copy($fajl,$ujfajl) )
			unlink($fajl);
		if( $kep != "" || $kep != 'NULL'){
			chmod($kep, 777);
			$temptomb = explode("/",$kep);
			$kepnev = $temptomb[count($temptomb)-1];
			$ujkep = "public/collection/zz_trash/".$kepnev;
			if( copy($kep,$ujkep) )
				unlink($kep);
			
			$this->db->query(" UPDATE `cadline2`.`collection` SET `kep` = '".$ujkep."' WHERE c_id = $c_id;");
		}
		
		$this->db->query(" UPDATE `cadline2`.`collection` SET `fajl` = '".$ujfajl."' WHERE c_id = $c_id;");
	}
}
?>