<?php
/*
   +----------------------------------------------------------------------+
   | Albums 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 a list of albums                                             |
   +----------------------------------------------------------------------+
*/

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

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

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

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

if (empty($sort)) {
	$sort = 7;
}

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

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

#Get Latest Albums
$album_list = Album::getAlbumList(2,$limit,$offset,$sort);
$smarty->assign('album_list',$album_list);

$total_rows = Album::getAlbumList(2,true);

#Get a list of album types
$album_type_list = AlbumCategory::getCategoryList();
$smarty->assign('album_type_list',$album_type_list);

#Get Side Albums
$other_albums = Album::getAlbumList('other',false,0,9);
$smarty->assign('other_albums',$other_albums);

#Create Pagination URL
$get_string = '';
foreach ($_GET as $key=>$value){
        if ($key !== 'offset'){
                $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("/albums/?$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'].'/albums.tpl');

?>
