#!/usr/bin/perl
$|=1;

#リクルート表示処理
=commentout
	初期設定はinit.plで行います
=cut

use CGI::Carp qw(fatalsToBrowser);
use lib '../master/lib';
#use CGI qw(:cgi-lib :cgi);
use CGI qw(:cgi);
use DBI;
use Jcode;
#use AE;
use AETmpl;
use AEPhone;

require 'init.pl';

$tmpl{'index'}	 = "tmp_index_top.html";
$offset	 = 0;
$limit	 = 999;

$cgifile = "disptopics_top.cgi";

foreach (param) {
	next unless $_;
	if (param("$_") =~ /\0/) {
		@{$_} = split(/\0/, param("$_"));
		@{$_} = map {jcode($data)->euc} @{$_};
	} else {
		${$_} = jcode(param("$_"))->euc;
	}
}

# テンプレート振り分け
if ($phone{'furiwake'} eq 'mobile') {
	foreach(keys %tmpl) {
		$tmpl{"$_"} =~ s/\.html/_m.html/;
	}
}

$dbh = DBI->connect($ds, $dbuser, $dbpass) or die("Cannot connect.");

&read_tmpl($tmpl{'index'});
print "Content-type: text/html\n\n";

$content{'cgifile'}		 = $cgifile;

#hit
$sth = $dbh->prepare("select count(*) from netmaster");
$sth->execute;
if ($sth->errstr) { print $sth->errstr; }
while($ref = $sth->fetchrow_arrayref){
	($hit) = @$ref;
}

$content{'hit'}		 = $hit;
$content{'start'}	 = $hit ? $offset+1 : 0;
$content{'prev'}	 = ($offset-$limit >= 0) ? $offset-$limit : '';
$content{'end'}		 = ($offset+$limit < $hit) ? $offset+$limit : $hit;
$content{'next'}	 = ($offset+$limit < $hit) ? $offset+$limit : '';
$nextnum			 = ($hit-$offset-$limit < $limit) ? $hit-$offset-$limit : $limit;

$sth = $dbh->prepare("select * from netmaster where public = '公開' 
	order by date desc, timeid desc
	offset $offset limit $limit");
$sth->execute;
if ($sth->errstr) { print $sth->errstr; }

$html{'head'} =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
print $html{'head'};
undef $html{'head'};

while($ref = $sth->fetchrow_hashref){
	my %data = %$ref;
	my $body = $html{'body'};
	my ($syear, $smonth, $sday) = split(/-/, $data{'date'});

	$content{'sdate'}	 = sprintf("%4d年%02d月%02d日", $syear, $smonth, $sday);
	$content{'buildname'}	 = $data{'buildname'};
	$content{'regname'}	 = $data{'regname'};
	$content{'sec1'}	 = $data{'sec1'};
	$content{'sec2'}	 = $data{'sec2'};
	$content{'sec3'}	 = $data{'sec3'};
	$content{'sec4'}	 = $data{'sec4'};
	$content{'sec5'}	 = $data{'sec5'};
	$content{'sec6'}	 = $data{'sec6'};
	$content{'sec7'}	 = $data{'sec7'};
	$content{'sec8'}	 = $data{'sec8'};
	$content{'sec9'}	 = $data{'sec9'};
	($content{'sec10'}	 = $data{'sec10'}) =~ s/\r\n|\r|\n/<br>\n/g;
	#$content{'url'}		 = qq{<a href="$data{'url'}">$data{'url'}</a>};

	$body =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
	print $body;
}
undef $html{'body'};

$content{'older'} = $content{'prev'} ne '' ?
	 qq{<input type="submit" name="submit" value="＜＜前$limit件">} : '';
$content{'newer'} = $content{'next'} ne '' ?
	 qq{<input type="submit" name="submit" value="次$nextnum件＞＞">} : '';

$html{'foot'} =~ s/%(\w+)%/jcode($content{"$1"})->sjis/ge;
print $html{'foot'};

$sth->finish if $sth;
$dbh->disconnect;

exit(0);
