#!/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 CGI;
use DBI;
use OMA;


local %metaand;
undef(%metaand);



          ###           ###               ###
           ##            ##                ##
  #####    ##     ####   #####   ####      ##
 ##  ##    ##    ##  ##  ##  ##     ##     ##
 ##  ##    ##    ##  ##  ##  ##  #####     ##
  #####    ##    ##  ##  ##  ## ##  ##     ##
     ##   ####    ####  ## ###   ### ##   ####
 #####
                   ##      ###
                            ##
         ##  ##   ###    #####    ####    ####
         ##  ##    ##   ##  ##   ##  ##  ##  ##
         ##  ##    ##   ##  ##   ######  ##  ##
          ####     ##   ##  ##   ##      ##  ##
           ##     ####   ### ##   ####    ####
########
sub global_video
{
	local $qstring = shift;
	local $conj = shift;
	local $meta = shift;
	local @terms;
	local $i;
	local %listed;

	@terms = split(/\ /,$qstring);
	$qstring="";
	for($i=0;$i<=$#terms;$i++) {
		$qstring .= "
           (ova_page.title ilike '%$terms[$i]%' OR
           ova_page.author ilike '%$terms[$i]%' OR
           ova_page.director ilike '%$terms[$i]%' OR
           ova_staff.staff ilike '%$terms[$i]%' OR
           ova_abstract.abstract ilike '%$terms[$i]%') ";
		$qstring .= " $conj " if($i < $#terms);
	}
	$st=$db->prepare("
				SELECT	item.sid,item.description,node.node,item.type,treemap.node,ova_conn.page
				FROM		item,ova_conn,ova_page,ova_abstract,ova_staff,node,treemap
				WHERE		ova_page.ID=ova_abstract.ID
				AND			ova_page.ID=ova_staff.ID
				AND			ova_page.ID=ova_conn.ova_page
				AND			item.sid=ova_conn.item_sid
				AND			item.node_sid=node.sid
				AND			treemap.sid=node.sid
				AND			$meta
				AND			( $qstring )
				ORDER BY	treemap.node,item.description
	");
	$st->execute();
	print "<font face=helvetica,arial size=-1>";

	while(@row=$st->fetchrow()) {
		if(!$listed{$row[0]}) {
			if($row[3] eq "meta") {
				$metaand{$row[0]}++;
				$results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=meta&item=$row[0]&page=$row[5]>$row[1]</a><br>\n";
			}
			else {
				$results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=$row[3]&item=$row[0]>$row[1]</a><br>\n";
			}

			$listed{$row[0]} = 1;
		}
	}
	$st->finish();
}



          ###           ###               ###
           ##            ##                ##
  #####    ##     ####   #####   ####      ##
 ##  ##    ##    ##  ##  ##  ##     ##     ##
 ##  ##    ##    ##  ##  ##  ##  #####     ##
  #####    ##    ##  ##  ##  ## ##  ##     ##
     ##   ####    ####  ## ###   ### ##   ####
 #####
                           ###     ##
                            ##
         ####    ##  ##  #####    ###     ####
            ##   ##  ## ##  ##     ##    ##  ##
         #####   ##  ## ##  ##     ##    ##  ##
        ##  ##   ##  ## ##  ##     ##    ##  ##
         ### ##   #####  ### ##   ####    ####
########
sub global_audio
{
	local $qstring = shift;
	local $conj = shift;
	local $meta = shift;
	local @terms;
	local $i;
	local %listed;

	print "<p>\n";
	@terms = split(/\ /,$qstring);
	$qstring="";
	for($i=0;$i<=$#terms;$i++) {
		$qstring .= "
           (ora_page.title ilike '%$terms[$i]%' OR
           ora_page.author ilike '%$terms[$i]%' OR
           ora_page.interpret ilike '%$terms[$i]%' OR
           ora_abstract.abstract ilike '%$terms[$i]%') ";
		$qstring .= " $conj " if($i < $#terms);
	}
	$st=$db->prepare("
				SELECT	item.sid,item.description,node.node,item.type,treemap.node,ora_conn.page
				FROM		item,ora_conn,ora_page,ora_abstract,node,treemap
				WHERE		ora_page.ID=ora_abstract.ID
				AND			ora_page.ID=ora_conn.ora_page
				AND			item.sid=ora_conn.item_sid
				AND			item.node_sid=node.sid
				AND			treemap.sid=node.sid
				AND			$meta
				AND			( $qstring )
				ORDER BY	item.description
	");
	$st->execute();
	print "<font face=helvetica,arial size=-1>";
	while(@row=$st->fetchrow()) {
		if(!$listed{$row[0]}) {
			if($row[3] eq "meta") {
				$metaand{$row[0]}++;
				$results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=$row[3]&item=$row[0]>$row[1]</a><br>\n";
			}
			else {
				$results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=$row[3]&item=$row[0]>$row[1]</a><br>\n";
			}
			$listed{$row[0]} = 1;
		}
	}
	$st->finish();
}



          ###           ###               ###
           ##            ##                ##
  #####    ##     ####   #####   ####      ##
 ##  ##    ##    ##  ##  ##  ##     ##     ##
 ##  ##    ##    ##  ##  ##  ##  #####     ##
  #####    ##    ##  ##  ##  ## ##  ##     ##
     ##   ####    ####  ## ###   ### ##   ####
 #####
                   ##             ##
                                  ##
        ## ###    ###     ####   #####   ##  ## ## ###    ####
         ##  ##    ##    ##  ##   ##     ##  ##  ### ##  ##  ##
         ##  ##    ##    ##       ##     ##  ##  ##      ######
         #####     ##    ##  ##   ## ##  ##  ##  ##      ##
         ##       ####    ####     ###    ##### ####      ####
############
sub global_picture
{
	local $qstring = shift;
	local $conj = shift;
	local $meta = shift;
	local @terms;
	local $i;
	local %listed;

	print "<p>\n";
	@terms = split(/\ /,$qstring);
	$qstring="";
	for($i=0;$i<=$#terms;$i++) {
		$qstring .= "
           (opa_page.title ilike '%$terms[$i]%' OR
           opa_page.subtitle ilike '%$terms[$i]%' OR
           opa_page.photographer ilike '%$terms[$i]%' OR
           opa_page.agency ilike '%$terms[$i]%' OR
           opa_abstract.abstract ilike '%$terms[$i]%') ";
		$qstring .= " $conj " if($i < $#terms);
	}
	$st=$db->prepare("
				SELECT	item.sid,item.description,node.node,item.type,treemap.node,opa_conn.page
				FROM		item,opa_conn,opa_page,opa_abstract,node,treemap
				WHERE		opa_page.ID=opa_abstract.ID
				AND			opa_page.ID=opa_conn.opa_page
				AND			item.sid=opa_conn.item_sid
				AND			item.node_sid=node.sid
				AND			treemap.sid=node.sid
				AND			$meta
				AND			( $qstring )
				ORDER BY	item.description
	");
	$st->execute();
	print "<font face=helvetica,arial size=-1>";
	while(@row=$st->fetchrow()) {
		if(!$listed{$row[0]}) {
			if($row[3] eq "meta") {
				$metaand{$row[0]}++;
				$results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=meta&item=$row[0]>$row[1]</a><br>\n";
			} else {
				$results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=$row[3]&item=$row[0]>$row[1]</a><br>\n";
			}
			$listed{$row[0]} = 1;
		}
	}
	$st->finish();
}



          ###           ###               ###
           ##            ##                ##
  #####    ##     ####   #####   ####      ##
 ##  ##    ##    ##  ##  ##  ##     ##     ##
 ##  ##    ##    ##  ##  ##  ##  #####     ##
  #####    ##    ##  ##  ##  ## ##  ##     ##
     ##   ####    ####  ## ###   ### ##   ####
 #####
          ##                      ##
          ##                      ##
         #####    ####  ##   ##  #####
          ##     ##  ##  ## ##    ##
          ##     ######   ###     ##
          ## ##  ##      ## ##    ## ##
           ###    ####  ##   ##    ###
########
sub global_text
{
	local $qstring = shift;
	local $conj = shift;
	local $meta = shift;
	local @terms;
	local $i;
	local %listed;

	print "<p>\n";
	@terms = split(/\ /,$qstring);
	$qstring="";
	for($i=0;$i<=$#terms;$i++) {
		$qstring .= "
					(ota_page.title ilike '%$terms[$i]%' OR
					ota_page.subtitle ilike '%$terms[$i]%' OR
					ota_page.description ilike '%$terms[$i]%' OR
					ota_page.author ilike '%$terms[$i]%' OR
					ota_page.publisher ilike '%$terms[$i]%' OR
					ota_abstract.abstract ilike '%$terms[$i]%') ";
		$qstring .= " $conj " if($i < $#terms);
	}
	$st=$db->prepare("
				SELECT  item.sid,item.description,node.node,item.type,treemap.node,ota_conn.page
				FROM    item,ota_conn,ota_page,ota_abstract,node,treemap
				WHERE   ota_page.ID=ota_abstract.ID
				AND     ota_page.ID=ota_conn.ota_page
				AND     item.sid=ota_conn.item_sid
				AND     item.node_sid=node.sid
				AND     treemap.sid=node.sid
				AND			$meta
				AND     ( $qstring )
				ORDER BY  item.description
	");
	$st->execute();
	print "<font face=helvetica,arial size=-1>";
	while(@row=$st->fetchrow()) {
		if(!$listed{$row[0]}) {
			if($row[3] eq "meta") {
				$metaand{$row[0]}++;
				$results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=meta&item=$row[0]>$row[1]</a><br>\n";
			} else {
				$results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=$row[3]&item=$row[0]>$row[1]</a><br>\n";
			}

			$listed{$row[0]} = 1;
		}
	}
	$st->finish();
}



          ###           ###               ###
           ##            ##                ##
  #####    ##     ####   #####   ####      ##
 ##  ##    ##    ##  ##  ##  ##     ##     ##
 ##  ##    ##    ##  ##  ##  ##  #####     ##
  #####    ##    ##  ##  ##  ## ##  ##     ##
     ##   ####    ####  ## ###   ### ##   ####   
 #####
                                                                  ##
                                                                  ##
        ## ###    ####  ##   ##  ####     ####    ####    ####   #####
         ##  ##  ##  ## ## # ##     ##   ##      ##      ##  ##   ##
         ##  ##  ###### ## # ##  #####    ####    ####   ######   ##
         ##  ##  ##     ####### ##  ##       ##      ##  ##       ## ##
         ##  ##   ####   ## ##   ### ##  #####   #####    ####     ###
########
sub global_newasset
{
    local $qstring = shift;
    local $conj = shift;
    local $meta = shift;
    local @terms;
    local $i;
    local %listed;

    print "<p>\n";
    @terms = split(/\ /,$qstring);
    $qstring="";
    for($i=0;$i<=$#terms;$i++) {
        $qstring .= "
           (
				newasset_page.title ilike '%$terms[$i]%'
           )";
        $qstring .= " $conj " if($i < $#terms);
    }
    $st=$db->prepare("
                SELECT  item.sid,item.description,node.node,item.type,treemap.node,newasset_conn.page
                FROM        item,newasset_conn,newasset_page,node,treemap
                WHERE       newasset_page.ID=newasset_conn.newasset_page
                AND         item.sid=newasset_conn.item_sid
                AND         item.node_sid=node.sid
                AND         treemap.sid=node.sid
                AND         $meta
                AND         ( $qstring )
                ORDER BY    item.description
    ");
    $st->execute();
    print "<font face=helvetica,arial size=-1>";
    while(@row=$st->fetchrow()) {
        if(!$listed{$row[0]}) {
            if($row[3] eq "meta") {
                $metaand{$row[0]}++;
                $results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=$row[3]&item=$row[0]>$row[1]</a><br>\n";
            }
            else {
                $results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=$row[3]&item=$row[0]>$row[1]</a><br>\n";
            }
            $listed{$row[0]} = 1;
        }
    }
    $st->finish();
}


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

sub global_reboot
{
    local $qstring = shift;
    local $conj = shift;
    local $meta = shift;
    local @terms;
    local $i;
    local %listed;

    print "<p>\n";
    @terms = split(/\ /,$qstring);
    $qstring="";
    for($i=0;$i<=$#terms;$i++) {
        $qstring .= "
           (
				reboot_page.dc__title ilike '%$terms[$i]%' OR
				reboot_page.dc__title__alternative ilike '%$terms[$i]%' OR
				reboot_page.dc__publisher ilike '%$terms[$i]%' OR
				reboot_page.dc__description__toc ilike '%$terms[$i]%' OR
				reboot_page.dc__description__abstract ilike '%$terms[$i]%' OR
				reboot_page.dc__description__long ilike '%$terms[$i]%' OR
				reboot_page.dc__type ilike '%$terms[$i]%' OR
				reboot_page.dc__subject ilike '%$terms[$i]%' OR
				reboot_page.dc__coverage__spatial ilike '%$terms[$i]%' OR
				reboot_page.dc__coverage__temporal ilike '%$terms[$i]%'
           )";
        $qstring .= " $conj " if($i < $#terms);
    }
    $st=$db->prepare("
                SELECT  item.sid,
						item.description,
						node.node,
						item.type,
						treemap.node,
						reboot_conn.page
                FROM	item,reboot_conn,reboot_page,node,treemap
                WHERE	reboot_page.ID=reboot_conn.reboot_page
                AND		item.sid=reboot_conn.item_sid
                AND		item.node_sid=node.sid
                AND		treemap.sid=node.sid
                AND		$meta
                AND		( $qstring )
                ORDER BY    item.description
    ");
    $st->execute();
    print "<font face=helvetica,arial size=-1>";
    while(@row=$st->fetchrow()) {
        if(!$listed{$row[0]}) {
            if($row[3] eq "meta") {
                $metaand{$row[0]}++;
                $results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=$row[3]&item=$row[0]>$row[1]</a><br/>\n";
            }
            else {
                $results{"$row[4]/$row[0]"} = "<img src=/img/$OMA::MEDIA_TYPE{$row[3]}.png> <a target=dwnwork href=$OMA::OMABIN/gen?op=showitem&type=$row[3]&item=$row[0]>$row[1]</a><br/>\n";
            }
            $listed{$row[0]} = 1;
        }
    }
    $st->finish();
}

                   ##

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

local $check1, $check2;
local $results;
local $last;
undef %results;

CGI::ReadParse();


print "<body bgcolor=#dfdfdf link=#007700 vlink=#770000>";
$db = OMA::opendb() if(!$db);


$check1 = $check2 = "";
$check1 = " checked " if($in{'conj'} eq "OR");
$check2 = " checked " if($in{'conj'} eq "AND");
print "
<font size=-1>
<form method=post>
	<input name=qstring value=\"$in{'qstring'}\">
	<input type=radio name=conj $check1 value=OR>OR
	<input type=radio name=conj $check2 value=AND>AND
</form><br>\n";

if($in{'qstring'}) {
	global_video($in{'qstring'},"OR","item.type = 'meta'") if($in{'qstring'});
	global_audio($in{'qstring'},"OR","item.type = 'meta'") if($in{'qstring'});
	global_picture($in{'qstring'},"OR","item.type = 'meta'") if($in{'qstring'});
	global_text($in{'qstring'},"OR","item.type = 'meta'") if($in{'qstring'});
	global_newasset($in{'qstring'},"OR","item.type = 'meta'") if($in{'qstring'});
	global_reboot($in{'qstring'},"OR","item.type = 'meta'") if($in{'qstring'});
#	foreach $k (keys %metaand) {
#		print "$k == $metaand{$k}<br>\n";
#	}

	global_video($in{'qstring'},$in{'conj'},"item.type != 'meta'") if($in{'qstring'});
	global_audio($in{'qstring'},$in{'conj'},"item.type != 'meta'") if($in{'qstring'});
	global_picture($in{'qstring'},$in{'conj'},"item.type != 'meta'") if($in{'qstring'});
	global_text($in{'qstring'},$in{'conj'},"item.type != 'meta'") if($in{'qstring'});
	global_newasset($in{'qstring'},$in{'conj'},"item.type != 'meta'") if($in{'qstring'});
	global_reboot($in{'qstring'},$in{'conj'},"item.type != 'meta'") if($in{'qstring'});
}

$last="";
foreach $k (sort(keys(%results))) {
	@patha=split(/\//,$k);
	$path=$k;
	$path =~ s/$patha[$#patha]$//;
	if($path ne $last) {
		print "<p>$path<br>";
	}
	print "$results{$k}";
	$last = $path;
}
