<?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 {
		
		// like page
		if ($action == 1) {
			$this->db1->fetch("INSERT INTO relations SET leader=".$idpage.", type_leader=1, subscriber=".$this->user->id.", rltdate='".time()."'");
			$this->db1->fetch("INSERT INTO pages_likes SET idpage=".$idpage.", iduser=".$this->user->id.", whendate='".time()."'");
			$idlike = $this->db1->insert_id();
			$this->db1->query("UPDATE pages SET num_likes=num_likes+1, num_followers=num_followers+1 WHERE idpage=".$idpage.' LIMIT 1');

			$idowner = $this->network->idOwnerPage($idpage);
			
			if ($this->user->id != $idowner) {
				$this->db1->query("INSERT INTO notifications SET notif_type=9, idresult=".$idlike.", to_user_id=".$idowner.", from_user_id=".$this->user->id.", notif_object_type=3, notif_object_id=".$idpage.",date='".time()."'");
				$this->db1->query("UPDATE users SET num_notifications=num_notifications+1 WHERE iduser=".$idowner.' LIMIT 1');
			}
			
			$txtreturn = '1: Ok';
			echo $txtreturn;
			return;
		}

		// unlike page
		if ($action == 2) {
			$this->db1->fetch("DELETE FROM pages_likes WHERE idpage=".$idpage." AND iduser=".$this->user->id." LIMIT 1");
			$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_likes=num_likes-1, num_followers=num_followers-1 WHERE idpage=".$idpage.' LIMIT 1');
			else $this->db1->query("UPDATE pages SET num_likes=num_likes-1 WHERE idpage=".$idpage.' LIMIT 1');
			$idowner = $this->network->idOwnerPage($idpage);
			if ($this->user->id != $idowner) {
				$this->db1->query("DELETE FROM notifications WHERE notif_type=9 AND to_user_id=".$idowner." AND from_user_id=".$this->user->id." AND notif_object_type=3 AND notif_object_id=".$idpage);
				$nnotifications = $this->network->getNumNotifications($idowner);
				if ($nnotifications <= 0) $nnotifications = 0;
				else $nnotifications = $nnotifications - 1;
				$this->db1->query("UPDATE users SET num_notifications=".$nnotifications." WHERE iduser=".$idowner.' LIMIT 1');
			}
				
			$txtreturn = '1: Ok';
			echo $txtreturn;
			return;
		}

	}
	
?>