## OpenCA - Command
## (c) 1998-2001 by Massimiliano Pala and OpenCA Group
##
##   File Name: certsList
##//       Brief: build Certificates' listing
##// Description: Build Certificates' listing given the dataType
##  Parameters: dataType, viewFrom, rows

sub cmdListCerts {

## Reserved variables
my ( $page, $certData, $sheet );

## We need some parameters to generate the list because of
## we do not want to generate a list of ALL issued certificates
## so get the numer of results we should give away, then the
## starting serial number.
my $from     = $query->param( 'viewFrom' );
my $matched  = $query->param( 'rows' );
my $dataType = $query->param( 'dataType' );

## Get required parameters ( return only the value string )
my $sheet    = getRequired( 'basecertslist' );
my $maxItems = getRequired( 'MaxReturnedItems' );
my $uptime   = $tools->getDate();

my @cols;

## Get the base Page ( got in $page variable )
if( not $page = $query->getFile( "$sheet" ) ) {
	configError ( i18nGettext ("File not Found : __FILE__", "__FILE__", $sheet) );
};

## Set the column titles
push( @cols, gettext("Mark") );
push( @cols, gettext("Serial") );
push( @cols, gettext("Common Name") );
push( @cols, gettext("Email") );
push( @cols, gettext("Role") );

if ( not $matched ) {
	$matched  = $db->elements( DATATYPE=>$dataType );
};

my @certsList = $db->listItems( DATATYPE=>$dataType,
			FROM=>$from, ITEMS=>$maxItems, PARSE_MODE => "BASIC" );
if (not @certsList) {
	$table = $query->buildRefs(
			ELEMENTS  => $matched,
			MAXITEMS  => $maxItems,
			FACTOR    => 5,
			MODE      => "EXP",
			NOW_FIRST => 0,
			NOW_LAST  => 0,
			FIRST     => 0,
			LAST      => 0);
} else {
	$table = $query->buildRefs(
			ELEMENTS  => $matched,
			MAXITEMS  => $maxItems,
			FACTOR    => 5,
			MODE      => "EXP",
			NOW_FIRST => $certsList[0]->getSerial($dataType),
			NOW_LAST  => $certsList[scalar (@certsList) -1]->getSerial($dataType),
			FIRST     => libDBGetFirstItem ($dataType)->getSerial($dataType),
			LAST      => libDBGetLastItem ($dataType)->getSerial($dataType));
}

$table .= $query->startTable( 	COLS=>[ @cols ],
				WIDTH=>"100%",
				TITLE_BGCOLOR=>"#DDCCFF" );

## Process all Files
foreach $cert ( @certsList ) {

	my ( $format, $key, $type, $ser_col, $check );
	my @vals;

	$key  = $cert->getSerial($dataType);
	$type = $dataType;

	$check = $query->newInput( -intype=>checkbox,
				   -name=>"$key",
				   -value=>'1',
				   -label=>"" );

	$ser_col = "<a href=\"$self?cmd=viewCert&dataType=$type" .
			"&key=$key\">". $key .
			"</a>";

	push( @vals, "<CENTER>$check</CENTER>" );
	push( @vals, $ser_col );
	push( @vals, ( $cert->getParsed()->{DN_HASH}->{CN}[0] || "<CENTER>---</CENTER>") );
	push( @vals, ( $cert->getParsed()->{EMAILADDRESS} || "<CENTER>---</CENTER>") );
	push( @vals, ( $cert->getParsed()->{HEADER}->{ROLE} || "<CENTER>".gettext ("n/a")."</CENTER>") );
	$table .= $query->addTableLine( DATA =>[ @vals ]);
}

## Close the Table
$table .= $query->endTable();

## Substitute the Variables in the $page
$page = $query->subVar( $page, '$uptime', $uptime );
$page = $query->subVar( $page, '$table', $table );
$page = $query->subVar( $page, '@COPY@', getCopyRight() );
$page = $query->subVar( $page, '@DATATYPE@', $dataType);

print "$page";

}

1;

