<?php
session_start();
include('../../globals/error-reporting.php');
global $ROOT, $PATH, $phpself;
$ROOT = $_SERVER['DOCUMENT_ROOT'];
$PATH = '../../';  
$phpself = basename(__FILE__);
require_once($PATH  .'globals/globals.php');
//$adminAct = $_REQUEST['adminAct']; 
$admin = 'project-time';

if (!empty($_POST['op'])) {
	$insertFields = '';
	$insertValues = '';
	
	foreach($_POST as $key => $value) {
		if (($key == 'op')||($key == 'submit')||($key == 'adminType')||(substr($key,0,5) == "time_")||(substr($key,0,4) == "dat_")||(substr($key,0,5) == "task_")||(substr($key,0,5) == "desc_")||(substr($key, 0, 4) == "inv_")||(substr($key, 0, 6) == "ppInv_")||(substr($key, 0, 7) == "ppPaid_")||(substr($key, 0, 5) == "bill_")) {
			// Do not include in update string
		}
		elseif (substr($key,0,7) == "delete_") {
			$query = 'UPDATE projectTime SET timeDisp=0 WHERE timeID=' . $updateID;
			$result = $con->query($query);;
			//echo '<p>'. $query . '</p>';
		}
		elseif ( (substr($key,0,11) == "datCurrent_")||(substr($key, 0, 12) == "timeCurrent_" )||(substr($key, 0, 12) == "taskCurrent_" )||(substr($key, 0, 12) == "descCurrent_")||(substr($key,0,12) == "billCurrent_")||(substr($key,0,11) == "invCurrent_")||(substr($key,0,13) == "ppInvCurrent_")||(substr($key,0,14) == "ppPaidCurrent_") ) {
			
			$updateArray = explode('_',$key);
			list($var,$updateID) = $updateArray;
			$query = '';
			
			if (substr($key, 0, 11) == "datCurrent_") {
				$newVar = 'dat_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = '';
				if ($newVal != $value) {
					$query = 'UPDATE projectTime SET timeDate="'. $newVal .'" WHERE timeID=' . $updateID;
				}
			}
			elseif (substr($key, 0, 12) == "timeCurrent_") {
				$newVar = 'time_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = '';
				if ($newVal != $value) {
					$query = 'UPDATE projectTime SET timeAmount='. $newVal .' WHERE timeID=' . $updateID;
				}
			}
			elseif (substr($key, 0, 12) == "taskCurrent_") {
				$newVar = 'task_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = 0;
				if ($newVal != $value) {
					$query = 'UPDATE projectTime SET taskID="'. $newVal .'" WHERE timeID=' . $updateID;
				}
			}
			elseif (substr($key, 0, 12) == "descCurrent_") {
				$newVar = 'desc_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = '';
				if ($newVal != $value) {
					$query = 'UPDATE projectTime SET timeDesc="'. $newVal .'" WHERE timeID=' . $updateID;
				}
			}
			elseif (substr($key, 0, 12) == "billCurrent_") {
				$newVar = 'bill_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = 0;
				if ($newVal != $value) {
					$query = 'UPDATE projectTime SET billable='. $newVal .' WHERE timeID=' . $updateID;
				}
			}
			elseif (substr($key, 0, 11) == "invCurrent_") {
				$newVar = 'inv_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = 0;
				if ($newVal != $value) {
					$query = 'UPDATE projectTime SET timeInvoiced='. $newVal .' WHERE timeID=' . $updateID;
				}
			}
			elseif (substr($key, 0, 13) == "ppInvCurrent_") {
				$newVar = 'ppInv_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = 0;
				if ($newVal != $value) {
					$query = 'UPDATE projectTime SET ppInv='. $newVal .' WHERE timeID=' . $updateID;
				}
			}
			elseif (substr($key, 0, 14) == "ppPaidCurrent_") {
				$newVar = 'ppPaid_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = 0;
				if ($newVal != $value) {
					$query = 'UPDATE projectTime SET ppPaid='. $newVal .' WHERE timeID=' . $updateID;
				} 
			}
			
			if (!empty($query)) {
				//echo '<p>'. $query . '</p>';
				$result = $con->query($query);
			}
		}
		else {
			$insertFields .= $key .',';
			$insertValues .= '"' . $value .'",';
		}
	}
	
	// Remove Last comma from string to avoid mySQL errors
	$insertFields = substr($insertFields, 0, strlen($insertFields)-1);
	$insertValues = substr($insertValues, 0, strlen($insertValues)-1);
	
	$insertTest = str_replace('"','',$insertValues);
	$insertTest = str_replace(',','',$insertTest);
	
	if (!is_numeric($insertTest)) {
		$query = 'INSERT INTO projectTime ('. $insertFields .') VALUES (' . $insertValues . ')';
		$result = $con->query($query);
	}
	
	$output = (($result) ? 'SUCCESS' : 'ERROR');
} else {
	$output = 'ERROR'; 	
}

if ($con) $con->close();
header('Content-Type: application/json; charset=utf-8', true); 
echo json_encode(array("output"=>$output));

?>