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

if (!empty($_POST['op'])) {
	$insertFields = '';
	$insertValues = '';
	
	foreach($_POST as $key => $value) {
		if (($key == 'op')||($key == 'submit')||($key == 'adminType')||(substr($key, 0, 9) == 'priority_')||(substr($key, 0, 10) == 'startDate_')||(substr($key, 0, 8) == 'dueDate_')||(substr($key, 0, 11) == 'taskStatus_')) {}
			// Do not include in update string
		
		elseif ( (substr($key, 0, 16) == "priorityCurrent_")||(substr($key, 0, 13) == "startCurrent_")||(substr($key, 0, 11) == "dueCurrent_")||(substr($key, 0, 14) == "statusCurrent_") ) {
			$updateArray = explode('_',$key);
			list($var,$updateID) = $updateArray;
		
			if (substr($key, 0, 16) == "priorityCurrent_") {
				$newVar = 'priority_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = '';
				$updateArray = explode('_',$newVar);
				list($var,$updateID) = $updateArray;
				if ($newVal != $value) {
					$query = 'UPDATE tasks SET priority='. $newVal .' WHERE taskID=' . $updateID;
					$result = $con->query($query);;
				}
			}
			elseif (substr($key, 0, 13) == "startCurrent_") {
				$newVar = 'startDate_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = '';
				$updateArray = explode('_',$newVar);
				list($var,$updateID) = $updateArray;
				if ($newVal != $value) {
					$query = 'UPDATE tasks SET startDate="'. $newVal .'" WHERE taskID=' . $updateID;
					$result = $con->query($query);;
				}
			}
			elseif (substr($key, 0, 11) == "dueCurrent_") {
				$newVar = 'dueDate_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = '';
				$updateArray = explode('_',$newVar);
				list($var,$updateID) = $updateArray;
				if ($newVal != $value) {
					$query = 'UPDATE tasks SET dueDate="'. $newVal .'" WHERE taskID=' . $updateID;
					$result = $con->query($query);;
				}
			}
			elseif (substr($key, 0, 14) == "statusCurrent_") {
				$newVar = 'taskStatus_'. $updateID;
				if (!empty($_REQUEST[$newVar]))
					$newVal = $_REQUEST[$newVar];
				else
					$newVal = '';
				$updateArray = explode('_',$newVar);
				list($var,$updateID) = $updateArray;
				if ($newVal != $value) {
					$query = 'UPDATE tasks SET taskStatus='. $newVal .' WHERE taskID=' . $updateID;
					$result = $con->query($query);;
				}
			}
			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 tasks ('. $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));

?>