<?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/dashboard.php');

	// We are here only if you're logged in
	if (!$this->user->is_logged) {
		echo('0: '.$this->lang('dashboard_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: '.$caderror);
		die();
	}

	if ($action == 1)	{

		$headline = $line1 = $line2 = $urldisplay = $urldestination = '';
		
		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($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... '; }

	}

	if ($action == 2)	{

		$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... '; }

	}
	
	// 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... '; }
		
	}
	
	// Delete an ad
	if ($action == 4)	{

		$codad = ''; 
		
		if (isset($_POST["ca"]) && !empty($_POST["ca"])) $codad = $this->db1->e(trim($_POST["ca"]));

		if (empty($codad)) { $errored = 1; $txterror .= 'Error... '; }
		
	}

	// Image for an ad
	if ($action == 5)	{

		$loadedimage = $codad = '';
		$didchanges = 0;
		if (isset($_POST["dch"]) && !empty($_POST["dch"])) $didchanges = $this->db1->e($_POST["dch"]);
		if (isset($_POST["limg"]) && !empty($_POST["limg"])) $loadedimage = $this->db1->e($_POST["limg"]);
		if (isset($_POST["codad"]) && !empty($_POST["codad"])) $codad = $this->db1->e($_POST["codad"]);
		if ($didchanges == 0) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($loadedimage)) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($codad)) { $errored = 1; $txterror .= 'Error... '; }
	}

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

			if (!($urldestination = fitsUrl($urldestination))) {
				$txtreturn = $this->lang('dashboard_myads_error_destinationurlinvalid');
				echo('0: '.$txtreturn);
				return;
			}
			
			$code = uniqueCode(11, 1, 'my_ads', 'code');

			$this->db1->query("INSERT INTO my_ads SET code='".$code."', iduser=".$this->user->id.", headline='".$headline."', descriptionline1='".$line1."', descriptionline2='".$line2."', url_destination='".$urldestination."', url_visible='".$urldisplay."', available=0, created='".time()."'");
			$txtreturn = 'Ok';
			echo('1: '.$txtreturn);
			return;
		}

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

			if (!($urldestination = fitsUrl($urldestination))) {
				$txtreturn = $this->lang('dashboard_myads_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."' AND iduser=".$this->user->id." LIMIT 1");
			$txtreturn = $this->lang('dashboard_myads_txt_adupdated');
			echo('1: '.$txtreturn);
			return;
		}
		
		if ($action == 3) {
			$this->db1->query("UPDATE my_ads SET status=".$status." WHERE code='".$codad."' AND iduser=".$this->user->id." LIMIT 1");
			$txtreturn = $this->lang('dashboard_myads_txt_msgstatusok');
			echo('1: '.$txtreturn);
			return;
		}

		if ($action == 4) {
			$this->db1->query("UPDATE my_ads SET status=2 WHERE code='".$codad."' AND iduser=".$this->user->id." LIMIT 1");
			$txtreturn = 'Ok';
			echo('1: '.$txtreturn);
			return;
		}
		
		if ($action == 5) {	
			if ($didchanges == 1) {
				$ext = explode(".",$loadedimage);
				$finalphoto = $codad.'.'.$ext[count($ext)-1];
				
				$this->db1->query("UPDATE my_ads SET image='".$finalphoto."' WHERE code='".$codad."' AND iduser=".$this->user->id.' LIMIT 1');
				
				$raiz = '../';
				$tmppath = $C->FOLDER_TMP;
				$adsfolder = $C->FOLDER_ADS;
								
				// move photo file
				if (file_exists($raiz.$tmppath.$loadedimage)) {
					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);

					$thumbnail->myresize($C->widthAd1, 'width');
					$thumbnail->saveImage($raiz.$adsfolder.$finalphoto);
					$thumbnail->close();
					
					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);
					$thumbnail->mycrop($C->widthAd1, $C->heightAd1);
					$thumbnail->saveImage($raiz.$adsfolder.'min2/'.$finalphoto);				
					$thumbnail->close();

					$thumbnail = new SmartImage($raiz.$tmppath.$loadedimage, true);
					$thumbnail->mycrop($C->widthAd2, $C->heightAd2);
					$thumbnail->saveImage($raiz.$adsfolder.'min1/'.$finalphoto);
					$thumbnail->close();
					
					unlink($raiz.$tmppath.$loadedimage);
			
					$txtreturn = $this->lang('dashboard_myads_form_ok');
					echo("1: ".$txtreturn);
					return;
				} else {
					$txtreturn=$this->lang('dashboard_myads_form_msg9');
					echo("0: ".$txtreturn);
					return;
				}		
			} else {
				$txtreturn = $this->leng('dashboard_myads_form_msg5');
				echo("0: ".$txtreturn);
				return;
			}
		}

	
	}
?>