<?php
class Mgroups extends Model
{
	var $ev;
	var $ho;
	var $nap;
	var $most;
	
	function mgroups()
	{
		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('groups')->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('groups')->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`.`groups` WHERE kat_id = '".$kategoria->dl_id."' AND nyelv = '".$nyelv."' ";
			
			$query = " SELECT count(*) db FROM `cadline2`.`groups` 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('groups')/*->where('nyelv',$lang)*/->orderby('nyelv, title')->get();
		
		if($query->num_rows()==0){
			return false;
		}else{
			return $query->result();
		}
	}
	
	function get_row($group_id){
		$query = $this->db->from('groups')->where('gr_id',$group_id)->get();
		if($query->num_rows()!=1){
			return false;
		}else{
			$query = $query->result();
			return $query[0];
		}
	}
	
	function newgroup($ujobj){
		$query = " INSERT INTO `cadline2`.`groups` VALUES( NULL, '".$ujobj->group."', '".$ujobj->text."', '".$ujobj->kategoria."', '".$ujobj->kep."', '".$ujobj->nyelv."', '".$ujobj->datum."', '".$ujobj->meret."', '".$ujobj->title."', '".$ujobj->email."', '".$ujobj->szerzo."', '".$ujobj->kat_id."' )";
		
		$this->db->query($query);
	}
	
	function editgroup($ujgr,$gr_id){
		if($ujgr->kep == ""){
			$query = " UPDATE `cadline2`.`groups` SET `group` = '".$ujgr->group."', `text` = '".$ujgr->text."', `kategoria` = '".$ujgr->kategoria."', `gr_size` = ".$ujgr->meret.", `title` = '".$ujgr->title."', `email` = '".$ujgr->email."', `szerzo` = '".$ujgr->szerzo."', `kat_id` = '".$ujgr->kat_id."'  WHERE `gr_id` = $gr_id";
		}
		else{
			$query = " UPDATE `cadline2`.`groups` SET `group` = '".$ujgr->group."', `text` = '".$ujgr->text."', `kategoria` = '".$ujgr->kategoria."', `kep` = '".$ujgr->kep."', `gr_size` = ".$ujgr->meret.", `title` = '".$ujgr->title."', `email` = '".$ujgr->email."', `szerzo` = '".$ujgr->szerzo."', `kat_id` = '".$ujgr->kat_id."'  WHERE `gr_id` = $gr_id";
		}
		$query = $this->db->query($query);
	}
	
	function del($grid){
		$csopfajl = $this->db->query(" SELECT * FROM `cadline2`.`groups` WHERE gr_id = $grid ")->result();
		$kep = $csopfajl[0]->kep;
		$csopfajl = $csopfajl[0]->group;
		
		$this->db->query(" DELETE FROM `cadline2`.`groups` WHERE gr_id = $grid;");
		chmod($csopfajl, 777);
		unlink($csopfajl);
		if( $kep != "" || $kep != 'NULL'){
			chmod($kep, 777);
			unlink($kep);
		}
	}
	
}
?>