<?php
/*
   +----------------------------------------------------------------------+
   | Search 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 search results                                     |
   +----------------------------------------------------------------------+
*/

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

$query = $_GET['query'];

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

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

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

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

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

if ($query) {
	$search_results = Photo::doSearch($query,$limit,$offset,$sort);
	$total_rows = Photo::doSearch($query,true);
	$smarty->assign('search_results',$search_results);

	#Search Albums
	$album_results = Album::doSearch($query);
	$album_rows = Album::doSearch($query,true);
	$smarty->assign('album_rows',$album_rows);
	$smarty->assign('album_results',$album_results);

}

$p = new Pagination($total_rows,$limit,$offset);
if (isset($_GET['all'])) { $all = $_GET['all']; } else { $all = ''; }
if (isset($_GET['sort'])) { $all = $_GET['sort']; } else { $sort = ''; }

$p->setURL('/search/?query='.$query.'&offset='.$offset.'&all='.$all.'&sort='.$sort);
$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->assign('ERROR',$ERROR);
$smarty->display($CONFIG['smarty_templates'].'/search.tpl');

?>
