#!/usr/bin/perl
#------------------------------------------------------------------------------
# Copyright (C) 2001 Thomax Kaulmann
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

use DBI;
use CGI;
use OVA;

local $db, $field, $table, @row, %in, $i, $find_what, $apage;
local $tag, $score, $lines, $length, $headline, $types, $id;
local @line;
local $pages, $npage, $x, $key;
local %hashrow;
undef(%in);
undef(%hashrow);
undef(@row);
undef(@line);





                  ##               ###     ##                             ##
                  ##                ##                                    ##
  #####   ####   #####           #####    ###   ## ###    ####    ####   #####
 ##  ##  ##  ##   ##            ##  ##     ##    ### ##  ##  ##  ##  ##   ##
 ##  ##  ######   ##            ##  ##     ##    ##      ######  ##       ##
  #####  ##       ## ##         ##  ##     ##    ##      ##      ##  ##   ## ##
     ##   ####     ###           ### ##   ####  ####      ####    ####     ###
 #####                  ########
sub get_direct
{
	local %check;
	undef %check;
	$find_what = $in{'find_what'};
	$find_what =~ tr/[A-Z]/[a-z]/;
	$order = "";
	$order = "$in{'field'}, " if(length($in{'field'})>0);

	$order = "node.node, " if($in{'field'} eq "department");
#	$in{'querystring'} =~ s/([A-Za-z])/\[\L$1\U$1\]/gi;
	$table = $db->prepare("
			SELECT  ova_page.*,node.node
			FROM  ova_page,  node, item, ova_conn
			WHERE $find_what like '%$in{'querystring'}%'
			AND ova_page.id = ova_conn.ova_page
			AND ova_conn.item_sid = item.sid
			AND item.node_sid = node.sid
			ORDER BY $order dc__title
	");
	$table->execute();
	$table->bind_columns(\(@hashrow{@{$table->{NAME_lc}}}));
}




                  ##                      ###     ###
                  ##                       ##      ##
  #####   ####   #####           ####      ##      ##
 ##  ##  ##  ##   ##                ##     ##      ##
 ##  ##  ######   ##             #####     ##      ##
  #####  ##       ## ##         ##  ##     ##      ##
     ##   ####     ###           ### ##   ####    ####
 #####                  ########
sub get_all
{
	local %check;
	undef %check;
	local $forid;
	$forid = "OR	ova_page.id=$in{'querystring'}" if($in{'querystring'}=~/^[0-9]+$/);

	$in{'field'} = "node.node" if($in{'field'} eq "department");
	$in{'field'} .= ", " if($in{'field'});

	$table = $db->prepare("
		SELECT DISTINCT ova_page.*
		FROM  ova_page, node, item, ova_conn
		WHERE ova_page.id=ova_abstract.ID
		AND ova_page.id = ova_conn.ova_page
		AND ova_conn.item_sid = item.sid
		AND item.node_sid = node.sid
		AND   (
				ova_page.username like '%$in{'querystring'}%'
		OR		ova_page.dc__title like '%$in{'querystring'}%'
		$forid  )
		ORDER BY $in{'field'} dc__title
	");

	$table->execute();
	$table->bind_columns(\(@hashrow{@{$table->{NAME_lc}}}));
}






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

CGI::ReadParse();

$db = OVA::opendb();

print "$OVA::OVA_BODY";
if($in{'querystring'})
{
	print "
		<BODY TEXT=#000000 LINK=#007700 VLINK=#770000 ALINK=#770000 BGCOLOR=#dfdfdf>
		<base href=http://$OVA::OMAHOST/>
		<script language=JavaScript id=oma>
		function Go2(x) {
			parent.frames[3].location.href=x;
		}
		</SCRIPT>
	";


	if($in{'find_what'} =~ /^all/) {
		get_all();
	}
	else {
		get_direct();
	}

	exit if($in{'lt'} eq "short");

	$x = ($in{'n'}+60<$DBI::rows)?$in{'n'}+60:$DBI::rows;

	$npage = int($in{'n'}/60);
	$pages = int($DBI::rows/60);
	$apage="";
	for($i=0;$i<=$pages;$i++) {
		$apage .= " | <a href=$OVA::CGI_BINS/ova-search?find_what=$in{'find_what'}&querystring=$in{'querystring'}&field=$in{'field'}&n=".($i*60).">$i</a>";
	}

	$key = $in{'dep'};
	$key =~ s/\ /%20/g;

	$page = "
		<table width=100%><tr><td>
		<td><pre>search: $in{'find_what'}, keyword: $in{'querystring'}</td>
		<td>$apage p$npage of $pages</td>
		<td align=right>
		<a href=javascript:self.back()><font size=+0>&lt;back</font></a>
		</td>
	";
	$page .= "
		<td align=right><a href=$OVA::CGI_BINS/ova-search?querystring=$in{'querystring'}&find_what=$in{'find_what'}&n=$x&field=$in{'field'}>
		<font size=+0>next&gt;</font></a>
		</td>"
	if($x<$DBI::rows);
	$page .= "</tr></table>";

	print $page;

#	print "<pre> <img src=/OVA/blindblock.gif>";
	print "<pre>    ";
	print "Title                               <font color=white>| <A HREF=$OVA::CGI_BINS/ova-search?find_what=$in{'find_what'}&querystring=$in{'querystring'}&field=author>author</A>               | <A HREF=$OVA::CGI_BINS/ova-search?find_what=$in{'find_what'}&querystring=$in{'querystring'}&field=interpret>artist</A>               | <A HREF=$OVA::CGI_BINS/ova-search?find_what=$in{'find_what'}&querystring=$in{'querystring'}&field=label>label</A>           | <A HREF=$OVA::CGI_BINS/ova-search?find_what=$in{'find_what'}&querystring=$in{'querystring'}&field=department>area</A>       | <A HREF=$OVA::CGI_BINS/ova-search?find_what=$in{'find_what'}&querystring=$in{'querystring'}&field=copyright>copyright</A></font>\n";

	$i=0;
	while(@row=$table->fetchrow()) {
		if($i >= $in{'n'} && $i < $x) {
			OVA::display_line(*row,*hashrow);
		}
		$i++
	}
	$table->finish();

	print "</pre>";



#	print "\n   <IMG SRC=/OVA/balken.gif ALIGN=BOTTOM BORDER=0>";
	print "</pre>";
	OVA::copyright();
}

