<?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();
	}

	// Create a plan
	if ($action == 1)	{

		$nameplan = '';
		$quantity = $price = $typeaction = 0;
		
		if (isset($_POST["np"]) && !empty($_POST['np'])) $nameplan = $this->db1->e(htmlspecialchars(trim($_POST["np"])));
		if (isset($_POST["tap"])) $typeaction = $this->db1->e(trim($_POST["tap"]));
		if (isset($_POST["pp"])) $price = $this->db1->e(trim($_POST["pp"]));
		if (isset($_POST["q"])) $quantity = $this->db1->e(trim($_POST["q"]));
		
		if ($typeaction == 0) { $errored = 1; $txterror .= 'Error... '; }
		if ($quantity == 0) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($nameplan)) { $errored = 1; $txterror .= 'Error... '; }
		
	}

	// Update a plan
	if ($action == 2)	{

		$nameplan = '';
		$quantity = $price = $typeaction = $idp = 0;
		
		if (isset($_POST["np"]) && !empty($_POST['np'])) $nameplan = $this->db1->e(htmlspecialchars(trim($_POST["np"])));
		if (isset($_POST["tap"])) $typeaction = $this->db1->e(trim($_POST["tap"]));
		if (isset($_POST["pp"])) $price = $this->db1->e(trim($_POST["pp"]));
		if (isset($_POST["q"])) $quantity = $this->db1->e(trim($_POST["q"]));
		if (isset($_POST["idp"])) $idp = $this->db1->e(trim($_POST["idp"]));
		
		if ($typeaction == 0) { $errored = 1; $txterror .= 'Error... '; }
		if ($quantity == 0) { $errored = 1; $txterror .= 'Error... '; }
		if ($idp == 0) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($nameplan)) { $errored = 1; $txterror .= 'Error... '; }
		
	}
	
	// Delete a plan
	if ($action == 3)	{

		$idplan = 0; 
		
		if (isset($_POST["idp"])) $idplan = $this->db1->e(trim($_POST["idp"]));

		if ($idplan == 0) { $errored = 1; $txterror .= 'Error... '; }
		
	}

	
	if ($errored == 1) {
		echo('0: '.$txterror);
	} else {

		if ($action == 1) {
			$nums = $this->db1->fetch_field("SELECT count(idplan) FROM my_ads_plans WHERE name_plan='".$nameplan."'");
			if ($nums != 0) {
				$txtreturn = $this->lang('admin_ads_plans_error_namerepeated');
				echo('0: '.$txtreturn);
				return;				
			} else {
				$code = uniqueCode(11, 1, 'my_ads_plans', 'code');
				$this->db1->query("INSERT INTO my_ads_plans SET code='".$code."', name_plan='".$nameplan."', typeaction=".$typeaction.", quantity=".$quantity.", price=".$price);
				$txtreturn = 'Ok';
				echo('1: '.$txtreturn);
				return;
			}
		}
		
		if ($action == 2) {
			$nums = $this->db1->fetch_field("SELECT count(idplan) FROM my_ads_plans WHERE idplan<>".$idp." AND name_plan='".$nameplan."'");
			if ($nums != 0) {
				$txtreturn = $this->lang('admin_ads_plans_error_namerepeated');
				echo('0: '.$txtreturn);
				return;				
			} else {
				$this->db1->query("UPDATE my_ads_plans SET name_plan='".$nameplan."', typeaction=".$typeaction.", quantity=".$quantity.", price=".$price." WHERE idplan=".$idp);
				$txtreturn = $this->lang('admin_ads_plans_txt_msgok');
				echo('1: '.$txtreturn);
				return;
			} 
		}
		
		if ($action == 3) {
			$nump = $this->db1->fetch_field("SELECT count(idplan) FROM my_ads_plans_assigned WHERE idplan=".$idplan);
			if ($nump > 0) {
				$txtreturn = $this->lang('admin_ads_plans_txt_nodelete');
				echo('0: '.$txtreturn);
				return;				
			} else {
				$this->db1->query("DELETE FROM my_ads_plans WHERE idplan=".$idplan." LIMIT 1");
				echo('1: Ok');
				return;
			}
		}
	
	}
?>