<?php
require_once(dirname(__FILE__).'/../../config.php');
require_once(DIR_SYSTEM.'/users/class.session.user.php');

$USER_ID	=	$_SESSION[USER_SESSION]['0']['user_id'];

$t	=	$prepare->sanitize($_GET['t'],'input');

if ( $t == 'text-ad' ) {
$id	=	$prepare->sanitize($_POST['id'],'input');


		if ( empty ( $id ) ) {
			//*** advertisers_ad_units ***//
			$aau_campaign		=	$prepare->sanitize($_POST['campaign'],'input');
			$aau_user_id		=	$USER_ID;
			$aau_type			=	'1';
			$aau_size			=	'0';
			$aau_target_device	=	'';
			$aau_target_country	=	$prepare->sanitize($_POST['country'],'input');
			$aau_language_id	=	'';
			$aau_adult			=	'0';
			$aau_title			=	$prepare->sanitize($_POST['title'],'input');
			$aau_summary		=	$prepare->sanitize($_POST['summary_1'],'input');
			$aau_summary_2		=	$prepare->sanitize($_POST['summary_2'],'input');
			$aau_url			=	$prepare->sanitize($_POST['display_url'],'input');
			$aau_target_url		=	$prepare->sanitize($_POST['target_url'],'input');
			$aau_file			=	'';
			$aau_status			=	'1';


			$insert = $db->NoQuery("INSERT INTO advertisers_ad_units VALUES(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)","sssssssssssssss",$aau_campaign,$aau_user_id,$aau_type,$aau_size,$aau_target_device,$aau_target_country,$aau_language_id,$aau_adult,$aau_title,$aau_summary,$aau_summary_2,$aau_url,$aau_target_url,$aau_file,$aau_status);
			
			if ( $insert ) {
			
			$aaus_ad_id			=	$db->LastID();
			$aaus_impressions	=	'0';
			$aaus_clicks		=	'0';
			$aaus_money_spent	=	'0';

			$db->NoQuery("INSERT INTO advertisers_ad_units_statistics VALUES(?,?,?,?)","ssss",$aaus_ad_id,$aaus_impressions,$aaus_clicks,$aaus_money_spent);
			$db->NoQuery("update advertisers_profiles set adv_profile_ads=adv_profile_ads+1 where adv_profile_user_id=?","i",$USER_ID);
				echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_ADVERTISER_AD_CREATED"].'</div><div id="ad_id">'. $aaus_ad_id .'</div>';
			} else {
				echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_ADVERTISER_AD_ERROR"].'</div>';
			}

		} else {
			//*** advertisers_ad_units ***//
			$aau_id				=	$prepare->sanitize($_POST['id'],'input');
			$aau_campaign		=	$prepare->sanitize($_POST['campaign'],'input');
			$aau_user_id		=	$USER_ID;
			$aau_type			=	'1';
			$aau_size			=	'0';
			$aau_target_device	=	'';
			$aau_target_country	=	$prepare->sanitize($_POST['country'],'input');
			$aau_language_id	=	'';
			$aau_adult			=	'0';
			$aau_title			=	$prepare->sanitize($_POST['title'],'input');
			$aau_summary		=	$prepare->sanitize($_POST['summary_1'],'input');
			$aau_summary_2		=	$prepare->sanitize($_POST['summary_2'],'input');
			$aau_url			=	$prepare->sanitize($_POST['display_url'],'input');
			$aau_target_url		=	$prepare->sanitize($_POST['target_url'],'input');
			$aau_file			=	'';
			$aau_status			=	'1';


			$update	=	$db->NoQuery("update advertisers_ad_units set aau_campaign=?, aau_target_device=?, aau_target_country=?, aau_language_id=?, aau_adult=?, aau_title=?, aau_summary=?, aau_summary_2=?, aau_url=?, aau_target_url=? where aau_user_id=? and aau_id=?","ssssssssssii",$aau_campaign,$aau_target_device,$aau_target_country,$aau_language_id,$aau_adult,$aau_title,$aau_summary,$aau_summary_2,$aau_url,$aau_target_url,$aau_user_id,$aau_id);
			if ( $update ) {
				echo '<div id="mode">3</div><div id="message">'.$LANG["WBS_ADVERTISER_AD_UPDATED"].'</div>';
			} else {
				echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_ADVERTISER_AD_ERROR"].'</div>';
			}
		}
}




if ( $t == 'ad-status' ) {
	$mode	=	$prepare->sanitize($_POST['mode'],'input');
	$id	=	$prepare->sanitize($_POST['id'],'input');

	$getStatus	=	$db->Query("select `aau_status` from advertisers_ad_units where aau_user_id=? and aau_id=?","ii",$USER_ID,$id);

if ( $getStatus['0']['aau_status'] != 3 ) {

	if ( $mode == 'start' ) {
			$db->NoQuery("update advertisers_ad_units set aau_status=? where aau_user_id=? and aau_id=?","iii",1,$USER_ID,$id);
	}

	if ( $mode == 'stop' ) {
			$db->NoQuery("update advertisers_ad_units set aau_status=? where aau_user_id=? and aau_id=?","iii",3,$USER_ID,$id);
	}

	if ( $mode == 'pause' ) {
			$db->NoQuery("update advertisers_ad_units set aau_status=? where aau_user_id=? and aau_id=?","iii",2,$USER_ID,$id);
	}
}

}




if ( $t == 'keyword' ) {
	$aauk_keyword	=	$prepare->sanitize($_POST['keyword'],'input');
	$aauk_price		=	$prepare->sanitize($_POST['value'],'input');
	$aauk_ad_id		=	$prepare->sanitize($_POST['ad_id'],'input');
	$aauk_clicks	=	'0';
	$aauk_views		=	'0';


$maxValue	=	$db->Query("select `aauk_price` from advertisers_ad_units_keywords where aauk_keyword=? and aauk_ad_id!=?","ss",$aauk_keyword,$aauk_ad_id);

if ( empty ( $maxValue ) ) { $rVal = '???'; } else { $rVal = $maxValue['0']['aauk_price'] + INCREASE_VALUE; }



$validate	=	$db->Query("select `aauk_price` from advertisers_ad_units_keywords where aauk_keyword=? and aauk_ad_id=?","ss",$aauk_keyword,$aauk_ad_id);

if ( empty ( $validate ) ) {
	$aauk_ad_id		=	$aauk_ad_id;
	$aauk_keyword	=	$aauk_keyword;
	$aauk_price		=	$aauk_price;
	$aauk_status	=	'1';
	
	$insert = $db->NoQuery("INSERT INTO advertisers_ad_units_keywords VALUES(NULL,?,?,?,?,?,?)","ssssss",$aauk_ad_id,$aauk_keyword,$aauk_price,$aauk_clicks,$aauk_views,$aauk_status);
	$K_ID	=	$db->LastID();
	if ( $insert ) {
		echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_ADVERTISER_AD_KEYWORD_CREATED"].$K_ID.'</div><div id="keyword_id">'.$K_ID.'</div><div id="rval">'. $rVal .'</div>';
	} else {
		echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_ADVERTISER_AD_KEYWORD_ERROR"].'</div><div id="LAST_ID"></div><div id="rval">'. $rVal .'</div>';
	}

} else {
		echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_ADVERTISER_AD_KEYWORD_ERROR_DOUBLEK"].'</div><div id="LAST_ID"></div><div id="rval">'. $rVal .'</div>';
}


}




if ( $t == 'remove-keyword' ) {
	$id 	= $prepare->sanitize( $_POST['id'], 'input' );
	$ad_id	= $prepare->sanitize( $_POST['ad_id'], 'input' );

$validate	=	$db->Query("select `aau_id` from advertisers_ad_units where aau_id=? and aau_user_id=?","ii",$ad_id,$USER_ID);

	if ( empty ( $validate ) ) {
		echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_ADVERTISER_KEYWORDS_ERROR"].'['.$ad_id.']'.'</div>';
	} else {
		$remove = $db->NoQuery("DELETE FROM advertisers_ad_units_keywords WHERE aauk_id=? and aauk_ad_id=?","ii",$id,$ad_id);
		echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_ADVERTISER_KEYWORDS_REMOVED"].'</div>';
	}

}


if ( $t == 'update-keyword' ) {
	$id 	= $prepare->sanitize( $_POST['id'], 'input' );
	$ad_id	= $prepare->sanitize( $_POST['ad_id'], 'input' );
	$value	= $prepare->sanitize( $_POST['value'], 'input' );

$validate	=	$db->Query("select `aau_id` from advertisers_ad_units where aau_id=? and aau_user_id=?","ii",$ad_id,$USER_ID);

	if ( empty ( $validate ) ) {
		echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_ADVERTISER_KEYWORDS_ERROR"].'['.$ad_id.']'.'</div>';
	} else {
		$update = $db->NoQuery("update advertisers_ad_units_keywords set aauk_price=? where aauk_id=? and aauk_ad_id=?","sii",$value,$id,$ad_id);
		echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_ADVERTISER_KEYWORDS_UPDATED"].'</div>';
	}

}












?>