<?php 
	
	// We check in which language we will work
	if (isset($_SESSION["DATAGLOBAL"][0]) && !empty($_SESSION["DATAGLOBAL"][0])) $C->LANGUAGE = $_SESSION["DATAGLOBAL"][0];

	$this->load_langfile('inside/admin.php');

	// We are here only if you're logged in
	if (!$this->user->is_logged || !$this->user->info->is_network_admin) {
		echo('0: '.$this->lang('admin_no_session'));
		die();
	}
	
	$errored = 0;
	$txterror = '';
	
	$action=0;
	
	if (isset($_POST["todo"]) && $_POST["todo"] != '') $action = $this->db1->e($_POST["todo"]);
	if (!is_numeric($action)) {
		$errored = 1;
		$txterror .= 'Error... ';
		echo('0: '.$txterror);
		die();
	}

	// Edit ad of user
	if ($action == 1)	{
		$codead = $headline = $line1 = $line2 = $urldisplay = $urldestination = '';
		
		if (isset($_POST["codad"]) && !empty($_POST["codad"])) $codead = $this->db1->e(htmlspecialchars($_POST["codad"]));
		if (isset($_POST["h"]) && !empty($_POST["h"])) $headline = $this->db1->e(htmlspecialchars($_POST["h"]));
		if (isset($_POST["ld1"]) && !empty($_POST["ld1"])) $line1 = $this->db1->e(htmlspecialchars($_POST["ld1"]));
		if (isset($_POST["ld2"]) && !empty($_POST["ld2"])) $line2 = $this->db1->e(htmlspecialchars($_POST["ld2"]));
		if (isset($_POST["urldisp"]) && !empty($_POST["urldisp"])) $urldisplay = $this->db1->e($_POST["urldisp"]);
		if (isset($_POST["urldest"]) && !empty($_POST["urldest"])) $urldestination = $this->db1->e($_POST["urldest"]);
		
		if (empty($codead)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($headline)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($line1)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($line2)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($urldisplay)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($urldestination)) { $errored = 1; $txterror .= 'Error... '; }		
	}

	// Assign plan to ad
	if ($action == 2)	{

		$codad = $codplan = '';
		
		if (isset($_POST["codad"]) && !empty($_POST['codad'])) $codad = $this->db1->e(htmlspecialchars(trim($_POST["codad"])));
		if (isset($_POST["cp"]) && !empty($_POST['cp'])) $codplan = $this->db1->e(trim($_POST["cp"]));
		
		if (empty($codad)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($codplan)) { $errored = 1; $txterror .= 'Error... '; }
		
	}

	// status ad
	if ($action == 3)	{

		$codad = '';
		$status = -1;
		
		if (isset($_POST["codad"]) && !empty($_POST['codad'])) $codad = $this->db1->e(trim($_POST["codad"]));
		if (isset($_POST["st"])) $status = $this->db1->e(trim($_POST["st"]));
		
		if (empty($codad)) { $errored = 1; $txterror .= 'Error... '; }
		if ($status == -1) { $errored = 1; $txterror .= 'Error... '; }
		
	}
	
	if ($errored == 1) {
		echo('0: '.$txterror);
	} else {

		if ($action == 1) {
			if (!fitsUrl($urldisplay)) {
				$txtreturn = $this->lang('admin_ads_userads_error_destinationurlinvalid');
				echo('0: '.$txtreturn);
				return;
			}

			if (!($urldestination = fitsUrl($urldestination))) {
				$txtreturn = $this->lang('admin_ads_userads_error_destinationurlinvalid');
				echo('0: '.$txtreturn);
				return;
			}
		
			$this->db1->query("UPDATE my_ads SET headline='".$headline."', descriptionline1='".$line1."', descriptionline2='".$line2."', url_destination='".$urldestination."', url_visible='".$urldisplay."' WHERE code='".$codead."' LIMIT 1");
			$txtreturn = $this->lang('admin_ads_userads_txt_adupdated');
			echo('1: '.$txtreturn);
			return;
		}
		
		if ($action == 2) {
			$idAd = $this->db2->fetch_field("SELECT idad FROM my_ads WHERE code='".$codad."' LIMIT 1");
			$idPlan = $this->db2->fetch_field("SELECT idplan FROM my_ads_plans WHERE code='".$codplan."' LIMIT 1");
			
			$this->db2->query("INSERT INTO my_ads_plans_assigned SET idad=".$idAd.", idplan=".$idPlan.", assignedby=1, idassigner=".$this->user->id.", whendate='".time()."'");	
		
			$r = $this->db2->query("SELECT * FROM my_ads_plans WHERE idplan=".$idPlan." LIMIT 1");
			
			if ($obj = $this->db2->fetch_object($r)) {
				$typeaction = $obj->typeaction;
				$quantity = $obj->quantity;
				if ($typeaction == 1) {
					$this->db2->query("UPDATE my_ads SET typeaction=1, last_assignedby=1, impressions_available=".$quantity.", clicks_available=0, num_impressions=0, num_impressions_unique=0, num_clicks=0, num_clicks_unique=0, available=1 WHERE idad=".$idAd);
				}
				if ($typeaction == 2) {
					$this->db2->query("UPDATE my_ads SET typeaction=2, last_assignedby=1, impressions_available=0, clicks_available=".$quantity.", num_impressions=0, num_impressions_unique=0, num_clicks=0, num_clicks_unique=0, available=1 WHERE idad=".$idAd);
				}
			}
			$txtreturn = $this->lang('admin_ads_userads_actv_msgok');
			echo('1: '.$txtreturn);
			return;			
		}

		if ($action == 3) {
			$this->db1->query("UPDATE my_ads SET status_admin=".$status." WHERE code='".$codad."' LIMIT 1");
			$txtreturn = $this->lang('admin_ads_userads_txt_status_msgok');
			echo('1: '.$txtreturn);
			return;
		}

	
	}
?>