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

#  This file is part of Open Admin for Schools.


%lex = ('Main' => 'Main',
	'Top' => 'Top',
	'No Records Found' => 'No Records Found',
	'All Records' => 'All Records',
	'Error' => 'Error',
	'Continue' => 'Continue',
	'Lastname' => 'Lastname',
	'Birthdate' => 'Birthdate',
	'Grade' => 'Grade',
	'Homeroom' => 'Homeroom',
	'Sort by' => 'Sort by',
	'Select by' => 'Select by',
	'View' => 'View',
	'Students' => 'Students',
	'Select by' => 'Select by',
	'OR' => 'OR',
	'Show Withdrawn' => 'Show Withdrawn',
	
);

use DBI;
use CGI;
use Cwd;

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


my $q = new CGI;
print $q->header( -charset, $charset );
my %arr = $q->Vars;

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

my ($sec, $min, $hour, $mday, $mon, $year, $wday, 
    $yday, $iddst) = localtime(time);
$year = $year + 1900;
$mon++;
$wday++;
my $currdate = "$dow[$wday], $month[$mon] $mday, $year";

# Display top of page
my $title = "Set Teacher/TA/EA Site access";

print qq{$doctype\n<html><head><title>$title</title>\n};
print qq{<link rel="stylesheet" href="$css" type="text/css">\n};
print qq{$chartype\n</head><body><a name="top"></a>\n};
print qq{[ <a href="$homepage">$lex{Main}</a> ]\n};

print qq{<h1>$title</h1>\n};

   
if ( not $arr{page} ) {
    showStartPage();

} else {
    delete $arr{page};
    updateAccess();
}


#---------------
sub updateAccess {
#---------------

    use Data::Dumper;
    $Data::Dumper::Purity = 1;
    $Data::Dumper::Indent = 0;

    # foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}<br>\n"; }
    
    my (@tchsite, @easite);
    foreach my $key ( sort keys %arr ) {
	my ($type,$val) = split(':', $key);
	if ( $type eq 'TCH' ) {
	    push @tchsite, $val;
	} elsif ( $type eq 'EA') {
	    push @easite, $val;
	} else {
	    print qq{ERROR for val:$val KEY:$key<br>\n};
	    exit;
	}
    }

#    print qq{TCH:@tchsite<br>\n};
#    print qq{EA:@easite<br>\n};

    my $sth = $dbh->prepare("update conf_system set datavalue = ? where id = ?");

    foreach my $dataname ( 'g_TchSiteAccess', 'g_EaSiteAccess' ) {

	my @pos; # list of positions
	if ( $dataname eq 'g_TchSiteAccess' ) {
	    @pos = @tchsite;
	} else {
	    @pos = @easite;
	}

	my $value_ref = [ ];
	my $name_ref = [ ];
	
#	print "<div>@pos</div>\n";
	@$name_ref;
	@$value_ref;
	
	# Get record id for teacher g_TchSiteAccess.
	my $sth1 = $dbh->prepare("select * from conf_system where dataname = ?");
	$sth1->execute($dataname);
	my $ref = $sth1->fetchrow_hashref;
	my %r = %$ref;
	my $id = $r{id}; # id of that teacher record.
    
	my $dataname = '*'. $dataname;
	push @$name_ref, $dataname;
	
	push @$value_ref, \@pos;

	
	my $d = Data::Dumper->new( $value_ref, $name_ref );
	my $datavalue = $d->Dump;

	print qq{Datavalue:$datavalue<br>\n};
	$sth->execute( $datavalue, $id );
	if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    }

    
    
    print qq{<h3>Site Access Variables Updated</h3>\n};

    # Now write the file update, if requested.
    print qq{<h4>Updating Configuration File from variables</h4>\n};
    updateFiles();

    print qq{</body></html>\n};

    exit;

}



#----------------
sub showStartPage { # Entry Values for Custom Script
#----------------

    # Get All positions in staff_multi table

    
    my @positions;
    my $sth = $dbh->prepare("select distinct field_value from staff_multi where field_name = 'position'
       order by field_value");
    $sth->execute;
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    while ( my $pos = $sth->fetchrow ) {
	push @positions, $pos;
    }
#    print "Positions:@positions<br>\n";

    
    # convert array into hash values for checking.
    my (%tchpos, %eapos); # teachers positions and ea positions loaded from vars.
    foreach my $pos ( @g_TchSiteAccess ) {
	$tchpos{$pos} = 1;
    }
    foreach my $pos ( @g_EaSiteAccess ) {
	$eapos{$pos} = 1;
    }


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

    print qq{<h3>Check those positions that will have access on that site</h3>\n};

    # Left Column will be teacher site, right side will be 
    print qq{<table cellpadding="3" cellspacing="0" border="0" };
    print qq{style="border:1px solid gray;padding:0.5em;float:left;margin:0.5em;">\n};
    
    print qq{<tr><td class="bla">Teacher Site Access Positions</td>};
    foreach my $pos ( @positions ) {
	my $chk;
	if ( $tchpos{$pos} ) { $chk = qq{checked="checked"}; }
	print qq{<tr><td><input type="checkbox" name="TCH:$pos" value="1" $chk>$pos</td></tr>\n};
    }
    print qq{</table>\n};
    
    # Right Side - EA Site Access
    print qq{<table cellpadding="3" cellspacing="0" border="0" };
    print qq{style="border:1px solid gray;padding:0.5em;float:left;margin:0.5em;">\n};
    
    print qq{<tr><td class="bla">EA/TA Site Access Positions</td>};
    foreach my $pos ( @positions ) {
	my $chk;
	if ( $eapos{$pos} ) { $chk = qq{checked="checked"}; }
	print qq{<tr><td><input type="checkbox" name="EA:$pos" value="1" $chk>$pos</td></tr>\n};
    }
    print qq{</table>\n};


    # Continue
    print qq{<p style="clear:left;margin:0.5em;"><input type="submit" value="$lex{Continue}"></p>\n};
    print qq{</form></body></html>\n};

    exit;

}


#--------------
sub updateFiles {
#--------------


    # this contains the db access info
    system("cp -f ../etc/admin.conf.root ../etc/admin.conf");

    my $filename = qq{../etc/admin.conf};

#    system("cat $filename");
#    return;

    # NOTE: APPEND!
    open(FH,">>", $filename) or die qq{Cannot open file $filename: $!\n};

    my $sth = $dbh->prepare("select id, datavalue from conf_system 
            where filename = 'admin' order by dataname");
    $sth->execute;
    if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }

    while ( my ($id, $value) = $sth->fetchrow ) {
#	print "ID:$id VAL:$value<br>\n";
	print FH $value, qq{\n};
    }

    print FH qq{\n1;\n};
    close FH;

    print qq{<h3>File admin.conf Updated</h3>\n};
   
    return; 

}
