<?php
class Mclusers extends Model
{
	var $ev;
	var $ho;
	var $nap;
	var $most;
	var $hun_cluser_tabla;
	var $eng_cluser_tabla;
	var $hun_email_tabla;
	var $eng_email_tabla;
	var $hun_history_tabla;
	var $eng_history_tabla;
	var $nyelv;
	
	function mclusers()
	{
		parent::Model();
		$now = time();
		
		$this->ev = date('Y', $now);
		$this->ho = date('m', $now);
		$this->nap = date('d', $now);
		$this->most = $now;
		$this->hun_user_tabla  = (UJUGYFELKEZELO===true) ? CRMADATBAZIS.'.users' : 'clusers.cl_users';
		$this->eng_user_tabla  = (UJUGYFELKEZELO===true) ? CRMADATBAZIS.'.users' : 'clusers_eng.cl_users';
		$this->spa_user_tabla  = (UJUGYFELKEZELO===true) ? CRMADATBAZIS.'.users' : 'clusers_spa.cl_users';
		$this->hun_email_tabla = (UJUGYFELKEZELO===true) ? CRMADATBAZIS.'.u_emails' : 'clusers.cl_users_emails';
		$this->eng_email_tabla = (UJUGYFELKEZELO===true) ? CRMADATBAZIS.'.u_emails' : 'clusers_eng.cl_users_emails';
		$this->hun_history_tabla = (UJUGYFELKEZELO===true) ? CRMADATBAZIS.'.u_history' : 'clusers.cl_history';
		$this->eng_history_tabla = (UJUGYFELKEZELO===true) ? CRMADATBAZIS.'.u_history' : 'clusers_eng.cl_history';
		
		$this->nyelv = $this->muser->user['lang'];
	}
	
	
	function addEvent($nUserID, $topicId, $info = "", $lang='hun', $datum=""){
		if(!$datum) $datum = date('Y-m-d',time());
		
		
		if(UJUGYFELKEZELO===true){
			$ujesem = array();
			$ujesem['nUserID']    = $nUserID;
			$ujesem['nTopicID']   = $topicId;
			$ujesem['dateEvent']  = $datum;
			$ujesem['strPlace']   = 'web';
			$ujesem['strInfo']    = $info;
			$ujesem['nManagerID'] = 36;
			
			$this->db->insert($this->hun_history_tabla,$ujesem);
			return;
		}
		
		
		$query = "INSERT INTO ";
		
		if($lang=='hun'){
			$query .= " `clusers`.";
		}
		elseif($lang=='esp'){
			$query .= " `clusers_spa`.";
		}
		else{
			$query .= " `clusers_eng`.";
		}
		
		$query .= "`cl_history` VALUES (NULL, ".$nUserID.", ".$topicId.", '".$datum."', 'web', '".$info."', 0)";
		
		$this->db->query($query);
	}
	
	function addEventEsp($nUserID, $topicId, $info = ""){
		if(UJUGYFELKEZELO===true){
			$ujesem = array();
			$ujesem['nUserID']    = $nUserID;
			$ujesem['nTopicID']   = $topicId;
			$ujesem['dateEvent']  = date('Y-m-d');
			$ujesem['strPlace']   = 'web';
			$ujesem['strInfo']    = $info;
			$ujesem['nManagerID'] = 36;
			
			$this->db->insert($this->hun_history_tabla,$ujesem);
			return;
		}
		
		$query = "INSERT INTO `clusers_spa`.";
		
		$query .= "`cl_history` VALUES (NULL, ".$nUserID.", ".$topicId.", '".date('Y-m-d',time())."', 'web', '".$info."', 0)";
		
		$this->db->query($query);
	}
	
	function get_nUserIDByEmail($email,$lang='hun'){
		if(!$email)
			return false;
		
		if(UJUGYFELKEZELO===true){
			$query = $this->db->query("SELECT nUserID FROM {$this->hun_email_tabla} WHERE email='".$email."' AND deleted='0'")->result();
			
			if( count($query)==1 ){
				return $query[0]->nUserID;
			}
			elseif( count($query)==0 ){
				return false;
			}
			else{
				// ha egyértelműen van olyan, aki nem lakber. iskola
				if($lang != 'hun')
					return false;
				
				$useridlist = ""; //print_r($query);
				foreach($query as $userid){
					$useridlist .= $userid->nUserID . ", ";
				}
				$useridlist = trim($useridlist,", ");
				
				$query2 = "SELECT nUserID FROM {$this->hun_user_tabla} WHERE nUserID IN ({$useridlist}) AND nUserProf!=7 ";
				$query2 = $this->db->query($query2)->result();
				if( count($query2)==1 )
					return $query2[0]->nUserID;
				else
					return false;
			}
			//return;
		}
		
		
		if($lang == 'hun')
			$query = " SELECT nUserID FROM `clusers`.`cl_users_emails` WHERE email = '".$email."' AND deleted = '0' ";
		elseif($lang == 'esp')
			$query = " SELECT nUserID FROM `clusers_spa`.`cl_users` WHERE strEMail = '".$email."'";
		else
			$query = " SELECT nUserID FROM `clusers_eng`.`cl_users_emails` WHERE email = '".$email."' AND deleted = '0' ";
		
		$query = $this->db->query($query)->result();
		
		
		if( count($query)==1 ){
			return $query[0]->nUserID;
		}
		elseif( count($query)==0 ){
			return false;
		}
		else{
			// ha egyértelműen van olyan, aki nem lakber. iskola
			if($lang != 'hun')
				return false;
			
			$useridlist = ""; //print_r($query);
			foreach($query as $userid){
				$useridlist .= $userid->nUserID . ", ";
			}
			$useridlist = trim($useridlist,", ");
			
			$query2 = "SELECT nUserID FROM `clusers`.`cl_users` WHERE nUserID IN ({$useridlist}) AND nUserProf!=7 ";
			$query2 = $this->db->query($query2)->result();
			if( count($query2)==1 )
				return $query2[0]->nUserID;
			else
				return false;
		}
	}
	
	function get_nUserIDByEmailFirst($email,$lang='hun'){
		if(!$email)
			return false;
		
		if($lang != 'eng')
			$query = " SELECT nUserID FROM {$this->hun_email_tabla} WHERE email = '".$email."' AND deleted = '0' ";
		else
			$query = " SELECT nUserID FROM {$this->eng_email_tabla} WHERE email = '".$email."' AND deleted = '0' ";
		
		$query = $this->db->query($query)->result();
		
		if( count($query) > 0 )
			return $query[0]->nUserID;
		else
			return false;
	}
	
	function addUserHun($tomb){
		if(UJUGYFELKEZELO===true){
			if(isset($tomb->strName))
				$tomb->old_db = "clusers";
			else
				$tomb['old_db'] = "clusers";
			$this->db->insert($this->hun_user_tabla,$tomb);
			return mysql_insert_id();
		}
		
		$this->db->insert('clusers.cl_users',$tomb);
		
		return mysql_insert_id();
	}
	
	function addUserEng($tomb){
		if(UJUGYFELKEZELO===true){
			if(isset($tomb->strName))
				$tomb->old_db = "clusers_eng";
			else
				$tomb['old_db'] = "clusers_eng";
			$this->db->insert($this->eng_user_tabla,$tomb);
			return mysql_insert_id();
		}
		
		$this->db->insert('clusers_eng.cl_users',$tomb);
		
		return mysql_insert_id();
	}
	
	function addUserSpa($tomb){
		if(UJUGYFELKEZELO===true){
			if(isset($tomb->strName))
				$tomb->old_db = "clusers_spa";
			else
				$tomb['old_db'] = "clusers_spa";
			$this->db->insert($this->eng_user_tabla,$tomb);
			return mysql_insert_id();
		}
		
		$this->db->insert('clusers_spa.cl_users',$tomb);
		
		return mysql_insert_id();
	}
	
	function addUserEmailHun($tomb){
		if(UJUGYFELKEZELO===true){
			$this->db->insert($this->hun_email_tabla,$tomb);
			return mysql_insert_id();
		}
		
		$this->db->insert('clusers.cl_users_emails',$tomb);
		
		return mysql_insert_id();
	}
	
	function addUserEmailEng($tomb){
		if(UJUGYFELKEZELO===true){
			$this->db->insert($this->eng_email_tabla,$tomb);
			return mysql_insert_id();
		}
		
		$this->db->insert('clusers_eng.cl_users_emails',$tomb);
		
		return mysql_insert_id();
	}
	
	function update_VMStats($VmName, $RdpFileBase64Decoded, $LastActivity, $CpuUsage, $SessionID){
	/*
		ba:
		AzureVM státuszai:

		0 - inicializálatlan, még a gép sincs létrehozva, kezdeti állapot
		1 - gép megvan, de nincs indítás alatt (rdp nincs és 1 percen belül nem volt adat a státuszról)
		2 - induló gép (rdp nincs, de 1 percen belül volt adat a státuszról)
		3 - futó gép (rdp van és 1 percen belül volt adat a státuszról)
		4 - leállás alatt
	*/
		$selectText = "SELECT * FROM ".CRMADATBAZIS.".AzureVM WHERE VMID='".$VmName."'";
		$azureMachine = $this->db->query($selectText)->result();
		if( count($azureMachine)!=1 )
			return false;

		$Date = date('Y-m-d H:i:s', time());
		$txt = "SELECT LAST_STATUS_MODIFIED, RDP FROM ".CRMADATBAZIS.".AzureVM WHERE VMID='".$VmName."'";
		$laststatusmodified = $this->db->query($txt)->row();
		$statusToSet = 1;
		if( ($Date - $laststatusmodified->LAST_STATUS_MODIFIED) > 60 )
		{
			if( $laststatusmodified->RDP == "" )
				$statusToSet = 1;
			else
				return false; //ba ez mikor lehet?
		}
		else
		{
			if( $laststatusmodified->RDP == "" )
				$statusToSet = 2;
			else
				$statusToSet = 3;
		}
		$parancs = "UPDATE ".CRMADATBAZIS.".AzureVM SET `RDP` = '".$RdpFileBase64Decoded."', `LAST_STATUS_MODIFIED` = '$Date', `STATUS` = '".$statusToSet."', `LAST_ACTIVITY` = '".$LastActivity."', `CPU_USAGE` = '".$CpuUsage."' WHERE VMID='".$VmName."'";
		$this->db->query($parancs);

		//ba statisztikázunk kicsit
		$ido = time();
		if( $statusToSet == 3 && $SessionID != "Unknown" ) //ba most indul a gép
		{
			$selectText = "SELECT START_TIME FROM ".CRMADATBAZIS.".AzureSessions WHERE VMID='".$azureMachine[0]->ID."' AND ID =(SELECT max(ID) FROM ".CRMADATBAZIS.".AzureSessions WHERE VMID='".$azureMachine[0]->ID."')";
			$sessiondata = $this->db->query($selectText)->result();
			$startido = $sessiondata[0]->START_TIME;
		
			if( $startido == 0 ) 
			{
				$query = "UPDATE ".CRMADATBAZIS.".AzureSessions SET `START_TIME` = '".$ido."' WHERE VMID='".$azureMachine[0]->ID."' AND SESSION_NAME='".$SessionID."'";
				$this->db->query($query);
			}
		}
		else if( $statusToSet == 2 && $SessionID != "Unknown" && $azureMachine[0]->STATUS == 3)  //ba most áll le a gép
		{
			$query = "UPDATE ".CRMADATBAZIS.".AzureSessions SET `END_TIME` = '".$ido."' WHERE VMID='".$azureMachine[0]->ID."' AND SESSION_NAME='".$SessionID."'";
			$this->db->query($query);
		}
		//
	}
	
	function get_VMStats($VmName){

		$selectText = "SELECT ID, GROUPID FROM ".CRMADATBAZIS.".AzureVM WHERE VMID='".$VmName."'";
		$azureVMs = $this->db->query($selectText)->result();
		if( count($azureVMs)!=1 )
			return false;
		
		$selectText = "SELECT * FROM ".CRMADATBAZIS.".AzureGroups WHERE ID='".$azureVMs[0]->GROUPID."'";
		$azureGroup = $this->db->query($selectText)->result();
		if( count($azureGroup)!=1 )
			return false;

		$selectText = "SELECT * FROM ".CRMADATBAZIS.".h_programs WHERE prAzureVMID='".$azureVMs[0]->ID."'";
		$usrname = $this->db->query($selectText)->result();
		if( count($usrname)!=1 )
			return false;
			
		$selectText = "SELECT ID, SESSION_NAME FROM ".CRMADATBAZIS.".AzureSessions WHERE VMID='".$azureVMs[0]->ID."' AND ID =(SELECT max(ID) FROM ".CRMADATBAZIS.".AzureSessions WHERE VMID='".$azureVMs[0]->ID."')";
		$sessiondata = $this->db->query($selectText)->result();
		
		$mit = $azureGroup[0]->MAX_IDLE_TIME;
		$mut = $azureGroup[0]->MAX_USING_TIME;
		$ssn = $azureGroup[0]->SUBSCRIPTION_NAME;
		$san = $azureGroup[0]->STORAGE_ACCOUNT_NAME;
		$rs = $azureGroup[0]->ROLE_SIZE;
		if( count($sessiondata)!=1 )
			$sid = "Unknown";
		else
			$sid = $sessiondata[0]->SESSION_NAME;
		return array('MaxIdleTime' => $mit, 'MaxUsingTime' => $mut, 'SubscriptionName' => $ssn, 'StorageAccountName' => $san, 'UserName' => $usrname[0]->prPass, 'RoleSize' => $rs, 'SessionID' => $sid);
	}
	
	function userUpdateIfEmpty($emailorid,$tomb){
		if(!is_array($tomb))
			$tomb = $_POST;
		
		if(!is_numeric($emailorid)){
			$nUserID = $this->get_nUserIDByEmail($emailorid);
		}else{
			$nUserID = $emailorid;
		}
		
		if( !is_numeric($nUserID) || $nUserID <= 0){
			return false;
		}
		
		$user = $this->db->query("SELECT * FROM ".CRMADATBAZIS.".users WHERE nUserID={$nUserID}")->row();
		
		if(!$user)
			return false;
		
		$updatetomb = array();
		
		if( !$user->strName && isset($tomb['strName']) )
			$updatetomb['strName'] = $tomb['strName'];
		elseif( !$user->strName && (isset($tomb['strName1']) || isset($tomb['strName2']) ) )
			$updatetomb['strName'] = trim($tomb['strName1']." ".$tomb['strName2']);
		
		if( !$user->strCompany && isset($tomb['strCompany']) )
			$updatetomb['strCompany'] = $tomb['strCompany'];
		
		if( !$user->strTel && isset($tomb['strTel']) )
			$updatetomb['strTel'] = $tomb['strTel'];
		
		if( !$user->strZip && isset($tomb['strZip']) )
			$updatetomb['strZip'] = $tomb['strZip'];
		
		if( !$user->strCity && isset($tomb['strCity']) )
			$updatetomb['strCity'] = $tomb['strCity'];
		
		if( !$user->strStreet && isset($tomb['strStreet']) )
			$updatetomb['strStreet'] = $tomb['strStreet'];
		
		if( !$user->nUserProf && isset($tomb['nUserProf']) )
			$updatetomb['nUserProf'] = $tomb['nUserProf'];
		
		
		if( $updatetomb != array() )
			$this->db->update(CRMADATBAZIS.".users",$updatetomb,"nUserID = ".$nUserID);
		
	}
	
	function levelesemeny($email, $osztaly, $fv, $topicid, $likeegyezes, $ujinfo, $nyelv="", $hibainfo=""){
		$akikkelnemfoglalkozom = array(
			"tomosas@freemail.hu",
			"stud1@t-online.hu",
			"dogado@t-online.hu"
		);
		
		//$ujinfo = str_replace("ő","õ",$ujinfo);
		
		if(!is_numeric($topicid)){
			$record->osztaly = $osztaly;
			$record->fv = $fv;
			$record->hiba = "Hibás topic id: ".$topicid;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			return "nincstopic";
		}
		
		if($nyelv != "eng" )
			$user = $this->db->query(" SELECT nUserID FROM {$this->hun_email_tabla} WHERE email = '".$email."' AND `deleted` = 0")->result();
		
		if( $nyelv == "eng" || (!$user && $nyelv == "") ){
			$user = $this->db->query(" SELECT nUserID FROM {$this->eng_email_tabla} WHERE email = '".$email."' AND `deleted` = 0")->result();
			$nyelv = "eng";
		}
		
		foreach($user as $u){
			$this->_levelbeszuras($u->nUserID, $email, $osztaly, $fv, $topicid, $likeegyezes, $ujinfo, $nyelv, $hibainfo);
		}
		
		/*if( count($user) != 1 && !in_array($email,$akikkelnemfoglalkozom) ){
			$record->osztaly = $osztaly;
			$record->fv = $fv;
			$record->hiba = "Nem egyértelmű a felhasználó: ".$email."\ndarab: ".count($user)."\n".$nyelv." , ".$topicid." , ".$hibainfo;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			return "nincsuser";//redirect($redirect);
		}
		
		
		
		/*if( $nyelv != "eng" )
			$parancs = " SELECT * FROM {$this->hun_history_tabla} WHERE nTopicID = ".$topicid." AND nUserID = ".$user[0]->nUserID." AND strInfo LIKE '%".$likeegyezes."%'";
		else
			$parancs = " SELECT * FROM {$this->eng_history_tabla} WHERE nTopicID = ".$topicid." AND nUserID = ".$user[0]->nUserID." AND strInfo LIKE '%".$likeegyezes."%'";
		
	//	$volteilyen = $this->db->query(" SELECT * FROM `clusers_eng`.`cl_history` WHERE nTopicID = ".$topicid." AND nUserID = ".$user[0]->nUserID." AND strInfo LIKE '%".$likeegyezes."%'")->result();
		$volteilyen = $this->db->query($parancs)->result();
		
		
		if( count($volteilyen) == 0 ){
			if( $nyelv != "eng" ){
				$this->addEvent($user[0]->nUserID, $topicid, $ujinfo);
				
				// esély átállítása
				$user2 = $this->db->query("SELECT nUserType, nEsely FROM {$this->hun_user_tabla} WHERE nUserID=".$user[0]->nUserID)->row();
				if( $user2->nUserType!=1 && $user2->nEsely=="" && in_array($topicid,array(138,126,128,130,136,131,140)) ){
					$levelesemenyek = "138,126,128,130,136,131,140";
					$levelesemenyekszama = $this->db->query("SELECT count(nEventID) darab FROM {$this->hun_history_tabla} WHERE nUserID=".$user[0]->nUserID." AND nTopicID IN ({$levelesemenyek})")->row();
					
					if($levelesemenyekszama->darab >= 2){
						$parancs = "UPDATE {$this->hun_user_tabla} SET nEsely=2 WHERE nUserID=".$user[0]->nUserID;
						$this->db->query($parancs);
					}elseif($levelesemenyekszama->darab == 1){
						$parancs = "UPDATE {$this->hun_user_tabla} SET nEsely=1 WHERE nUserID=".$user[0]->nUserID;
						$this->db->query($parancs);
					}
				}
			}
			else{
				$this->addEvent($user[0]->nUserID, $topicid, $ujinfo, "eng");
				
				// esély átállítása
				$user2 = $this->db->query("SELECT nUserType, nEsely FROM {$this->eng_user_tabla} WHERE nUserID=".$user[0]->nUserID)->row();
				if( $user2->nUserType!=1 && $user2->nEsely=="" && in_array($topicid,array(152,165,155,154,156,163,164,157,147,153,151,176)) ){
					$levelesemenyek = "152,165,155,154,156,163,164,157,147,153,151,176";
					$levelesemenyekszama = $this->db->query("SELECT count(nEventID) darab FROM {$this->eng_history_tabla} WHERE nUserID=".$user[0]->nUserID." AND nTopicID IN ({$levelesemenyek})")->row();
					
					if($levelesemenyekszama->darab >= 2){
						$parancs = "UPDATE {$this->eng_user_tabla} SET nEsely=2 WHERE nUserID=".$user[0]->nUserID;
						$this->db->query($parancs);
					}elseif($levelesemenyekszama->darab == 1){
						$parancs = "UPDATE {$this->eng_user_tabla} SET nEsely=1 WHERE nUserID=".$user[0]->nUserID;
						$this->db->query($parancs);
					}
				}
			}
		}
		else{
			$record->osztaly = $osztaly;
			$record->fv = $fv;
			$record->hiba = "Dupla kattintás, felhasználó: ".$email."\n".$topicid." , ".$hibainfo;
			$record->datum = date('Y-m-d H:i:s',time());
	//		$this->db->insert('a_hibak',$record);
		}*/
		
		return count($volteilyen);
	}
	
	function _levelbeszuras($nUserID, $email, $osztaly, $fv, $topicid, $likeegyezes, $ujinfo, $nyelv="", $hibainfo=""){
		// 1 eseményt csak 1x
		$parancs = " SELECT * FROM {$this->eng_history_tabla} WHERE nTopicID = ".$topicid." AND nUserID = ".$nUserID." AND strInfo LIKE '%".$likeegyezes."%'";
		
		$volteilyen = $this->db->query($parancs)->result();
		
		
		if( count($volteilyen) == 0 ){
			if( $nyelv != "eng" ){
				$this->addEvent($nUserID, $topicid, $ujinfo);
				
				// esély átállítása
				$user2 = $this->db->query("SELECT nUserType, nEsely FROM {$this->hun_user_tabla} WHERE nUserID=".$nUserID)->row();
				if( $user2->nUserType!=1 && $user2->nEsely=="" && in_array($topicid,array(138,126,128,130,136,131,140)) ){
					$levelesemenyek = "138,126,128,130,136,131,140";
					$levelesemenyekszama = $this->db->query("SELECT count(nEventID) darab FROM {$this->hun_history_tabla} WHERE nUserID=".$nUserID." AND nTopicID IN ({$levelesemenyek})")->row();
					
					if($levelesemenyekszama->darab >= 2){
						$parancs = "UPDATE {$this->hun_user_tabla} SET nEsely=2 WHERE nUserID=".$nUserID;
						$this->db->query($parancs);
					}elseif($levelesemenyekszama->darab == 1){
						$parancs = "UPDATE {$this->hun_user_tabla} SET nEsely=1 WHERE nUserID=".$nUserID;
						$this->db->query($parancs);
					}
				}
			}
			else{
				$this->addEvent($nUserID, $topicid, $ujinfo, "eng");
				
				// esély átállítása
				$user2 = $this->db->query("SELECT nUserType, nEsely FROM {$this->eng_user_tabla} WHERE nUserID=".$nUserID)->row();
				if( $user2->nUserType!=1 && $user2->nEsely=="" && in_array($topicid,array(152,165,155,154,156,163,164,157,147,153,151,176)) ){
					$levelesemenyek = "152,165,155,154,156,163,164,157,147,153,151,176";
					$levelesemenyekszama = $this->db->query("SELECT count(nEventID) darab FROM {$this->eng_history_tabla} WHERE nUserID=".$nUserID." AND nTopicID IN ({$levelesemenyek})")->row();
					
					if($levelesemenyekszama->darab >= 2){
						$parancs = "UPDATE {$this->eng_user_tabla} SET nEsely=2 WHERE nUserID=".$nUserID;
						$this->db->query($parancs);
					}elseif($levelesemenyekszama->darab == 1){
						$parancs = "UPDATE {$this->eng_user_tabla} SET nEsely=1 WHERE nUserID=".$nUserID;
						$this->db->query($parancs);
					}
				}
			}
		}
	}
	
	function levelesemenyEsp($email, $osztaly, $fv, $topicid, $likeegyezes, $ujinfo, $nyelv="esp", $hibainfo=""){
		$akikkelnemfoglalkozom = array();
		
		if(!is_numeric($topicid)){
			$record->osztaly = $osztaly;
			$record->fv = $fv;
			$record->hiba = "Hibás topic id: ".$topicid;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			return "nincstopic";
		}
		
		$user = $this->db->query("SELECT nUserID, nUserType, nEsely FROM `clusers_spa`.`cl_users` WHERE strEMail LIKE '%".$email."%'")->result();
		
		
		if( count($user) != 1 && !in_array($email,$akikkelnemfoglalkozom) ){
			$record->osztaly = $osztaly;
			$record->fv = $fv;
			$record->hiba = "Nem egyértelmű a felhasználó: ".$email."\ndarab: ".count($user)."\n".$nyelv." , ".$topicid." , ".$hibainfo;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			return "nincsuser";//redirect($redirect);
		}
		
		
		
		$parancs = " SELECT * FROM `clusers_spa`.`cl_history` WHERE nTopicID = ".$topicid." AND nUserID = ".$user[0]->nUserID." AND strInfo LIKE '%".$likeegyezes."%'";
		
		$volteilyen = $this->db->query(" SELECT * FROM `clusers_spa`.`cl_history` WHERE nTopicID = ".$topicid." AND nUserID = ".$user[0]->nUserID." AND strInfo LIKE '%".$likeegyezes."%'")->result();
		
	/*	if($email == "sztikee@gmail.com")
			print $parancs;*/
		
		if( count($volteilyen) == 0 ){
			$this->addEventEsp($user[0]->nUserID, $topicid, $ujinfo);
			
			if( $user[0]->nUserType!=1 && $user[0]->nEsely=="" && in_array($topicid,array(131,132,133,134,135,136,137)) ){
				$levelesemenyek = "135,136,131,132,133,137,134";
				$levelesemenyekszama = $this->db->query("SELECT count(nEventID) darab FROM `clusers_spa`.`cl_history` WHERE nUserID=".$user[0]->nUserID." AND nTopicID IN ({$levelesemenyek})")->row();
				
				if($levelesemenyekszama->darab >= 2){
					$parancs = "UPDATE `clusers_spa`.`cl_users` SET nEsely=2 WHERE nUserID=".$user[0]->nUserID;
				//	print $parancs;
					$this->db->query($parancs);
				}elseif($levelesemenyekszama->darab == 1){
					$parancs = "UPDATE `clusers_spa`.`cl_users` SET nEsely=1 WHERE nUserID=".$user[0]->nUserID;
				//	print $parancs;
					$this->db->query($parancs);
				}else{
				//	print "0 darab esemeny ...";
				}
			}
		}
		else{
			$record->osztaly = $osztaly;
			$record->fv = $fv;
			$record->hiba = "Dupla kattintás, felhasználó: ".$email."\n".$topicid." , ".$hibainfo;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
		}
		
		return count($volteilyen);
	}
	
	function hlesemeny($kulcs, $esemid, $eseminfo="", $date=""){
		$ujesem = array();
		$ujesem['hiHlNum']  = $kulcs;
		$ujesem['hiDate']   = ($date!="") ? $date : date('Y-m-d');
		$ujesem['hiManID']  = 36;
		$ujesem['hiTypeID'] = $esemid;
		$ujesem['hiTime']   = date('Y-m-d H:i:s');
		$ujesem['hiInfo']   = ($eseminfo!="") ? $eseminfo : NULL;
		
		$this->db->insert(CRMADATBAZIS.".h_history",$ujesem);
		return;
	}
	
}