package TDS::Explain;
# $Id: Explain.pm,v 1.9 2000/12/03 20:55:22 tom Exp $
################################################################

=head1 NAME

TDS::Explain - explain to showed diary

=head1 SYNOPSIS

 use TDS::Explain;
 use TDS::Status;

 $status = new TDS::Status;
 $e = new TDS::Explain;
 print $e->AsHTML($status);

=cut


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

use strict;
use vars qw(@ISA);

use ObjectTemplate;

use Template;
use CGI::QueryString;
use CGI::Tools;

@ISA = qw(ObjectTemplate);

use vars qw($Recent $Month $Day $Part $EachPart $Search);

=head1 STATIC VARIABLES

 $Recent        current temlate
 $Month         月別テンプレート
 $Day           日別テンプレート
 $Part          旬別テンプレート
 $EachPart      旬文字列テンプレート
 $Search        検索テンプレート

=cut

# テンプレート静的変数
$Recent = "最新 %num 日分" unless defined $Recent;
$Month = "%year年%month月分" unless defined $Month;
$Day = "%year年%month月%day日分" unless defined $Day;
$Part = "%year年%month月%part" unless defined $Part;
$EachPart = "すべて,上旬,中旬,下旬" unless defined $EachPart;
$Search = "検索: %keyword" unless defined $Search;

attributes qw(recent month day part each_part search);

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

=head1 MEMBER FUNCTIONS

=cut

sub initialize($)
{
    my $self = shift;
    
    $self->recent($Recent);
    $self->month($Month);
    $self->day($Day);
    $self->part($Part);
    $self->each_part($EachPart);
    $self->search($Search);
    
    $self->SUPER::initialize;
}


=head2 $e->AsString($status);

$status に従い、文字列として展開して返す

=cut

sub AsString($$$$)
{
    my ($self, $status) = @_;

    require TDS::Collection;
    my $template;
    my $day_part_string = $status->day_part;
    my $params = {
	num=>$TDS::Collection::RecentNum,
	keyword=>($TDS::Status->keyword) ? $TDS::Status->keyword->escaped : ""
    };
    # 日付
    require DateTime::Date;
    my $date = new DateTime::Date(year=>$status->year,
				  month=>$status->month,
				  day=>$status->day_part);
    $date->GetParams($params);

    if ($status->mode eq 'RECENT'){
	$template = $self->recent;
    } elsif ($status->mode eq 'SEARCH'){
	$template = $self->search;
    } else {
	my @part_string;
	if ($status->day_part =~ /[abc]/){
	    my @each_part = split(',', $self->each_part);
	    my %tmp =  ('a'=>$each_part[1], 'b'=>$each_part[2],
			'c'=>$each_part[3]);
	    $day_part_string = $tmp{$status->day_part};
#	    print "part: $day_part_string\n";
#	    print %tmp;
#	    print ",", $status->day_part, "\n";
	    $params->{part} = $day_part_string;
	    $template = $self->part;
	} elsif ($status->day_part =~ /[0-9]+/){
	    $template = $self->day;
	} else {
	    $template = $self->month;
	}
    }

    # author mode ならその旨表示
#    $template .= "[A]" if $status->is_author;
    
    Expand($template, $params);
}
1;


