This repository was archived by the owner on Nov 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_task.php
More file actions
104 lines (87 loc) · 3.18 KB
/
save_task.php
File metadata and controls
104 lines (87 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
require('ensure_login.inc.php');
require('db.inc.php');
if(isset($_GET['id'])) {
$get = $db->prepare('SELECT title, weekly_category_id, deadline FROM tasks WHERE id = :id;');
$get->bindValue('id', (int) $_GET['id']);
$get->execute();
if(!$task = $get->fetchObject()) {
header('Location: index.php');
}
$update = true;
}
if(!empty($_POST['title']) && isset($_POST['weekly_category']) && isset($_POST['deadline'])) {
if(!empty($_POST['deadline'])) {
$deadline = (new DateTime($_POST['deadline']))->format('Y-m-d H:i:s');
} else {
$deadline = null;
}
if(!empty($_POST['weekly_category'])) {
$weekly_category_id = (int) $_POST['weekly_category'];
} else {
$weekly_category_id = null;
}
if(isset($update)) {
$query = 'UPDATE tasks SET title = :title, weekly_category_id = :weekly_category_id, deadline = :deadline WHERE id = :id;';
} else {
$query = 'INSERT INTO tasks(title, weekly_category_id, deadline) VALUES(:title, :weekly_category_id, :deadline);';
}
$save = $db->prepare($query);
$save->bindValue('title', htmlspecialchars($_POST['title']));
$save->bindValue('weekly_category_id', $weekly_category_id);
$save->bindValue('deadline', $deadline);
if(isset($update)) {
$save->bindValue('id', (int) $_GET['id']);
}
if($save->execute()) {
header('Location: index.php');
} else {
$has_error = true;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ToDo! Enregistrement d'une tâche</title>
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="todo.css">
</head>
<body>
<header>
<h1>ToDo!</h1>
<h2>Enregistrement d'une tâche</h2>
<a href="index.php" id="back">Retour</a>
</header>
<form class="form" method="POST">
<?php
if(isset($has_error) && $has_error) {
?>
<p class="error" title="<?php echo htmlspecialchars($add->errorInfo()[2]); ?>">
Une erreur a eu lieu lors de l'enregistrement de la tâche.
</p>
<?php
}
?>
<input type="text" name="title" placeholder="Titre de la tâche" value="<?php echo htmlspecialchars($_POST['title'] ?? $task->title ?? '', null, null, false); ?>">
<select name="weekly_category">
<option value="">Sans catégorie</option>
<?php
$categories = $db->query('SELECT id, name FROM weekly_categories;');
$defaultSelected = $_POST['weekly_category'] ?? $task->weekly_category_id ?? '';
while($category = $categories->fetchObject()) {
?>
<option value="<?php echo $category->id; ?>"<?php if($defaultSelected == $category->id) { echo ' selected'; } ?>>
<?php echo $category->name; ?>
</option>
<?php
}
?>
</select>
<label for="deadline">Date limite</label>
<input type="date" name="deadline" value="<?php echo htmlspecialchars($_POST['deadline'] ?? $task->deadline ?? ''); ?>">
<button type="submit">Ajouter</button>
</form>
</body>
</html>