<!DOCTYPE html>
<html>
<head>
<title>Task List Manager</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<header>
<h1>Task List Manager</h1>
</header>
<main>
<p><?php print_r($task_list); ?></p>
<!-- part 1: the errors -->
<?php if (is_array($errors) && count($errors) > 0) : ?>
<h2>Errors:</h2>
<ul>
<?php foreach($errors as $error) : ?>
<li><?php echo htmlspecialchars($error); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- part 2: the tasks -->
<h2>Tasks:</h2>
<?php if (is_array($task_list) && count($task_list) > 0) : ?>
<ul>
<?php foreach( $task_list as $id => $task ) : ?>
<li><?php echo $id + 1 . '. ' .
htmlspecialchars($task); ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>There are no tasks in the task list.</p>
<?php endif; ?>
<br>
<!-- part 3: the add form -->
<h2>Add Task:</h2>
<form action="." method="post" >
<?php if (is_array($task_list) && count($task_list) > 0) : ?>
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]"
value="<?php echo htmlspecialchars($task); ?>">
<?php endforeach; ?>
<?php endif; ?>
<label>Task:</label>
<input type="text" name="newtask" id="newtask"> <br>
<label> </label>
<input type="submit" name="action" value="Add Task">
</form>
<br>
<!-- part 4: the modify/promote/delete form -->
<?php if (is_array($task_list) && count($task_list) > 0 && empty($task_to_modify)) : ?>
<h2>Select Task:</h2>
<form action="." method="post" >
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]"
value="<?php echo htmlspecialchars($task); ?>">
<?php endforeach; ?>
<label>Task:</label>
<select name="taskid">
<?php foreach( $task_list as $id => $task ) : ?>
<option value="<?php echo $id; ?>">
<?php echo htmlspecialchars($task); ?>
</option>
<?php endforeach; ?>
</select>
<br>
<label> </label>
<input type="submit" name="action" value="Modify Task">
<input type="submit" name="action" value="Promote Task">
<input type="submit" name="action" value="Delete Task">
<br><br>
<label> </label>
<input type="submit" name="action" value="Sort Tasks">
</form>
<?php endif; ?>
<!-- part 5: the modify save/cancel form -->
<?php if (!empty($task_to_modify)) : ?>
<h2>Task to Modify:</h2>
<form action="." method="post" >
<?php if (is_array($task_list) && count($task_list) > 0) : ?>
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]"
value="<?php echo htmlspecialchars($task); ?>">
<?php endforeach; ?>
<?php endif; ?>
<label>Task:</label>
<input type="hidden" name="modifiedtaskid" value="<?php echo $task_index; ?>">
<input type="text" name="modifiedtask" value="<?php echo $task_to_modify; ?>"><br>
<label> </label>
<input type="submit" name="action" value="Save Changes">
<input type="submit" name="action" value="Cancel Changes">
</form>
<?php endif; ?>
</main>
</body>
</html>
<?php
//get tasklist array from POST
$task_list = filter_input(INPUT_POST, 'tasklist',
FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
if ($task_list === NULL) {
$task_list = [];
// add some hard-coded starting values to make testing easier
// $task_list[] = 'Write chapter';
// $task_list[] = 'Edit chapter';
// $task_list[] = 'Proofread chapter';
}
//get action variable from POST
$action = filter_input(INPUT_POST, 'action');
//initialize error messages array
$errors = [];
//process
switch( $action ) {
case 'Add Task':
$new_task = filter_input(INPUT_POST, 'newtask');
if (empty($new_task)) {
$errors[] = 'The new task cannot be empty.';
} else {
array_push($task_list, $new_task);
}
break;
case 'Delete Task':
$task_index = filter_input(INPUT_POST, 'taskid', FILTER_VALIDATE_INT);
if ($task_index === NULL || $task_index === FALSE) {
$errors[] = 'The task cannot be deleted.';
} else {
unset($task_list[$task_index]);
$task_list = array_values($task_list);
}
break;
case 'Modify Task':
$task_index = filter_input(INPUT_POST,'taskid', FILTER_VALIDATE_INT);
//if list is empty
if ($task_index === NULL || $task_index === FALSE) {
$errors[] = 'The task can not be modified';
} else {
$task_to_modify = $task_list[$task_index];
}
break;
case 'Save Changes':
// get task id
$i = filter_input(INPUT_POST,'modifiedtaskid', FILTER_VALIDATE_INT);
$modified_task = filter_input(INPUT_POST,'modifiedtask');
if (empty($modified_task)){
//if new task is empty
$errors[] = 'The modified task can not be empty';
} elseif ($i === NULL || $i === FALSE) {
$errors[] = 'The task can not be modified';
} else {
//update task
$task_list[$i] = $modified_task;
$modified_task ='';
}
break;
case 'Cancel Changes':
$modified_task ='';
break;
case 'Promote Task':
$task_index = filter_input(INPUT_POST,'taskid', FILTER_VALIDATE_INT);
if ($task_index === NULL || $task_index === FALSE) {
$errors[] = 'Task can not be promoted';
} elseif( $task_index == 0) {
$errors[] = 'Can not promote first task';
}else{
//get the starting index and the index before it
$starting_index = $task_list[$task_index];
$new_index =$task_list[$task_index-1];
//chage the two tasks around
$task_list[$task_index-1] = $starting_index;
$task_list[$task_index] = $new_index;
break;
}
case 'Sort Tasks':
sort($task_list);
break;
}
include('task_list.php');
?>