<?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 **//

$USER_ID	=	$_SESSION[USER_SESSION]['0']['user_id'];
$ID			=	$prepare->sanitize($_GET['id'],'input');


$view->countries = $db->Query("SELECT `country_id`, `country_name` FROM countries");
$view->campaigns = $db->Query("SELECT `adv_campaign_id`, `adv_campaign_user_id`, `adv_campaign_title`, `adv_campaign_daily_budget`, `adv_campaign_status` FROM advertisers_campaigns where adv_campaign_user_id=?","i",$USER_ID);
$view->sizes	 = $db->Query("SELECT `adv_sizes_id`, `adv_sizes_name`, `adv_sizes_width`, `adv_sizes_height` FROM advertisers_sizes");


$view->ad	=	$ad = $db->Query("SELECT `aau_id`, `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` FROM advertisers_ad_units where aau_user_id=? and aau_id=?","ii",$USER_ID,$ID);

if ( !empty ( $ad ) ) {
	$view->get_size = $db->Query("SELECT `adv_sizes_width`, `adv_sizes_height` FROM advertisers_sizes where adv_sizes_id=? limit 1","i",$ad['0']['aau_size']);
}


if ( empty( $ad ) && !empty( $ID ) ) { 
	header ( 'Location: '.HTTP_SERVER_WEBSITE.'dashboard/advertiser/campaigns' ); exit;
}



//*** GET POST REQUEST AND SAVE IMAGE ***//
$upload_directory = DIR_UPLOADS_BANNER_ADS;

if( $_FILES ) {
	$replace_image	=	'false';

//** SET-UP LOGO UPLOAD **//
$object = new chip_upload();
$files = $object->get_upload_var( $_FILES['banner-image'] );



	foreach( $files as $file ) {
        $args = array(
			'upload_file'         =>   $file,
			'upload_directory'    =>   $upload_directory,
			'allowed_size'        =>   MAX_UPLOAD_SIZE,
			'extension_check'     =>   TRUE,
			'upload_overwrite'    =>   FALSE,
	);

	$allowed_extensions = array(
		'pdf'   => FALSE,
		'png'   => TRUE,
		'jpg'   => TRUE,
		'jpeg'   => TRUE,
	);

	$upload_hook = $object->get_upload( $args, $allowed_extensions );
	if( $upload_hook['upload_move'] == TRUE ) {
			$replace_image	=	'true';
            $upload_output = $object->get_upload_move();
			$bannerImage	=	$upload_output['uploaded_file'].'.'.$upload_output['uploaded_extension'];			
        } else {
			$replace_image	=	'false';
		}
    }



$aau_id				=	$prepare->sanitize( $_POST['adv-ad-id'],'input' );
$aau_campaign		=	$prepare->sanitize( $_POST['adv-ad-campaign'],'input' );
$aau_user_id		=	$USER_ID;
$aau_type			=	'2';





//get the size from db!
$size	=	$prepare->sanitize( $_POST['banner-size-value'],'input' );
$eSize	=	explode( "x" , $size);
$get_size = $db->Query("SELECT `adv_sizes_id` FROM advertisers_sizes where adv_sizes_width=? and adv_sizes_height=? limit 1","ii",$eSize['0'],$eSize['1']);




$aau_size			=	$get_size['0']['adv_sizes_id'];
$aau_target_device	=	'';
$aau_target_country	=	$prepare->sanitize( $_POST['adv-ad-country'],'input' );
$aau_language_id	=	'';
$aau_adult			=	'';
$aau_title			=	$prepare->sanitize( $_POST['adv-ad-title'],'input' );
$aau_summary		=	'';
$aau_summary_2		=	'';
$aau_url			=	'';
$aau_target_url		=	$prepare->sanitize( $_POST['adv-ad-target-url'],'input' );
$aau_file			=	$bannerImage;
$aau_status			=	'1';



//** VALIDATE ID **//
$validate = $prepare -> is_natural_no_zero( $aau_id );




if ( $aau_id > 0 ) {
	if ( $validate == 1 ) {


//** UPDATE **//



			$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 ( $replace_image == "true" ) {
			//remove old image
				$oldImg	=	$ad = $db->Query("SELECT `aau_id`, `aau_title`, `aau_file` FROM advertisers_ad_units where aau_user_id=? and aau_id=?","ii",$USER_ID,$aau_id);

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

			}


				$aau_file	=	$bannerImage;
				$db->NoQuery("update advertisers_ad_units set aau_file=? where aau_user_id=? and aau_id=?","sii",$aau_file,$aau_user_id,$aau_id);
			}

	header ( 'Location: '.HTTP_MASTER.'dashboard/advertiser/ad/banner?id='.$aau_id );

	} else { header ( 'Location: '.HTTP_MASTER.'dashboard/advertiser/campaigns' ); }
		} else {
		//** INSERT **//
			//*** advertisers_ad_units ***//

			$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);
				header ( 'Location: '.HTTP_SERVER_WEBSITE.'dashboard/advertiser/keywords?id='.$aaus_ad_id );
			} else {
				header ( 'Location: '.HTTP_SERVER_WEBSITE.'dashboard/advertiser/ad/banner?m=error' );
			}
		}



















 
}













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

//**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/ad.banner.php');
?>