#!/usr/bin/perl
# ================================================================================
# $Rev: 159 $ $URL: svn://localhost/PlayChess/trunk/cgi/holidays.cgi $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Author: $Author: Thomas $
# Modified: $Date: 2005-09-25 13:47:37 +0200 (So, 25 Sep 2005) $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Copyright at playchess.de - all rights reserved
# ================================================================================
use lib "../cgi-bin";
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
use Member;
use PCPlayer;
use PC;
use PCLeagueUtil;
use PCSession;
use Template;
use Util;
use ChessConfig;
use MyDbi;
##--------------------------------------------------------------------------
## LOCAL CONFIGURATION
##--------------------------------------------------------------------------
local $config = getConfig();
$URL = $ENV{DOCUMENT_URI}; # url of your domain
$ROOT = $ENV{DOCUMENT_ROOT}; # path to document root
$SERVER = $ENV{SERVER_NAME}; # server name
$HOST = $ENV{HTTP_HOST};
local ($starttime,$endtime) = (time(),0);
$cgi = CGI->new();
$sobj = PCSession->new($cgi);
# Fetch CGI-parameter names
# ~~~~~~~~~~~~~~~~~~~~~~~~~
@params = $cgi->param();
$action = $cgi->param( 'action' );
$begin = $cgi->param( 'begin' );
$duration = $cgi->param( 'duration' );
# Page requires login
# ~~~~~~~~~~~~~~~~~~~
$sobj->assertLogged();
# Page requires membership if option is set and not email
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# $sobj->assertMember() if($option && $option ne 'email');
# Get holiday attributes from session
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local $since = $sobj->getValue( 'holiday-since' );
local $until = $sobj->getValue( 'holiday-until' );
local $taken = 0+$sobj->getValue( 'holiday-taken' );
local $special = 0+$sobj->getValue( 'holiday-special' );
local $pid = $sobj->getValue( 'pid' );
local $name = $sobj->getValue( 'name' );
local $available = 7 * 86400;
if( $sobj->getValue('isStandardMember') ) { $available = 14*86400; }
elsif( $sobj->getValue('isPremiumMember') ) { $available = 21*86400; }
elsif( $sobj->getValue('isStdPremiumMember') ) { $available = 21*86400; }
# Sonderurlaub in Tagen
$available += $special * 86400;
# print "action = $action
\n";
# CGI parameters ==> store in DB and session
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( !$action )
{
$available -= $taken;
$available = 0 if $available < 0;
showOverview();
}
elsif( $action eq 'grant' )
{
# Following actions require database access
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$dbh = MyDbi::dbiConnect() or exit(1);
# $dbh->{RaiseError} = 0;
# $dbh->{PrintError} = 1;
# Confirm holiday attributes from database
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
($since, $until, $taken, $special) =
@{ MyDbi::getRow('select since,until,taken,special from tbl_holiday where pid=?', $pid ) };
$available += $special * 86400;
$available -= $taken;
$available = 0 if $available < 0;
# Check CGI parameters
# ~~~~~~~~~~~~~~~~~~~~
showOverview( 'You are already on a leave' ) if( $until > time );
showOverview( 'Duration too long' ) if( ($duration-1)*86400 > $available );
showOverview( 'Duration out of range' ) if( $duration < 1 || $duration > 14+$special );
showOverview( 'Invalid Begin' ) if( $begin < 0 || $begin > 3 );
# Change holiday attributes
# ~~~~~~~~~~~~~~~~~~~~~~~~~
$period = ($available > $duration*86400) ? $duration*86400 : $available;
$since = time + $begin*86400;
$until = $since+$period;
$taken += $period;
$available -= $period;
# Update statement
# ~~~~~~~~~~~~~~~~
my $stmt = "
replace into tbl_holiday
set pid=?, since=?, until=?, taken=?
";
my $rc = MyDbi::doStmt( $stmt, $pid, $since, $until, $taken );
if( $rc )
{
$sobj->setValue( 'holiday-since', $since );
$sobj->setValue( 'holiday-until', $until );
$sobj->setValue( 'holiday-taken', $taken );
}
showOverview( 'Your leave request has been granted' );
}
elsif( $action eq 'cancelpage' )
{
showCancelpage();
}
elsif( $action eq 'cancel' )
{
# Following actions require database access
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$dbh = MyDbi::dbiConnect() or exit(1);
# Confirm holiday attributes from database
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
($since, $until, $taken) = @{ MyDbi::getRow('select since,until,taken from tbl_holiday where pid=?', $pid ) };
# Check parameters
# ~~~~~~~~~~~~~~~~
showCancelpage( 'You are not on a leave' ) if( $until < time );
# Sample games to be resumed
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
my $stmt = "
select pg.gid
from
tbl_game g,
tbl_player_game pg
where
pg.pid = ?
and g.lastmove = ?
and g.gid = pg.gid
and pg.color = g.tomove
and g.result = ''
";
my( @games ) = @{ MyDbi::getColumn($stmt, $pid, $until) };
# print "Select games: pid=$pid, until=$until
\n";
# my $rows = MyDbi::getRows( $stmt, $pid, $until );
# my $lastrow = $#{ $rows };
# my $nRows = $lastrow+1;
# print "Resume games: ", scalar @games, " found
\n";
# showCancelpage( "No games found that could be resumed" ) unless(@games>0);
# Setting lastmove to current time restarts these games
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( @games > 0 )
{
showCancelpage( 'Cancel not yet allowed: '.scalar(@games) )
if( $since+86400 > time );
my( $ustmt, $usth, $gid, $rc );
$ustmt = "update tbl_game set lastmove=? where gid=?";
$usth = $dbh->prepare( $ustmt );
foreach $gid (@games)
{
$rc = $usth->execute( time(), $gid );
# print "Setting lastmove: gid=$gid, rc=$rc
\n";
showCancelpage( "Incomplete operation, please try again (rc=$rc)" ) unless($rc);
}
}
# All games are restarted => update holiday table and session object
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my $ustmt = "update tbl_holiday set until=?, taken=? where pid=?";
my $remaining = $until - time;
$until = time;
$taken = $taken - $remaining;
$rc = MyDbi::doStmt( $ustmt, $until, $taken, $pid );
showCancelpage( "Error updating holiday table (rc=$rc)" ) unless($rc==1);
$sobj->setValue( 'holiday-until', $until );
$sobj->setValue( 'holiday-taken', $taken );
showOverview();
}
# =======================
sub showOverview
# =======================
{
my ( $err ) = @_;
# database is no longer needed
#-----------------------------
$dbh->disconnect() if( $dbh );
my $maxDays = int($available/86400);
$maxDays = 14 if( $maxDays > 14 && $special == 0 );
my @days = (1..$maxDays);
# Load page template
# ~~~~~~~~~~~~~~~~~~
my $tpl = Template->new( "holidays.tpl" );
$tpl->addMakro( 'NAME', $name );
$tpl->addMakro( 'ONLEAVE', $until > time );
$tpl->addMakro( 'SINCE', Util::getDate($since) );
$tpl->addMakro( 'UNTIL', Util::getDate($until) );
$tpl->addMakro( 'TAKEN', $taken );
$tpl->addMakro( 'TAKEN-DAYS', int($taken/86400) );
$tpl->addMakro( 'AVAIL-DAYS', int($available/86400) );
$tpl->addMakro( 'AVAIL-HOURS', int( ($available%86400) / 3600) );
$tpl->addMakro( 'EX-FROM', Util::getDate(time+86400) );
$tpl->addMakro( 'EX-UNTIL', Util::getDate(time+8*86400) );
$tpl->addMakro( 'ERR', $err );
$tpl->addArray( 'DAYS', \@days );
print $tpl->Expand($sobj);
$sobj->close();
exit(0);
}
# =======================
sub showCancelpage
# =======================
{
my ( $err ) = @_;
# database is no longer needed
#-----------------------------
$dbh->disconnect() if( $dbh );
my $tpl = Template->new( "holidays-cancel.tpl" );
$tpl->addMakro( 'NAME', $name );
$tpl->addMakro( 'ONLEAVE', $until > time );
$tpl->addMakro( 'MAYCANCEL', time < $since || $since+86400 < time );
$tpl->addMakro( 'CANCELTIME', Util::getDate($since+86400) );
$tpl->addMakro( 'SINCE', Util::getDate($since) );
$tpl->addMakro( 'UNTIL', Util::getDate($until) );
$tpl->addMakro( 'TAKEN', $taken );
$tpl->addMakro( 'TAKEN-DAYS', int($taken/86400) );
$tpl->addMakro( 'AVAIL-DAYS', int($available/86400) );
$tpl->addMakro( 'AVAIL-HOURS', int( ($available%86400) / 3600) );
$tpl->addMakro( 'ERR', $err );
print $tpl->Expand($sobj);
$sobj->close();
exit(0);
}