File: /home/imensosw/www/imenso.co/timesheet/api/timesheet.php
<?php
ob_start();
require_once '../lib/lib.inc.php';
logit(INFO, 'starting admin');
$resp = new stdClass();
$resp->error = false;
$resp->msg = [];
if(!dbConnect()){
$resp->error = "Database error";
}
$timesheet = new Timesheet();
//logit(INFO, "REQUEST: " . print_r($_REQUEST,1));
switch ($_REQUEST["action"]) {
case "gettimesheet":
if($timesheet->checkAuth()){
$timesheet->makeTimesheet();
$timesheet->getTimesheet();
}
break;
case "getadd":
if($timesheet->checkAuth()){
// $timesheet->makeTimesheet();
$timesheet->getAdd();
}
break;
case "saveaddform":
if($timesheet->checkAuth()){
$timesheet->makeTimesheet();
$timesheet->saveAddForm();
}
break;
case "deletetimesheet":
if($timesheet->checkAuth()){
$timesheet->makeTimesheet();
$timesheet->deleteTimesheet();
}
break;
default:
$timesheet->resp->error = "No command found";
logit(INFO, "GOT TO DEFAULT");
} //-es
if ($resp->error) {
error();
} else {
respond();
}
class Timesheet
{
public function __construct()
{
global $resp;
$this->resp = &$resp;
$this->init();
} //- ef
private function init() {
} //- init
public function checkAuth()
{
if(tokenTimeout($_REQUEST["token"]) || !isAdmin($_REQUEST["token"]))
{
$this->resp->error = true;
$this->resp->msg = "timeout out";
return false;
}
return true;
}
public function getTimesheet(){
//access_level NOT IN(3) and
global $config;
$resultsperpage=$config['resultsperpage'];
$search_title=$this->timesheet['search_title'];
$page=$this->timesheet['page'];
$sorting=$this->timesheet['sorting'];
$sortingby=$this->timesheet['sortingby'];
if($sorting=="")
{
$sorting="th.id";
$sortingby="desc";
}
$offset = $resultsperpage * ($page-1) ;
$in = array (
":user_id" =>$this->timesheet['user_id'],
":task_date" =>$this->timesheet['date']
);
//$sql = "SELECT SQL_CALC_FOUND_ROWS th.id,th.project_id as project_name,th.task_id as task_name,th.time,th.comment FROM timesheet_history th where th.task_date=:task_date and th.user_id=:user_id order by $sorting $sortingby limit $offset,$resultsperpage";
$sql = "SELECT SQL_CALC_FOUND_ROWS th.id,projects.project_name as project_name,tm.taskname as task_name,th.time,th.comment FROM timesheet_history th LEFT JOIN projects ON projects.id = th.project_id LEFT JOIN task_master tm ON tm.id = th.task_id where th.task_date=:task_date and th.user_id=:user_id order by $sorting $sortingby limit $offset,$resultsperpage";
if ($rc=dbPdoQuery($sql,$in)) {
while ($row = dbPdoFetch($rc)) {
$this->resp->timesheets[] = $row;
}
$this->paginationResult($this->timesheet['page']);
} else {
logit(WARN,"Did not get User List");
$this->resp->msg[] = "not found.";
}
} //- getUser
public function makeTimesheet()
{
$this->timesheet=$_REQUEST["timesheet"];
}
public function getAdd(){
$sql = "SELECT id, project_name as name FROM projects order by project_name asc";
if ($rc=dbPdoQuery($sql)) {
while ($row = dbPdoFetch($rc)) {
$this->resp->getproject[] = $row;
}
} else {
logit(WARN,"Did not get User List");
$this->resp->msg[] = "not found.";
}
$sql = "SELECT id, taskname as name FROM task_master order by taskname asc";
if ($rc=dbPdoQuery($sql)) {
while ($row = dbPdoFetch($rc)) {
$this->resp->gettask[] = $row;
}
} else {
logit(WARN,"Did not get User List");
$this->resp->msg[] = "not found.";
}
} //- getAddForm
public function saveAddForm()
{
// var_dump($this->timesheet);
$in = array (
":project_id" => $this->timesheet['project_name'],
":task_id" => $this->timesheet['task_name'],
":user_id" => $this->timesheet['user_id'],
":time" => $this->timesheet['time'],
":comment" => $this->timesheet['comment'],
":task_date" => $this->timesheet['date'],
":created_at"=>date('Y-m-d H:i:s'),
":updated_at"=>date('Y-m-d H:i:s'),
);
$sql="INSERT INTO timesheet_history (project_id,task_id,user_id,comment,time,task_date,created_at,updated_at) VALUES(:project_id,:task_id,:user_id,:comment,:time,:task_date,:created_at,:updated_at)";
if ($rc = dbPdoQuery($sql,$in)) {
logit(INFO, "SQL ", $sql);
logit(INFO,"Inserted record with ID ");
} else {
logit(WARN," DB Error: $sql in ".__FILE__." on line: ".__LINE__);
error("Database error [db03]");
}
}
public function deleteTimesheet()
{
$in = array (
":id" => $this->timesheet['id'],
);
$sql = "SELECT * FROM timesheet_history where id=:id";
if ($rc=dbPdoQuery($sql,$in) AND $row = dbPdoFetch($rc)) {
$sql="DELETE FROM timesheet_history WHERE id=:id";
$rc = dbPdoQuery($sql,$in);
logit(INFO,"deleted timesheet history with ID " . $this->timesheet['id']);
}
else {
logit(WARN," DB Error: $sql in ".__FILE__." on line: ".__LINE__);
error("Database error [db02]");
}
}
public function paginationResult($page)
{
global $config;
$sql_count="SELECT FOUND_ROWS() as total";
if ($rc=dbPdoQuery($sql_count)) {
if(count($row = dbPdoFetch($rc))>0)
{
$total=ceil($row["total"] /$config['resultsperpage']);
$this->resp->paginationresult=array("total"=>$total,"page"=>$page);
}
}
}
}
?>