<?php
	if( !$this->user->is_logged ) {
		$this->redirect('home');
	}
	
	
	/*************************************************************************/
	// needed before proceeding
	require_once('_all-required-language.php');
	
	$this->load_langfile('global/global.php');	
	$this->load_langfile('inside/dashboard.php');

	/*************************************************************************/
	
	require_once('_all-required-dashboard.php');

	/*************************************************************************/
	
	$D->codalbum = '';
	if ($this->param('a')) $D->codalbum = $this->param('a');

	if (!empty($D->codalbum)) {
		$r = $this->db2->query("SELECT * FROM albums WHERE code='".$D->codalbum."' AND iduser=".$this->user->id." LIMIT 1");
		
		if (!$obj = $this->db2->fetch_object($r)) $this->redirect($C->SITE_URL.'dashboard/myalbums');
		else $D->album = $obj;

		$D->idalbum = $D->album->idalbum;
		$D->whosee = $D->album->who_see;
		$D->namealbum = stripslashes($D->album->namealbum);

		//We load the photos of albums
		$D->totalphotos = $this->db2->fetch_field('SELECT COUNT(idalbum) FROM albums_photos WHERE idalbum='.$D->idalbum);
		$r = $this->db2->query('SELECT * FROM albums_photos WHERE idalbum='.$D->idalbum.' ORDER BY whendate DESC LIMIT 0,'.$C->NUM_PHOTOS_PAGE);
	
		$D->numphotos = $this->db2->num_rows();
		
		$D->htmlPhotosAlbums = '';
		
		while( $obj = $this->db2->fetch_object($r) ) {
			$D->codephoto = $obj->code;
			$D->idphoto = $obj->idphoto;
			$D->photofile = $obj->photo;
			$D->isMyPhoto = $obj->iduser == $this->user->id ? TRUE : FALSE;
			$D->htmlPhotosAlbums .= $this->load_template('__one-photo-albums.php', FALSE);
		}
			
		unset($r, $obj);

		$D->optionactive = 14;
		$this->load_template('dashboard-myalbums-details.php');
		
	} else {
	
		//We load the albums
		$D->totalalbums = $this->db2->fetch_field('SELECT COUNT(idalbum) FROM albums WHERE iduser='.$this->user->id);
		$r = $this->db2->query('SELECT * FROM albums WHERE iduser='.$this->user->id.' ORDER BY created DESC LIMIT 0,'.$C->NUM_ALBUMS_PAGE);
	
		$D->numalbums = $this->db2->num_rows();
		
		$D->htmlAlbums = '';
		
		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 = $obj->iduser == $this->user->id ? TRUE : FALSE;
			
			$D->htmlAlbums .= $this->load_template('__one-albums.php', FALSE);
		}
			
		unset($r, $obj);
	
		/*************************************************************************/
	
		$D->optionactive = 14;
		$this->load_template('dashboard-myalbums.php');
	}
?>