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

package webliberty::App::Search;

use strict;
use base qw(webliberty::Basis);
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_form;

	return;
}

### 検索フォーム
sub output_form {
	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_search}");
	$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});

	$skin_ins->replace_skin(
		$diary_ins->info
	);

	$self->{html}->{header}   = $skin_ins->get_data('header');
	$self->{html}->{contents} = $skin_ins->get_replace_data(
		'contents',
		FORM_WORD => $self->{query}->{word}
	);
	$self->{html}->{footer} = $skin_ins->get_data('footer');

	if ($self->{query}->{word}) {
		my $words = $self->{query}->{word};
		$words =~ s/　/ /g;
		my @words = split(/\s+/, $words);

		opendir(DIR, "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_diary_dir}");
		my @dir = sort { $b <=> $a } readdir(DIR);
		closedir(DIR);

		my $flag = 1;

		$self->{html}->{navi}  = $skin_ins->get_data('navi_head');
		$self->{html}->{navi} .= $skin_ins->get_data('navi');
		$self->{html}->{navi} .= $skin_ins->get_data('navi_foot');

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

		foreach my $entry (@dir) {
			if ($entry !~ /^\d\d\d\d\d\d\.$self->{init}->{data_ext}$/) {
				next;
			}
			if ($flag) {
				open(FH, "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$entry") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_diary_dir}$entry");
				while (<FH>) {
					chomp;
					my($no, $id, $stat, $break, $comt, $tb, $field, $date, $name, $subj, $text, $color, $icon, $file, $host) = split(/\t/);

					my $show_flag;

					foreach my $word (@words) {
						$word = lc($word);
						my $string = lc("$no\t$id\t$date\t$name\t$subj\t$text");

						if (index($string, $word) >= 0) {
							$show_flag = 1;
							if ($self->{query}->{cond} eq 'or') {
								last;
							}
						} else {
							if ($self->{query}->{cond} eq 'and') {
								$show_flag = 0;

								last;
							}
						}
					}

					if ($show_flag) {
						$self->{html}->{diary} .= $skin_ins->get_replace_data(
							'diary',
							$diary_ins->diary_article($no, $id, $stat, $break, $comt, $tb, $field, $date, $name, $subj, $text, $color, $icon, $file, $host)
						);
					}
				}
				close(FH);
			}
		}

		$self->{html}->{diary} .= $skin_ins->get_data('diary_foot');
	}

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

	return;
}

1;
