<?
/*
################################################################
# >>> Time Recording System #
################################################################
# > Authors: Lucian Pricop and David Sturtevant #
# > E-mail: hide@address.com #
# > Date: 17 April 2007 #
# #
# This web application allows your staff to submit their time #
# sheets on line #
################################################################
# Copyright (C) 2007 Oxford Archaeology #
# #
# This program 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. #
# #
# This program is distributed in the hope that it will be #
# useful, but WITHOUT ANY WARRANTY; without even the #
# implied warranty of MERCHANTABILITY or FITNESS FOR A #
# PARTICULAR PURPOSE. See the GNU General Public License #
# for more details. #
# #
# You should have received a copy of the GNU General #
# Public License along with this program; if not, write to #
# the Free Software Foundation, Inc., 59 Temple Place - #
# Suite 330, Boston, MA 02111-1307, USA. #
################################################################
*/
require("header.php");//this is where session data is checked
function __autoload($class_name) {
require_once $class_name . '.php';
}
//VALUES NEEDED FROM THE db: basicRate, otDay, otSun, minHours
//values that the user can edit but they have an implicit value taken from the DB: resolution, showWeekendDays, startTime, stopTime
//values always inputed by the user: weekEndingDate
if(isset($_GET["refid"]))
$refid = intval($_GET["refid"]);
//if an administrator user is creating a new time sheet for another employee
if(isset($_POST["refid"]) && $_SESSION["usertype".$privateKey] == 1)
{
$employeeRefid = intval($_POST["refid"]);
$_SESSION["adminCreatingTimeSheetFor".$privateKey] = $employeeRefid;
}
else
{
$employeeRefid = $_SESSION["refid".$privateKey];
unset($_SESSION["adminCreatingTimeSheetFor".$privateKey]);
}
$nrOfWeeksAheadAllowed = 4;
$dbconn = new dbcontrol();
$connectionStatus = $dbconn->connectdb();
$sameaslastweek = isset($_GET["sameaslastweek"]);
//This is a feature to make things faster for some users
//it's supposed to add a new time sheet for the next available week
//according to the data entered in the immediately previous week.
if($sameaslastweek)
{
//search for the last week entered and check if it's appropriate to add the next
$queryString="SELECT * FROM tbl_office_time_sheet WHERE staffrefid = ".$employeeRefid." ORDER BY startdate ASC";
$queryResult = $dbconn->sendquery2($queryString);
$rowNr = $dbconn->numberofrows($queryResult);
if($rowNr==0)
{
header("Location: newTimesheet.php?status=noLastWeek");
die();
}
//get the necessary data to start the time sheet
$rowarray = pg_fetch_array($queryResult,$rowNr-1,PGSQL_ASSOC);
$refid = intval($rowarray["refid"]);
$time = strtotime($rowarray["startdate"]);
$weekEndingDate = date("d/m/Y",strtotime("+7days",$time));
$usDate = date("m/d/Y",strtotime("+7days",$time));
$seconds = 60 * 60 * 24 * 7 * ($nrOfWeeksAheadAllowed-1);
if(strtotime($usDate) > strtotime("next Friday")+$seconds)
{
header("Location: newTimesheet.php?status=futureDate");
die();
}
$viewingMode = "sameaslastweek";
}
else
$viewingMode = "edit";
//variables from DB, stored in SESSION vars
if(isset($_SESSION["dailyGross".$privateKey]) && strcmp($_SESSION["dailyGross".$privateKey],"")!=0)
$basicRate=$_SESSION["dailyGross".$privateKey];
else
$basicRate="basicRate";
if(isset($_SESSION["otHalf".$privateKey] ) && strcmp($_SESSION["otHalf".$privateKey] ,"")!=0)
$otDay=$_SESSION["otHalf".$privateKey];
else
$otDay="otDay";
if(isset($_SESSION["otDouble".$privateKey] ) && strcmp($_SESSION["otDouble".$privateKey] ,"")!=0)
$otSun=$_SESSION["otDouble".$privateKey];
else
$otSun="otSun";
if(isset($_SESSION["minHours".$privateKey] ) && strcmp($_SESSION["minHours".$privateKey] ,"")!=0)
$minHours=$_SESSION["minHours".$privateKey];
else
$minHours=37.5;
//searching for the given date in the database
if(isset($_POST["weekendingdate"]) && !$sameaslastweek)
{
$weekEndingDate=strtolower(trim($_POST["weekendingdate"]));
$dateArr = split("/",$weekEndingDate);
$usDate = $dateArr[1]."/".$dateArr[0]."/".$dateArr[2];
$queryString="SELECT * FROM tbl_office_time_sheet WHERE staffrefid = ".$employeeRefid." AND startdate = '".date("Y-m-d",strtotime($usDate))."'";
$queryResult = $dbconn->sendquery2($queryString);
$rowNr=pg_num_rows($queryResult);
if($rowNr ==1)
{
$rowarray = pg_fetch_array($queryResult,null,PGSQL_ASSOC);
$refid = intval($rowarray["refid"]);
$submitted = $rowarray["submitted"];
}
}
if(isset($refid))
{ //checking if this user has access to the referenced timesheet
$credentials = checkCredentials($dbconn,$refid);
if($credentials!=1 && $credentials!=3)//only the owner and administrators are allowed to edit the time sheet
{
header("Location: index2.php?status=baracuda");
die();
}
$queryString="SELECT * FROM tbl_office_time_sheet WHERE refid = ".$refid;
$queryResult = $dbconn->sendquery2($queryString);
$rowNr = $dbconn->numberofrows($queryResult);
if($rowNr==0)
{
header("Location: newTimesheet.php");
die();
}
$rowarray = pg_fetch_array($queryResult,null,PGSQL_ASSOC);
if(strcmp($rowarray["submitted"],"t")==0 && !$sameaslastweek && $credentials !=3)
{
header("Location: index2.php?status=sneaky");
die();
}
if(!$sameaslastweek)
$weekEndingDate = date("d/m/Y",strtotime($rowarray["startdate"]));
$resolution = strval($rowarray["guiresolution"]);
if($rowarray["showweekend"] == "t")
$showWeekendDays = "true";
else
$showWeekendDays = "false";
$startTime = $rowarray["starttime"];
$stopTime = $rowarray["stoptime"];
//if this is the case of an administrator editing this time sheet
if($credentials == 3)
{
define("ADMINEDIT",true);
//setting variables related to the owner of the timesheet
$queryString = "SELECT a.refid,a.fname,a.lname,a.minhours,b.submitted FROM tbl_staff_lookup AS a,tbl_office_time_sheet AS b WHERE a.refid = b.staffrefid AND b.refid = ".$refid;
$queryResult = $dbconn->sendquery2($queryString);
if($dbconn->numberofrows() == 0)
return 0;
$rowArray = pg_fetch_array($queryResult,null,PGSQL_ASSOC);
$ownersName = $rowArray["fname"]." ".$rowArray["lname"];
$minHours = $rowArray["minhours"];
$viewingMode = "adminedit";
}
}
else
{
if(!isset($_POST["resolution"]) || !isset($_POST["starttime"]) || !isset($_POST["stoptime"]) || !isset($_POST["weekendingdate"]))
{
header("Location: newTimesheet.php?status=noData");
die();
}
//variables from DB as implicit will be transfered via POST whether they were modified or not
$resolution=floatval(trim($_POST["resolution"]));
if(strcmp($_POST["showweekend"],"true")==0)
$showWeekendDays="true";
else
$showWeekendDays="false";
$startTime=pg_escape_string(htmlspecialchars(strtolower(trim($_POST["starttime"]))));
if(ereg("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$",$startTime)===FALSE)
{
header("Location: newTimesheet.php?status=startTimeError");
die();
}
$stopTime=pg_escape_string(htmlspecialchars(strtolower(trim($_POST["stoptime"]))));
if(ereg("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$",$stopTime)===FALSE)
{
header("Location: newTimesheet.php?status=stopTimeError");
die();
}
//variables that always need user's attention
$weekEndingDate=strtolower(trim($_POST["weekendingdate"]));
if(ereg("^(([0-2]{0,1}[0-9]{1}|3[0-1]{1}){1}\/([1-9]{1}|1[0-2]{1}){1}\/[1-2]{1}[0-9]{3}){1}$",$weekEndingDate)===FALSE)
{
header("Location: newTimesheet.php?status=dateError");
die();
}
$dateArr = split("/",$weekEndingDate);
$usDate = $dateArr[1]."/".$dateArr[0]."/".$dateArr[2];
//checking if the given date is a friday
$attributes = getdate(strtotime($usDate));
if($attributes["wday"] != 5)
{
header("Location: newTimesheet.php?status=notFriday");
die();
}
$seconds = 60 * 60 * 24 * 7 * ($nrOfWeeksAheadAllowed-1);
if(strtotime($usDate) > strtotime("next Friday")+$seconds)
{
header("Location: newTimesheet.php?status=futureDate");
die();
}
//saving user preferences for future use
$queryString="UPDATE tbl_staff_preferences SET resolution = ".floatval($resolution)." , starttime = '".$startTime."' , stoptime = '".$stopTime."', showweekend = ".$showWeekendDays." WHERE refid = ".$employeeRefid;
$queryResult = $dbconn->sendquery2($queryString);
if(pg_affected_rows($queryResult)==0)//this is because this user has no preferences set yet
{
$queryString="INSERT INTO tbl_staff_preferences (resolution,starttime,stoptime,showweekend,refid) VALUES (".floatval($resolution)." , '".$startTime."' , '".$stopTime."' , ".$showWeekendDays." , ".$employeeRefid.")";
$queryResult = $dbconn->sendquery($queryString);
}
$refid = -1;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta http-equiv="expires" value="Thu, 16 Mar 2000 11:00:00 GMT"/>
<meta http-equiv="pragma" content="no-cache"/>
<title>Time Recording System</title>
<link href="favicon.ico" type="image/x-icon" rel="shortcut icon"/>
<link rel='stylesheet' type='text/css' href='timesheets.css?version=3' />
<script type="text/javascript" src="yui/build/yahoo/yahoo.js"></script>
<script type="text/javascript" src="yui/build/dom/dom.js"></script>
<script type="text/javascript" src="yui/build/event/event.js"></script>
<script type="text/javascript" src="yui/build/connection/connection.js"></script>
<script type="text/javascript" src="yui/build/animation/animation.js"></script>
<script type="text/javascript" src="yui/build/autocomplete/autocomplete.js"></script>
<script type="text/javascript" src="js/toolbox.js?version=3"></script>
<script type="text/javascript" src="js/EntryClass.js?version=3"></script>
<script type="text/javascript">
<!--
window.onload=function(){
var entry = new entryModule(<?echo "\"".$basicRate."\",\"".$otDay."\",\"".$otSun."\",\"".$resolution."\",\"".strval($showWeekendDays)."\",\"".$startTime."\",\"".$stopTime."\",\"".$weekEndingDate."\",\"".$minHours."\",\"callBack\",\"".$refid."\",\"".$viewingMode."\",\"".$_SESSION["variable".$privateKey]."\""?>);
entry.initialize();
}
// window.
-->
</script>
</head>
<body>
<div id="doc" class="yui-t1">
<div id="hd">
<h1>Time Recording System</h1>
<!-- <h4>click in the table to choose your worked intervals</h4> -->
</div>
<?require("mainMenu.php");
echo "\t\t\t<div id=\"infoSection\">\n";
if(defined("ADMINEDIT"))
echo "This time sheet belongs to <span class=\"name\">".$ownersName."</span>";
else
echo "Click inside the table to select your worked intervals.";
?>
</div>
<div id="bd">
<div id="yui-main">
<div class="navset" id="nav">
<div class="hd" id="menuTop">
</div>
<div class="bd" id="menuMiddle">
</div>
<div class="bd" id="menuBottom">
</div>
</div>
<div id='content'></div>
</div>
</div>
</div>
<?require("footer.php");?>
<div id="errors"></div>
</body>
</html>