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

###
# instructions are commented out - FileMaker templates need to be redone!
# SH 30.4.2006
###

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

$username = $ENV{'REMOTE_USER'};

# 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");
	
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");
}


$theData2=&encodeForm($params{theData});

if ($bwMysqlAuth) {
	$authLevel = &checkAuthLevel;
	warn $authLevel;
} else {
	$authLevel = 1;
}

$sqlQuery = "SELECT crlf FROM settings where user='$username'";
$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;
$setVal=$rowArray[0];

if ($setVal) {
	if ($setVal == 1) {
		$crlf = "Macintosh";
		$selected{"Macintosh"}="SELECTED";
	} elsif ($setVal == 2) {
		$crlf = "DOS";
		$selected{"Windows"}="SELECTED";
	} elsif ($setVal == 3) {
		$crlf = "UNIX";
		$selected{"Other"}="SELECTED";
	}
} else {
	$browserProg = $ENV{'HTTP_USER_AGENT'};
	if ($browserProg=~m/Windows/i) {
		$crlf = "DOS";
		$selected{"DOS"}="SELECTED";
	} elsif ($browserProg=~m/(Macintosh|Mac_PowerPC)/i) {
		if ($browserProg=~m/OS *X/) {
			$crlf = "UNIX (incl. OS X)";
			$selected{"Other"}="SELECTED";
		} else {
			$crlf = "Macintosh (OS 9 and below)";
			$selected{"Macintosh"}="SELECTED";
		}
	} else {
		$crlf = "UNIX";
		$selected{"Other"}="SELECTED";
	}

}

if ($authLevel) {
	$context = '<SELECT NAME="contSize">
				<OPTION SELECTED VALUE="1"> 1 &lt;s&gt;-unit
				<OPTION VALUE="2"> 2 &lt;s&gt;-units
				<OPTION VALUE="3"> 3 &lt;s&gt;-units
				<OPTION VALUE="4"> 4 &lt;s&gt;-units
				<OPTION VALUE="5"> 1 word
				<OPTION VALUE="6"> 2 words
				<OPTION VALUE="7"> 3 words
				<OPTION VALUE="8"> 4 words
				<OPTION VALUE="9"> 5 words
				<OPTION VALUE="10"> 6 words
				<OPTION VALUE="11"> 7 words
				<OPTION VALUE="12"> 8 words
				<OPTION VALUE="13"> 9 words
				<OPTION VALUE="14"> 10 words
				</SELECT>';
} else {
	$context = '<INPUT TYPE="hidden" NAME="contSize" VALUE="1">one &lt;s&gt;-unit';
}


print "Content-type: text/html\n\n";
print <<END;

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

<TITLE>Download concordance</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" CELLPADDING="4" WIDTH="100%">
	<TR WIDTH="100%">
		<TD HEIGHT="26" BGCOLOR="#CCCCFF" COLSPAN="2">
			<P ALIGN="LEFT"><B>Download concordance</B>
		</TD>
	</TR>
	<TR>
		<TD HEIGHT="20" BGCOLOR="#FFCC99" COLSPAN="2">
			<B>Output format options</B>
 		</TD>
 	</TR>
	<TR BGCOLOR="#EEEEEE">
		<TD WIDTH="40%">
			
			<FORM ACTION="/$bwCGIalias/BNCdownload.pl" METHOD=GET>
			<INPUT TYPE="hidden" NAME="qname" VALUE="$params{qname}">
			<INPUT TYPE="hidden" NAME="queryID" VALUE="$params{queryID}">
			<INPUT TYPE="hidden" NAME="theData" VALUE="$theData2">
			<INPUT TYPE="hidden" NAME="SQL" VALUE="$params{SQL}">
			<INPUT TYPE="hidden" NAME="thMode" VALUE="$params{thMode}">
	
			Choose operating system on which you will be working with the file:
		</TD>
		<TD>
			<SELECT NAME="crlf">
				<OPTION $selected{Macintosh} > Macintosh (OS 9 and below)
				<OPTION $selected{DOS} > DOS/Windows
				<OPTION $selected{Other} > UNIX (incl. OS X)
			</SELECT>
		</TD>
	</TR>
	<TR BGCOLOR="#EEEEEE">
		<TD>
			Print <A HREF="/$bwCGIalias/codes.pl">codes (numbers) or full values</A> for metatextual categories:*
		</TD>
		<TD>
			<SELECT NAME="codes">
				<OPTION VALUE="0"> codes
				<OPTION SELECTED VALUE="1"> full values
			</SELECT>
		</TD>
	</TR>
	<TR BGCOLOR="#EEEEEE">
		<TD>
			Mark query result in sentence (format: &lt;&lt;&lt; result &gt;&gt;&gt;):
		</TD>
		<TD>
			<SELECT NAME="mark">
				<OPTION VALUE="0"> no
				<OPTION SELECTED VALUE="1"> yes
			</SELECT>
		</TD>
	</TR>
	<TR BGCOLOR="#EEEEEE">
		<TD>
			Size of context:
		</TD>
		<TD>
			$context
		</TD>
	</TR>
	
	<TR BGCOLOR="#EEEEEE">
		<TD>
			Download both tagged and untagged version of your results:*
		</TD>
		<TD>
			<SELECT NAME="tagged">
				<OPTION VALUE="0"> no
				<OPTION SELECTED VALUE="1"> yes
			</SELECT>
		</TD>
	</TR>
	
	<TR BGCOLOR="#EEEEEE">
		<TD>
			Write information about order of categories at the beginning of file:*
		</TD>
		<TD>
			<SELECT NAME="info">
				<OPTION SELECTED VALUE="0"> no
				<OPTION VALUE="2"> yes - as header row
				<OPTION VALUE="1"> yes - printer-friendly version
			</SELECT>
		</TD>
	</TR>
	
	<TR BGCOLOR="#EEEEEE">
		<TD>
			Format of output: KWIC or list:*
		</TD>
		<TD>
			<SELECT NAME="view">
				<OPTION SELECTED VALUE="list"> List
				<OPTION VALUE="kwic"> KWIC
			</SELECT>
		</TD>
	</TR>
<!--	
	<TR BGCOLOR="#EEEEEE">
		<TD>
			Include additional mark-up such as pauses, voice quality, etc.
		</TD>
		<TD>
			<SELECT NAME="markup">
				<OPTION SELECTED VALUE="1"> Yes
				<OPTION VALUE="0"> No
			</SELECT>
		</TD>
	</TR>	
--!>
	<TR BGCOLOR="#EEEEEE">
		<TD>
			Include corpus positions (required for re-import)*
		</TD>
		<TD>
			<SELECT NAME="corpusPositions">
				<OPTION SELECTED VALUE="1"> Yes
				<OPTION VALUE="0"> No
			</SELECT>
		</TD>
	</TR>	
	<TR BGCOLOR="#EEEEEE">
		<TD>
			Include URL to context display*
		</TD>
		<TD>
			<SELECT NAME="server">
				<OPTION SELECTED VALUE="1"> Yes
				<OPTION VALUE="0"> No
			</SELECT>
		</TD>
	</TR>	
	<TR BGCOLOR="#EEEEEE">
		<TD>
			Enter name for the downloaded file:
		</TD>
		<TD>
			<INPUT TYPE="text" NAME="saveName" VALUE="$username" SIZE=34 MAXLENGTH=34>
		</TD>
	</TR>

	<TR>
		<TD COLSPAN="2">
			* If you use the <A HREF="/$bwHTMLalias/FileMaker_template.zip">FileMaker Pro template</A> provided to import your data, you should 
			not change the default value - unless you know what you are doing....
		</TD>
	</TR>


	<TR>
		<TD COLSPAN="2" HEIGHT="20" BGCOLOR="#DDDDDD">
			<B>Please check the categories you want to have included in your output</B>
 		</TD>
 	</TR>
 	<TR>
 		<TD COLSPAN="2" BGCOLOR="#EEEEEE">
			<INPUT TYPE="checkbox" NAME="cat" VALUE="spowri&cat=alltyp&cat=genre&cat=alltim&cat=derivedtexttype&cat=wrisam&cat=wrimed&cat=wridom&cat=wrilev&cat=wriaag&cat=wriad&cat=wriase&cat=wriaty&cat=wriaud&cat=writas&cat=wrista&cat=spolog&cat=sporeg&cat=sdeage&cat=sdesex&cat=sdecla&cat=scgdom&catsp=age&catsp=sex&catsp=class&catsp=firstLanguage&catsp=education&catsp=dialect"> Include all
 			&nbsp;
 		</TD>
 	</TR>

 	<TR>
 		<TD COLSPAN="2" BGCOLOR="#DDDDDD">
 			Text information:
 		</TD>
 	</TR>


	<TR BGCOLOR="#EEEEEE">
		<TD WIDTH="40%" VALIGN="TOP">
			<INPUT TYPE="checkbox" NAME="cat" VALUE="spowri"> Overall: Spoken or Written
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="alltyp"> Overall: Text Type
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="genre"> Overall: David Lee's Genre Classification
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="alltim"> Overall: Publication Date (spoken <b>and</b> written!)
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="derivedtexttype"> Overall: Derived Text Type
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="wrisam"> Written: Text Sample
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="wrimed"> Written: Medium of Text
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="wridom"> Written: Text Domain
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="wrilev"> Written: Perceived Level of Difficulty
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="wriaag"> Written: Age of Author
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="wriad"> Written: Domicile of Author
		</TD>
		<TD VALIGN="TOP">
			<INPUT TYPE="checkbox" NAME="cat" VALUE="wriase"> Written: Sex of Author
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="wriaty"> Written: Type of Author
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="wriaud"> Written: Age of Audience
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="writas"> Written: Sex of Audience
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="wrista"> Written: Estimated Circulation Size
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="spolog"> Spoken Texts: Type of Interaction
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="sporeg"> Spoken Texts: Region where spoken text was captured
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="sdeage"> Spoken demographic: Age of Respondent
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="sdesex"> Spoken demographic: Sex of Respondent
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="sdecla"> Spoken demographic: Social class of Respondent
			<BR><INPUT TYPE="checkbox" NAME="cat" VALUE="scgdom"> Spoken context-governed: Domain
		</TD>
	</TR>
 	
	<TR>
		<TD COLSPAN="2" BGCOLOR="#DDDDDD">
 			Speaker information:
		</TD>
	</TR>
	<TR BGCOLOR="#EEEEEE">
		<TD WIDTH="30%" VALIGN="TOP">
			<INPUT TYPE="checkbox" NAME="catsp" VALUE="age"> Age of speaker
			<BR><INPUT TYPE="checkbox" NAME="catsp" VALUE="sex"> Sex of speaker
			<BR><INPUT TYPE="checkbox" NAME="catsp" VALUE="class"> Social class of speaker
		</TD>
		<TD VALIGN="TOP">
			<INPUT TYPE="checkbox" NAME="catsp" VALUE="firstLanguage"> First language of speaker
			<BR><INPUT TYPE="checkbox" NAME="catsp" VALUE="education"> Education of speaker
			<BR><INPUT TYPE="checkbox" NAME="catsp" VALUE="dialect"> Dialect/accent of speaker
		</TD>
	</TR>
	<TR BGCOLOR="#BBBBBB">
		<TD COLSPAN="2" VALIGN="CENTER">
			<INPUT TYPE="hidden" NAME="urlTest" VALUE="yes">
			<INPUT TYPE="submit" VALUE="Download!">
			<INPUT TYPE="reset" VALUE="Clear form">
		</TD>
	</TR>
</TABLE>
</FORM>
<!--
<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="2" WIDTH="100%">
	<TR>
		<TD>
			<BR><B>Instructions:</B>
			<BR>Import your query result into the Filemaker database template, available for 
			<A HREF="/$bwHTMLalias/databases_fmPro_4.sit">Version 4</A> and <A HREF="/$bwHTMLalias/databases_fmPro_5.sit">Version 5</A>, and have access to 
			all available metatextual categories.  The template 
			should be compatible with Macintosh and Windows versions of Filemaker version 3 and higher.<BR><BR>
			A word of warning: This feature doesn't stop you from downloading all instances of the
			word <I>the</I>. Especially if you are working with a modem-connection, this may take a while...<BR><BR>
		</TD>
	</TR>

</TABLE>
<SPACER TYPE="VERTICAL" SIZE="3">
--!>
</BODY>

END
&printFooter();


exit(0);

