<?php

require_once "../header.php";

require_login();

$data = array();
$data["error"] = false;

if ( !isset($_REQUEST['id']) ) pushData("Invalid delete request");
$task = new Task(intval($_REQUEST['id']));
if ( $task->getID() == 0 ) pushData("Task not found");
$deleted = $task->delete();
if ( $deleted ) {
    $data["message"] = "Deleted Task: \"{$task->getTitle(HTMLSAFE)}\"";
} else {
    pushData("Task deletion error!!!");
}

pushData();

exit();

function pushData($errormsg = null) {
    global $data;

    if ( !is_null($errormsg) ) {
        $data["error"] = true;
        $data["message"] = $errormsg;
    }
    header('Content-Type: application/json');
    echo json_encode($data);
    exit();
}
