#!/usr/bin/perl
#
#
### 変数設定部 （詳細は上記ページをご覧下さい） ######################

### 以下は配色の設定 ###

$col_anm = "#000000"; # No.xx 〜 No.xx
$col_num = "#000000"; # No.
$col_ttl = ""; # タイトル
$col_gnr = "#000000"; # 分野
$col_day = "#000000"; # 日付
$col_url = "#000000"; # ＵＲＬ
$col_key = "#000000"; # キーワード
$col_pid = "#000000"; # ＩＤ

### （以下は表示に関するカスタマイズ）

# 表示単位（１ページに〜件ずつ）
$show_scale = 20; 
# 「登録順」を選んだとき何ページ分表示するか
$default_pages = 1; 
# 検索記録を取るか（1→とる、0→とらない）
$user_trace = 0;
# 「全表示」が選択された場合の表示限度数（0→無制限）
$show_max = 1; 
# 更新日から何日以内なら「New」「Up」を付けるか
$new_date = 7;
# "NEW" の画像
$image_new = "";
# "UP" の画像
$image_up = "";


### 変数設定部 （ここまで）###########################################

require './lib.pl';
require './design.pl';
require './list01.pl';

&main;

sub main{
	&init_variables;
	&check_input;
	&open_datafile;
	&process_data;
	&show_html;
}



########## 変数の初期化
sub init_variables{
	$basedir = $tblib'basedir;
	
	$datafile = "./list01/data.txt";
	$genrefile = "./list01/genre.txt";
	$ulogfile = "./list01/userlog.txt";
	 $mainsubfile = "data/indexsub.html"; #タイトルファイル
}



########## 入力のチェック
sub check_input{
	&tblib'parseform;
	
	$s = $tblib'F{'s'}; # 検索文字列
	$m = $tblib'F{'m'}; # 検索モード
	$g = $tblib'F{'g'}; # 分野コード
	$p = $tblib'F{'p'}; # ページ
	$l = $tblib'F{'l'}; # ビルトインファイル（言語）
	$sdec = &tblib'decode($s);


if($l eq "e"){
$menufile = "data/menu_e.txt"; #メニューファイル_ENGLISH
}else{
$menufile = "data/menu.txt"; #メニューファイル
}
if($l eq "e"){
 $titlefile = "data/title_e.txt"; #タイトルファイル_ENGLISH
}else{
 $titlefile = "data/title.txt"; #タイトルファイル
}


########## タイトルなどの置換

	&tblib'openfile($titlefile, *title);
	&tblib'openfile($menufile, *menu);

	foreach (@title){
		($titlea, $titleb, $titlec, $titled, $titlee, $titlef) = split(",",$_,6);

if($titlea eq ""){
$titlea = "名称未設定";
}else{
$titlea = "$titlea";
}

if($titleb eq ""){
$titleb = "#FFFFFF";
}

if($titlec eq ""){
$titlec = "#FFFFFF";
}

if($l eq "e"){
	$tpfile = "./00000_e.html"; # レイアウトファイル_ENGLISH
}else{
	$tpfile = "./00000.html"; # レイアウトファイル
}
$design = $titled;

########## 左側の置換

	&tblib'openfile($mainsubfile, *detailsub);

	foreach (@detailsub){
	&design'design($titled, $titledurl, $shopdbfile, $d_design);
	}
$detailsub = "<table width=174 border=0 cellpadding=0 cellspacing=0><tr><td valign=\"top\" align=\"left\">@detailsub</td></tr></table>";




##メニュー自動追加ここから
	&design'menu($titled, $titledurl, *menu, $l);
	
	if( -e "./data/$sub_menu_design.txt"){
	$menufile_sub = "./data/$sub_menu_design.txt";
	&tblib'openfile($menufile_sub, *menu_sub);
	&design'menu_sub($titled, $titledurl, *menu_sub);
	}
##メニュー自動追加ここまで
}
	
	### スイッチ
#	($m =~ /i/) && ($sw_sid = 1); # ＩＤ表示
#	($m =~ /k/) && ($sw_kwd = 1); # キーワード表示
#	($m =~ /a/) && ($sw_all = 1); # 一気表示
}



########## データファイルのオープン
sub open_datafile{
	&tblib'openfile($datafile, *datas);
	&tblib'openfile($tpfile, *tps);
	&tblib'openfile($genrefile, *genres);
	
	&tblib'set_genre(*genres);
}



########## データの加工
sub process_data{
	local($us, $uw, $uz); # ユーザの行動記録用
	if($s){
		$us = "S"; $uw = $sdec;
		$stype = "検索結果";
		&search_by_word($sdec);
		$g = "";
	}
	elsif($g){
		$stype = "検索結果";
		($tblib'G{$g}) && &search_by_genre($g);
		$s = "";
	}
	else{
		$uz = "0";
		$stype = "検索結果";
		&search_by_word($uz);
		$g = "";
	}
	
	$hitno = @hits; # ヒット件数
	
	### 全表示の場合の表示単位セット
	($sw_all) && ($show_scale = $show_max || $hitno);
	
	### ユーザ行動の保存（トレースオン、検索、ページでない場合）
	($user_trace) && ($us) && (!$p) && &record_action($us, $uw, $hitno);
	
	### データを表示範囲でカット
	($hitno) && (&cut_data);
	
	$resultmsg .= "";
	
}



############### 検索関数：ここから ###############
##### 単語検索
sub search_by_word{
	local($word) = @_;
	
	# 検索文字の処理
	$efind = $word;
	$efind =~ s/　/ /g;
	$efind =~ s/(\W)/\\$1/g;
	@efinds = split(/\\\s+/, $efind);
	if($efinds[0] eq "\\-"){
		shift(@efinds);
		$sw_or = 1;
	}
	
	local($match, $tmp, $001, $002, $003, $004, $005, $006, $007, $008, $009, $010, $011, $012, $013, $014, $015, $016, $017, $018, $019, $020, $021, $022, $023, $024, $025, $026, $027, $028, $029, $030, $031, $032, $033, $034, $035, $036, $037, $038, $039, $040);
		$datestr = &get_date_string;

	### 検索
	foreach(@datas){
		($id, $tmp, $tmp, $001, $002, $003, $004, $005, $006, $007, $008, $009, $010, $011, $012, $013, $014, $015, $016, $017, $018, $019, $020, $021, $022, $023, $024, $025, $026, $027, $028, $029, $030, $031, $032, $033, $034, $035, $036, $037, $038, $039, $040) = split("\t");
		$str = "$id";
		$match = 0;

		# マッチング
		foreach $pattern(@efinds){
			if($str =~ /$pattern/i){
				$match++;
		$shopname = $001;
			}
		}
		### and検索の場合、マッチ数が文字数に一致しなければダメ
		(!$sw_or) && ($match < @efinds) && ($match = 0);
		($match !=0) && unshift(@hits, $_);
	}
}



########## 分野検索
sub search_by_genre{
	local($gcode) = @_;
	local($id, $gbuf, $match, $tmp, $001, $002, $003, $004, $005, $006, $007, $008, $009, $010, $011, $012, $013, $014, $015, $016, $017, $018, $019, $020, $021, $022, $023, $024, $025, $026, $027, $028, $029, $030, $031, $032, $033, $034, $035, $036, $037, $038, $039, $040, $datestr);
		$datestr = &get_date_string;
	
	foreach (@datas){
		($id, $gbuf, $tmp, $001, $002, $003, $004, $005, $006, $007, $008, $009, $010, $011, $012, $013, $014, $015, $016, $017, $018, $019, $020, $021, $022, $023, $024, $025, $026, $027, $028, $029, $030, $031, $032, $033, $034, $035, $036, $037, $038, $039, $040) = split("\t");
		($gbuf =~ /:$gcode:/) && unshift(@hits, $_);
	}
}



########## 日付順検索
sub search_by_date{
	local($amount) = @_;
	local($id, $gbuf, $match, $tmp, $001, $002, $003, $004, $005, $006, $007, $008, $009, $010, $011, $012, $013, $014, $015, $016, $017, $018, $019, $020, $021, $022, $023, $024, $025, $026, $027, $028, $029, $030, $031, $032, $033, $034, $035, $036, $037, $038, $039, $040, $datestr);
		$datestr = &get_date_string;
	
	### 末尾が最新なのでひっくり返すだけ
	foreach (@datas){
		($id, $gbuf, $tmp, $001, $002, $003, $004, $005, $006, $007, $008, $009, $010, $011, $012, $013, $014, $015, $016, $017, $018, $019, $020, $021, $022, $023, $024, $025, $026, $027, $028, $029, $030, $031, $032, $033, $034, $035, $036, $037, $038, $039, $040) = split("\t");
	@hits = @datas;
	($amount) && (@hits = splice(@hits, 0, $amount));
}
}
############### 検索関数：ここまで ###############





########## ユーザ行動の記録
sub record_action{
	local($type, $word, $hitno) = @_;
	local($udate, $logline,);
	
	$udate = &tblib'get_time(0, 2);
	$logline[0] = "$udate\tTYPE:$type($word)\tHITS:$hitno\tHOST:$ENV{'REMOTE_HOST'}\t\n";
	
	&tblib'updatefile($ulogfile, *logline, 1);
}



########## データを表示単位で切り抜く
sub cut_data{
	$p = int($p);
	$allpage = int(($hitno-1) / $show_scale) + 1;
	($p > 0) && ($p <= $allpage) || ($p = 1);
	
	# 表示範囲の特定
	$t = $p * $show_scale;
	$f = $t - $show_scale + 1;
	($t < $hitno) ? ($next = $p + 1) : ($t = $hitno);
	($f > 1) && ($prev = $p - 1);
	
	# 前後を切り取る
	@hits = reverse(@hits);
	unshift(@hits, "dmy");
	@hits = splice(@hits, $f, $show_scale);
	
	# 前後へのリンク
	$pagelink = "<FONT SIZE=\"2\"><B><FONT COLOR=\"$col_anm\">（No.$f 〜 No.$t）</FONT> ／ ";
	$linkcgi = "list01.cgi?s=$s&g=$g&m=$m";
	($prev) && ($pagelink .= "<A HREF=\"$linkcgi&p=$prev\">前の$show_scale件</A> ／ ");
	for(1 .. $allpage){
		$pagelink .= ($_ == $p) ? "$_ " : "<A HREF=\"$linkcgi&p=$_\">$_</A> ";
	}
	($next) && ($pagelink .= "／ <A HREF=\"$linkcgi&p=$next\">次の$show_scale件</A>");
	$pagelink .= "</B></FONT>";
}



########## 表示
sub show_html{
	local($urllist, $genrebar);
	
		##perlバージョン対策ここから

foreach(@menu){
$menu_tmp .= $_;
}


##perlバージョン対策ここまで

	$urllist = ($hitno) ? &html_urllist : &html_nohit;
	$genrebar = &html_genrebar;
	$C{'keys'} = $sdec;
	
	### 表示用ハッシュ
	if($m){
		$C{'m'} = $m;
		($sw_sid) && ($C{'mode'} .= "[1");
		($sw_kwd) && ($C{'mode'} .= "[2]");
		($sw_all) && ($C{'mode'} .= "[3]");
	}
	
	$C{'allno'} = @datas; # 総登録件数
	
	($s || $m || $g) || ($tptype = "firstmsg");
	
	print "Content-type: text/html\n\n";
	
	$tpfrag = 1;
		$titlea = "$titlea／$shopname";
	foreach (@tps){
		(/^<TEMPLATE TYPE="(\w+)">/) && ($1 ne $tptype) && ($tpfrag = 0); 
		(/^<\/TEMPLATE>/) && ($tpfrag = 1);
		($tpfrag) || next;
		
		$main = $resultmsg;
		$main .= "<br><br>";
#		$main .= $pagelink;
#		$main .= "<br><br>";
		$main .= $urllist;
		$main .= "<br><br>";
#		$main .= $pagelink;
		
		
		s/#GENRELIST#/$genrebar/;
		s/#BACKURL#/$tblib'backurl/;
		s/#CHK:(\w+)#/$C{$1}/g;
		s/#TITLEA#/$titlea/;
		s/#TITLEB#/$titleb/;
		s/#TITLEC#/$titlec/;
		s/#TITLED#/$titled/;
		s/#TITLEE#/$titlee/;
		s/#TITLEF#/$titlef/;
		s/#FILETITLE#/$filetitle/;
		s/#MENU#/$menu_tmp/g;
		s/#MAIN#/$main/g;
		s/#SEARCHRESULTS#//g;
		s/#MAINSUB#/$detailsub/g;

		print;
	}
}



########## ヒットしたＵＲＬの表示
sub html_urllist{
	local($id, $gcode, $date, $001, $002, $003, $004, $005, $006, $007, $008, $009, $010, $011, $012, $013, $014, $015, $016, $017, $018, $019, $020, $021, $022, $023, $024, $025, $026, $027, $028, $029, $030, $031, $032, $033, $034, $035, $036, $037, $038, $039, $040, $datestr);
	local($htmlbuf, @gs, $gbuf, $gdate, $isNew);
	
$datestr = &get_date_string;

	if($new_date){
		$new_date = &tblib'get_time(time() - $new_date * 60 * 60 * 24);
		$new_date =~ s/\///g;
	}
	
	foreach(@hits){
		($id, $gcode, $date, $001, $002, $003, $004, $005, $006, $007, $008, $009, $010, $011, $012, $013, $014, $015, $016, $017, $018, $019, $020, $021, $022, $023, $024, $025, $026, $027, $028, $029, $030, $031, $032, $033, $034, $035, $036, $037, $038, $039, $040) = split("\t");
		
		### キーワードの強調表示
		foreach(@efinds){
			$com1 =~ s/($_)/$1/ig;
			$com1 =~ s/\\(\w)/$1/ig;
		}
		
		### 分野の表示
		undef($gbuf);
		@gs = split(":", $gcode);
		foreach (@gs){
			($_) && ($gbuf .= " [$tblib'G{$_}]");
		}
		
		### new, up の表示
		if($new_date){
			undef($img);
			$isNew = ($com1 =~ /<UP\/>$/) ? 0 : 1;
			$gdate = $date;
			$gdate =~ s/\///g;
			if($gdate >= $new_date){
				$img = ($isNew) ? $image_new : $image_up;
			}
		}
		
		($sw_sid) && ($ibuf = "<FONT COLOR=\"$col_pid\"><B>[ID]</B> No.$id</FONT><BR>\n");
		($sw_kwd) && ($kbuf = "<FONT COLOR=\"$col_key\"><B>[Keys]</B> $keywords</FONT><BR>\n");
		
		### 検索結果表示部 ($htmlbuf) ############
		###
		### ※ 変更前に初期状態のものを必ず保存しておいて、
		###    いつでも元に戻せるようにしておいてください。
		##
		## $gbuf  ... 分野
		## $ibuf  ... ＩＤ（ＩＤ表示モードのときのみ）
		## $kbuf  ... キーワード（キーワード表示モードのときのみ）
		## 

if($007){$0077 = "<td nowrap width=30>TEL:</td><td>$007</td></tr>";}else{$0077 = "";}
if($008){$0088 = "<tr><td nowrap width=30>FAX:</td><td>$008</td>";}else{$0088 = "";}
if($005){$0055 = "■営業時間";}else{$0055 = "";}
if($006){$0066 = "■定休日";}else{$0066 = "";}
if($012){$0122 = "<img src=\"http://www.nakamachi.gr.jp/$012\" border=0 hspace=3>";}else{$0122 = "";}
if($013){$0133 = "<table width=\"100\" height=\"100\" class=\"list01_detail_013_back\"><tr><td align=\"center\" valign=\"middle\"><img src=\"http://www.nakamachi.gr.jp/$013\" border=0></td></tr></table>";}else{$0133 = "";}
if($010){$0100 = "<a href=\"$011\" target=\"_blank\"><img src=\"data/list01_detail_li01.gif\" width=\"60\" height=\"45\" border=\"0\" align=\"middle\" hspace=\"3\">より詳細な情報はこちら</a>";}else{$0100 = "";}

$googlemap="<IFRAME src=\"detail_googlemap_2007.cgi?s=$id\" width=\"350\" height=\"350\" scrolling=\"NO\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\"></IFRAME>";


$htmlbuf .="<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"  class=indexlist_table><tr><td align=\"left\" valign=\"top\">
<img src=\"data/00000_shopinfo.gif\" alt=\"\" height=\"25\" width=\"120\" border=\"0\" vspace=\"3\"><br>
$001<br><br>
$003$004<br>
<table cellspacing=\"0\" cellpadding=\"0\"><tr>$0077$0088</tr></table><br>
$0055<br>
$005<br><br>
$0066<br>
$006<br><br>
<table cellspacing=\"5\" cellpadding=\"0\"><tr><td align=\"left\" valign=\"top\">
$0133
</td><td align=\"left\" valign=\"top\">
$009<br /><br />
$0100
</table>

</td><td align=\"left\" valign=\"top\">
$0122<br><br>
$googlemap


</td>			</tr><tr><td align=\"left\" valign=\"top\" colspan=\"2\">

<img src=\"data/00000_shoplog.gif\" alt=\"\" height=\"25\" width=\"120\" border=\"0\" vspace=\"3\"><br>";

$htmlbuf .= &list01'list(logadd,$id);


$htmlbuf .="</td>			</tr>		</table>";
$014 =~ s/\n//g;
if($014){
$htmlbuf .="<IMG SRC=\"../cgi-bin/wwwcount.cgi?hide+00.gif+name+$014\">";
}		
		##
		##
		### ここまで ##################
		
		$f++;
	}
	
	$htmlbuf;
}



########## 分野リンク
sub html_genrebar{
	local($htmlbuf, $sgcode, $sgname, $sgcom);
	
	$htmlbuf = "--<A HREF=\"./list01.cgi?m=$m\">■ 新着順</A><BR><P>\n";
	foreach (@genres){
		(/^\w/) || next;
		($sgcode, $sgname, $sgcom) = split("\t");
		$htmlbuf .= "--<A HREF=\"./list01.cgi?g=$sgcode&m=$m\">■ $sgname</A><BR>\n";
	}
	
	$htmlbuf;
}



##### ノーヒットの表示
sub html_nohit{
	"<CENTER><FONT SIZE=\"4\">現在、最新情報はございません。</FONT></CENTER>";
}


##### 半年前の日時を読み込む

sub get_date_string {

local($sec, $min, $hour, $day, $mon, $year);

( $sec, $min, $hour, $day, $mon, $year ) = localtime(time - 15811200);

$year += 1900;

$mon++;

$mon = sprintf("%02d",$mon);

$day = sprintf("%02d",$day);

return "$year$mon$day";

}

sub put_comma {
  my $num = $_[0];
  $num = reverse $num;
  $num =~ s/(\d{4})(?=\d)(?!\d*\.)/$1-/g;
  $num = reverse $num;
  return "$num";
}

