<?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: '.$caderror);
		die();
	}

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

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

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

		$idfather = $idcat = -1; 
		$ncategory = '';
		
		if (isset($_POST["idc"])) $idcat = $this->db1->e(trim($_POST["idc"]));
		if (isset($_POST["idf"])) $idfather = $this->db1->e(trim($_POST["idf"]));
		if (isset($_POST["ncat"]) && !empty($_POST["ncat"])) $ncategory = $this->db1->e(htmlspecialchars(trim($_POST["ncat"])));

		if ($idcat == -1) { $errored = 1; $txterror .= 'Error... '; }		
		if ($idfather == -1) { $errored = 1; $txterror .= 'Error... '; }
		if (empty($ncategory)) { $errored = 1; $txterror .= 'Error... '; }
		
	}
	
	// Delete a category
	if ($action == 3)	{

		$idfather = $idcat = -1; 
		
		if (isset($_POST["idc"])) $idcat = $this->db1->e(trim($_POST["idc"]));
		if (isset($_POST["idf"])) $idfather = $this->db1->e(trim($_POST["idf"]));

		if ($idcat == -1) { $errored = 1; $txterror .= 'Error... '; }		
		if ($idfather == -1) { $errored = 1; $txterror .= 'Error... '; }
		
	}

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

		if ($action == 1) {
			$this->db1->query("INSERT INTO pages_cat SET idfather=".$idfather.", name='".$ncategory."'");
			if ($idfather != 0) $this->db1->query("UPDATE pages_cat SET num_children=num_children+1 WHERE idcategory=".$idfather." LIMIT 1");
			$txtreturn = $this->lang('admin_txt_msgok');
			echo('1: '.$txtreturn);
			return;
		}
		
		if ($action == 2) {
			$this->db1->query("UPDATE pages_cat SET name='".$ncategory."' WHERE idcategory=".$idcat." AND idfather=".$idfather." LIMIT 1");
			$txtreturn = $this->lang('admin_txt_msgok');
			echo('1: '.$txtreturn);
			return;
		}
		
		if ($action == 3) {
			if ($idfather == 0) {
				$numsubc = $this->db1->fetch_field("SELECT count(idcategory) FROM pages_cat WHERE idfather=".$idcat);
				if ($numsubc == 0) {
					$this->db1->query("DELETE FROM pages_cat WHERE idcategory=".$idcat." LIMIT 1");
					echo('1: Ok');
					return;
				} else {
					$txtreturn = $this->lang('admin_pages_txterror_delete_cat_withsubcat');
					echo('0: '.$txtreturn);
					return;					
				}
			} else {
				$numpages = $this->db1->fetch_field("SELECT count(idpage) FROM pages WHERE idcategory=".$idcat);
				if ($numpages == 0) {
					$this->db1->query("DELETE FROM pages_cat WHERE idcategory=".$idcat." LIMIT 1");
					$this->db1->query("UPDATE pages_cat SET num_children=num_children-1 WHERE idcategory=".$idfather);
					echo('1: Ok');
					return;					
				} else {
					$txtreturn = $this->lang('admin_pages_txterror_delete_subcat_withpages');
					echo('0: '.$txtreturn);
					return;
				}
			}
		}
	
	}
?>