<?php
/*
   +----------------------------------------------------------------------+
   | Album Contents Page                                                  |
   | Author: Adam Goldberg                                                |
   +----------------------------------------------------------------------+
   | Copyright (c) 2008-2009 Adam J Goldberg                              |
   | This file is part of Lifebox.                                        |
   |                                                                      |
   | Lifebox is free software: you can redistribute it and/or modify      |
   | it under the terms of the GNU General Public License as published by |
   | the Free Software Foundation, either version 3 of the License, or    |
   | (at your option) any later version.                                  |
   |                                                                      |
   | Lifebox is distributed in the hope that it will be useful,           |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of the GNU General Public License    |
   | along with Lifebox.  If not, see <http://www.gnu.org/licenses/>.     |
   +----------------------------------------------------------------------+
   | Display all photos and videos within a particular album		  |
   +----------------------------------------------------------------------+
*/

require_once 'config/global.inc.php';
require_once 'Album.inc.php';
require_once 'Pagination.inc.php';

if (empty($_GET['offset'])){
        $offset = 0;
} else {
        $offset = $_GET['offset'];
}

if (empty($_GET['all'])) {
	$limit = $CONFIG['perpage_contents'];
} else {
	$limit = '10000';
}

if (isset($_GET['sort'])) {
	$sort = $_GET['sort'];
} else {
	$sort = '';
}

$all_url = '/'.$a.'/&all=true&sort='.$sort;
$smarty->assign('all_url',$all_url);

#If SORT Post
if (isset($_POST['sort']) AND $_POST['sort']) {
	header('Location: /'.$a.'/&offset='.$offset.'&sort='.$_POST['sort']);
	exit(0);
}

$album = new Album($a);
$album_info = $album->getAll();
if (empty($album_info)) {
        header ('Location: /');
        exit(0);
}
$smarty->assign('album_info',$album_info);

#If the user is requesting to download, perform the action
if (isset($_GET['download'])) {
        Photo::getPhotoTar($album_info['album_id']);
	exit(0);
}


$album->updateViews();

$photo_list = Photo::getPhotoList($a,false,$limit,$offset,$sort);
$total_rows = Photo::getPhotoList($a,false,true);

#If there are no photos to be displayed, either because
#the album is empty or the user does not have access,
#redirect back to the homepage
if (empty($photo_list)) {
        header('Location: /');
        exit(0);
}

$smarty->assign('photo_list',$photo_list);

#Create Pagination URL
$get_string = '';
foreach ($_GET as $key=>$value){
        if ($key !== 'offset' AND
	    $key !== 'a'){
                $value = urlencode($value);
                $key = urlencode($key);
                $get_string .= "$key=$value&";
        }
}
$get_string = substr($get_string,0,-1);

$p = new Pagination($total_rows,$limit,$offset);
$p->setURL("/$a/&$get_string");
$pagination = $p->getNavigation();


$smarty->assign('pagination',$pagination);
$smarty->assign('total_pages',$p->getTotalPages());
$smarty->assign('total_items',$p->getTotalItems());
$smarty->assign('current_page',$p->getCurrentPage());
$smarty->assign('first_result',$p->getFirstResult());
$smarty->assign('last_result',$p->getLastResult());

$smarty->display($CONFIG['smarty_templates'].'/contents.tpl');

?>
