#!/usr/bin/perl
#
# index.cgi
# ＴＯＰページ生成ＰＧ
# 2004/04/11
#

use strict;								#コーディングを厳格化
use Fcntl qw(:DEFAULT :flock);			#ファイルロックモジュール
use CGI;								#ＣＧＩモジュール

require'./cgi/kanri/common.pl';			# 初期値ライブラリ

use vars qw ($q $photo_h $photo_w $template %vals $csvfile @lines @line %category @category $to_item $t_item $t_category @t_line $val_no $ransu $ransu2);

# 画像表示サイズ(ここを変更するとレイアウトが崩れる可能性がある)
$photo_w = "135";
$photo_h = "135";

# TOPページのテンプレートファイル名
$template = "t_index.html";

# 商品ファイル
$csvfile = "./cgi/kanri/item.csv";

# ＴＯＰページに表示させる商品のカテゴリ(カテゴリコードを記述する)
@category = ('1','2','3','4');

# ひとつのカテゴリで表示させる商品数
$to_item = 2;

#----------------初期設定ここまで-----------------------------------------#

$q = new CGI;


# 商品ファイル開く
sysopen(IN, $csvfile, O_RDONLY) or die &error("システムエラーFILE1");
	flock(IN, LOCK_EX) or die &error("システムエラーLOCK2");
	@lines = <IN>;
close(IN);

# 乱数発生の為の各カテゴリの登録件数を取得(０からこの数字までを乱数として発生させる)
foreach(@lines){
	@line = split(/,/);
	$category{"$line[6]"}++;
}

# テーブルタグ生成
$val_no = 1;
foreach $t_category(@category){

	#１カテゴリ中$to_item件の商品を表示
	OUTERLOOP: for(my $i=0; $i<$to_item; $i++){

		# 該当カテゴリ件数を上限に乱数発生させる。ただし前のループで求めた乱数以外、もしくは１０回ループ後でないとループを抜けない。
		my $k = 0;
		while(1){
			$ransu = int( rand($category{"$t_category"}) );
			if($ransu ne $ransu2){ last; }
			$k++;
			if($k > 10){ $val_no++;  last(OUTERLOOP); } # 10回ループしても違う乱数が求まらない場合は登録件数が１件しかないと判断
		}
		my $j = 0;
		#商品データ読み込み
		foreach $t_item(@lines){
			@t_line = split(/,/,$t_item);

			# 該当カテゴリでなければ次
			if($t_line[6] ne $t_category || $t_line[12] eq '1'){ next; }

			# 発生した乱数と同じ順番であれば商品テーブルタグ生成
			if($j eq $ransu ){
				$vals{"item$val_no"} = &table(@t_line);
				$val_no++;
				$ransu2 = $ransu; 
			}
			$j ++;
		}
	}

}


# テンプレートファイル開いて標準出力へはきだし
sysopen(IN, $template, O_RDONLY) or die &error("システムエラーFILE");
	flock(IN, LOCK_EX) or die &error("システムエラーLOCK");

	print $q->header(-charset=>'Shift_JIS');
	while(<IN>){
		s/__%(.+?)%__/$vals{$1}/g;
		print;
	}
close(IN);

exit;




#------------------------ 以下サブルーチン ----------------------------#

#-----------------------------------------#
# テンプレートに吐き出す商品テーブル生成  #
#-----------------------------------------#
sub table{

	my(@item,$price);

	@item = @_;

	# 単価
	if($item[3] eq '1'){
		my @temp = split(/<br>/,$item[4]);
		foreach(@temp){ $price .= $_."円<br>\n";}

	}else{
		$price = $item[4]."円";
	}

	# 画像がある場合はタグを生成
	my $phototag = "<img src=\"../img/nophoto.gif\" width=\"$photo_h\" height=\"$photo_h\" border=\"0\" alt=\"詳細へ\">";
	if($item[14]){ 
		$phototag = "<img src=\"./cgi/kanri/cgiphoto/$item[14]\" width=\"$photo_w\" height=\"$photo_h\" border=\"0\" alt=\"詳細へ\">";
	}

my $tag = <<EOF;
<table width="135" border="0" cellspacing="0" cellpadding="0">
	<tr> 
		<td>
		<table width="135" border="0" cellspacing="0" cellpadding="0">
		<tr> 
			<td bgcolor="#888888">
				<table width="100%" border="0" cellspacing="1" cellpadding="0">
				<tr> 
				<td bgcolor="#ffffff"><a href="./cgi/item.cgi?itemno=$item[0]">$phototag</a></td>
				</tr>
				</table>
			</td>
		</tr>
		</table>
		</td>
	</tr>
	<tr> 
		<td><img src="img/spacer.gif" width="1" height="10"></td>
	</tr>
	<tr> 
		<td><strong>$item[1]</strong><br>
		<br>
		$price</td>
	</tr>
</table>
EOF

return $tag;

}

#------------#
#    エラー  #
#------------#
sub error{
# 引数１：エラーメッセージ

print $q->header(-charset=>'Shift_JIS');
print $q->start_html();
print "<center><b><font color=\"#ff0000\">$_[0]</font></b></center>";
print $q->end_html;
exit;
}

__END__