#!/usr/bin/perl
#  Copyright 2001-2025 Leslie Richardson

#  This file is part of Open Admin for Schools.
#  Origin: from pvTestDelete.pl

my %lex = ('Main' => 'Main',
	   'Error' => 'Error',
	   'Delete' => 'Delete',
	   'Continue' => 'Continue',
	   'Date' => 'Date',
	   'No User Id' => 'No User Id',
	   'No Password' => 'No Password',
	   'Please Log In' => 'Please Log In',
	   'Record(s) Deleted' => 'Record(s) Deleted',
	   'Admin Password' => 'Admin Password',
	   'Not Allowed' => 'Not Allowed',
	   'Grade' => 'Grade',
	   'Author' => 'Author',
	   'Close' => 'Close',

	   );

use DBI;
use CGI;

my $adminpassword = 'meisowen';
my $self = 'fpDelete.pl';

# Configuration Settings ---------------------
my $allow_author = 1; # allow author to delete
my $allow_fp = 1; # allow user with fp access code to delete
#---------------------------------------------

my $q = new CGI;
my %arr = $q->Vars;

my @time = localtime(time);
my $year = $time[5] + 1900;
my $month = $time[4] + 1;
my $currdate = "$year-$month-$time[3]";


eval require "../../etc/admin.conf";
if ( $@ ) {
    print $lex{Error}. " $self: $@<br>\n";
    die $lex{Error}. "$self: $@\n";
}

my $userid = $ENV{'REMOTE_USER'};

my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);

print $q->header(-charset, $charset);

# Check for fp access code
my $sth = $dbh->prepare("select field_value from staff_multi 
  where field_name = 'access' and userid = ?");
$sth->execute( $userid );
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }

my $access_fp;
while ( my $val = $sth->fetchrow ) {
    if ( uc $val eq 'FP' ) { $access_fp = 1; }
    # print qq{VAL:$val ACCESS:$access_fp<br>\n};
}


# Page Header
my $title = qq{$lex{Delete} FP Record};
print qq{$doctype\n<html><head><title>$title</title>\n};
print qq{<link rel="stylesheet" href="$tchcss" type="text/css">\n};

print qq{<style type="text/css">td.ra { text-align:right; }
td.bcn { font-size:100%;text-align:center;font-weight:bold; }
</style>\n};

print qq{$chartype\n</head><body style="padding:1em 2em;">\n};

print qq{[ <a href="$tchpage">$lex{Main}</a> ]\n};
print qq{<h1>$lex{Delete} F&amp;P Reading Record</h1>\n};


if ( not $arr{page} ) {
    selectRecord( $arr{id} );
    
} elsif ( $arr{page} == 1 ) {
    delete $arr{page};
    deleteRecord();
}


#---------------
sub selectRecord {
#---------------

    #foreach my $key ( sort keys %arr ) { print qq{K:$key V:$arr{$key}<br>\n}; }

    my $id = shift;

    # Get Record
    my $sth = $dbh->prepare("select * from fp_book where id = ?");
    $sth->execute( $id );
    if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
    my $ref = $sth->fetchrow_hashref;
    my %r = %$ref;
    my $studnum = $r{studnum};
    
    # Access Controls
    my $failflag;
    if ( not $allow_author and not $allow_fp ) { $failflag = 1; } # nobody deletes
    elsif ( $allow_author and $r{tauthor} ne $userid ) { $failflag = 1; } # not author

    if ( $allow_fp and $access_fp ) { $failflag = 0; } # allow dra override of author failure
    if ( $adminpassword eq $arr{adminpassword} ) { $failflag = 0; } # allow admin override
    if ( $failflag ) {
	print qq{<h3>$lex{Delete} $lex{'Not Allowed'}</h3>\n};

	print qq{<form action="$self" method="post"> \n};
	print qq{<input type="hidden" name="id" value="$id">\n};
	print qq{<p>$lex{'Admin Password'} <input type="text" size="10" name="adminpassword">\n};
	print qq{<input type="submit" value="$lex{Continue}">\n};
	print qq{</p></form>\n};
	print qq{</body></html>\n};
	exit;
    }

    
    # Get Student Name
    my $sth1 = $dbh->prepare("select lastname, firstname, grade from student where studnum = ?");
    $sth1->execute( $studnum );
    if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
    my ( $lastname, $firstname,$grade ) = $sth1->fetchrow;
    # print qq{Name: $lastname $firstname<br>\n};

    # Print Form Heading
    print qq{<form action="$self" method="post"> \n};
    print qq{<input type="hidden" name="page" value="1">\n};
    print qq{<input type="hidden" name="id" value="$id">\n};


    print qq{<div><input type="submit" value="$lex{Delete} Reading Record"></div>\n};

    
    # Print Table Header and Heading values

    
    print qq{<table cellpadding="3" cellspacing="0" border="0" style="border:1px solid gray;margin:0.5em;">\n};

    print qq{<tr><td colspan="3" class="bcn">$firstname $lastname</td></tr>\n};

    print qq{<tr><td class="bra">$lex{Grade}</td><td class="la">$grade</td></tr>\n};

    print qq{<tr><td class="bra">Title</td><td class="la">$r{title}</td></tr>\n}; 
    
    print qq{<tr><td class="bra">$lex{Date}</td><td class="la">$r{tdate}</td></tr>\n}; 

    print qq{<tr><td class="bra">$lex{Author}</td><td class="la">$r{tauthor}</td></tr>\n};
    
    print qq{</table>\n};

    print qq{<div><input type="submit" value="$lex{Delete} Reading Record"></div>\n};
    
    print qq{</form></body></html>\n};

    exit;

}


#--------------
sub deleteRecord {
#--------------

    foreach my $key ( sort keys %arr ) { print qq{K:$key V:$arr{$key}<br>\n}; }

    my $id = $arr{id};

    # Delete Test
    $sth = $dbh->prepare("delete from fp_book where id = ?");  
    $sth->execute( $id );
    if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }

    print qq{<h1>Record Deleted</h1>\n};

    print qq{<p><form><input type="hidden" name="none">\n};
    print qq{<input type="button" value="$lex{Close} this Tab" };
    print qq{onClick="parent.close()"></form></p>};
    
    print qq{</body></html>\n};

    exit;

}
