<?php
$allAds = $this->db1->fetch_all('
SELECT * 
FROM my_ads 
WHERE 
status=1 
AND status_admin=0 
AND available=1 
ORDER BY RAND() 
LIMIT '.$C->ADS_AT_ONCE);

$total = count($allAds);

$D->htmlAdsUsers = '';
$cont = 0;
$D->adsEnds = 0;
foreach($allAds as $oneAds) {
	$typeaction = $oneAds->typeaction;
	if ($typeaction == 1) {
		$availables = $oneAds->impressions_available - $oneAds->num_impressions_unique;
	}

	if ($typeaction == 2) {
		$availables = $oneAds->clicks_available - $oneAds->num_clicks_unique;
	}

	// stopped ads
	if ($availables <= 0) {
		$this->db1->query('UPDATE my_ads SET available=0 WHERE idad='.$oneAds->idad);
	}
	
	// add impressions
	$this->db1->query('UPDATE my_ads SET num_impressions=num_impressions+1 WHERE idad='.$oneAds->idad);

	//statistical
	$idcampania = $this->db1->fetch_field('SELECT idassigned FROM my_ads_plans_assigned WHERE idad='.$oneAds->idad.' ORDER BY idassigned DESC LIMIT 1');
	$dt1 = date('Y-m-d');
	$this->db1->query("UPDATE my_ads_impress_days SET impress_simple=impress_simple+1 WHERE idcampania=".$idcampania." AND whendate='".$dt1."' LIMIT 1");
	if( $this->db1->affected_rows() == 0 ) $this->db1->query("INSERT INTO my_ads_impress_days SET impress_simple=1, idad=".$oneAds->idad.", idcampania=".$idcampania.", whendate='".$dt1."'");

	$dt2 = date('Y-m-d H');
	$this->db1->query("UPDATE my_ads_impress_hours SET impress_simple=impress_simple+1 WHERE idcampania=".$idcampania." AND whendate='".$dt2."' LIMIT 1");
	if( $this->db1->affected_rows() == 0 ) $this->db1->query("INSERT INTO my_ads_impress_hours SET impress_simple=1, idad=".$oneAds->idad.", idcampania=".$idcampania.", whendate='".$dt2."'");
	
	$D->ads_Headline = stripslashes($oneAds->headline);
	$D->ads_Description1 = stripslashes($oneAds->descriptionline1);
	$D->ads_Description2 = stripslashes($oneAds->descriptionline2);
	$D->ads_URLVisible = stripslashes($oneAds->url_visible);
	$D->ads_code = $oneAds->code;
	$D->ads_image = $oneAds->image;

	$cont++;
	if ($cont>=$total) $D->adsEnds = 1;	
	$D->htmlAdsUsers .= $this->load_template('__one-ads.php', FALSE);	
	

	// Cookies and update impressiones
	$stringNamecookie = "_adfrly_I_".$oneAds->idad;
	$valueCookie = $oneAds->idad;
	
	// add unique impressions
	if ($_COOKIE[$stringNamecookie] == NULL) {
		$this->db1->query('UPDATE my_ads SET num_impressions_unique=num_impressions_unique+1 WHERE idad='.$oneAds->idad);
		
		//statistical
		$this->db1->query("UPDATE my_ads_impress_days SET impress_unique=impress_unique+1 WHERE idcampania=".$idcampania." AND whendate='".$dt1."' LIMIT 1");
		if( $this->db1->affected_rows() == 0 ) $this->db1->query("INSERT INTO my_ads_impress_days SET impress_unique=1, idad=".$oneAds->idad.", idcampania=".$idcampania.", whendate='".$dt1."'");
	
		$this->db1->query("UPDATE my_ads_impress_hours SET impress_unique=impress_unique+1 WHERE idcampania=".$idcampania." AND whendate='".$dt2."' LIMIT 1");
		if( $this->db1->affected_rows() == 0 ) $this->db1->query("INSERT INTO my_ads_impress_hours SET impress_unique=1, idad=".$oneAds->idad.", idcampania=".$idcampania.", whendate='".$dt2."'");

	}
	
	setcookie($stringNamecookie, $valueCookie, time() + 1800); //duration cookie 30 minuts
	
}
?>