#!/usr/bin/perl -wT
#
#     ESJ 各種申し込みページへの入り口表示プログラム
#
#     2008-12-26 Takenaka, A.
# rev 2010-07-22 Takenaka, A.

use strict;
use warnings;

use utf8;

use CGI;
use Encode;
use HTML::Template;
use CGI::Carp('fatalsToBrowser');

#=comment
use lib qw(/var/www/vhosts/esj.ne.jp/httpdocs/pm);
use ESJ::Util;
use ESJ::Info;
use ESJ::Entrance;
#=cut

my $charset = "UTF-8";

my $Data = new CGI; #   CGI オブジェクトを取得。global 変数。
$Data->charset($charset);
print $Data->header(-type=>"text/html");


my $lang = $ENV{'QUERY_STRING'};

my $n_meeting = &ESJ::Info::get_n_meeting();
my $meeting   = &ESJ::Info::get_meeting_abbrev();

my $notes    = &ESJ::Entrance::load_notes_on_entrance();
my $notes_li = &ESJ::Entrance::notes_to_html($notes);

my ($time_limit, $time_to_start);

my $n_entry = 0;
my $item;

open (my $handle, "<:utf8", 'entrance_tmpl.html' ) || die;
my $tmpl = HTML::Template->new( filehandle => $handle );
close $handle;

my $meeting_full_name = &ESJ::Info::get_meeting_full_name();
my $meeting_full_name_e = &ESJ::Info::get_meeting_full_name_e();

$tmpl->param(meeting_full_name => $meeting_full_name);
$tmpl->param(meeting_full_name_e => $meeting_full_name_e);
$tmpl->param(n_meeting => $n_meeting);
$tmpl->param(notes     => $notes_li);

my @items     = ();
my @schedules = ();
my @loop_data = ();  # initialize an array to hold your loop

&make_item_list;

while (@items and @schedules) {

     my %row_data;  # get a fresh hash for the row data
     $row_data{item}     = shift @items;
     $row_data{schedule} = shift @schedules;

     # the crucial step - push a reference to this row into the loop!
     push(@loop_data, \%row_data);
}

# finally, assign the loop data to the loop param, again with a reference:
$tmpl->param(ITEM_LOOP => \@loop_data);

print $tmpl->output;

####################


sub make_item_list
{

    if (&ESJ::Info::is_sympo_proposal_entry_visible()) {
        
        # print ("Sympo ");
        # print &ESJ::Info::is_sympo_proposal_entry_visible();
        
        my $schedule;
    	my $item = "シンポジウムの提案 (Proposal of symposium)";
        
    	$time_limit    = &ESJ::Info::get_sympo_proposal_limit();
    	$time_to_start = &ESJ::Info::get_sympo_proposal_start();

    	if (&ESJ::Util::is_to_start($time_to_start) 
    	    && &ESJ::Util::is_within_time_limit($time_limit)) {

            $item = "<A href = \"../sympo/index.cgi\">$item</A>";
            $schedule = "締め切り $time_limit (deadline)";
    	}
    	else {
    		unless (&ESJ::Util::is_to_start($time_to_start)) {
                $schedule = "$time_to_start 受け付け開始予定  (will be available)";
            }
            else {
                $schedule = "受け付け終了  (terminated) ";
            }
    	}

        push @items,     $item; # Encode::encode('utf8', $item);
        push @schedules, $schedule; # Encode::encode('utf8', $schedule);
    }


#####

    if (&ESJ::Info::is_reg_entry_visible()) {

        my $schedule;
        my $item;
        
    	$time_to_start        = &ESJ::Info::get_presen_reg_start();
        my $presen_time_limit = &ESJ::Info::get_presen_reg_limit();
    	my $reg_time_limit    = &ESJ::Info::get_reg_limit();

        unless (&ESJ::Util::is_to_start($time_to_start)) {

    	    $item = "講演者・企画者の参加申し込み・変更 (Registration / Update) ";
            $schedule = "$time_to_start 受け付け開始予定 (will be available)";

        }
        else {
            if (&ESJ::Util::is_within_time_limit($presen_time_limit)) {
        	    $item = "講演者・企画者の参加申し込み・変更 (Registration / Update)";
                $item = "<A href = \"../reg/index.cgi\">$item</A>";
                $schedule = "締め切り $presen_time_limit (deadline)";
            }
#    	    elsif (&ESJ::Util::is_within_time_limit($reg_time_limit)) {
#        	    $item = "講演者・企画者の参加申し込み";
#                $item = "<A href = \"../reg/index.cgi\">$item</A>";
#                $schedule = "締め切り $reg_time_limit (一般講演は受け付け終了)";
#            }
            else {
        	    $item = "講演者・企画者の参加申し込み (registration)";
                $schedule = "受け付け終了 (terminated) ";
            }
        }

        push @items,     $item; # Encode::encode('utf8', $item);
        push @schedules, $schedule; # Encode::encode('utf8', $schedule);
    }

#####

    if (&ESJ::Info::is_reissue_confirmation_visible()) {

        my $schedule;
        my $item;
        
    	$item = "登録確認メールの再発行 (Reissue confirmation mail)";
        $item = "<A href = \"../reg/index_reissue.cgi\">$item</A>";
        $schedule = "稼働中";

        push @items,     $item; # Encode::encode('utf8', $item);
        push @schedules, $schedule; # Encode::encode('utf8', $schedule);
    }
#####

#=comment
    if (&ESJ::Info::is_proposal_entry_visible()) {

        my $schedule;
#        my $item = "シンポ・フォーラム・企画集会・自由集会の概要の登録";
        my $item;

        # my @types = ("企画集会の提案", "自由集会の提案", "フォーラムの提案");
        my @types = ("企画集会の提案 (Organized session proposal)", 
                     "自由集会の提案 (Workshop proposal)", 
                     "フォーラムの提案 (Forum proposal)");
        my @keys  = ("organized", "ws", "forum");

    	$time_limit    = &ESJ::Info::get_proposal_limit();
    	$time_to_start = &ESJ::Info::get_proposal_start();

        my $n_items = @types;
        
        for (my $i = 0; $i < $n_items; ++$i) {

            my $item = $types[$i];
            my $key  = $keys[$i];

        	if (&ESJ::Util::is_to_start($time_to_start) 
        	    && &ESJ::Util::is_within_time_limit($time_limit)) {
                $item = "<A href = \"../proposal/index.cgi?$key\">$item</A>";
                $schedule = "締め切り $time_limit (deadline)";
        	}
        	else {
        		unless (&ESJ::Util::is_to_start($time_to_start)) {
                    $schedule = "$time_to_start 受け付け開始予定 (will be available)";
                }
                else {
                    $schedule = "受け付け終了  (terminated) ";
                }
        	}
            push @items,     $item; # Encode::encode('utf8', $item);
            push @schedules, $schedule; # Encode::encode('utf8', $schedule);
        }
    }
# =cut



#####

    if (&ESJ::Info::is_abstract_entry_visible()) {

        my $schedule;
    	$item = "講演要旨（一般講演，シンポ，企画集会）の登録 (Abstract submission)";

    	$time_limit    = &ESJ::Info::get_abst_limit();
    	$time_to_start = &ESJ::Info::get_abst_start();

    	if (&ESJ::Util::is_to_start($time_to_start) 
    	    && &ESJ::Util::is_within_time_limit($time_limit)) {

            $item = "<A href = \"../abstract/index.cgi\">$item</A>";
            $schedule = "締め切り $time_limit  (deadline)";
    	}
    	else {
    		unless (&ESJ::Util::is_to_start($time_to_start)) {
                $schedule = "$time_to_start 受け付け開始予定  (will be available)";
            }
            else {
                $schedule = "受け付け終了 (terminated) ";
            }
    	}
        push @items,     $item; # Encode::encode('utf8', $item);
        push @schedules, $schedule; # Encode::encode('utf8', $schedule);
    }

#####
=comment

    if (&ESJ::Info::is_cancel_entry_visible()) {

        my $schedule;
    	$item = "一般講演のキャンセル (Cancelation)";

    	$time_limit    = &ESJ::Info::get_presen_cancel_limit();
    	$time_to_start = &ESJ::Info::get_presen_cancel_start();

    	if (&ESJ::Util::is_to_start($time_to_start) 
    	    && &ESJ::Util::is_within_time_limit($time_limit)) {

            $item = "<A href = \"../cancel/index.cgi\">$item</A>";
            $schedule = "締め切り $time_limit  (deadline)";
    	}
    	else {
    		unless (&ESJ::Util::is_to_start($time_to_start)) {
                $schedule = "$time_to_start 受け付け開始予定  (will be available)";
            }
            else {
                $schedule = "受け付け終了 (terminated) ";
            }
    	}
        push @items,     $item; # Encode::encode('utf8', $item);
        push @schedules, $schedule; # Encode::encode('utf8', $schedule);
    }
=cut

#####

    if (&ESJ::Info::is_errata_entry_visible()) {

        my $schedule;
    	$item = "講演のタイトル・著者の誤りの正誤表掲載登録 (Erratum)";

    	$time_limit    = &ESJ::Info::get_errata_limit();
    	$time_to_start = &ESJ::Info::get_errata_start();

    	if (&ESJ::Util::is_to_start($time_to_start) 
    	    && &ESJ::Util::is_within_time_limit($time_limit)) {

            $item = "<A href = \"../errata/index.cgi\">$item</A>";
            $schedule = "締め切り $time_limit  (deadline)";
    	}
    	else {
    		unless (&ESJ::Util::is_to_start($time_to_start)) {
                $schedule = "$time_to_start 受け付け開始予定  (will be available)";
            }
            else {
                $schedule = "受け付け終了 (terminated) ";
            }
    	}
        push @items,     $item; # Encode::encode('utf8', $item);
        push @schedules, $schedule; # Encode::encode('utf8', $schedule);
    }

#####

    if (&ESJ::Info::is_presen_file_entry_visible()) {

        my $schedule;
    	$item = "口頭発表（一般講演）プレゼンファイルの登録 (Presentation file upload for oral session)";

    	$time_limit    = &ESJ::Info::get_presen_file_limit();
    	$time_to_start = &ESJ::Info::get_presen_file_start();

    	if (&ESJ::Util::is_to_start($time_to_start) 
    	    && &ESJ::Util::is_within_time_limit($time_limit)) {

            $item = "<A href = \"../presen_file/index.cgi\">$item</A>";
            $schedule = "締め切り $time_limit  (deadline)";
    	}
    	else {
    		unless (&ESJ::Util::is_to_start($time_to_start)) {
                $schedule = "$time_to_start 受け付け開始予定  (will be available)";
            }
            else {
                $schedule = "受け付け終了  (terminated) ";
            }
    	}
        push @items,     $item; # Encode::encode('utf8', $item);
        push @schedules, $schedule; # Encode::encode('utf8', $schedule);
    }

=comment
#####

    if (&ESJ::Info::is_reissue_confirmation_visible()) {

        my $schedule;
    	$item = "登録確認メールの再発行";

    	$time_to_start = &ESJ::Info::get_presen_reg_start();
       	$time_limit    = &ESJ::Info::get_reissue_limit();

    	if (&ESJ::Util::is_to_start($time_to_start) 
    	    && &ESJ::Util::is_within_time_limit($time_limit)) {
            $item = "<A href = \"../reg/index_reissue.cgi\">$item</A>";
            $schedule = "締め切り $time_limit  (deadline)";
            push @items,     $item;
            push @schedules, $schedule;
    	}
    }
=cut

#####

    if (@items == 0) {
        push @items,     "";
        push @schedules, "現在稼働中の受付システムはありません．";
   }

}

