<?php
/**
 * @package		Ad Platform
 * @author		Uta Marian - Alexandru ( -uMarian- )
 * @license		http://codecanyon.net/licenses
 * @link		http://www.uta-marian.com
 * @version		Version 1.0
 */

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

//** CHECK IF ADVERTISER IS ON or OFF **//
if ( ADVERTISER != 'ON' ) { header ( 'Location: '.HTTP_SERVER_WEBSITE.'dashboard' ); exit; }
//** END CHECK **//
$m			=	$prepare->sanitize($_GET['m'],'input');
$USER_ID	=	$_SESSION[USER_SESSION]['0']['user_id'];
$ID			=	$prepare->sanitize($_GET['campaign'],'input');


$view->campaign	=	$campaign = $db->Query("SELECT `adv_campaign_id`, `adv_campaign_title`, `adv_campaign_details`, `adv_campaign_daily_budget`, `adv_campaign_daily_spent`, `adv_campaign_status` FROM advertisers_campaigns where adv_campaign_user_id=? and adv_campaign_id=?","ii",$USER_ID,$ID);

if ( empty( $campaign ) ) { 
	header ( 'Location: '.HTTP_SERVER_WEBSITE.'dashboard/advertiser/campaigns' ); exit;
}




$view->advertiser = $db->Query("SELECT `adv_profile_deposits`, `adv_profile_spent`, `adv_profile_campaigns`, `adv_profile_ads` FROM advertisers_profiles where adv_profile_user_id=?","i",$USER_ID);

//GET ACTIVE APLICATIONS LIST
$adsList	=	$db->Query("SELECT `aau_id`, `aau_type`, `aau_campaign`, `aau_user_id`, `aau_title`, `aau_status`, `aaus_impressions`, `aaus_clicks`, `aaus_money_spent` FROM advertisers_ad_units left join advertisers_ad_units_statistics on advertisers_ad_units_statistics.aaus_ad_id = advertisers_ad_units.aau_id where aau_user_id=? and aau_campaign=? order by aau_id DESC","ii",$USER_ID,$ID);
$numbers = count ($adsList);

//PAGINATION
$page		=	$prepare->sanitize($_GET['p'], 'input');
$on_page	=	"10";
$page=!empty($page)?$page:"1";
$limitfrom=($page-1)*$on_page;
$limitto=($page*$on_page)-($page-1)*$on_page;
$itemsPerPage=$on_page;
$upsy = $paginator['rows']=$numbers;	 // rows
	  $paginator['pager']=$prepare->page($page,$upsy,$itemsPerPage);
	  $paginator['limitfrom']=$limitfrom+1;
	  $paginator['limitto']=$limitfrom+$upsy;
	  $paginator['itemsnum']=$upsy;
	  $paginator['page']=$page;
$view->paginator	=	$paginator;
//END PAGINATION
$view->ads = array_slice($adsList, $limitfrom, $limitto);
//***********************************************************************//



if ( $m == 'delete' ) {
	$AD_ID	=	$prepare->sanitize($_GET['id'],'input');
	//** GET AD DETAILS FOR DELETING VALIDATION **//
	$view->ad	=	$ad = $db->Query("SELECT `aau_id`, `aau_title` FROM advertisers_ad_units where aau_user_id=? and aau_id=?","ii",$USER_ID,$AD_ID);
}

if ( $m == 'yes-delete' ) {

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

	$validate	=	$ad = $db->Query("SELECT `aau_id`, `aau_title`, `aau_file` FROM advertisers_ad_units where aau_user_id=? and aau_id=?","ii",$USER_ID,$AD_ID);

if ( !empty ( $validate ) ) {
	$AD_ID	=	$prepare->sanitize($_GET['id'],'input');
	$C_ID	=	$prepare->sanitize($_GET['campaign'],'input');

	$db->NoQuery("DELETE FROM advertisers_ad_units WHERE aau_id=?","i",$AD_ID);
	$db->NoQuery("DELETE FROM advertisers_ad_units_statistics WHERE aaus_ad_id=?","i",$AD_ID);
	$db->NoQuery("DELETE FROM advertisers_ad_clicks WHERE aac_ad_id=?","i",$AD_ID);
	$db->NoQuery("DELETE FROM advertisers_ad_units_keywords WHERE aauk_ad_id=?","i",$AD_ID);

$db->NoQuery("update advertisers_profiles set adv_profile_ads=adv_profile_ads-1 where adv_profile_user_id=?","i",$USER_ID);

	if ( !empty ( $validate['0']['aau_file'] ) ) {
		//** REMOVE IT! **//
		$image_location	=	DIR_UPLOADS_BANNER_ADS.$validate['0']['aau_file'];
		unlink($image_location);

	}
}

	header ( 'Location: '.HTTP_SERVER_WEBSITE.'dashboard/advertiser/ads?campaign='.$C_ID ); exit;
}





/* set page title */
$view->title = $LANG["WBS_C_ADS_SEO"];
$view->description = $LANG["WBS_C_ADS_SEO"];
$view->keywords = $LANG["WBS_C_ADS_SEO"];

//**DEFINE CUSTOM CSS/JS FILLES**//
$view->customCode = '<link href="'.HTTP_SERVER_WEBSITE.'templates/'.WEBSITE_TEMPLATE.'/assets/css/dashboard.css" rel="stylesheet">';

$view->advertiser_menu = 'on';
$view->header = $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/header.php');
$view->footer = $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/footer.php');

echo $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/plugins/advertiser/ads.php');
?>