<?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('global/global.php');
	$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();
	}
	
	$D->is_logged = 0;
	if ($this->user->is_logged) {
		$D->me = $this->user->info;
		$D->is_logged = 1;
		$D->id_wall = $D->me->code;
	}
	
	$errored = 0;
	$txterror = '';

	$numitems = $iduser = 0;
	
	if (isset($_POST["ni"]) && !empty($_POST["ni"])) $numitems = $this->db1->e($_POST["ni"]);
	if (isset($_POST["idu"]) && !empty($_POST["idu"])) $iduser = $this->db1->e($_POST["idu"]);
	
	if (!is_numeric($numitems) || $numitems <= 0) { $errored = 1; $txterror .= 'Error... '; }
	if (!is_numeric($iduser) || $iduser <= 0) { $errored = 1; $txterror .= 'Error... '; }
	
	if ($errored == 1) {
		echo('0: '.$txterror);
	} else {

		//We load the posts
		$totalitems = $this->db2->fetch_field("
		SELECT count(idpost) 
		FROM posts 
		WHERE 
		iduser=".$this->user->id." 
		AND posted_in=0 
		AND typepost='video' 
		");
		
		$r = $this->db2->query("
		SELECT posts.*, username, firstname, lastname, avatar, users.iduser, users.code as ucode 
		FROM posts, users 
		WHERE 
		posts.iduser=".$this->user->id." 
		AND posted_in=0 
		AND typepost='video' 
		AND posts.iduser=users.iduser 
		ORDER BY posts.whendate DESC 
		LIMIT ".$numitems.",".$C->NUM_ACTIVITIES_PAGE);
	
		$numitemsnow = $this->db2->num_rows();

		$htmlResults = ''; 

		while( $obj = $db2->fetch_object($r) ) {
			$D->a_date = $obj->whendate;
			$D->codeUser = $obj->ucode;
			
			$D->isMyPost = $obj->iduser == $this->user->id ? TRUE : FALSE;
			
			$D->isOnlyOne = $D->isWithOther = 0;
			
			if ($obj->posted_in == 0) {
				if ($obj->iduser == $obj->id_wall) {
					$D->isOnlyOne = 1;
					$D->codeUser = $obj->ucode;
					$D->userName = $obj->username;
					$D->nameUser = (empty($obj->firstname) || empty($obj->lastname))?$obj->username:($obj->firstname.' '.$obj->lastname);
					$D->userAvatar = $obj->avatar;
				}
				
				if ($obj->iduser != $obj->id_wall) {
					$D->isWithOther = 1;
					$D->codeUser = $obj->ucode;
					$D->userName = $obj->username;
					$D->nameUser = (empty($obj->firstname) || empty($obj->lastname))?$obj->username:($obj->firstname.' '.$obj->lastname);
					$D->userAvatar = $obj->avatar;
	
					$wallsec = $this->network->infoBasicWall($obj->posted_in, $obj->id_wall);
					$D->other_code = $wallsec->code;
					$D->other_userName = $wallsec->username;
					$D->other_nameUser = (empty($wallsec->firstname) || empty($wallsec->lastname))?$wallsec->username:($wallsec->firstname.' '.$wallsec->lastname);
					$D->postInMyWall = FALSE;
					if ($D->is_logged == 1) {
						$D->postInMyWall = $obj->id_wall == $this->user->id ? TRUE : FALSE;
					}
				}
			}
			
			$D->idpost = $obj->idpost;
			$D->codepost = $this->network->getCodePost($D->idpost);
			$onePost = new post($D->codepost);
			$D->idUser = $onePost->iduser;
			$D->typepost = $onePost->typepost;
			$D->numlikes = $onePost->numlikes;
			$D->numcommentstotal = $onePost->numcomments;
			$D->post = stripslashes($onePost->post);
			$D->typepost = $onePost->typepost;
			$D->valueattach = $onePost->valueattach;
			$D->whosee = $onePost->whosee;
			
			// see if the favorite is for the observer
			$D->liketoUser = 0;
			if ($D->is_logged == 1) {
				if ($onePost->likeOfUser($this->user->id) > 0) $D->liketoUser = 1;
			}
			
			$D->htmlcommentspost = '';
			$D->totalcomments = $onePost->numComments();
			$allcommentspost = $onePost->getComments(0,$C->NUM_COMMENTS_PER_POST);
			$D->numcomments = count($allcommentspost);
			
			$allcommentspost = array_reverse($allcommentspost);	
			
			foreach($allcommentspost as $onecomment){
				$D->o_comment = stripslashes($onecomment->comment);
				$D->o_username = stripslashes($onecomment->username);
				$D->o_firstname = stripslashes($onecomment->firstname);
				$D->o_lastname = stripslashes($onecomment->lastname);
				$D->o_ucode = $onecomment->ucode;
				$D->o_nameUser = (empty($D->o_firstname) || empty($D->o_lastname))?stripslashes($D->o_username):(stripslashes($D->o_firstname).' '.stripslashes($D->o_lastname));
				$D->o_whendate = $onecomment->whendate;
				$D->o_avatar =  empty($onecomment->avatar)?$C->AVATAR_DEFAULT:$onecomment->avatar;
				$D->o_idcomment = $onecomment->idcomment;
				$D->o_idUser = $onecomment->iduser;
				$D->o_idpost = $D->idpost;
				$D->o_idUserOwner = $D->idUser;
				$D->o_codepost = $D->codepost;
				$D->htmlcommentspost .= $this->load_template('__onecomment-post.php', FALSE);
			}
			unset($onecomment);
			
			$htmlResults .= $this->load_template('__one-post.php', FALSE);
			unset($onePost);
		}
		
		unset($r, $obj);
		
		if ($totalitems <= $numitemsnow + $numitems) {
			echo("2: ".$htmlResults);
			return;
		} else {
			echo("1: ".$htmlResults);
			return;	
		}	
	}
?>