#!/usr/local/bin/perl

#####ページタイトル
$title   = "トピックス";
$imgurl  = "../images";

#####読み込み用ファイル
$txtfile    = 'new.csv';

#####日本語コード変換用ファイル
$jcodelib   = 'jcode.pl';

###################################  データ読込
#################################################

#####ライブラリ読み込み
require "$jcodelib";
#####読み込める事を前提に
open(TXT, "+<$txtfile");
#####データファイルのロック
&lock_file(TXT);

#####過去のデータファイルを全部読みこむ
@txt = <TXT>;

#####データファイルのアンロック
&unlock_file(TXT);
#####データファイルのクローズ
close(TXT);

#####################################  HTML表示
#################################################

#####一覧表示
&head;
($this_id,$this_short,$this_main,$this_image_data,$this_start_date) = split(/,/, $txt[0]);
($thisyear,$thismonth,$thisday) = split(/\//, $this_start_date);
if($thismonth < 4){
$thisyear = $thisyear-1;
}

for($i = 0; $i < 6; $i++){
        ($id,$short,$main,$image_data,$start_date,$end_date,$ctg) = split(/,/, $txt[$i]);
        ($img_name,$img_width,$img_height) = split(/--/, $image_data);
        ($year,$month,$day) = split(/\//, $start_date);
        ($e_year,$e_month,$e_day) = split(/\//, $end_date);
if($e_day ne 'no_input'){
$end = "〜$e_day";
	if($e_month ne 'no_input'){
	$end = "〜$e_month\.$e_day";
		if($e_year ne 'no_input'){
		$e_year = $e_year+1988;
		$end = "〜$e_year\.$e_month\.$e_day";
		if($e_year == $year){
		$end = "〜$e_month\.$e_day";
			if($e_month == $month){
			$end = "〜$e_day";
			}
		}
		}
	}
}else{
$end = "";
}

$page = $year;
if($year < $page){
$page--;
}else{
if($year == $page && $month < 4){$page--;}
}

$year = $year+1988;
$date = "$year\.$month\.$day$end";
      $short_p = &wrap_change2($short);
      $date_p = &wrap_change2($date);

if($img_name ne 'no_input'){
$img = qq|<img src="../img/top/picture.gif" alt="写真有り" width="17" height="13" class="picture">|;
}else{
$img = qq||;
}

if($ctg eq 'ctg1'){
$ctgimg = qq|<img src="../img/infomation/icon-ctg1.gif" alt="お知らせ" width="56" height="14">|;
}elsif($ctg eq 'ctg2'){
$ctgimg = qq|<img src="../img/infomation/icon-ctg2.gif" alt="イベント" width="56" height="14">|;
}elsif($ctg eq 'ctg3'){
$ctgimg = qq|<img src="../img/infomation/icon-ctg3.gif" alt="活動報告" width="56" height="14">|;
}else{
$ctgimg = qq|&nbsp;|;
}



if($id ne ''){
print <<__BODY__;
<tr>
<td class="align-left"><p>$date_p</p></td>
<td class="ctg"><p>$ctgimg</p></td>
<td><p><a href="../infomation/index.cgi?new=$page#$id" target="_parent">$short_p</a>$img</p></td>
</tr>
__BODY__
}

}

&foot;
exit();

#################################  サブルーチン
#################################################

# 使用できない文字の置換
sub norm_input {
    local($string) = @_;
    $string =~ s/&/&amp;/g;
    $string =~ s/"/&quot;/g;
    $string =~ s/</&lt;/g;
    $string =~ s/>/&gt;/g;
    $string =~ s/,/&#44;/g; # 保存時のデータ区切りと混乱するため
    $string =~ s/\r\n/\n/g;
    $string =~ s/\r/\n/g;
    $string =~ s/\n\n/<br> <br>/g;
    $string =~ s/\n//g;
    return $string;
}

# ロック
sub lock_file {
    local(*FILE) = @_;
    if ($uselock) {
        eval("flock(FILE, 2)"); # 2=LOCK_EX
        if ($@) {
            # flock が使えない場合、ここに来る。
            return 0;
        }
    }
    return 1;
}

# アンロック
sub unlock_file {
    local(*FILE) = @_;
    if ($uselock) {
        eval("flock(FILE, 8)"); # 8=LOCK_UN
    }
}

#=========================#
# 内容確認ヘッダフッタ
#=========================#
sub head{
#####ヘッダ
print <<__HEAD__;
content-type:text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>
<title>スケジュール</title>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis">
<link rel="stylesheet" href="../css/reset.css" type="text/css" />
<link rel="stylesheet" href="../css/iframe.css" type="text/css" />
</head>

<body>
<div class="temp-new">
<table border="0" cellspacing="0" cellpadding="0">
__HEAD__

}

sub foot{
#####フッタ
print <<__FOOT__;
</table>
</div>
</body>
</html>
__FOOT__

}

#=====================#
# 文字の置換
#=====================#
sub wrap_change2 {
##### 使用できない文字の置換
    local($string) = @_;
    $string =~ s/&#44;/,/g; # 保存時のデータ区切りと混乱するため
    $string =~ s/&gt;/>/g;
    $string =~ s/&lt;/</g;
    $string =~ s/&quot;/"/g;
    $string =~ s/&amp;/&/g;
    return $string;
}
