#webliberty::App::Trackback.pm (2006/02/18)
#Copyright(C) 2002-2006 Knight, All rights reserved.

package webliberty::App::Trackback;

use strict;
use base qw(webliberty::Basis);
use webliberty::Lock;
use webliberty::Skin;
use webliberty::App::Diary;

### コンストラクタ
sub new {
	my $class = shift;

	my $self = {
		init   => shift,
		config => shift,
		query  => shift,
		html   => undef
	};
	bless $self, $class;

	return $self;
}

### メイン処理
sub run {
	my $self = shift;

	$self->output_list;

	return;
}

### トラックバック一覧
sub output_list {
	my $self = shift;

	my $skin_ins = new webliberty::Skin;
	$skin_ins->parse_skin("$self->{init}->{skin_dir}$self->{init}->{skin_header}");
	$skin_ins->parse_skin("$self->{init}->{skin_dir}$self->{init}->{skin_trackback}");
	$skin_ins->parse_skin("$self->{init}->{skin_dir}$self->{init}->{skin_diary}");
	$skin_ins->parse_skin("$self->{init}->{skin_dir}$self->{init}->{skin_footer}");

	my $diary_ins = new webliberty::App::Diary($self->{init}, $self->{config}, $self->{query});

	if (!$self->{config}->{site_url}) {
		$self->error('サイトのURLが設定されていません。');
	}

	$skin_ins->replace_skin(
		$diary_ins->info,
		INFO_NO => $self->{query}->{no}
	);

	my($index_date, $index_no, $flag);

	open(FH, "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$self->{init}->{data_diary_index}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_diary_dir}$self->{init}->{data_diary_index}");
	while (<FH>) {
		chomp;
		my($date, $no, $id, $stat, $field, $name) = split(/\t/);

		if ($self->{query}->{no} == $no) {
			$index_date = $date;
			$index_no   = $no;

			$flag = 1;

			last;
		}
	}
	close(FH);

	if (!$flag) {
		$self->error('指定された記事は存在しません。');
	}

	my $data_file;
	if ($index_date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
		$data_file = "$1$2\.$self->{init}->{data_ext}";
	}

	$self->{html}->{header}   = $skin_ins->get_data('header');
	$self->{html}->{contents} = $skin_ins->get_data('contents');

	open(FH, "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$data_file") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_diary_dir}$data_file");
	while (<FH>) {
		chomp;
		my($no, $id, $stat, $break, $comt, $tb, $field, $date, $name, $subj, $text, $color, $icon, $file, $host) = split(/\t/);

		if ($index_no == $no) {
			if (!$tb) {
				$self->error('この記事にトラックバックを送信することはできません。');
			}

			last;
		}
	}
	close(FH);

	if (-e "$self->{init}->{data_dir}$self->{init}->{data_tb_dir}$self->{query}->{no}\.$self->{init}->{data_ext}") {
		$self->{html}->{contents} .= $skin_ins->get_replace_data(
			'trackback_head',
			ARTICLE_TRACKBACK_START => '<!--',
			ARTICLE_TRACKBACK_END   => '-->',
		);

		open(FH, "$self->{init}->{data_dir}$self->{init}->{data_tb_dir}$self->{query}->{no}\.$self->{init}->{data_ext}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_tb_dir}$self->{query}->{no}\.$self->{init}->{data_ext}");
		while (<FH>) {
			chomp;
			my($no, $pno, $stat, $date, $blog, $title, $url, $excerpt) = split(/\t/);

			$self->{html}->{contents} .= $skin_ins->get_replace_data(
				'trackback',
				$diary_ins->trackback_article($no, $pno, $stat, $date, $blog, $title, $url, $excerpt)
			);
		}
		close(FH);

		$self->{html}->{contents} .= $skin_ins->get_replace_data(
			'trackback_foot',
			ARTICLE_TRACKBACK_START => '<!--',
			ARTICLE_TRACKBACK_END   => '-->',
		);
	}

	$self->{html}->{footer} = $skin_ins->get_data('footer');

	print $self->header;
	foreach ($skin_ins->get_list) {
		print $self->{html}->{$_};
	}

	return;
}

1;
