#!perl
use LSE::Database::Connection;
use LSE::Config;
use CGI;
use strict;
use warnings;
my $q = new CGI;
print $q->header;
my $dbh = getConnection();
#Check to make sure the current user has the right priveleges
my $current_user = $dbh->quote($ENV{REMOTE_USER}) ;
my $sth = $dbh->prepare("select * from users where userid = $current_user") ;
$sth->execute ;
my $current_user_info = $sth->fetchrow_hashref ;
#If not an administrator don't allow to view page
if ($current_user_info->{gid} ne 'admin')
{
print "Error: you aren't authorized to view this page" ;
exit ;
}
# Deactivate - delete input types
if($q->param('action') and $q->param('action') eq 'Deactivate') {
my $s_atype = $dbh->quote($q->param('atype'));
my $sinput = $q->param('sinput_table');
my $dinput = $q->param('dinput_table');
# This shouldn't happen unless they're mucking with CGI by hand
if($s_atype eq 'null' or !$sinput or !$dinput) {
print "
Invalid form parameters
\n";
exit;
} else {
$dbh->do("delete from $sinput where type = $s_atype");
$dbh->do("delete from $dinput where type = $s_atype");
}
}
# Activate - insert new type and its input
if($q->param('action') and $q->param('action') eq 'Activate') {
my $aclass = $q->param('aclass');
my $sinput = '';
my $dinput = '';
my $types = '';
if($aclass = 'sentence') {
$sinput = 'annotation_input';
$dinput = 'sentence_annotation_document_input';
$types = 'annotation_types';
} elsif ($aclass = 'doc') {
$sinput = 'document_annotation_input';
$dinput = 'document_annotation_sentence_input';
$types = 'document_annotation_types';
} else {
print "Invalid form parameters
\n";
exit;
}
my $s_atype = $dbh->quote($q->param('atype'));
$dbh->begin_work;
if($q->param('addnew')) {
my $s_desc = $dbh->quote($q->param('desc'));
$dbh->do("INSERT INTO $types (type, description) VALUES ($s_atype, $s_desc)");
}
foreach my $input ($q->param('sentence_input')) {
my $s_input = $dbh->quote($input);
$dbh->do("INSERT INTO $sinput (type, input) VALUES ($s_atype, $s_input)");
}
foreach my $input ($q->param('document_input')) {
my $s_input = $dbh->quote($input);
$dbh->do("INSERT INTO $dinput (type, input) VALUES ($s_atype, $s_input)");
}
$dbh->commit;
}
our (%config, $s_types, $d_types);
my $config = getAnnotConfig();
local %config = %{$config};
# These aren't real annotation types, so don't complain that they aren't
# configured in the database
delete $config{general};
delete $config{collection};
delete $config{database};
delete $config{job_priority};
$s_types = $dbh->selectall_arrayref("select type from annotation_types order by type");
$d_types = $dbh->selectall_arrayref("select type from document_annotation_types order by type");
# Display sentence annotation types
print "Sentence Annotation Types
\n";
dumpAnnotationTypes("annotation_types","annotation_input","sentence_annotation_document_input");
# Display document annotation types
print "
Document Annotation Types
\n";
dumpAnnotationTypes("document_annotation_types","document_annotation_sentence_input","document_annotation_input");
# Display annotation types mentioned in config file but not in database
print "
New Annotation Types
\n";
foreach my $atype (keys(%config)) {
print "
$atype
";
while(my ($k,$v) = each %{$config{$atype}}) {
print "- $k: $v
\n";
}
print "
\n";
delete $config{$atype};
print qq(\n);
}
sub dumpAnnotationTypes {
our %config, $s_types, $d_types;
# Tables listing annotation types, sentence input, document input
my ($types,$sinput,$dinput) = @_;
my $dbh = getConnection();
my $sth = $dbh->prepare("select * from $types order by type");
$sth->execute();
while(my $row = $sth->fetchrow_hashref) {
my $atype = $row->{type};
print qq(
$atype
\n);
print "$row->{description}
\n";
print qq();
}
}