<?php 
class Trail {
	
	function Trail(){
		global $db;	
		$this->db = $db;
		
		//Setup cookie
		if(!$_COOKIE['ool_trail_identity']){
			$expire=time()+60*60*24*10;
			$hash = sha1(mktime() . "-" . $_SERVER['REMOTE_ADDR']);
			setcookie("ool_trail_identity", $hash, $expire);
			$this->trail_id = $hash;
		}else{
			$this->trail_id =$_COOKIE['ool_trail_identity'];
		}

	}
	
	function InsertLook($id){
		$result = $this->db->query("SELECT product_id,user_id FROM `trails` WHERE hash='" . $this->trail_id . "' ORDER BY time DESC LIMIT 0,1");
		$fetch = $result->fetch();
		
		if(($fetch['user_id'] == 0)AND($_SESSION['user_id'] > 0)){
			$result = $this->db->query("UPDATE `trails` SET user_id='" . $_SESSION['user_id'] . "' WHERE hash='" . $this->trail_id . "'");
		}
		
		if($fetch['0'] != $id){
			$result = $this->db->query("INSERT INTO `trails` (`hash`,`product_id`,`user_id`) VALUE ('" . $this->trail_id . "','" . $id . "','" . $_SESSION['user_id'] . "')");
		}
	}
	
	
	//Zadnjih nekaj za tega uporabnika
	function GetLast($limit = 3,$product_id = false){
		if($product_id){
			$where = "AND product_id!='" . $product_id . "'";
		}
		
		if($limit == 0)
			$LIM = "";
		else
			$LIM = "LIMIT 0,$limit";
		
		$sql = "SELECT DISTINCT product_id FROM `trails` WHERE hash='" . $this->trail_id . "' $where ORDER BY time DESC $LIM";

		$result = $this->db->query($sql);
		while($fetch = $result->fetch()){
			$out .= "|" . $fetch['0'];
		}
		
		return $out;
	}
	
	function GetLastArray($limit = 3,$product_id = false){
		if($product_id){
			$where = "AND product_id!='" . $product_id . "'";
		}
		
		if($limit == 0)
			$LIM = "";
		else
			$LIM = "LIMIT 0,$limit";
		
		$sql="
		SELECT DISTINCT i.*,p.ime AS proiz_ime FROM modul_izdelki i,modul_izdelki_proizvajalci p, trails t
			WHERE
				i.id=t.product_id AND
				t.hash='" . $this->trail_id . "' AND
				i.proizvajalec=p.id
				$where
			ORDER BY t.time DESC $LIM;
		";
		
		$arr=array();
		
		$result = $this->db->query($sql);
		while($fetch = $result->fetch(MYSQL_ASSOC)){
			array_push($arr,$fetch);
		}
		
		return $arr;
	}
	
	//Zadnjih nekaj, ki so imeli tudi ta izdelek
	function GetLastFull($product_id,$limit = false){

		$sql = "SELECT hash FROM `trails` WHERE product_id='" . $product_id . "' GROUP BY hash";
		$result = $this->db->query($sql);
		
		while($fetch = $result->fetch()){ 
			$result2 = $this->db->query("SELECT product_id FROM `trails` WHERE hash='" . $fetch[0] . "' AND product_id!='" . $product_id . "' GROUP BY product_id");
			while($fetch2 = $result2->fetch()){
				$out .= "|" . $fetch2['0'];
			}
		}
		$ex = explode("|",$out);
		$ex2 = array_unique($ex);
		unset($out);
		
		if($limit){
			for($x = 2;$x <= $limit + 1;$x++){
				$out .= "|" . $ex2[$x];
			}
		}else{
			$out = "|" . implode("|",$ex2);
		}
		return $out;
	}
	
	function Size($limit = 3,$product_id = false){
		if($product_id){
			$where = "AND product_id!='" . $product_id . "'";
		}
		
		if($limit == 0)
			$LIM = "";
		else
			$LIM = "LIMIT 0,$limit";
		
		$sql="
		SELECT i.*,p.ime AS proiz_ime FROM modul_izdelki i,modul_izdelki_proizvajalci p, trails t
			WHERE
				i.id=t.product_id AND
				t.hash='" . $this->trail_id . "' AND
				i.proizvajalec=p.id
				$where
			ORDER BY t.time DESC $LIM;
		";
		
		$result=$this->db->query($sql);
		
		return $result->size();
		
	}
}
?>