#!/usr/bin/perl
#
# BNCweb: A user-friendly interface to the British National Corpus - Version 4.3
# Copyright (C) 2005-10 Hoffmann/Evert/Lehmann
#
# Updates and information available at http://www.bncweb.info
#
# This file is part of BNCweb.
# 
# BNCweb 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 3 of the License, or
# (at your option) any later version.
# 
# BNCweb 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, see <http://www.gnu.org/licenses/>.

#
# based on BNCweb Version 2.0:
# © 1996/2004 Lehmann/Hoffmann/Schneider
#

use lib "../lib_files";
use bncLibCQPXML;


# connect to mysql
$bncHandle = DBI->connect("DBI:mysql:$bwMYSQLtable:$bwMYSQLhost:$bwMysqlPort", "$bwMysqlUser", "$bwMysqlPwd") or bark("Can't connect to mysql on $bwMYSQLhost at port: $bwMysqlPort <BR>$userHandle::errstr\n");


### no longer supported -- check if some regexps need //m modifier
# $*=1; 
	
unless (getParams($ENV{'QUERY_STRING'})) {
	barkFull("incomplete transmition of URL<BR>This problem is likely to occur in combination with proxy servers<BR>turn off your web-proxy and try again");
}

# print html header
print "Content-type: text/html\n\n";

print <<END;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>

<TITLE>Setting Information for $params{text}</TITLE>

<META NAME="generator" CONTENT="BBEdit 4.5">
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<TABLE BORDER="0" WIDTH="100%" CELLPADDING=3 CELLSPACING=2>
	<TR>
		<TD HEIGHT="27" BGCOLOR="#CCCCFF" COLSPAN="2"> <FONT SIZE="4"><B>Setting Information for $params{text}</B></FONT></TD>
	</TR>
	<TR>
		<TD HEIGHT="27" BGCOLOR="#CCCCCC" COLSPAN="2"> 
		 <b>&lt;s&gt;-units $params{begin} to $params{end}</b>
		 </TD>
	<TR>
END

$begin = $params{begin};
$end = $params{end};

$sqlQuery = "select beginPosition from bnc_s_unit_pos where filename = '$params{text}' and sunit = $begin";
$sth = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery:" . $bncHandle::errstr);
$sth->execute or bark("Can't execute $sqlQuery: $sth::errstr");
@rowArray=$sth->fetchrow_array;
$begin = $rowArray[0];

$sqlQuery = "SELECT speaker_list, locale, activity, placeName  
		FROM spoken_settings WHERE beginPosition <= $begin order by beginPosition desc limit 1";
$sth = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery: " . $bncHandle::errstr);
$sth->execute or bark("Can't execute $sqlQuery: " . $sth::errstr);
$res = $sth->rows;

if ($res > 1) {
	# shouldn't really get here...
	print <<END;
	<TR><TD COLSPAN="2">The context display spans across two or more settings.</TD></TR>
	</TABLE>
END
} else {

	$resArrayResult=$sth->fetchall_arrayref;
	
	for $k ( 0 .. ($#{$resArrayResult}) ) {
		$speakerList = $resArrayResult->[$k][0];
		$locale      = $resArrayResult->[$k][1];
		$activity    = $resArrayResult->[$k][2];
		$placeName   = $resArrayResult->[$k][3];
		
		$locale = "n/a" unless ($locale);
		$activity = "n/a" unless ($activity);
		$placeName = "n/a" unless ($placeName);
		
		
	}
	if ($speakerList=~m/ /) {
		@speakers = split(/ /, $speakerList);
		foreach $speaker (@speakers) {
			$speakers .= "<A HREF=\"/$bwCGIalias/speakerInfo_new.pl?text=$paramsEnc{text}&spid=$speaker&urlTest=yes\"<B>$speaker</B></A> ";
		}
	} else {
		$speakers = "<A HREF=\"/$bwCGIalias/speakerInfo_new.pl?text=$paramsEnc{text}&spid=$speakerList&urlTest=yes\"<B>$speakerList</B></A>";
	}
	
	print <<END;
	<TR BGCOLOR="#DDDDDD"><TD WIDTH="25%">Locale:</TD><TD>$locale</TD></TR>
	<TR BGCOLOR="#DDDDDD"><TD>Activity:</TD><TD>$activity</TD></TR>
	<TR BGCOLOR="#DDDDDD"><TD>Placename:</TD><TD>$placeName</TD></TR>
	<TR BGCOLOR="#DDDDDD"><TD>Speakers (other than unknown):</TD><TD>$speakers</TD></TR>
	</TABLE>
END
	
	

}

# print end of HTML
&printFooter();
exit(0);

