#!/usr/local/bin/perl

#####送信データ取得
&get_form_data;

#####ページタイトル
$title   = "学校法人大彦学園　開志学園高等学校";
$gotop    = "http://www.kaishi.ed.jp/";

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

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

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

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

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

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

#=========================#
# 送信データ取得
#=========================#
sub get_form_data{
#####GETデータ取得
$query = $ENV{'QUERY_STRING'};
($year) = split(/&/, $query);
($name,$value) = split(/=/, $year);
$nendo = $value;

if($nendo > 25 || $nendo < 14){
undef $nendo;
undef $value;
}

}

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

#####一覧表示
($new_id,$new_short,$new_main,$new_image_data,$new_start_date) = split(/,/, $txt[0]);
($new_year,$new_month,$new_day) = split(/\//, $new_start_date);

($last_id,$last_short,$last_main,$last_image_data,$last_start_date) = split(/,/, $txt[-1]);
($last_year,$last_month,$last_day) = split(/\//, $last_start_date);

if($new_month < 4){
$new_year = $new_year-1;
}
if($last_month < 4){
$last_year = $last_year-1;
}

if($value ne'' ){
$new_years = $value;
}else{
$new_years = $new_year;
}
if($nendo eq''){$nendo = $new_year;}
&head;
$print_out = 0;

for($i = @txt; $i > 0; $i--){
        ($id,$short,$main,$image_data,$start_date,$end_date,$ctg) = split(/,/, $txt[-$i]);
        ($start_year,$start_month,$start_day) = split(/\//, $start_date);
        ($end_year,$end_month,$end_day) = split(/\//, $end_date);
if($end_day ne 'no_input'){
$end = "〜$end_day日";
	if($end_month ne 'no_input'){
	$end = "〜</nobr><br><nobr>$end_month月$end_day日";
		if($end_year ne 'no_input'){
		$end = "〜</nobr><br><nobr>$end_year年$end_month月$end_day日";
		if($end_year == $start_year){
		$end = "〜</nobr><br><nobr>$end_month月$end_day日";
			if($end_month == $start_month){
			$end = "〜$end_day日";
			}
		}
		}
	}
}else{
$end = "";
}
        
      $main_p = &wrap_change2($main);
($imagefile,$imgw,$imgh) = split(/--/, $image_data);
if($imagefile ne '' && $imagefile ne 'no_input' && $imagefile ne 'error'){
$imgtag = qq|<p><img src="../imgbox/$imagefile" border="0" width="$imgw" height="$imgh" alt="$imagefile-$imgw-$imgh" /></p>\n|;
}else{
undef $imgtag;
}

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


if($start_year == $new_years && $start_month > 3){
print <<__BODY__;
		          <tr>
          <td align="left" valign="top" class="month" nowrap="nowrap"><p><a name="$id" id="$id"></a>$start_month月</p></td>
          <td align="left" valign="top" class="days"><p><nobr>$start_day日$end</nobr></p></td>
          <td align="left" valign="top">$imgtag<p>$ctgimg$main_p</p></td>
		          </tr>
__BODY__
$print_out++;
}elsif($start_year == $new_years+1 && $start_month < 4){
print <<__BODY__;
		          <tr>
          <td align="left" valign="top" class="month" nowrap="nowrap"><p><a name="$id" id="$id"></a>$start_month月</p></td>
          <td align="left" valign="top" class="days"><p><nobr>$start_day日$end</nobr></p></td>
          <td align="left" valign="top">$imgtag<p>$ctgimg$main_p</p></td>
		          </tr>
__BODY__
$print_out++;
}else{
}
}

if ($print_out == 0){
if($nendo > 0){
print <<__BODY__;
		          <tr>
          <td><p>平成$nendo年度の新着NEWS&amp;お知らせはありません。</p></td>
		          </tr>
__BODY__
}else{
print <<__BODY__;
		          <tr>
          <td><p>現在新着NEWS&amp;お知らせはありません。</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

<?xml version="1.0" encoding="shift_jis"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
<title>学校法人 大彦学園　開志学園高等学校｜新着NEWS&amp;お知らせ</title>
<link rel="stylesheet" href="../css/common.css" type="text/css" />
<link rel="stylesheet" href="../css/infomation.css" type="text/css" />
</head>
<body>
<div id="temp-hd">
  <div class="temp-box">
    <div id="hd-lo">
      <div id="hd-ttl">
        <h1><a href="../index.html" title="学校法人 大彦学園　開志学園高等学校"><img src="../img/temp/hd-ttl.jpg" alt="学校法人 大彦学園　開志学園高等学校" width="288" height="122" /></a></h1>
      </div>
      <div id="hd-mn">
        <div id="hd-sct">
          <ul>
            <li id="sct-01"><a href="../index.html" title="HOME"><span>HOME</span></a></li>
            <li id="sct-02"><a href="../infomation/index.cgi" title="新着NEWS&amp;お知らせ"><span>新着NEWS&amp;お知らせ</span></a></li>
            <li id="sct-03"><a href="../topics/index.cgi" title="スケジュール"><span>スケジュール</span></a></li>
            <li id="sct-04"><a href="../privacy/index.html" title="プライバシーポリシー"><span>プライバシーポリシー</span></a></li>
          </ul>
          <p>学校法人大彦学園 開志学園高等学校「個性の力は無限大」</p>
        </div>
        <div id="hd-mct">
          <ul>
            <li id="mct-01"><a href="../school/index.html" title="学校紹介"><span>学校紹介</span></a></li>
            <li id="mct-02"><a href="../entrance/index.html" title="入試案内"><span>入試案内</span></a></li>
            <li id="mct-03"><a href="../access/index.html" title="アクセス"><span>アクセス</span></a></li>
            <li id="mct-04"><a href="../qa/index.html" title="Q&amp;A"><span>Q&amp;A</span></a></li>
            <li id="mct-05"><a href="http://www.kaishi.ed.jp/secure/entry/index.html" title="学校説明会"><span>学校説明会</span></a></li>
            <li id="mct-06"><a href="https://www.c-form.net/kaishi2150259/require.php" title="資料請求"><span>資料請求</span></a></li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</div>
<div id="temp-bd">
  <div class="temp-box">
    <div id="dir-bg">
      <p id="bread"><a href="../index.html">HOME</a> &gt; 新着NEWS&amp;お知らせ</p>
    </div>
  </div>
</div>
<div id="temp-ct">
  <div class="temp-box">
    <div id="ct-lo">
      <div id="ct-bg">
        <h3>新着NEWS&amp;お知らせ</h3>
__HEAD__

if($new_year > 0){
print <<__LOGHEAD__;
<h4 class="topics$nendo">平成$nendo年度</h4>
<div id="topics-log">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
__LOGHEAD__

$nendo_list=0;
for($y = $new_year; $y >= $last_year; $y--){
if($nendo_list<10){print <<__LINK__;
<td nowrap="nowrap" class="topics$y"><p><a href="index.cgi?scl=$y">＞平成$y年度</a></p></td>
__LINK__
$nendo_list++;
} else {print <<__LINK__;
<td nowrap="nowrap" class="topics$y"><p><a href="index.cgi?scl=$y">＞平成$y年度</a></p></td></tr><tr>
__LINK__
$nendo_list=0;
}
}

print <<__LOGFOOT__;
</tr>
</table>
</div>
__LOGFOOT__
}

print <<__HEAD2__;
<table width="830" border="0" cellspacing="0" cellpadding="0" id="topics-table" class="topics$nendo">
__HEAD2__

}

sub foot{
#####フッタ
print <<__FOOT__;
</table>
      </div>
      <div class="temp-gotop">
        <p><a href="#temp-hd">このページの先頭へ</a></p>
      </div>
    </div>
  </div>
</div>
<div id="temp-ft">
  <div class="temp-box">
    <p id="ft-school">学校法人 大彦学園<br />
      <strong>開志学園高等学校</strong></p>
    <p id="ft-address">〒950-0925 新潟市中央区弁天橋通1丁目4-1　\TEL 025-287-3390　\FAX 025-287-3363</p>
    <p id="ft-copy">Copyright(C) kaishigakuen high school. All right reserved. 禁無断転載</p>
  </div>
</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;
}
