<?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 || $action==2)	{
		$idpage = 0;
		if (isset($_POST["idp"])) $idpage = $this->db1->e($_POST["idp"]);
		if (!is_numeric($idpage) || $idpage <= 0) { $errored = 1; $txterror .= 'Error. '; }
	}
	
	if ($errored == 1) {
		echo('0: '.$txterror);
	} else {
		
		// Follow
		if ($action == 1) {
			$this->db1->fetch("INSERT INTO relations SET leader=".$idpage.", type_leader=1, subscriber=".$this->user->id.", rltdate='".time()."'");
			$this->db1->query("UPDATE pages SET num_followers=num_followers+1 WHERE idpage=".$idpage.' LIMIT 1');
			$txtreturn = '1: Ok';
			echo $txtreturn;
			return;
		}

		// unFollow
		if ($action == 2) {
			$this->db1->fetch("DELETE FROM relations WHERE leader=".$idpage." AND type_leader=1 AND subscriber=".$this->user->id." LIMIT 1");
			if ($this->db1->affected_rows()>0) $this->db1->query("UPDATE pages SET num_followers=num_followers-1 WHERE idpage=".$idpage.' LIMIT 1');
			$txtreturn = '1: Ok';
			echo $txtreturn;
			return;
		}
		
		
	}
	
?>