<?php 

$codad = '';
if ($this->param(a)) $codad = $this->db1->e($this->param(a));

$r = $this->db1->query("SELECT * FROM my_ads WHERE code='".$codad."' LIMIT 1");

if ($obj = $this->db1->fetch_object($r)) {
	
	$stringNamecookie = "_adfrly_C_".$obj->idad;
	$valueCookie = $obj->idad;
	
	//statistical
	$idcampania = $this->db1->fetch_field('SELECT idassigned FROM my_ads_plans_assigned WHERE idad='.$obj->idad.' ORDER BY idassigned DESC LIMIT 1');
	$dt1 = date('Y-m-d');	
	$this->db1->query("UPDATE my_ads_clicks_days SET clicks_simple=clicks_simple+1 WHERE idcampania=".$idcampania." AND whendate='".$dt1."' LIMIT 1");
	if( $this->db1->affected_rows() == 0 ) $this->db1->query("INSERT INTO my_ads_clicks_days SET clicks_simple=1, idad=".$obj->idad.", idcampania=".$idcampania.", whendate='".$dt1."'");

	$dt2 = date('Y-m-d H');
	$this->db1->query("UPDATE my_ads_clicks_hours SET clicks_simple=clicks_simple+1 WHERE idcampania=".$idcampania." AND whendate='".$dt2."' LIMIT 1");
	if( $this->db1->affected_rows() == 0 ) $this->db1->query("INSERT INTO my_ads_clicks_hours SET clicks_simple=1, idad=".$obj->idad.", idcampania=".$idcampania.", whendate='".$dt2."'");


	// add clicks 
	$this->db1->query('UPDATE my_ads SET num_clicks=num_clicks+1 WHERE idad='.$obj->idad);
	
	// add unique click
	if ($_COOKIE[$stringNamecookie] == NULL) {
		$this->db1->query('UPDATE my_ads SET num_clicks_unique=num_clicks_unique+1 WHERE idad='.$obj->idad);	

		//statistical
		$this->db1->query("UPDATE my_ads_clicks_days SET clicks_unique=clicks_unique+1 WHERE idcampania=".$idcampania." AND whendate='".$dt1."' LIMIT 1");
		if( $this->db1->affected_rows() == 0 ) $this->db1->query("INSERT INTO my_ads_clicks_days SET clicks_unique=1, idad=".$obj->idad.", idcampania=".$idcampania.", whendate='".$dt1."'");
	
		$this->db1->query("UPDATE my_ads_clicks_hours SET clicks_unique=clicks_unique+1 WHERE idcampania=".$idcampania." AND whendate='".$dt2."' LIMIT 1");
		if( $this->db1->affected_rows() == 0 ) $this->db1->query("INSERT INTO my_ads_clicks_hours SET clicks_unique=1, idad=".$obj->idad.", idcampania=".$idcampania.", whendate='".$dt2."'");
	}

	$url_destination = $obj->url_destination;
	
	setcookie($stringNamecookie, $valueCookie, time() + 1800); //duration cookie 30 minuts
	$this->redirect($url_destination);	
} else {
	// white
}

?>