######################################################################
#
#  Show Last "n" EPrints added.
#
######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 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 2 of the License, or
#  (at your option) any later version.
#  
#  EPrints 2 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 EPrints 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

use EPrints;

use strict;
my $session = new EPrints::Session;
exit( 0 ) unless( defined $session );

my $ds = $session->get_repository->get_dataset( "archive" );

my $citation = $session->get_repository->get_conf( "latest_tool_citation" );

my $max = 20;
my $mode = $session->param( "mode" );
$mode = "default" if( !defined $mode );
my $filters = [];
my $conf = $session->get_repository->get_conf( "latest_tool_modes", $mode );
my $show_conditions = 0;
if( defined $conf )
{
	foreach my $key (keys %{$conf} )
	{
		$citation = $conf->{"citation"} if( $key eq "citation" );
		if( $key eq "filters" )
		{
			$filters = $conf->{"filters"};
			$show_conditions = 1;
		}
		$max = $conf->{"max"} if( $key eq "max" );
	}
}
push @{$filters}, { meta_fields=>[ 'metadata_visibility' ], value=>'show', match=>'EX', describe=>0 };
my $number_to_get = $max;
my $n = $session->param( "n" );
if( $n > 0 && $n <= $max )
{
	$number_to_get = $n;
}
my $searchexp = new EPrints::Search(
	allow_blank => 1,
	session => $session,
	filters => $filters,
	custom_order => "-datestamp",
	dataset => $ds );
my $title;
if( $show_conditions )
{
	$title = $session->html_phrase( 
		"cgi/latest_tool:title_matching",
		n => $session->make_text( $number_to_get ),
		search => $searchexp->render_conditions_description );
}
else
{
	$title = $session->html_phrase( 
		"cgi/latest_tool:title",
		n => $session->make_text( $number_to_get ) );
}

my $results = $searchexp->perform_search();
my $format = $session->param( "output" );
if( defined $format )
{
	my @plugins = $session->plugin_list(
		type=>"Export",
		can_accept=>"list/eprint",
		is_visible=>"all" );
		
	my $ok = 0;
	foreach( @plugins ) { if( $_ eq "Export::$format" ) { $ok = 1; last; } }
	if( $ok ) 
	{
		my $plugin = $session->plugin( "Export::".$format );
		$session->send_http_header( 
				"content_type" => $plugin->param( 'mimetype' ) );
		print $results->export( $format );
	}
	else
	{
		my $page = $session->html_phrase( 
					"lib/searchexpression:export_error_format" );
		$session->build_page( $title, $page, "latest_tool" );
		$session->send_page();
	}
	
	$results->dispose;
	$session->terminate;
	exit;
}

my @records = $results->get_records( 0, $number_to_get );
$results->dispose;

my $page = $session->make_doc_fragment();
my $type = $session->get_citation_type( $ds, $citation );
my $container;
if( $type eq "table_row" )
{
	$container = $session->make_element( "table", class=>"ep_latest_tool_list" );
}
else
{
	$container = $session->make_element( "div", class=>"ep_latest_tool_list" );
}
$page->appendChild( $container );
my $n = 1;
foreach my $item ( @records )
{
	my $row = $item->render_citation_link( 
		$citation,
		n => [$n++, "INTEGER"] );
	if( $type eq "table_row" )
	{
		$container->appendChild( $row );
	}
	else
	{
		my $div = $session->make_element( "div", class=>"ep_latest_tool_result" );
		$div->appendChild( $row );
		$container->appendChild( $div );
	}
}
$session->build_page( $title, $page, "latest_tool" );
$session->send_page();

$session->terminate;
exit;

use POSIX qw(strftime);
sub RFC822_time
{
	return( strftime( "%a,  %d  %b  %Y  %H:%M:%S  %z",localtime ) );
}
