<?php
include_once('includes/projects/projects_components.php');

$callFn = 'AJAX_'.check_vars($_GET['todo']);

switch($callFn) {
	default:
		if(function_exists($callFn)) {
			$returndata = $callFn();
		} else {
			$returndata = array('status' => false, 'errorMessage' => 'Request could not be handled, no such sys fn');
		}
}

function AJAX_edit_project() {
	global $fyrk_projects;
	$variables = array();
	
	$variables['id'] = check_vars($_GET['id']);
	
	if($result = edit_project($variables)) {
		$returndata['status'] = true;
		$returndata['htmlData'] = $result;
		// $returndata['sessionData'] = json_encode($_SESSION);
		// $returndata['projectUserData'] = json_encode($fyrk_projects);
	} else {
		$returndata['status'] = false;
		$returndata['errorMessage'] = 'Error: Could not create new guest row';
	}
	
	return $returndata;
}

function AJAX_edit_project_task() {
	global $fyrk_projects;
	$variables = array();
	
	$variables = get_passed_variables('get');
	
	if($result = edit_project_task($variables)) {
		$returndata['status'] = true;
		$returndata['htmlData'] = $result;
		// $returndata['sessionData'] = json_encode($_SESSION);
		// $returndata['projectUserData'] = json_encode($fyrk_projects);
	} else {
		$returndata['status'] = false;
		$returndata['errorMessage'] = 'Error: Could not edit project task';
	}
	
	return $returndata;	
}

function AJAX_save_project_task() {
	global $fyrk_projects, $sop;
	$variables = array();
	
	$variables = get_passed_variables('post');
	$variables['end_date'] = $sop->date_to_db($variables['end_date']);
	$variables['description'] = $_POST['description']; // Get raw description
	
	if($result = save_project_task($variables)) {
		$returndata['description'] = $variables['description'];
		$returndata['status'] = true;
		$returndata['query'] = $result;
		$returndata['projectId'] = $variables['project_id'];
	} else {
		$returndata['status'] = false;
		$returndata['errorMessage'] = 'Error: Could not save project task';
		
		// $returndata['globalErrors'] = $GLOBALS['errors'];
	}
	
	return $returndata;	
	
}

function AJAX_delete_project_task() {
	global $fyrk_projects, $sop;
	$variables = array();

	if($fyrk_projects->my_permission <= 50) {
		return array('status' => false, 'errorMessage' => 'You do not have permission for this action');
	}	
	$variables = get_passed_variables('get');

	if($result = delete_project_task($variables)) {
		$returndata['status'] = true;
		$returndata['projectId'] = $result;
	} else {
		$returndata['status'] = false;
		$returndata['errorMessage'] = 'Error: Could not delete project task. ';
		
		// $returndata['globalErrors'] = $GLOBALS['errors'];
	}
	
	return $returndata;	
	
}

function AJAX_undo_project_task_delete() {
	global $fyrk_projects, $sop;
	$variables = array();

	if($fyrk_projects->my_permission <= 50) {
		return array('status' => false, 'errorMessage' => 'You do not have permission for this action');
	}	
	
	if(!isset($_SESSION['undo']['project_task_delete'])) {
		return array('status' => false, 'errorMessage' => 'No undo data found!');
	}

	$variables = $_SESSION['undo']['project_task_delete'];
	$variables['force_insert'] = true;

	if($result = save_project_task($variables)) {
		$returndata['status'] = true;
		/*
		$returndata['result'] = json_encode($result);
		$returndata['undo_data'] = json_encode($_SESSION['undo']['project_task_delete']);
		*/
		$returndata['projectId'] = $_SESSION['undo']['project_task_delete']['project_id'];
		
		unset($_SESSION['undo']['project_task_delete']);
	} else {
		$returndata['status'] = false;
		$returndata['errorMessage'] = 'Error: Could not undo deleted project task';
		
		// $returndata['globalErrors'] = $GLOBALS['errors'];
	}
	
	return $returndata;	
	
}

function AJAX_test_project_reminder_function() {
global $fyrk_projects, $sop;
	$variables = array();

	if($fyrk_projects->my_permission <= 50) {
		return array('status' => false, 'errorMessage' => 'You do not have permission for this action');
	}	
	$variables = get_passed_variables('get');
	$variables['date'] = $sop->date_to_db($variables['date']);

	if($result = test_project_reminder_function($variables)) {
		$returndata['status'] = true;
		$returndata['htmlData'] = $result;
	} else {
		$returndata['status'] = false;
		$returndata['errorMessage'] = 'Error: Could not retrieve reminder data. ';
		
		// $returndata['globalErrors'] = $GLOBALS['errors'];
	}
	
	return $returndata;	
}

?>