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

#  This file is part of Open Admin for Schools.

#  Open Admin for Schools 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 2 of 
#  the License, or (at your option) any later version.


my %lex = ('Error' => 'Error',
	   'Announcements' => 'Announcements',
	   'Hover=Full View' => 'Hover on Title to see Full Text',
	   'View All' => 'View All',
	   'No Announcements' => 'No Announcements',
	   'Closed' => 'Closed',

	   );

my $self = 'eindex.pl';

my $defaultpage = 'ea.html';
my $defaultpath = './';

my $textlength = 100;

use DBI;
use CGI;
use Time::JulianDay;
use Cwd;


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

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


# contains DayInCycle function
eval require "../lib/libschedule.pl";
if ( $@ ) {
    print $lex{Error}. " $@<br>\n";
    die $lex{Error}. " $@\n";
}

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

my $title = qq{Ed Assistant - Open Admin $g_OpenadminVersion};

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

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

# Check if allowed by g_EaSiteAccess position values

# Get Staff Position
my $sth = $dbh->prepare("select field_value from staff_multi
   where field_name = 'position' and userid = ?");
$sth->execute( $userid );
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
my $position = $sth->fetchrow;

# print "Position $position\n";
	
# Load positions for this site
my $failflag = 1; # 1 and 2 are used for userid and password failures.
my $sth = $dbh->prepare("select datavalue from conf_system where dataname = ?");
$sth->execute('g_EaSiteAccess' );
if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr;}
my $val = $sth->fetchrow;

eval $val;
if ( $@ ) {
    print $lex{Error}. ": $@<br>\n";
    die $lex{Error}. ": $@\n";
}

foreach my $pos ( @g_EaSiteAccess ) {
    if ($pos eq $position) {
	$failflag = 0;
	last;
    }
}

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

if ( $failflag ) {
    print qq{<h3>Access not allowed for staff position "<i>$position</i>"</h3>\n};
    exit;
}

displayPage();


#--------------
sub displayPage {
#--------------

	print qq{<!DOCTYPE html>\n};
	print qq{<html><head><title>$title</title>\n};
	print qq{<meta charset="utf-8">\n};
	print qq{<link rel="stylesheet" type="text/css" href="/eadmin.css">\n};

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

	print qq{<!--\n};
	print qq{Copyright 2001-2026 Leslie Richardson\n\n};

	print qq{This file is part of Open Admin for Schools.\n};

	print qq{Open Admin for Schools is free software; you can redistribute it \n};
	print qq{and/or modify it under the terms of the GNU General Public License\n};
	print qq{as published by the Free Software Foundation; either version 2 of \n};
	print qq{the License, or (at your option) any later version.\n};
	print qq{-->\n};

	# Div element around heading section (2 floated divs)
	print qq{<div style="margin:1em 1em 0.5em 1em;">\n};
	
	print qq{<h1 style="display:inline;margin:1em;text-align:left;">};
	print qq{Educational Assistant Admin };
	print qq{<span style="font-size:80%; font-style:italic;padding:0;margin:0;">};
	print qq{Open Admin $g_OpenadminVersion</span></h1>\n\n};
	
	print qq{<div style="display:inline;margin:1em 1em 10em;">\n};

	# Display Day In Cycle. - from libSchedule.pl
	my $cycleday = findDayInCycle($currdate, $dbh);

	# check if closed.
	my $sth = $dbh->prepare("select * from dates where date = ?");
	$sth->execute( $currdate );
	if ( $DBI::errstr ) { print DBI::errstr; die $DBI::errstr; }
	my $ref = $sth->fetchrow_hashref;
	my %r = %$ref;

	if ( $r{dayfraction} > 0.99 or $cycleday == 0 ) { # closed all day
	    $cycleday = qq{<i>$lex{'Closed'}</i>};
	}

	print qq{<span style="font-weight:bold;">Day $cycleday in Cycle</span>\n};

	print qq{<span style="border:1px solid gray;padding:0.3em;border-radius:10px;">$userid</span>\n};
	print qq{</div>\n<br clear="left">\n};
	print qq{</div><!-- End of Header Div -->\n};
	

	$file = $defaultpage; 

#    } # end main page header

    my $path = "$defaultpath/$file";

    open(FH,'<', $path) || die "Cannot open:\n";

    my $htmlfile;
    { local $/; $htmlfile = <FH>; close FH; }
    print $htmlfile;

    print qq{\n<div style="float:left;">\n};
    viewAnnouncements();
    print qq{</div>\n};

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

} # end of displayPage()



#--------------------
sub viewAnnouncements {
#--------------------

#    print qq{<h2>$lex{Announcements}</h2>\n};
#    print qq{<div style="font-weight:bold;font-style:italic;font-size:80%;text-align:left;">\n};
    
    print qq{<div class="heading">$lex{Announcements} \n};
    print qq{<span style="font-size:75%;">$lex{'Hover=Full View'}</span> | };
    print qq{<form action="/tcgi-bin/announce/announceview.pl" method="post" };
    print qq{target="_blank" style="display:inline;">\n};
    print qq{<input type="submit" value="$lex{'View All'}"></form>\n};
    print qq{</div><hr>\n};


    # Load Top Announcements
    my $acount = 1; # announcement counter;
    my $topselect = "where topstay = 1";
    my $sth = $dbh->prepare("select * from announce $topselect order by adate desc");
    $sth->execute;
    if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
    my %topstay = ();
    my $first = 1;

    while ( my $ref = $sth->fetchrow_hashref ) {
	$ref->{atopic} = qq{<span style="color:red;">*</span>}. $ref->{atopic}; # mark top
	my $id = $ref->{id};
	$topstay{$id} = 1; # holds records already displayed.
	prAnnounce( $ref );
	$first = 0;
	$acount++;
    }

    # Rest of Page Announcements
    if ( $select ) { $select = 'where '. $select; } # put in 'where'

    $sth = $dbh->prepare("select * from announce $select order by adate desc");
    $sth->execute;
    if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }

    while ( my $ref = $sth->fetchrow_hashref ) {
	if ( $topstay{ $ref->{id} } ) { next; } # skip if already displayed on top.
	prAnnounce( $ref );
	$first = 0;
	$acount++;
	if ( $acount > 10) { last; }
	    
    }

    if ( $first ) {
	print "<h3>$lex{'No Announcements'}</h3>\n";
    }

    return;

} # end of viewAnnouncements


#-------------
sub prAnnounce {
#-------------

    my $ref = shift;
    my $date = fmtdate( $ref->{adate} );
    my $text = substr( $ref->{adesc}, 0, $textlength );
    $text =~ m/(^.+\b)/;
    $text = $1;
    my $titleattr = $q->escapeHTML( $ref->{adesc} );

    print qq{<div class="annce" title="$titleattr">};
    print qq{<b>$ref->{atopic}</b><br><i>$date</i></div>\n};
    print qq{<div>$text ...</div><hr>\n\n};
}



#----------
sub fmtdate {
#----------

    my $date = shift;
    my ($yr, $mo, $da) = split(/-/, $date);

    my $month = $month[$mo];

    my $jd = julian_day( split(/-/,$date) );
    my $dayindex = day_of_week( $jd )+ 1;
    my $day = $dow[$dayindex];

    my $rv = "$day, $month $da, $yr";

    return $rv;

}
