Example #1
0
}
if (!isset($_POST["action"])) {
    todolog("todo.php | No action found. Redirecting to home");
    redirect(VIEWS . "/home.php");
}
$action = $_POST["action"];
if ($action === "Add") {
    todolog("todo.php | Add action");
    if (isset($_POST["description"]) && isset($_POST["scheduledDate"])) {
        $description = $_POST["description"];
        $scheduledDate = date("Y-m-d", strtotime($_POST["scheduledDate"]));
        //using this format for MySQL storage
        todolog("todo.php | description: {$description}");
        todolog("todo.php | scheduled date: {$scheduledDate}");
        //validate task description
        $descValid = is_todo_description_valid($description);
        $errors = array();
        if (!$descValid) {
            array_push($errors, "Description must not be empty and can have a max of 256 characters");
        }
        $dateValid = is_scheduled_date_valid($scheduledDate);
        if (!$dateValid) {
            array_push($errors, "Scheduled date may only be a max of 7 days from today");
        }
        if (count($errors) > 0) {
            todolog("todo.php | Validation errors found");
            $_SESSION["errors"] = $errors;
        } else {
            todolog("todo.php | Valid todo. Saving.");
            //valid todo. save.
            $todo = new_todo($description, $scheduledDate, $_SESSION[CURRENT_USER]);
/**
 * Update todo list
 * Can only update the status of a Todo in the order of "Not Started=>Started=>Mid-way=>Completed"
 * @param string $todoId
 * @param string $description
 * @param string $status
 * @return boolean
 */
function update_todo_object($todoId, $description, $status)
{
    $updated = false;
    /*    var_dump($todoId);
        var_dump($description);
        var_dump($status);
        die(); */
    // get current status
    $connection = get_connection();
    $query = "SELECT status FROM todo WHERE id=?";
    $stmt = $connection->prepare($query);
    $stmt->bind_param("s", $todoId);
    $stmt->execute();
    $res = $stmt->get_result();
    $current_value = $res->fetch_assoc();
    $current_status = $current_value['status'];
    //validate task description
    $descValid = is_todo_description_valid($description);
    $errors = array();
    if (!$descValid) {
        array_push($errors, "Description must not be empty and can have a max of 256 characters");
    } else {
        switch ($status) {
            case 'N':
                if ($current_status === 'Not Started') {
                    $updated = true;
                }
                break;
            case 'S':
                if ($current_status === 'Started') {
                    $updated = true;
                } else {
                    if ($current_status === 'Not Started') {
                        //Update
                        $query = "UPDATE todo SET description=?, status='Started' WHERE id=?";
                        $stmt = $connection->prepare($query);
                        $stmt->bind_param("ss", $description, $todoId);
                        $stmt->execute();
                        $updated = true;
                    }
                }
                break;
            case 'M':
                if ($current_status === 'Midway') {
                    $updated = true;
                } else {
                    if ($current_status === 'Started') {
                        //Update
                        $query = "UPDATE todo SET description=?, status='Midway' WHERE id=?";
                        $stmt = $connection->prepare($query);
                        $stmt->bind_param("ss", $description, $todoId);
                        $stmt->execute();
                        $updated = true;
                    }
                }
                break;
            case 'C':
                if ($current_status === 'Completed') {
                    $updated = true;
                } else {
                    if ($current_status === 'Midway') {
                        //Update
                        $query = "UPDATE todo SET description=?, status='Completed' WHERE id=?";
                        $stmt = $connection->prepare($query);
                        $stmt->bind_param("ss", $description, $todoId);
                        $stmt->execute();
                        $updated = true;
                    }
                }
                break;
        }
    }
    return $updated;
}