<?php
class Mforum extends Model
{
	var $ev;
	var $ho;
	var $nap;
	var $most;
	var $user_tabla;
	var $hun_user_tabla;
	var $nyelv;
	
	function mforum()
	{
		parent::Model();
		$now = time();
		
		$this->ev = date('Y', $now);
		$this->ho = date('m', $now);
		$this->nap = date('d', $now);
		$this->most = $now;
		$this->user_tabla = '`cadline2`.`forum_user`';
		//$this->hun_user_tabla = '`clusers`.`forum_user`';
		$this->hun_user_tabla = '`cadline2`.`forum_user`';
		
		$this->nyelv = $this->muser->user['lang'];
		
		$this->hszoldal = 10;
		$this->temaoldal = 10;
		$this->temakoroldal = 15;
		
		$this->userekoldal = 30;
		
		$this->userdb = $this->load->database('clusers',true);
	}
	
	function get_alap($oldal = 0)
	{
		$lang = $this->muser->user['lang'];
		$honnan = $oldal*$this->temaoldal;
		
		$query = " SELECT * FROM ( ( SELECT t.tema_id, t.cim, f.nev megnyito, count(*) szam, max(h.datum) datum FROM `cadline2`.`temak` t, `cadline2`.`hozzaszolasok` h, `clusers`.`forum_user` f WHERE f.user_id = t.megnyito AND t.tema_id = h.tema_id AND t.nyelv = '$lang' GROUP BY t.tema_id)";
		$query .= " UNION (SELECT t.tema_id, t.cim, f.nev megnyito, 0 szam, t.datum FROM `cadline2`.`temak` t, `clusers`.`forum_user` f WHERE f.user_id = t.megnyito AND t.tema_id NOT IN (SELECT tema_id FROM hozzaszolasok) AND t.nyelv = '$lang' )";
		$query .= " ) tt ORDER BY tt.datum DESC LIMIT $honnan,".$this->temaoldal;
		
		$query = $this->db->query($query)->result();
		
		$temak = $query;
		return $temak;
	}
	
	function get_alap2($temakor_id, $oldal = 0)
	{
		$lang = $this->muser->user['lang'];
		if($lang != "hun" && $lang != "eng" )
			return false;
		
		
		$honnan = $oldal*$this->temaoldal;
		
		$query = " SELECT * FROM ( ( SELECT t.tema_id, t.cim, f.nev megnyito, count(*) szam, max(h.datum) datum";
		if(hun())
			$query .= " FROM `cadline2`.`temak` t, `cadline2`.`hozzaszolasok` h, ".$this->hun_user_tabla." f";
		else
			$query .= " FROM `cadline2`.`temak` t, `cadline2`.`hozzaszolasok` h, ".$this->user_tabla." f";
		
		$query .= " WHERE f.user_id = t.megnyito AND t.tema_id = h.tema_id AND t.nyelv = '$lang' AND t.temakor_id = '$temakor_id' AND t.deleted = 0 AND h.deleted = 0 ";
		if(!isset($_SESSION['default']['fuser']))
			$query .= " AND t.public=1 ";
		$query .= " GROUP BY t.tema_id)";
		
		$query .= " UNION (SELECT t.tema_id, t.cim, f.nev megnyito, 0 szam, '2010-01-01 10:10:10' datum";
		if(hun())
			$query .= " FROM `cadline2`.`temak` t, ".$this->hun_user_tabla." f";
		else
			$query .= " FROM `cadline2`.`temak` t, ".$this->user_tabla." f";
		
		$query .= " WHERE f.user_id = t.megnyito AND t.deleted = 0 AND t.tema_id NOT IN (SELECT tema_id FROM hozzaszolasok h WHERE h.deleted=0) AND t.nyelv = '$lang' AND t.temakor_id = '$temakor_id' ";
		if(!isset($_SESSION['default']['fuser']))
			$query .= " AND t.public=1 ";
		$query .= " )";
		
		
		if(eng()){
		// Azok a temak, amiknek toroltek a megnyitojat, de van benne hozzaszolas
		
			$query .= " UNION (
				SELECT t.tema_id, t.cim, '?' megnyito, count(*) szam, max(h.datum) datum";
				$query .= " FROM `cadline2`.`temak` t, `cadline2`.`hozzaszolasok` h";
			
			$query .= " WHERE t.deleted = 0 AND h.deleted = 0 AND t.megnyito NOT IN (";
			$query .= "SELECT user_id FROM ";
						if(hun())
							$query .= "".$this->hun_user_tabla." f";
						elseif(eng())
							$query .= $this->user_tabla." f";
			$query .= ") AND t.tema_id = h.tema_id AND t.nyelv = '$lang' AND t.temakor_id = '$temakor_id' GROUP BY t.tema_id)";
		}
		
		$query .= " ) tt ORDER BY tt.datum DESC LIMIT ".$honnan.",".$this->temaoldal;
		
		$query = $this->db->query($query)->result();
		
		$temak = $query;
		return $temak;
	}
	
	function get_alaptemakorok($oldal = 0)
	{
		$lang = $this->muser->user['lang'];
		if($lang != "hun" && $lang != "eng" )
			return false;
		
		$honnan = $oldal*$this->temakoroldal;
		
		$nemkelltemakor = '';
		
		if(!isset($_SESSION['default']['fuser'])){
			$nemkelltemakor = 'WHERE tt.temakor_id != 24 AND tt.temakor_id != 38 AND tt.temakor_id != 39 AND tt.temakor_id != 40';
		}
		
		$query = " SELECT * FROM ( ( SELECT t.temakor_id, t.cim, f.nev megnyito, count(*) szam, '2010-05-25 15:16:32' datum";
		if(hun())
			$query .= " FROM `cadline2`.`temakorok` t, `cadline2`.`temak` tt, ".$this->hun_user_tabla." f";
		elseif(eng())
			$query .= " FROM `cadline2`.`temakorok` t, `cadline2`.`temak` tt, ".$this->user_tabla." f";
		
		$query .= " WHERE f.user_id = t.megnyito AND tt.temakor_id = t.temakor_id AND t.nyelv = '$lang' AND tt.deleted = 0 AND t.deleted = 0 ";
		if(!isset($_SESSION['default']['fuser']))
			$query .= " AND tt.public=1 AND t.public=1 ";
		$query .= " GROUP BY t.temakor_id)";
		$query .= " UNION (SELECT t.temakor_id, t.cim, f.nev megnyito, 0 szam, t.datum";
		
		if(hun())
			$query .= " FROM `cadline2`.`temakorok` t, ".$this->hun_user_tabla." f";
		elseif(eng())
			$query .= " FROM `cadline2`.`temakorok` t, ".$this->user_tabla." f";
		
		$query .= " WHERE f.user_id = t.megnyito AND t.deleted = 0 AND t.temakor_id NOT IN (SELECT temakor_id FROM temak) AND t.nyelv = '$lang' )";
		
		
		
		
		// azok a temakorok, amiknek toroltek a megnyitojat, de van benne tema
		if(eng()/*true*/){
			$query .= " UNION (
				SELECT t.temakor_id, t.cim, '?' megnyito, count(*) szam, t.datum 
				 FROM `cadline2`.`temakorok` t, `cadline2`.`temak` tt 
				 WHERE tt.temakor_id = t.temakor_id AND t.nyelv = '$lang' AND t.megnyito NOT IN 
				 	(SELECT user_id FROM ";
				 	if(hun())
				 		$query .= "".$this->hun_user_tabla." f";
				 	elseif(eng())
				 		$query .= "".$this->user_tabla." f";
				 	$query .= ") 
				 GROUP BY t.temakor_id)";
		}
		
		$query .= " ) tt ";
		
		$query .= "{$nemkelltemakor} ORDER BY tt.datum DESC LIMIT ".$honnan.",".$this->temakoroldal;
		
		$query = $this->db->query($query)->result();
		
		$temak = $query;
		return $temak;
	}
	
	function get_temak($nyelv="hun"){
		$query = " SELECT t.tema_id, t.cim cim, t.megnyito, t.temakor_id t_tkid, tk.cim tkcim, tk.megnyito tkmegnyito  FROM `cadline2`.`temak` t LEFT JOIN `cadline2`.`temakorok` tk ON tk.temakor_id = t.temakor_id WHERE t.`deleted` = 0 AND t.`nyelv` = '".$nyelv."' ORDER BY tk.cim, t.cim ";
		$query = $this->db->query($query)->result();
		
		return $query;
	}
	
	function get_utolsouserTema($tema_id){
		if(hun())
			$query = "SELECT * FROM `cadline2`.`hozzaszolasok` h LEFT JOIN ".$this->hun_user_tabla." f";
		else
			$query = "SELECT * FROM `cadline2`.`hozzaszolasok` h LEFT JOIN ".$this->user_tabla." f";
		
		$query .= " ON h.user = f.user_id WHERE h.tema_id = $tema_id AND h.deleted = 0 ORDER BY h.datum DESC LIMIT 0,1";
		
		$query = $this->db->query($query)->row();
		
		return $query;
	}
	
	function get_tkuthsz($tk_id){
		
		$query = "SELECT h.datum, user FROM `cadline2`.`temak` tt, `cadline2`.`hozzaszolasok` h  WHERE tt.temakor_id = $tk_id AND h.tema_id = tt.tema_id AND h.deleted = 0 ORDER BY h.datum DESC LIMIT 0,1";
		
		$query = $this->db->query($query)->row();
		if( isset($query->datum) )
			return $query;
		else{
			$xxx->datum = "2010-01-01 10:10:10";
			$xxx->user = "0";
			return $xxx;
		}
	}
	
	function get_tkdate($tk_id){
		$query = "SELECT datum FROM `cadline2`.`temakorok` WHERE temakor_id = $tk_id AND deleted = 0";
		
		$query = $this->db->query($query)->row();
		if( isset($query->datum) )
			return $query->datum;
		else
			return "2010-01-01 10:10:10";
	}
	
	function get_lista($temaid,$oldal){
		$ennyit = $this->hszoldal;
		$oszam = $oldal*$ennyit;
		
		$query = "SELECT * FROM( (SELECT hsz_id, belso.user user_id, user.nev user, belso.szoveg, valasz, kep, belso.datum, user.rang rang";
		if(hun())
			$query .= " FROM ".$this->hun_user_tabla." user,";
		else
			$query .= " FROM ".$this->user_tabla." user,";
		
		$query .= " (SELECT hsz_id, user,szoveg, valasz, kep, h.datum FROM hozzaszolasok h ";
		
		if(!isset($_SESSION['default']['fuser']))
			$query .= " LEFT JOIN temak t ON t.tema_id=h.tema_id ";
		
		$query .= " WHERE h.tema_id = $temaid AND h.deleted = 0";
		
		if(!isset($_SESSION['default']['fuser']))
			$query .= " AND t.public=1 ";
		
		$query .= ") belso WHERE user.user_id = belso.user) ";
		
		$query .= "UNION ( SELECT hsz_id, user user_id, 'torolt felhasznalo' user, szoveg, valasz, kep, h.datum, f.rang rang";
		if(hun())
			$query .= " FROM ".$this->hun_user_tabla." f, hozzaszolasok h";
		else
			$query .= " FROM ".$this->user_tabla." f, hozzaszolasok h";
			
		$query .= " WHERE tema_id = $temaid AND h.deleted = 0 AND h.user NOT IN ( SELECT user_id";
		if(hun())
			$query .= " FROM ".$this->hun_user_tabla." )";
		else
			$query .= " FROM ".$this->user_tabla." )";
		
		$query .= " ) ) cucc ORDER BY datum DESC LIMIT $oszam,$ennyit";
		
		$query = $this->db->query($query)->result();
		
		return $query;
	}
	
	function get_mypost($uid){
		if( $uid )
			$query = "SELECT * FROM `cadline2`.`hozzaszolasok` WHERE user = ".$uid." AND deleted = 0 ORDER BY datum DESC";
		else
			return false;
		
		$query = $this->db->query($query)->result();
		
		return $query;
	}
	
	function get_valaszok($hsz_id){
		$query = "SELECT hsz_id, tema_id, szoveg, user, valasz, kep, h.datum, h.nyelv, h.deleted, user_id, nev";
		
		if(hun())
			$query .= " FROM `cadline2`.`hozzaszolasok` h, ".$this->hun_user_tabla." u";
		elseif(eng())
			$query .= " FROM `cadline2`.`hozzaszolasok` h, ".$this->user_tabla." u";
		
		$query .= " WHERE valasz = ".$hsz_id." AND h.user = u.user_id AND h.deleted = 0 ORDER BY h.datum DESC";
		
		$query = $this->db->query($query)->result();
		
		return $query;
	}
	
	function get_tema1($tema_id){
		$query = "SELECT temakor_id, cim FROM `cadline2`.`temak` WHERE tema_id = ".$tema_id." AND deleted = 0";
		
		$query = $this->db->query($query)->row();
		
		return $query;
	}
	
	function get_temakor1($temakor_id){
		$query = "SELECT cim FROM `cadline2`.`temakorok` WHERE temakor_id = ".$temakor_id." AND deleted = 0";
		
		$query = $this->db->query($query)->row();
		
		return $query;
	}
	
	function get_temaidByHszid($hszid){
		$query = "SELECT tema_id FROM `cadline2`.`hozzaszolasok` h WHERE h.hsz_id = ".$hszid." AND h.deleted = 0";
		
		$query = $this->db->query($query)->row();
		
		return $query->tema_id;
	}
	
	function get_cim($temaid){
		$query = $this->db->query("SELECT cim FROM temak WHERE tema_id = $temaid")->row();
		return $query;
	}
	
	function get_tkcim($temakorid){
		$query = $this->db->query("SELECT cim FROM temakorok WHERE temakor_id = $temakorid")->row();
		return $query;
	}
	
	function get_temaoldal($tema_id){
		$query = $this->db->query("SELECT count(*) szam FROM hozzaszolasok WHERE tema_id = $tema_id AND deleted = 0")->row();
		$oldalak = $query->szam;
		return ($oldalak/$this->hszoldal);
	}
	
	function get_toldal(){
		$lang = $this->muser->user['lang'];
		$query = $this->db->query("SELECT count(*) szam FROM temak WHERE nyelv = '$lang' AND deleted = 0")->row();
		$oldalak = $query->szam;
		return ($oldalak/$this->temaoldal);
	}
	
	function get_tkoldal(){
		$lang = $this->muser->user['lang'];
		$query = $this->db->query("SELECT count(*) szam FROM temakorok WHERE nyelv = '$lang' AND deleted = 0")->row();
		$oldalak = $query->szam;
		return ($oldalak/$this->temakoroldal);
	}
	
	function get_nev($login){
		
		if(is_numeric($login)){
			if(hun())
				$query = $this->db->query(" SELECT nev FROM ".$this->hun_user_tabla." WHERE user_id = '$login' ")->row();
			else
				$query = $this->db->query(" SELECT nev FROM ".$this->user_tabla." WHERE user_id = '$login' ")->row();
		}else{
		
			if(hun())
				$query = $this->db->query(" SELECT nev FROM ".$this->hun_user_tabla." WHERE login = '$login' ")->row();
			else
				$query = $this->db->query(" SELECT nev FROM ".$this->user_tabla." WHERE login = '$login' AND nyelv = '".$this->nyelv."'")->row();
		}
		
		return $query->nev;
	}
	
	function get_userid($login){
		if(hun())
			$query = $this->db->query(" SELECT user_id FROM ".$this->hun_user_tabla." WHERE login = '{$login}' AND nyelv = 'hun' ")->row();
		elseif(eng())
			$query = $this->db->query(" SELECT user_id FROM ".$this->user_tabla." WHERE login = '{$login}' AND nyelv = 'eng' ")->row();
		
		return $query->user_id;
	}
	
	function getrow($user_id){
		$ered = $this->db->query(" SELECT * FROM forum_user WHERE user_id = ".$user_id)->row();
		
		return $ered;
	}
	
	function szabalyzat($uid){
		if(hun())
			$query = $this->db->query(" SELECT szabaly FROM ".$this->hun_user_tabla." WHERE user_id = '$uid' ")->row();
		elseif(eng())
			$query = $this->db->query(" SELECT szabaly FROM ".$this->user_tabla." WHERE user_id = '$uid' ")->row();
		
		if( $query->szabaly == 0)
			return false;
		else
			return true;
	}
	
	function elfogad($uid){
		$Date = date('Y-m-d H:i:s', $this->most);
		if(hun())
			$query = $this->db->query(" UPDATE ".$this->hun_user_tabla." SET `szabaly`=1, `datum` = '$Date' WHERE user_id = '$uid' ");
		elseif(eng())
			$query = $this->db->query(" UPDATE ".$this->user_tabla." SET `szabaly`=1, `datum` = '$Date' WHERE user_id = '$uid' ");
	}
	
	function temabeszur($temacim){
		if($temacim != ""){
			
			$tomb = $this->phpsession->get('fuser');
			$user = $tomb['name'];
			$lang = $tomb['lang'];
			$user_id = $this->get_userid($user);
			
			$record = $this->mdb->set_post_object('temak');
			$record->tema_id = 'NULL';
			$record->cim = $temacim;
			$record->megnyito = $tomb['name'];
			$record->nyelv = $this->muser->user['lang'];
			$record->datum = date('Y-m-d H:i:s',$this->most);
			
			$temakor_id = 0;
			$temakor_id = $this->phpsession->get('temakor');
			
			$this->load->model('mdb');
			$query = "INSERT INTO `cadline2`.`temak` VALUES (NULL,'" . $temacim ."','" . $user_id ."','" . $lang. "','".$record->datum."', ".$temakor_id.",'0')";
			
			$this->db->query($query);
		}
	}
	
	function temakorbeszur($temacim){
		if($temacim != ""){
			
			$tomb = $this->phpsession->get('fuser');
			$user = $tomb['name'];
			$lang = $tomb['lang'];
			$user_id = $this->get_userid($user);
			
			$record = $this->mdb->set_post_object('temak');
			$record->tema_id = 'NULL';
			$record->cim = $temacim;
			$record->megnyito = $tomb['name'];
			$record->nyelv = $this->muser->user['lang'];
			$record->datum = date('Y-m-d H:i:s',$this->most);
			
			$temakor_id = 0;
			
			$this->load->model('mdb');
			$query = "INSERT INTO `cadline2`.`temakorok` VALUES (NULL,'" . $temacim ."','" . $user_id ."','" . $lang. "','".$record->datum."', '0')";
			
			$this->db->query($query);
		}
	}
	
	function letezik($tema_id){
		return true;
	}
	
	function hszbeszur($szoveg,$tema_id,$vsz,$kep = "NULL"){
		if($szoveg != ""){ //&& letezik($tema_id)){
			if($vsz){
				$query = " SELECT tema_id FROM hozzaszolasok WHERE hsz_id = '$vsz'";
				$query = $this->db->query($query)->row();
				$hsz_temaja = $query->tema_id;
				if( $hsz_temaja != $tema_id ) $vsz = 'NULL';
			}
			
			$tomb = $this->phpsession->get('fuser');
			$user = $tomb['name'];
			$user_id = $tomb['user_id'];
			$lang = $this->muser->user['lang'];
			$datum = date('Y-m-d H:i:s',$this->most);
			if(!$user_id){
				if(hun()) print "Nincs bejelentkezve";
				else print "You don't login";
				exit;
			}
			
			$ujtomb = array();
			$ujtomb['tema_id'] = $tema_id;
			$ujtomb['szoveg']  = $szoveg;
			$ujtomb['user']    = $user_id;
			$ujtomb['valasz']  = $vsz;
			$ujtomb['kep']     = $kep;
			$ujtomb['datum']   = date('Y-m-d H:i:s',$this->most);
			$ujtomb['nyelv']   = $this->muser->user['lang'];
			$ujtomb['deleted'] = 0; 
			
			$this->db->insert("hozzaszolasok",$ujtomb);
			
		}
	}
	
	function moderator($fuser){
		if(hun())
			$query = $this->userdb->from($this->hun_user_tabla)->where("login",$fuser);
		elseif(eng())
			$query = $this->userdb->from($this->user_tabla)->where("login",$fuser);
		
		$query = $query->select('rang')->get()->row();
		
		if( isset($query->rang) && $query->rang == "moderator" )
			return true;
		else
			return false;
	}
	
	function hszszerk($ujszoveg,$hszid){
		$this->db->update( "hozzaszolasok", array('szoveg'=>$ujszoveg), array('hsz_id'=>$hszid) );
	}
	
	function tatnevez($tema_id,$ujcim){
		$query = " UPDATE `cadline2`.`temak` SET `cim` = '$ujcim' WHERE `tema_id` = $tema_id";
		$query = $this->db->query($query);
	}
	
	function tkatnevez($tema_id,$ujcim){
		$query = " UPDATE `cadline2`.`temakorok` SET `cim` = '$ujcim' WHERE `temakor_id` = $tema_id";
		$query = $this->db->query($query);
	}
	
	function get_Text($hszid){
		$query = $this->db->query("SELECT szoveg FROM `cadline2`.`hozzaszolasok` WHERE hsz_id = $hszid")->row();
		return $query;
	}
	
	function get_sajathsz($uid, $hszid){
		$query = $this->db->query(" SELECT user FROM `cadline2`.`hozzaszolasok` WHERE hsz_id = $hszid")->row();
		
		if( $query->user == $uid )
			return true;
		else
			return false;
	}
	
	function get_Kep($hszid){
		$query = $this->db->query("SELECT kep FROM hozzaszolasok WHERE hsz_id = $hszid")->row();
		return $query;
	}
	
	function get_hsz($hszid){
		if( $hszid == 0 || $hszid == "" )
			return false;
		
		if(hun())
			$query = $this->db->query(" SELECT f.nev user, h.datum, h.user user_id, h.szoveg FROM ".$this->hun_user_tabla." f, `cadline2`.`hozzaszolasok` h WHERE hsz_id=$hszid AND f.user_id = h.user AND h.deleted = 0")->row();
		else
			$query = $this->db->query(" SELECT f.nev user, h.datum, h.user user_id, h.szoveg FROM ".$this->user_tabla." f, `cadline2`.`hozzaszolasok` h WHERE hsz_id=$hszid AND f.user_id = h.user AND h.deleted = 0")->row();
		
		return $query;
	}
	
	function get_hsz2($hszid){
		$query = $this->db->query(" SELECT user, datum FROM hozzaszolasok WHERE hsz_id=$hszid AND deleted = 0")->row();
		return $query;
	}
	
	function get_user_szem($login){
		$query = " SELECT f.user_id, f.login, f.nev, f.rang, e.email email, f.ertesites, ff.hlNum aktiv_kod, f.viewsub";
		
		if(UJUGYFELKEZELO===true){
			$query .= " FROM ".$this->hun_user_tabla." f, `".CRMADATBAZIS."`.`users` u, `".CRMADATBAZIS."`.`hardlock` ff, `".CRMADATBAZIS."`.`u_emails` e ";
		}
		else{
			if(hun())
				$query .= " FROM ".$this->hun_user_tabla." f, `clusers`.`cl_users` u, `clusers`.`cl_fo_hardlock` ff, `clusers`.`cl_users_emails` e ";
			elseif(eng())
				$query .= " FROM ".$this->user_tabla." f, `clusers_eng`.`cl_users` u, `clusers_eng`.`cl_fo_hardlock` ff, `clusers_eng`.`cl_users_emails` e ";
		}
		
		$query .= "WHERE e.nUserID = u.nUserID AND ff.hlUser=u.nUserID AND f.nUserID = u.nUserID AND f.login = '$login' AND e.default = 1";
		$query = $this->db->query($query)->result();
		return $query;
	}
	
	function get_nUserID($kulcs){
		//$query = " SELECT h.hlUser FROM `clusers`.`cl_fo_hardlock` h WHERE h.hlNum = '$kulcs' ";
		$query = " SELECT h.hlUser FROM `".CRMADATBAZIS."`.`hardlock` h WHERE h.hlNum = '$kulcs' ";
		$query = $this->db->query($query)->row();
		return $query->hlUser;
	}
	
	function get_nUserID2($uid){
		if(hun())
			$query = " SELECT f.nUserID FROM ".$this->hun_user_tabla." f WHERE f.user_id = '$uid' ";
		else
			$query = " SELECT f.nUserID FROM ".$this->user_tabla." f WHERE f.user_id = '$uid' ";
		
		$query = $this->db->query($query)->row();
		
		if( isset($query->nUserID) )
			return $query->nUserID;
		else
			return false;
	}
	
	function get_nUserID30($email){
		/*if(hun())
			$query = " SELECT f.nUserID FROM `clusers`.`cl_users_emails` f WHERE f.email = '$email' ";
		else
			$query = " SELECT f.nUserID FROM `clusers_eng`.`cl_users_emails` f WHERE f.email = '$email' ";*/
		
		$query = " SELECT f.nUserID FROM `".CRMADATBAZIS."`.`u_emails` f WHERE f.email = '$email' ";
		
		$query = $this->db->query($query)->row();
		if( isset($query->nUserID) )
			return $query->nUserID;
		else
			return false;
	}
	
	function get_user_kulcsok($login){
		$query = " SELECT ff.hlNum kulcs_kod";
		
		if(UJUGYFELKEZELO===true){
			$query .= " FROM cadline2.forum_user f LEFT JOIN ".CRMADATBAZIS.".hardlock ff ON ff.hlUser=f.new_nUserID";
			$query .= " WHERE f.login = '$login' AND";
		}
		else{
			if(hun())
				$query .= " FROM ".$this->hun_user_tabla." f, `clusers`.`cl_users` u, `clusers`.`cl_fo_hardlock` ff";
			elseif(eng())
				$query .= " FROM ".$this->user_tabla." f LEFT JOIN `clusers_eng`.`cl_users` u ON f.nUserID = u.nUserID LEFT JOIN `clusers_eng`.`cl_fo_hardlock` ff ON ff.hlUser=u.nUserID";
			$query .= " WHERE ff.hlUser=u.nUserID AND f.nUserID = u.nUserID AND f.login = '$login' AND";
		}
		
		
		if(hun())
			$query .= " ff.hlCtrID='36'";
		elseif(eng())
			$query .= " ff.hlCtrID='44'";
			
		$query .= " AND ff.hlStat != 2";
		$query = $this->db->query($query)->result();
		return $query;
	}
	
    function getProgramsByHlNum($hlNum){
        
		if(UJUGYFELKEZELO===true){
			$sql = "SELECT * FROM ".CRMADATBAZIS.".h_programs p LEFT JOIN ".CRMADATBAZIS.".p_versions v ON v.verID=p.prVerID LEFT JOIN ".CRMADATBAZIS.".p_types t ON t.typeCode=p.prTypeID WHERE prHlNum = '{$hlNum}' ORDER BY prID DESC";
			$query = $this->db->query($sql);
		}
		else{
			if(hun())
				$query = $this->userdb->where('prHlNum', $hlNum)->join('cl_fo_versions', 'cl_fo_versions.verID=cl_fo_programs.prVerID', 'left')->join('cl_fo_types', 'cl_fo_types.typeCode=cl_fo_programs.prTypeID', 'left')->order_by('prID', 'DESC')->get('cl_fo_programs');
			
			elseif(eng()){
				$query = " SELECT * FROM `clusers_eng`.`cl_fo_programs` LEFT JOIN `clusers_eng`.`cl_fo_versions` ON cl_fo_versions.verID=cl_fo_programs.prVerID LEFT JOIN `clusers_eng`.`cl_fo_types` ON cl_fo_types.typeCode=cl_fo_programs.prTypeID WHERE `prHlNum` = '".$hlNum."' ORDER BY `prID` DESC";
				$query = $this->db->query($query);
			}
		}
        
        $result = $query->result();
        return $result;
    }
    
    function getModulesByPrID($prID){
		if(UJUGYFELKEZELO===true){
			$sql = "SELECT * FROM ".CRMADATBAZIS.".p_modules m LEFT JOIN ".CRMADATBAZIS.".p_mtypes mt ON m.mdID=mt.ID WHERE m.mdPrID = {$prID}";
			$query = $this->db->query($sql);
		}
		else{
			if(hun())
				$query = $this->userdb->where('mdPrID', $prID)->join('cl_modultypes', 'cl_modultypes.ID=cl_fo_modules.mdID', 'left')->get('cl_fo_modules');
			elseif(eng()){
				$query = " SELECT * FROM `clusers_eng`.`cl_fo_modules` LEFT JOIN `clusers_eng`.`cl_modultypes` ON cl_modultypes.ID=cl_fo_modules.mdID WHERE mdPrID = ".$prID;
				$query = $this->db->query($query);
			}
		}
        
        $result = $query->result();
        return $result;
    }
    
	function get_user_alap($login){
		if(hun())
			$query = " SELECT f.rang, f.user_id, f.login, f.nev, f.email, f.ertesites, f.viewsub FROM ".$this->hun_user_tabla." f WHERE f.login = '$login' AND nyelv = 'hun'";
		elseif(eng())
			$query = " SELECT f.rang, f.user_id, f.login, f.nev, f.email, f.ertesites FROM ".$this->user_tabla." f WHERE f.login = '$login' AND nyelv = 'eng'";
		
		$query = $this->db->query($query)->result();
		return $query;
	}
	
	function get_user($login){
	
		
		if(UJUGYFELKEZELO===true){
			$query  = " SELECT f.login, f.nev, u.strEmail email, p.prHlNum aktiv_kod, tip.typeName pr_tip, ver.verName";
			$query .= " FROM `cadline2`.`forum_user` f, `".CRMADATBAZIS."`.`users` u, `".CRMADATBAZIS."`.`hardlock` ff, `".CRMADATBAZIS."`.`h_programs` p, `".CRMADATBAZIS."`.`p_types` tip, `".CRMADATBAZIS."`.`p_versions` ver";
			$query .= " WHERE ver.verID=p.prVerID AND tip.typeCode=p.prTypeID AND p.prHlNum = ff.hlNum AND ff.hlUser=u.nUserID AND f.nUserID = u.nUserID AND f.login = '$login'";
			$query = $this->db->query($query)->result();
		}
		else{
		
			$query = " SELECT ff.hlNum aktiv_kod FROM ".$this->hun_user_tabla." f, `clusers`.`cl_users` u, `clusers`.`cl_fo_hardlock` ff WHERE ff.hlUser=u.nUserID AND f.nUserID = u.nUserID AND f.login = '$login'";
			$kulcsok = $this->db->query($query)->result();
			
			$query = " SELECT f.login, f.nev, u.strEmail email, p.prHlNum aktiv_kod, tip.typeName pr_tip, ver.verName FROM `clusers`.`forum_user` f, `clusers`.`cl_users` u, `clusers`.`cl_fo_hardlock` ff, `clusers`.`cl_fo_programs` p, `clusers`.`cl_fo_types` tip, `clusers`.`cl_fo_versions` ver WHERE ver.verID=p.prVerID AND tip.typeCode=p.prTypeID AND p.prHlNum = ff.hlNum AND ff.hlUser=u.nUserID AND f.nUserID = u.nUserID AND f.login = '$login'";
			$query = $this->db->query($query)->result();
		}
		return $query;
	}
	
	function get_userUid($uid,$nyelv=false){
		if($nyelv){
			if($nyelv == "hun")
				$query = $this->db->query(" SELECT * FROM ".$this->hun_user_tabla." WHERE user_id = $uid")->row();
			else
				$query = $this->db->query(" SELECT * FROM ".$this->user_tabla." WHERE user_id = $uid")->row();
		}
		else{
			if(hun())
				$query = $this->db->query(" SELECT * FROM ".$this->hun_user_tabla." WHERE user_id = $uid")->row();
			elseif(eng())
				$query = $this->db->query(" SELECT * FROM ".$this->user_tabla." WHERE user_id = $uid")->row();
		}
		return $query;
	}
	
	function get_login($uid){
		if(hun())
			$query = $this->db->query(" SELECT login FROM ".$this->hun_user_tabla." WHERE user_id = $uid")->row();
		elseif(eng())
			$query = $this->db->query(" SELECT login FROM ".$this->user_tabla." WHERE user_id = $uid")->row();
		return $query->login;
	}
	
	function get_login2($email){
		/*if(hun())
			$query = $this->db->query(" SELECT login FROM ".$this->hun_user_tabla." WHERE email = '$email'")->row();
		else
			$query = $this->db->query(" SELECT login FROM ".$this->user_tabla." WHERE email = '$email' AND nyelv = '".$this->nyelv."'")->row();
		*/
		$query = $this->db->select("login")->where("email",$email)->where("nyelv",$this->nyelv)->get($this->user_tabla)->row();
		
		return $query->login;
	}
	
	function set_user($uid,$nev,$email,$jelszo,$login,$rang){
		if(hun())
			$query = " UPDATE ".$this->hun_user_tabla."";
		elseif(eng())
			$query = " UPDATE ".$this->user_tabla."";
		
		if($jelszo==""){
			$query .= " SET `nev` = '$nev', `login` = '$login', `rang` = '$rang' WHERE `user_id` = $uid";
			
		}
		else{
			$query .= " SET `nev` = '$nev', `jelszo` = MD5('$jelszo'), `login` = '$login', `rang` = '$rang' WHERE `user_id` = $uid";
		}
		$query = $this->db->query($query);
	}
	
	function get_users($oldal,$ord,$vissza){
		$db = $this->userekoldal;
		$oszam = ($db*$oldal);
		if(hun())
			$query = " SELECT * FROM ".$this->hun_user_tabla." f WHERE nyelv = '".$this->nyelv."' ORDER BY $ord";
		elseif(eng())
			$query = " SELECT * FROM ".$this->user_tabla." f WHERE nyelv = '".$this->nyelv."' ORDER BY $ord";
		
		if($vissza)
			$query .= " DESC";
		$query .= " LIMIT $oszam,$db";
		
		$query = $this->db->query($query)->result();
		
		return $query;
	}
	
	function get_kusers($oldal,$log,$nev,$mail,$belep,$kulcs,$ord,$vissza){
		return $this->get_kusersujcrm($oldal,$log,$nev,$mail,$belep,$kulcs,$ord,$vissza);
		
		$db = $this->userekoldal;
		$oszam = ($db*$oldal);
		
		
		// `clusers`.`cl_users` cl_u LEFT JOIN `clusers`.`cl_history` cl_h ON cl_h.nUserID = cl_u.nUserID
		
		
		if(hun())
			$query = " (SELECT user_id, nUserID, login, jelszo, nev, email, rang, szabaly, datum, nyelv, active, ertesites, deleted FROM ".$this->hun_user_tabla." f LEFT JOIN  `clusers`.`cl_fo_hardlock` h ON h.hlUser=f.nUserID";
		elseif(eng())
			$query = " (SELECT user_id, nUserID, login, jelszo, nev, email, rang, szabaly, datum, nyelv, active, ertesites, deleted FROM ".$this->user_tabla." f LEFT JOIN `clusers_eng`.`cl_fo_hardlock` h ON h.hlUser=f.nUserID";
		
		if($belep != -1 && $belep != "" ){
			$query .= " WHERE f.login LIKE '%$log%' AND f.nev LIKE '%$nev%' AND f.email LIKE '%$mail%' AND h.hlNum LIKE '%$kulcs%' AND f.szabaly = '$belep'";
		}
		else{
			$query .= " WHERE h.hlUser=f.nUserID AND f.login LIKE '%$log%' AND f.nev LIKE '%$nev%' AND f.email LIKE '%$mail%' AND h.hlNum LIKE '%$kulcs%'";
		}
		
		if(hun())
			$query .= " AND h.hlCtrId = 36";
		elseif(eng())
			$query .= " AND h.hlCtrId = 44 AND f.nyelv = '".$this->nyelv."'";
		
		
		$query .= " GROUP BY f.login)";
		
		if( $kulcs == "" ){
			$query .= " UNION (";
			
			if(hun())
				$query .= " SELECT user_id, nUserID, login, jelszo, nev, email, rang, szabaly, datum, nyelv, active, ertesites, deleted FROM ".$this->hun_user_tabla." f WHERE f.nUserID IS NULL ";
			elseif(eng())
				$query .= " SELECT user_id, nUserID, login, jelszo, nev, email, rang, szabaly, datum, nyelv, active, ertesites, deleted FROM ".$this->user_tabla." f WHERE f.nUserID IS NULL ";
			
			$query .= "AND f.login LIKE '%$log%' AND f.nev LIKE '%$nev%' AND f.email LIKE '%$mail%' AND f.nyelv = '".$this->nyelv."'";
			
			if($belep != -1 && $belep != "" ){
				$query .= " AND  f.szabaly = '$belep'";
			}
			
			$query .= " GROUP BY f.login )";
		}
		
		$query .= " ORDER BY $ord";
		
		if($vissza)
			$query .= " DESC";
		
		$query .= "  LIMIT $oszam,$db";
	//	print $query;
		$query = $this->db->query($query)->result();
		
		return $query;
	}
	
	
	function get_kusersujcrm($oldal,$log,$nev,$mail,$belep,$kulcs,$ord,$vissza){
		$db = $this->userekoldal;
		$oszam = ($db*$oldal);
		
		
		$query = " (SELECT user_id, new_nUserID, login, jelszo, nev, email, rang, szabaly, datum, nyelv, active, ertesites, deleted FROM ".$this->hun_user_tabla." f LEFT JOIN `".CRMADATBAZIS."`.`hardlock` h ON h.hlUser=f.new_nUserID";
		
		$query .= " WHERE f.login LIKE '%$log%' AND f.nev LIKE '%$nev%' AND f.email LIKE '%$mail%' AND h.hlNum LIKE '%$kulcs%'";
		
		if($belep != -1 && $belep != "" ){
			$query .= " AND f.szabaly = '$belep'";
		}
		
		if(hun())
			$query .= " AND h.hlCtrId = 36";
		elseif(eng())
			$query .= " AND h.hlCtrId = 44 AND f.nyelv = '".$this->nyelv."'";
		
		
		$query .= " GROUP BY f.login)";
		
		if( $kulcs == "" ){
			$query .= " UNION (";
			
			$query .= " SELECT user_id, new_nUserID, login, jelszo, nev, email, rang, szabaly, datum, nyelv, active, ertesites, deleted FROM ".$this->hun_user_tabla." f WHERE f.new_nUserID IS NULL ";
			
			$query .= "AND f.login LIKE '%$log%' AND f.nev LIKE '%$nev%' AND f.email LIKE '%$mail%' AND f.nyelv = '".$this->nyelv."'";
			
			if($belep != -1 && $belep != "" ){
				$query .= " AND  f.szabaly = '$belep'";
			}
			
			$query .= " GROUP BY f.login )";
		}
		
		$query .= " ORDER BY $ord";
		
		if($vissza)
			$query .= " DESC";
		
		$query .= "  LIMIT $oszam,$db";
	//	print $query;
		$query = $this->db->query($query)->result();
		
		return $query;
	}
	
	
	
	function get_usersdb(){
		if(hun())
			$query = " SELECT count(*) szam FROM ".$this->hun_user_tabla." WHERE nyelv = '".$this->nyelv."'";
		elseif(eng())
			$query = " SELECT count(*) szam FROM ".$this->user_tabla." WHERE nyelv = '".$this->nyelv."'";
			
		$query = $this->db->query($query)->row();
		$db = $query->szam - 1;
		return ($db/$this->userekoldal);
	}
	
	function get_kusersdb($klog,$knev,$kmail,$belep,$kulcs){
		return $this->get_kusersdbujcrm($klog,$knev,$kmail,$belep,$kulcs);
		
		if($kulcs)
			return 0;
		
		$query = "SELECT count(*) szam FROM (";
		
		if(hun())
			$query .= " SELECT count(*) szam FROM ".$this->hun_user_tabla." f LEFT JOIN `".CRMADATBAZIS."`.`hardlock` h ON h.hlUser=f.nUserID";
		elseif(eng())
			$query .= " SELECT count(*) szam FROM ".$this->user_tabla." f LEFT JOIN `".CRMADATBAZIS."`.`hardlock` h ON h.hlUser=f.nUserID";
		
		
		if($belep != -1 && $belep != "")
			$query .= " WHERE h.hlUser=f.nUserID AND login LIKE '%$klog%' AND nev LIKE '%$knev%' AND email LIKE '%$kmail%' AND szabaly = '$belep' AND h.hlNum LIKE '%$kulcs%'";
		else
			$query .= "  WHERE h.hlUser=f.nUserID AND login LIKE '%$klog%' AND nev LIKE '%$knev%' AND email LIKE '%$kmail%' AND h.hlNum LIKE '%$kulcs%'";
		
		if(hun())
			$query .= " AND h.hlCtrId = 36 AND f.nyelv = '".$this->nyelv."'";
		elseif(eng())
			$query .= " AND h.hlCtrId = 44 AND f.nyelv = '".$this->nyelv."'";
			
		$query .= " GROUP BY f.login) tabla";
		
		
		$query = $this->db->query($query)->row();
		$db = $query->szam - 1;
		return ($db/$this->userekoldal);
	}
	
	
	function get_kusersdbujcrm($klog,$knev,$kmail,$belep,$kulcs){
		if($kulcs)
			return 0;
		
		$query = "SELECT count(*) szam FROM (";
		
		$query .= " SELECT count(*) szam FROM ".$this->hun_user_tabla." f LEFT JOIN `".CRMADATBAZIS."`.`hardlock` h ON h.hlUser=f.new_nUserID";
		
		
		$query .= "  WHERE login LIKE '%$klog%' AND nev LIKE '%$knev%' AND email LIKE '%$kmail%' AND h.hlNum LIKE '%$kulcs%'";
		
		if($belep != -1 && $belep != "")
			$query .= " AND szabaly = '$belep' ";
		else
		
		if(hun())
			$query .= " AND h.hlCtrId = 36 AND f.nyelv = '".$this->nyelv."'";
		elseif(eng())
			$query .= " AND h.hlCtrId = 44 AND f.nyelv = '".$this->nyelv."'";
			
		$query .= " GROUP BY f.login) tabla";
		
		
		$query = $this->db->query($query)->row();
		$db = $query->szam - 1;
		return ($db/$this->userekoldal);
	}
	
	
	function get_galeria($user_id,$lang){
		$query = " SELECT * FROM `cadline2`.`forum_fajlok` WHERE user_id = '".$user_id."' AND megosztas = 1";
		
		$ret = $this->db->query($query)->result();
		
		return $ret;
	}
	
	function is_email($email){
		if(hun())
			$query = " SELECT login FROM ".$this->hun_user_tabla." WHERE email = '$email' AND nyelv = '".$this->nyelv."'";
		elseif(eng())
			$query = " SELECT login FROM ".$this->user_tabla." WHERE email = '$email' AND nyelv = '".$this->nyelv."'";
		
		$query = $this->db->query($query)->row();
		if( $query->login )
			return true;
		else
			return false;
	}
	
	function is_temaid($tema_id){
		$query = " SELECT megnyito FROM `cadline2`.`temak` WHERE tema_id = $tema_id";
		$query = $this->db->query($query)->row();
		if( $query->megnyito )
			return true;
		else
			return false;
	}
	
	function ujuser($login,$nev,$jsz1,$email,$nUserID,$rang){
		//$this->db->query("SET NAMES latin2");
		if(hun()){
			$query = " INSERT INTO ".$this->hun_user_tabla."";
		}elseif(eng()){
			$query = " INSERT INTO ".$this->user_tabla."";
		}
		
		$query .= " ( `nUserID`, `nev`, `jelszo`, `email`, `login` , `rang`, `nyelv`) VALUES ( '$nUserID', '$nev' , MD5('$jsz1'), '$email', '$login', '$rang', '".$this->nyelv."')";
		$this->db->query($query);
		
		// az új táblába is
		/*if(hun()){
			$nyelv = $this->nyelv;
			$query = " INSERT INTO ".$this->user_tabla."";
			$query .= " ( `nUserID`, `nev`, `jelszo`, `email`, `login` , `rang`, `nyelv`) VALUES ( '$nUserID', '$nev' , MD5('$jsz1'), '$email', '$login', '$rang', '$nyelv')";
			$this->db->query($query);
		}*/
		
		//$this->db->query("SET NAMES utf8");
	}
	
	function ujuser_eng($login,$nev,$jsz1,$email,$nUserID,$rang){
		//$this->db->query("SET NAMES latin2");
		$query = " INSERT INTO `clusers_eng`.`forum_user_eng` ( `nUserID`, `nev`, `jelszo`, `email`, `login` , `rang`) VALUES ( $nUserID, '$nev' , MD5('$jsz1'), '$email', '$login', '$rang')";
		$this->db->query($query);
		//$this->db->query("SET NAMES utf8");
	}
	
	function del_user($id){
		if(hun()){
			//$this->db->query(" DELETE FROM `clusers`.`forum_user` WHERE user_id = $id;");
			$this->db->query(" UPDATE ".$this->hun_user_tabla." SET deleted = 1 WHERE user_id = $id;");
		}
		elseif(eng()){
			//$this->db->query(" DELETE FROM `clusers_eng`.`forum_user_eng` WHERE user_id = $id;");
			$this->db->query(" UPDATE ".$this->user_tabla." SET deleted = 1 WHERE user_id = $id;");
		}
	}
	
	function vanmegoldal($tema_id,$szam){
		$query = " SELECT count(*) szam FROM hozzaszolasok h WHERE tema_id = '$tema_id' AND deleted = 0";
		$query = $this->db->query($query)->row();
		$hszek = $query->szam;
		if( $hszek > ($szam*$this->hszoldal+$this->hszoldal))
			return true;
		else
			return false;
	}
	
	function vanmeguser($oszam){
		if(hun())
			$query = " SELECT count(*) szam FROM ".$this->hun_user_tabla."";
		elseif(eng())
			$query = " SELECT count(*) szam FROM ".$this->user_tabla." WHERE nyelv = '".$this->nyelv."'";
		
		$query = $this->db->query($query)->row();
		$hszek = $query->szam;
		if( $hszek > ($oszam*$this->userekoldal+$this->userekoldal))
			return true;
		else
			return false;
	}
	
	function vanmegkuser($oszam,$klog,$knev,$kmail,$belep,$kulcs){
		if($kulcs)
			return false;
		
		
		if(hun())
			$query = " (SELECT count(*) szam FROM ".$this->hun_user_tabla." f LEFT JOIN  `clusers`.`cl_fo_hardlock` h ON h.hlUser=f.nUserID";
		elseif(eng())
			$query = " (SELECT count(*) szam FROM ".$this->user_tabla." f LEFT JOIN  `clusers_eng`.`cl_fo_hardlock` h ON h.hlUser=f.nUserID";
		
		if($belep != -1 && $belep != "" ){
			$query .= " WHERE h.hlUser=f.nUserID AND f.login LIKE '%$klog%' AND f.nev LIKE '%$knev%' AND f.email LIKE '%$kmail%' AND h.hlNum LIKE '%$kulcs%' AND f.szabaly = '$belep'";
		}
		else{
			$query .= " WHERE h.hlUser=f.nUserID AND f.login LIKE '%$klog%' AND f.nev LIKE '%$knev%' AND f.email LIKE '%$kmail%' AND h.hlNum LIKE '%$kulcs%'";
		}
		
		if(hun())
			$query .= " AND h.hlCtrId = 36 AND f.nyelv = '".$this->nyelv."'";
		elseif(eng())
			$query .= " AND h.hlCtrId = 44 AND f.nyelv = '".$this->nyelv."'";
		
		
		
		$query .= " GROUP BY f.login) UNION (";
		
		
		
		if(hun())
			$query .= " SELECT count(*) szam FROM ".$this->hun_user_tabla." f WHERE f.nUserID IS NULL ";
		elseif(eng())
			$query .= " SELECT count(*) szam FROM ".$this->user_tabla." f WHERE f.nUserID IS NULL ";
		
		$query .= "AND f.login LIKE '%$klog%' AND f.nev LIKE '%$knev%' AND f.email LIKE '%$kmail%' AND f.nyelv = '".$this->nyelv."'";
		
		if($belep != -1 && $belep != "" ){
			$query .= " AND  f.szabaly = '$belep'";
		}
		
		$query .= " GROUP BY f.login )";
		
		
		
		$query = $this->db->query($query)->result();
		$hszek = count($query)-1;
		
		
		if( $hszek > ($oszam*$this->userekoldal+$this->userekoldal))
			return true;
		else
			return false;
		
		
		
		
		
		
		
		
		
		/*
		
		if(hun())
			$query = " SELECT count(*) szam FROM ".$this->hun_user_tabla." f";
		elseif(eng())
			$query = " SELECT count(*) szam FROM ".$this->user_tabla." f";
		
		if($belep != -1 && $belep != "")
			$query .= " WHERE login LIKE '%$klog%' AND nev LIKE '%$knev%' AND email LIKE '%$kmail%' AND szabaly = $belep";// AND sorozat_sz LIKE '%$kssz%'";
		else
			$query .= " WHERE login LIKE '%$klog%' AND nev LIKE '%$knev%' AND email LIKE '%$kmail%'";
		
		if(!hun()){
			$query .= " AND nyelv = '".$this->nyelv."'";
		}
		
		$query = $this->db->query($query)->row();
		$hszek = $query->szam;
		if( $hszek > ($oszam*$this->userekoldal+$this->userekoldal))
			return true;
		else
			return false;
		*/
	}
	
	
	function vanmegkuserujcrm($oszam,$klog,$knev,$kmail,$belep,$kulcs){
		if($kulcs)
			return false;
		
		
		$query = " (SELECT count(*) szam FROM ".$this->hun_user_tabla." f LEFT JOIN  `".CRMADATBAZIS."`.`hardlock` h ON h.hlUser=f.new_nUserID";
		
		$query .= " WHERE f.login LIKE '%$klog%' AND f.nev LIKE '%$knev%' AND f.email LIKE '%$kmail%' AND h.hlNum LIKE '%$kulcs%'";
		
		if($belep != -1 && $belep != "" ){
			$query .= " AND f.szabaly = '$belep' ";
		}
		
		if(hun())
			$query .= " AND h.hlCtrId = 36 AND f.nyelv = '".$this->nyelv."'";
		elseif(eng())
			$query .= " AND h.hlCtrId = 44 AND f.nyelv = '".$this->nyelv."'";
		
		
		$query .= " GROUP BY f.login) UNION (";
		
		
		$query .= " SELECT count(*) szam FROM ".$this->hun_user_tabla." f WHERE f.new_nUserID IS NULL ";
		
		$query .= "AND f.login LIKE '%$klog%' AND f.nev LIKE '%$knev%' AND f.email LIKE '%$kmail%' AND f.nyelv = '".$this->nyelv."'";
		
		if($belep != -1 && $belep != "" ){
			$query .= " AND  f.szabaly = '$belep'";
		}
		
		$query .= " GROUP BY f.login )";
		
		
		
		$query = $this->db->query($query)->result();
		$hszek = count($query)-1;
		
		
		if( $hszek > ($oszam*$this->userekoldal+$this->userekoldal))
			return true;
		else
			return false;
		
		
	}
	
	
	function vanmegtema( $oldalszam, $temakor_id ){
		$lang = $this->muser->user['lang'];
		$query = " SELECT count(*) szam FROM temak t WHERE nyelv = '$lang' AND deleted = 0 AND temakor_id = ".$temakor_id;
		$query = $this->db->query($query)->row();
		$temak = $query->szam;
		if( $temak > ($oldalszam*$this->temaoldal+$this->temaoldal))
			return true;
		else
			return false;
	}
	
	function vanmegtemakor( $oldalszam ){
		$lang = $this->muser->user['lang'];
		$query = " SELECT count(*) szam FROM temakorok WHERE deleted = 0 AND nyelv = '$lang'";
		$query = $this->db->query($query)->row();
		$temak = $query->szam;
		if( $temak > ($oldalszam*$this->temakoroldal+$this->temakoroldal))
			return true;
		else
			return false;
	}
	
	function torol($hszid){
		$query = $this->db->query(" SELECT kep,user FROM `hozzaszolasok` WHERE hsz_id = '$hszid'")->row();
		$kep = $query->kep;
		$uid = $query->user;
		if( $kep ){
			$user_id = $query->user;
			if(hun())
				$mit = "public/img/forum/".$user_id."/".$kep;
			else
				$mit = "public/img/forum/eng/".$user_id."/".$kep;
			chmod($mit, 777);
			//unlink($mit);
		}
		//$query = " DELETE FROM `hozzaszolasok` where hsz_id = '$hszid'";
		$query = " UPDATE `hozzaszolasok` SET deleted = '1' WHERE hsz_id = '$hszid'";
		$this->db->query($query);
		
		return $uid;
	}
	
	function kepki($hszid){
		$query = $this->db->query(" SELECT kep,user FROM `hozzaszolasok` WHERE hsz_id = '$hszid'")->row();
		$kep = $query->kep;
		$uid = $query->user;
		if( $kep ){
			$user_id = $query->user;
			if(hun())
				$mit = "public/img/forum/".$user_id."/".$kep;
			else
				$mit = "public/img/forum/eng/".$user_id."/".$kep;
			chmod($mit, 777);
			unlink($mit);
		}
		
		$query = " UPDATE `cadline2`.`hozzaszolasok` SET `kep` = 'NULL' WHERE `hsz_id` = '$hszid'";
		$this->db->query($query);
	}
	
	function tematorol($temaid){
		//$query = " DELETE FROM `temak` WHERE tema_id = $temaid";
		$query = " UPDATE `temak` SET deleted = 1 WHERE tema_id = $temaid";
		$this->db->query($query);
		/*$kepek = array();
		$kepek = $this->db->query(" SELECT kep, user FROM hozzaszolasok WHERE tema_id = $temaid")->result();
		foreach( $kepek as $kep ){
			$kep2 =  "public/img/forum/".$kep->user."/". $kep->kep;
			chmod($kep2, 777);
			unlink($kep2);
		}*/
		//$query = " DELETE FROM `hozzaszolasok` WHERE tema_id = $temaid";
		$query = " UPDATE `hozzaszolasok` SET deleted = 1 WHERE tema_id = $temaid";
		$this->db->query($query);
	}
	
	function temakortorol($tid){
		//$query = " DELETE FROM `temakorok` WHERE temakor_id = $tid";
		$query = " UPDATE `temakorok` SET deleted = 1 WHERE temakor_id = $tid";
		$this->db->query($query);
		
		$query = " SELECT tema_id FROM `temak` WHERE temakor_id = $tid";
		
		$temak = $this->db->query($query)->result();
		foreach($temak as $tema){
			$this->tematorol($tema->tema_id);
		}
	}
	
	function belep($name,$passwd,$kod = true){
		if($kod)
			$pw = md5($passwd);
		else
			$pw = $passwd;
		
		
		if(hun()){
			$query = $this->userdb->from($this->hun_user_tabla)->where("login",$name)->where('jelszo',$pw)->where('active',"1")->where('deleted != 1')->where("nyelv",'hun');
			//$query = $query->select('count(*) db')->get()->row();
			$query = $this->db->query(" SELECT count(*) db FROM ".$this->hun_user_tabla." WHERE login = '".$name."' AND jelszo = '".$pw."' AND active = 1 AND deleted != 1 AND nyelv = 'hun'")->row();
			//print $this->db->last_query();
		}
		elseif(eng()){
			$query = " SELECT count(*) db FROM ".$this->user_tabla." WHERE login = '".$name."' AND jelszo = '".$pw."' AND nyelv = 'eng' AND active = 1 AND deleted != 1";
			$query = $this->db->query($query)->row();
		}
		
		$db = $query->db;
		
		return $db;
	}
	
	function get_rang($uid){
		$query = " SELECT rang FROM ".$this->user_tabla." WHERE user_id = ".$uid;
		$query = $this->db->query($query)->row();
		
		return $query->rang;
	}
	
	function szabadlogin($login,$myid){
		/*if(hun())
			$query = $this->userdb->from($this->hun_user_tabla)->where('login = "$login"');
		elseif(eng())
			$query = $this->db->from($this->user_tabla)->where('login = "$login"');
		
		$query = $query->where('nyelv',$this->nyelv)->select('user_id')->get()->row();
		*/
		
		if(hun())
			$query = "SELECT user_id FROM ".$this->hun_user_tabla." WHERE login = '".$login."'";
		elseif(eng())
			$query = "SELECT user_id FROM ".$this->user_tabla." WHERE login = '".$login."'";
		
		$query .= " AND nyelv = '".$this->nyelv."'";
		
		$query2 = $this->db->query($query)->row();
		
		
		
		if(!$query2)
			return true;
		else if($query2->user_id == $myid)
			return true;
		else
			return false;
	}
	
	function myedit($login, $nev, $jelszo, $uid, $ertesit=NULL){
		
		if(hun())
			$query = " UPDATE ".$this->hun_user_tabla."";
		elseif(eng())
			$query = " UPDATE ".$this->user_tabla."";
		
		if($jelszo!=""){
			$query .= " SET `nev` = '$nev', `jelszo` = MD5('$jelszo'), `login` = '$login', `ertesites` = '$ertesit' WHERE `user_id` = $uid";
		}
		else{
			$query .= " SET `nev` = '$nev', `login` = '$login', `ertesites` = '$ertesit' WHERE `user_id` = $uid";
		}
		
		if(eng())
			$query .= " AND nyelv = '".$this->nyelv."'";
		
		$query = $this->db->query($query);
		
	//	print $this->db->last_query();
	}
	
	function vegrehajt($parancs){
		$this->db->query($parancs);
	}
	
	function ujjelszo($email){
		
		$chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKMNOPQRSTUVWXYZ0123456789";
		$pass = '' ;
		for($i=0; $i<7; $i++){
			$num = rand() % strlen($chars);
			$pass = $pass . $chars[$num];
		}
		
		
		$rand = rand(100000,999999);
		$hm = $pass;//$rand;
		
		$query = $this->db->from('forum_user')->where('email',$email)->where('nyelv',$this->nyelv);
		$bennevan = $query->select('user_id uid')->get()->row();
		
		if( isset($bennevan->uid) && $bennevan->uid > 0 ){
			if(hun())
				$query = " UPDATE ".$this->hun_user_tabla." SET `jelszo` = MD5('$hm') WHERE `user_id` = '".$bennevan->uid."'";
			elseif(eng())
				$query = " UPDATE ".$this->user_tabla." SET `jelszo` = MD5('$hm') WHERE `user_id` = '".$bennevan->uid."'";
			
			$this->db->query($query);
			return $hm;
		}
		else{
			return false;
		}
	}
	
	function jelszovaltmod($s){
		$query = " UPDATE ".$this->hun_user_tabla." SET `jelszo` = MD5('archxp2011$s') WHERE login = 'moderator' ";
		$this->db->query($query);
	}
	
	function default_mail($email){
		$lang = $this->muser->user['lang'];
		$sql1 = "SELECT nUserID FROM `cadline2`.`forum_user` WHERE nUserID IS NOT NULL AND nUserID!='' AND email = '{$email}' AND nyelv='{$lang}'";
		$res1 = $this->db->query($sql1)->row();
		
		if(!is_numeric($res1->nUserID))
			return false;
		
		$sql2 = "SELECT email FROM `".CRMADATBAZIS."`.`u_emails` WHERE `default` = 1 AND nUserID = ".$res1->nUserID;
		$res2 = $this->db->query($sql2)->row();
		return $res2->email;
		
		/*$query = "SELECT email FROM `".CRMADATBAZIS."`.`u_emails` WHERE `default` = 1 AND nUserID IN (SELECT nUserID FROM `cadline2`.`forum_user` WHERE email = '".$email."')";
		$nUserID = $this->db->query($query)->row();
		
		return $nUserID->email;*/
	}
	
	function get_cl_users(){
		//$query = "SELECT DISTINCT ( cl_u.nUserID), cl_u.strName,cl_u.strEMail FROM `clusers`.`cl_users` cl_u LEFT JOIN `clusers`.`cl_history` cl_h ON cl_h.nUserID = cl_u.nUserID WHERE cl_u.nUserType = '1' ORDER BY cl_u.strName ASC";
		
		$query = "SELECT DISTINCT ( cl_u.nUserID), cl_u.strName, cl_u.strEMail FROM `".CRMADATBAZIS."`.`users` cl_u LEFT JOIN `".CRMADATBAZIS."`.`u_history` cl_h ON cl_h.nUserID = cl_u.nUserID WHERE cl_u.nUserType = '1' ORDER BY cl_u.strName ASC";
		
		$users = $this->db->query($query)->result();
		//print "<pre>";
		//print_r($users);
		
		return $users;
	}
	
	function get_cl_users_eng(){
		//$query = $this->db->from('`clusers_eng`.`cl_fo_hardlock`')->join('`clusers_eng`.`cl_users`','cl_fo_hardlock.hlUser=cl_users.nUserID','left')->orderby('hlNum','desc')->where("hlCtrID ='44'")->where('hlStat',1)->limit(99999)->get();
		
		$query = $this->db->from('`'.CRMADATBAZIS.'`.`hardlock`')->join('`'.CRMADATBAZIS.'`.`users`','hardlock.hlUser=users.nUserID','left')->orderby('hlNum','desc')->where("hlCtrID ='44'")->where('hlStat',1)->limit(99999)->get();
		
		$users = $query->result();
		/*print "<pre>";
		print_r($users);*/
		
		return $users;
	}
	
	function cseremail($nUserID){
		$ret = "";
		
		if(hun()){
			$query1 = "SELECT email FROM `".CRMADATBAZIS."`.`u_emails` WHERE `default` = 1 AND nUserID = ".$nUserID;
			$query2 = "SELECT email FROM ".$this->hun_user_tabla." WHERE nUserID = ".$nUserID;
			
			$cl_email = $this->db->query($query1)->row();
			$cl_email = $cl_email->email;
			$forum_email = $this->db->query($query2)->row();
			$forum_email = $forum_email->email;
			
			if( $cl_email != $forum_email && $cl_email != "" && $forum_email != "" ){
				//print "<br />cl: ".$cl_email.", forum: ".$forum_email."<br /><br /><br /><br /><br /><br />";
				
				//  Uj email cim beirasa a forum_user-be
				$query = " UPDATE ".$this->hun_user_tabla." SET `email` = '".$cl_email."' WHERE nUserID = ".$nUserID;
				//$this->db->query($query);
				
				$ret = $nUserID . " Kicserélve ".$forum_email.", erre: ".$cl_email."<br />";
			}
		}
		
		return $ret;
	}
	
	function nincsaktiv(){
		$query1 = "SELECT * FROM `".CRMADATBAZIS."`.`users` clu, ".$this->hun_user_tabla." fu WHERE clu.nUserID = fu.nUserID";
		$userek = $this->db->query($query1)->result();
		
		foreach($userek as $user){
			
			$nUserID = $user->nUserID;
			
			$query2 = "SELECT hlNum FROM `".CRMADATBAZIS."`.`hardlock` hl WHERE hlUser = ".$nUserID." AND hl.hlCtrID='36' AND hlStat != 2";
			$kulcsok = $this->db->query($query2)->result();
			
			foreach($kulcsok as $kulcs){
				$query3 = "SELECT * FROM `".CRMADATBAZIS."`.`h_programs` pr WHERE prHlNum = '".$kulcs->hlNum."' AND prStat = 0";
				$programok= $this->db->query($query3)->result();
				if( $programok == array() )
					print "Nincs aktív program: ".$user->strName;
			}
		}
	}
	
	function vanaktivprog($login){
		/*if( $login != '1231212132121254651' )
			return true;*/
		
		if(hun())
			$u = $this->db->query(" SELECT nUserID, rang FROM ".$this->hun_user_tabla." fu WHERE login = '".$login."'")->row();
		else
			$u = $this->db->query(" SELECT nUserID, rang FROM ".$this->user_tabla." fu WHERE login = '".$login."'")->row();
		
	//	if(!isset($u->rang))
	//		return false;
		
		if( $u->rang == "moderator" || $u->nUserID == "" )
			return true;
		
		$nUserID = $u->nUserID;
		
		if(hun())
			$query2 = "SELECT hlNum FROM `".CRMADATBAZIS."`.`hardlock` hl WHERE hlUser = ".$nUserID." AND hl.hlCtrID='36' AND hlStat != 2";
		else
			$query2 = "SELECT hlNum FROM `".CRMADATBAZIS."`.`hardlock` hl WHERE hlUser = ".$nUserID." AND hl.hlCtrID='44' AND hlStat != 2";
		$kulcsok = $this->db->query($query2)->result();
		
		foreach($kulcsok as $kulcs){
			$query3 = "SELECT * FROM `".CRMADATBAZIS."`.`h_programs` pr WHERE prHlNum = '".$kulcs->hlNum."' AND prStat = 0";
			$programok= $this->db->query($query3)->result();
			//print "<pre>";
			//print_r($programok);
		}
		
		if($programok != array())
			return true;
		else
			return false;
	}
	
	function feltolt($uid,$fajl,$meret,$szamla,$megoszt,$info,$lang="hun"){
		//$lang = $this->muser->user['lang'];
		$datum = date('Y-m-d H:i:s',time());
		$query = " INSERT INTO `cadline2`.`forum_fajlok` (`fajl_id`, `user_id`, `nyelv`, `fajl`, `fajl_meret`, `szamla`, `megosztas`, `info`, `datum`) VALUES( NULL , '".$uid."', '".$lang."', '".$fajl."', '".$meret."', '".$szamla."' , '".$megoszt."' , '".$info."' , '".$datum."' )";
		
		$this->db->query($query);
	}
	
	function fajlok($uid){
		$query = " SELECT * FROM `cadline2`.`forum_fajlok` WHERE user_id = '".$uid."'";
		
		$query = $this->db->query($query)->result();
		return $query;
	}
	
	function sajatfajl($fajl_id, $user_id){
		$query = " SELECT * FROM `cadline2`.`forum_fajlok` WHERE user_id = '".$user_id."' AND fajl_id = '".$fajl_id."'";
		
		$ered = $this->db->query($query)->row();
		
		if( $ered->fajl_id )
			return true;
		else
			return false;
	}
	
	function fajlinfo($fajl_id){
		$q1 = " SELECT * FROM `cadline2`.`forum_fajlok` WHERE fajl_id = '".$fajl_id."'";
		$q1 = $this->db->query($q1)->row();
		return $q1;
	}
	
	function delfajl($fajl_id){
		$q1 = " SELECT * FROM `cadline2`.`forum_fajlok` WHERE fajl_id = '".$fajl_id."'";
		$q1 = $this->db->query($q1)->row();
		$fajl = $q1->fajl;
		
		if( $fajl ){
			$user_id = $q1->user_id;
			if(hun())
				$mit = "public/forum-galeria/hun/".$user_id."/".$fajl;
			else
				$mit = "public/forum-galeria/eng/".$user_id."/".$fajl;
			chmod($mit, 777);
			unlink($mit);
		}
		
		$query = " DELETE FROM `cadline2`.`forum_fajlok` WHERE fajl_id = ".$fajl_id;
		
		$this->db->query($query);
		
		return $fajl;
	}
	
	function editgaleria($fajl_id,$leiras,$megoszt){
		$query = " UPDATE `cadline2`.`forum_fajlok` SET `info` = '".$leiras."', `megosztas` = '".$megoszt."' WHERE fajl_id = '".$fajl_id."'";
		
		$this->db->query($query);
	}
	
	function confirm($kod){
		$query = " SELECT * FROM `cadline2`.`forum_user_temp` WHERE kod = '".$kod."' AND deleted = 0";
		$sor = $this->db->query($query)->row();
		
		if( $sor->login != "" && $sor->jelszo != "" ){
			$beszur = " INSERT INTO ".$this->user_tabla." (`login`, `jelszo`, `nev`, `email`, `rang`, `nyelv`) VALUES ('".$sor->login."', '".$sor->jelszo."', '".$sor->nev."', '".$sor->login."', '".$sor->rang."', '".$this->nyelv."') ";
			$this->db->query($beszur);
			
			$atir = " UPDATE `cadline2`.`forum_user_temp` SET `deleted` = '1' WHERE temp_id = ".$sor->temp_id;
			$this->db->query($atir);
			
			return true;
		}else{
			return false;
		}
	}
	
	function get_userByKod($kod){
		$query = " SELECT * FROM `cadline2`.`forum_user_temp` WHERE kod = '".$kod."' AND deleted = 1";
		$sor = $this->db->query($query)->row();
		
		return $sor;
	}
	
	function ertesites($tema_id){
		if(hun())
			$lang = "hun";
		else
			$lang = "eng";
		
		$query = " SELECT * FROM `cadline2`.`forum_user` WHERE (`ertesites` = 'mind' OR `ertesites` LIKE '%;".$tema_id.";%') AND `rang` = 'moderator' AND nyelv = '".$lang."' ";
		
		$query = $this->db->query($query)->result();
		return $query;
	}
	
	function get_row($uid){
		$query = " SELECT * FROM `cadline2`.`forum_user` WHERE user_id = '".$uid."' ";
		
		$query = $this->db->query($query)->row();
		return $query;
	}
	
}
?>