<?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');
	
	$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 {
		
		$D->is_my_profile = 0;
		$D->friendship = 0;
		if ($this->user->is_logged) {
			$D->is_my_profile	=  ($iduser == $this->user->id);
			$D->friendship = $this->user->friendship($iduser);	
		}
		
		if ($D->friendship == 1 || $D->is_my_profile) {
			$cadSee = ' (who_see=0 OR who_see=1) ';
		} else {
			$cadSee = ' who_see=0 ';
		}
		
		$totalitems = $this->db2->fetch_field('SELECT COUNT(idalbum) FROM albums WHERE iduser='.$iduser.' AND '.$cadSee);
		$r = $this->db2->query('SELECT * FROM albums WHERE iduser='.$iduser.' AND '.$cadSee.' ORDER BY created DESC LIMIT '.$numitems.','.$C->NUM_ALBUMS_PAGE);
	
		$numitemsnow = $this->db2->num_rows();
		
		$htmlResults = '';
		
		$D->u->username = $this->db1->fetch_field('SELECT username FROM users WHERE iduser='.$iduser.' LIMIT 1');
		
		while( $obj = $this->db2->fetch_object($r) ) {
			$D->namealbum = stripslashes($obj->namealbum);
			$D->numphotos = $obj->numphotos;
			$D->codealbum = $obj->code;
			$D->idalbum = $obj->idalbum;
			$D->whosee = $obj->who_see;
			
			if ($D->numphotos > 0) {
				$D->coveralbum = $this->db1->fetch_field('SELECT photo FROM albums_photos WHERE idalbum='.$D->idalbum.' ORDER BY whendate DESC LIMIT 1');
			}
			
			$D->isMyAlbum = FALSE;
			if ($this->user->is_logged) {
				$D->isMyAlbum = $obj->iduser == $this->user->id ? TRUE : FALSE;
			}
			
			$htmlResults .= $this->load_template('__one-albums-profile.php', FALSE);
		}
		
		unset($r, $obj);
		
		if ($totalitems <= $numitemsnow + $numitems) {
			echo("2: ".$htmlResults);
			return;
		} else {
			echo("1: ".$htmlResults);
			return;	
		}		
	}
?>