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

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


$username = $ENV{'REMOTE_USER'};

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 "Content-type: text/html\n\n";
print <<END;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
	<TITLE>David Lee's Genre Classification</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 COLSPAN="4" HEIGHT="27" BGCOLOR="#CCCCFF"> <FONT SIZE="4"><B>David Lee's Genre Classification</B></FONT></TD>
	</TR>
END

#settings
$fileInfoCgi = "fileInfo.pl";

$theQuery= $ENV{'QUERY_STRING'};
$theQuery =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$theQuery = "&" . $theQuery . "&";
$theQuery =~s/\+/_/g;
bark("Empty search string!\n") if  ($theQuery=~m/\A&*\Z/);


# 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>$bncHandle::errstr\n");

$sqlQuery = "SELECT * FROM settings where user='$username'";
$sth= $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery:" . $bncHandle::errstr);
$res = $sth->execute or bark("Can't execute $sqlQuery:" . $bncHandle::errstr);
$res = $sth->rows;
	
# get default view (kwic or list) for display
@rowArray=$sth->fetchrow_array;
$view = $rowArray[13];


# piece together SQL query
$sqlQuery = "";
if ($params{item1}=~m/\A(\d+)\Z/) {
	$sqlQuery = "SELECT fname from headerInfo where genre = $1 order by fname asc";
} else {

	for ($i = 1; $i <= 4; $i++) {
		$sqlQuery .= " (";
		while ($theQuery=~m/&item${i}=([^&]+)/mg) {
			$sqlQuery .= "data${i}.genre = \'$1\' OR ";
			$done{$i}++;
			$countCategories++;
		}
		$sqlQuery=~s/OR \Z//;
		$sqlQuery .= ") AND ";
	}
	$sqlQuery=~s/AND \Z//;
	
	$sqlQuery=~s/ AND\s*\(\)//g;
	$sqlQuery=~s/ \(\) AND\s*//g;
	
	if ($countCategories) {
	
	$equal = "";
	$sql = "";
	$countKeys = 0;
	foreach $key (keys(%done)) {	
		$sql .= "genres as data${key}, ";
		$countKeys++;
		if ($countKeys > 1) {
			$equal .= "data" . ($key - 1) . ".fname = data${key}.fname AND ";
		}
		$lastKey = $key;
	}
	$sql =~ s/, \Z//;
	
	$sql = "SELECT distinct(data${lastKey}.fname) from " . $sql;
	
	$sqlQuery = $sql . " WHERE " . $equal . $sqlQuery . " order by data${lastKey}.fname asc";
	} else {
		$sqlQuery = "SELECT fname from headerInfo";
	}
}

$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) {
	print "<TR><TD COLSPAN=\"4\"><B>There are no files that match your input</B></TD></TR></TABLE>\n";
	&printFooter();
	exit(0);
} else {
	
	$filelist="»";
	for ($i = 0; $i < $res; $i++) {
		@rowArray=$sth->fetchrow_array;
		$resFile = $rowArray[0];
		$filelist .= $rowArray[0] . "»";
		$files[$i] = $resFile;
	}
}


if ($res == 1) {
	$printText= "<B>1 text was found:</B>";
} else {
	$printText= "<B>$res texts were found:</B>";
}

$subcorpusList="<OPTION SELECTED VALUE=\"new\">New subcorpus\n";
$sqlQuery = "SELECT subcorpus FROM subcorpora where username = '$username' and textOrSpeaker = 'text' order by subcorpus";
$sth2 = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery:" . $bncHandle::errstr);
$sth2->execute or bark("Can't execute $sqlQuery:" . $sth::errstr);
$resArrayResult=$sth2->fetchall_arrayref;

for $k ( 0 .. ($#{$resArrayResult}) ) {
	$theCorpus=$resArrayResult->[$k][0];
	$subcorpusList .= "<OPTION VALUE=\"$theCorpus\">$theCorpus\n" unless ($theCorpus=~m/\Alast_(written|spoken)\Z/);;
}

	print <<END;
	<TR>
		<TD COLSPAN="4" HEIGHT="27" BGCOLOR="#FFCC66">
			$printText
		</TD>
	</TR>
	<TR>
		<TD BGCOLOR="#FF9966">
			<B>Text</B>
		</TD>
		<TD BGCOLOR="#FF9966">
			<B>Genre Information</B>
		</TD>
		<TD BGCOLOR="#FF9966">
			<B>Text</B>
		</TD>
		<TD NOWRAP BGCOLOR="#FF9966">
			<B>Include in subcorpus</B>
		</TD>
		<FORM ACTION="/$bwCGIalias/subcorpAdmin.pl" METHOD="GET">
	</TR>
END


$theList="";
for ($i = 0; $i < $res; $i++) {
	$filename = $files[$i];
	$sqlQuery = "SELECT title from headerInfo WHERE fname = '$filename'";
	$sth1 = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery:" . $bncHandle::errstr);
	$sth1->execute or bark("Can't execute $sqlQuery: $sth::errstr");
	@rowArray=$sth1->fetchrow_array;
	$title = $rowArray[0];
	
	$sqlQuery = "SELECT genre from genres2 WHERE fname = '$filename'";
	$sth1 = $bncHandle->prepare($sqlQuery) or bark("Can't prepare $sqlQuery:" . $bncHandle::errstr);
	$sth1->execute or bark("Can't execute $sqlQuery: $sth::errstr");
	@rowArray=$sth1->fetchrow_array;
	$genreAll = $rowArray[0];
	
	$genreAll=~s/\A\%(.*?)\%\Z/$1/;
	$genreAll=~s/\%/_/g;

	
	$title =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
	$title=~s/\$1\$1\$2/ -- /g;
	$title=~s/ -- an electronic (transcription|version|sample|collection)\Z//g;
	$chunk=$title;
	$chunk = &output2($chunk);
	$link = "<A HREF=\"/$bwCGIalias/${fileInfoCgi}?text=$filename&urlTest=yes\">";
	$fnum = &Filemapping($filename);
	
	print <<END;
	<TR>
		<TD VALIGN=TOP BGCOLOR="#EEEEEE">
			<B>$link$filename</A></B>
		</TD>
		<TD VALIGN=TOP BGCOLOR="#EEEEEE">
			$genreAll
		</TD>
		<TD VALIGN=TOP BGCOLOR="#EEEEEE">
			$chunk
		</TD>
		<TD VALIGN=TOP BGCOLOR="#EEEEEE">
			&nbsp;<INPUT TYPE="CHECKBOX" NAME="t" VALUE="$fnum">
		</TD>
	</TR>
END
	$theList .= "·$fnum";
}
$theList=~s/\A·//;

print <<END;

	<TR>
		<TD COLSPAN="2" BGCOLOR="#DDDDDD">
			Choose subcorpus:
		</TD>
		<TD BGCOLOR="#DDDDDD">
			<SELECT NAME="subcorpus">
				$subcorpusList
			</SELECT>
			<INPUT TYPE="SUBMIT" VALUE="Add">
			<INPUT TYPE="hidden" NAME="spowri" VALUE="written">
			<INPUT TYPE="hidden" NAME="textOrSpeaker" VALUE="text">
			<INPUT TYPE="hidden" NAME="username" VALUE="$username">
		</TD>
		<TD BGCOLOR="#DDDDDD">
			&nbsp;<INPUT TYPE="CHECKBOX" NAME="all" VALUE="$theList">include all files
		</TD>
	</TR>
	<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
	</FORM>
	</TABLE>
END

&printFooter();
exit(0);

