Skip to content

Commit

Permalink
Todo CRUD, progress bar and status improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kutluhanazafli committed Jul 10, 2024
1 parent 0e79de6 commit faf8432
Show file tree
Hide file tree
Showing 7 changed files with 429 additions and 87 deletions.
135 changes: 134 additions & 1 deletion Controller/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,23 @@
}


$q = $db -> prepare("INSERT INTO todos SET todo_title = ?, todo_description = ?, todo_color = ?, todo_start_date = ?, todo_end_date = ?, category_id = ?, user_id = ?");
$q = $db -> prepare("INSERT INTO todos SET
todo_title = ?,
todo_description = ?,
todo_color = ?,
todo_progress = ?,
todo_status = ?,
todo_start_date = ?,
todo_end_date = ?,
category_id = ?,
user_id = ?");

$insert = $q -> execute([
$post['title'],
$post['description'],
$post['color'] ?? '#007bff',
intval($post['progress']) ?? '0',
$post['status'] ?? 'a',
$start_date,
$end_date,
$post['category_id'] ?? 0,
Expand All @@ -75,4 +87,125 @@
exit();
}

}

elseif (route(1) == 'edittodo') {
$post = filter($_POST);
$start_date = date('Y-m-d H:i:s');
$end_date = date('Y-m-d H:i:s');

if (!$post['title']) {

$status = 'error';
$title = 'Ops! Error!';
$msg = 'Please enter a title.';
echo json_encode(['status' => $status, 'title' => $title, 'msg' => $msg]);
exit();

}

if (!$post['description']) {

$status = 'error';
$title = 'Ops! Error!';
$msg = 'Please enter a description.';
echo json_encode(['status' => $status, 'title' => $title, 'msg' => $msg]);
exit();

}

if ($post['start_date_time'] && $post['start_date']) {
$start_date = $post['start_date'] . ' ' . $post['start_date_time'];
}

if ($post['end_date_time'] && $post['end_date']) {
$end_date = $post['end_date'] . ' ' . $post['end_date_time'];
}

if ($post['category_id']){
$user_id = get_session('user_id');
$category_id = $post['category_id'];
// We need to check if the category exists and belongs to the user.
// Variables are filtered so we can use them directly in the query.
$q = $db -> query("SELECT category_id FROM categories WHERE user_id = '$user_id' AND category_id = '$category_id'");
$get = $q -> fetch(PDO::FETCH_ASSOC);
if (!$get) {
$status = 'error';
$title = 'Ops! Error!';
$msg = 'The category does not exist or does not belong to you.';
echo json_encode(['status' => $status, 'title' => $title, 'msg' => $msg]);
exit();
}
}


$q = $db -> prepare("UPDATE todos SET
todo_title = ?,
todo_description = ?,
todo_color = ?,
todo_progress = ?,
todo_status = ?,
todo_start_date = ?,
todo_end_date = ?,
category_id = ?
WHERE todos.todo_id = ? && todos.user_id = ?
");

$update = $q -> execute([
$post['title'],
$post['description'],
$post['color'] ?? '#007bff',
intval($post['progress']) ?? '0',
$post['status'] ?? 'a',
$start_date,
$end_date,
$post['category_id'] ?? 0,
$post['id'],
get_session('user_id')
]);

if ($update) {
$status = 'success';
$title = 'Success!';
$msg = 'The todo has been updated successfully.';
echo json_encode(['status' => $status, 'title' => $title, 'msg' => $msg, 'redirect' => url('todo/list')]);
exit();
} else {
$status = 'error';
$title = 'Ops! Error!';
$msg = 'An error occurred while updating the todo.';
echo json_encode(['status' => $status, 'title' => $title, 'msg' => $msg]);
exit();
}

}

elseif (route(1) == 'removetodo') {

$post = filter($_POST);

if (!$post['id']) {
$status = 'error';
$title = 'Ops! Error!';
$msg = 'Invalid ID.';
echo json_encode(['status' => $status, 'title' => $title, 'msg' => $msg]);
exit();
}

$q = $db -> query("DELETE FROM todos WHERE todos.todo_id = '{$post['id']}' AND todos.user_id = '".get_session('user_id')."'");

if ($q) {
$status = 'success';
$title = 'Success!';
$msg = 'The todo has been deleted successfully.';
echo json_encode(['status' => $status, 'title' => $title, 'msg' => $msg, 'id' => $post['id']]);
exit();
} else {
$status = 'error';
$title = 'Ops! Error!';
$msg = 'An error occurred while deleting the todo.';
echo json_encode(['status' => $status, 'title' => $title, 'msg' => $msg]);
exit();
}

}
47 changes: 7 additions & 40 deletions Controller/todo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
redirect('login');
}

if (route(0) == 'categories' && !route(1)) {
if (route(0) == 'todo' && !route(1)) {
/*
if (isset($_POST['submit'])) {
Expand Down Expand Up @@ -44,49 +44,16 @@

view('todo/add', $return['data']);

} elseif (route(0) == 'categories' && route(1) == 'list') {
} elseif (route(0) == 'todo' && route(1) == 'list') {

$return = model('categories', [], 'list');

view('categories/list', $return['data']);

} elseif (route(0) == 'categories' && route(1) == 'edit' && is_numeric(route(2))) {

if (isset($_POST['submit'])) {

$_SESSION['post'] = $_POST;
$return = model('todo', [], 'list');

$category_title = post('category_title');
$category_id = post('category_id');
view('todo/list', $return['data']);

$return = model('categories', [
'category_title' => $category_title,
'category_id' => $category_id
], 'edit');

if($return['success'] == true) {
add_session('error', [
'type' => $return['type'] ?? '',
'message' => $return['message'] ?? ''
]);
if (isset($return['redirect'])) {
redirect($return['redirect']);
}
} else {
add_session('error', [
'type' => $return['type'] ?? '',
'message' => $return['message'] ?? ''
]);
}
}

$return = model('categories', ['category_id' => route(2)], 'getsingle');
} elseif (route(0) == 'todo' && route(1) == 'edit' && is_numeric(route(2))) {

view('categories/edit', $return['data']);
$return = model('todo', ['todo_id' => route(2)], 'getsingle');

} elseif (route(0) == 'categories' && route(1) == 'remove' && is_numeric(route(2))) {

$return = model('categories', ['category_id' => route(2)], 'remove');
view('todo/edit', $return['data']);

redirect('categories/list/?type='. $return['type'].'&message='.$return['message']);
}
59 changes: 59 additions & 0 deletions Model/todo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

if ($process == 'list') {

$q = $db -> prepare("SELECT * FROM todos
LEFT JOIN categories ON todos.category_id = categories.category_id
WHERE todos.user_id = ?");
$get = $q -> execute([get_session('user_id')]);

if ($q -> rowCount()) {

return [
'success' => true,
'type' => 'success',
'data' => $q -> fetchAll(PDO::FETCH_ASSOC)
];

} else {

return [
'success' => false,
'type' => 'success',
'data' => []
];

}

}


elseif ($process == 'getsingle') {

$id = $data['todo_id'];

$q = $db -> prepare("SELECT * FROM categories WHERE user_id = ?");
$get = $q -> execute([get_session('user_id')]);
$category = $q -> fetchAll(PDO::FETCH_ASSOC);

$q = $db -> prepare("SELECT * FROM todos
WHERE todos.todo_id = ? && todos.user_id = ?");
$get = $q -> execute([$id, get_session('user_id')]);

if ($q -> rowCount()) {

return [
'success' => true,
'type' => 'success',
'data' => array_merge($q -> fetch(PDO::FETCH_ASSOC), ['categories' => $category])
];

} else {
return [
'success' => false,
'type' => 'danger',
'data' => []
];
}

}
30 changes: 27 additions & 3 deletions View/todo/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<form id="todo" action="" method="POST">

<div class="card-body">

<div class="form-group">
<label for="category_id">Categories</label>
<select class="form-control" id="category_id" name="category_id">
Expand All @@ -49,33 +50,51 @@
<?php endforeach; ?>
</select>
</div>

<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="todo_title" placeholder="Enter ToDo title">
</div>

<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" id="description" name="todo_description" placeholder="Enter ToDo title">
</div>

<div class="form-group">
<label for="status">Status</label>
<select class="form-control" id="status" name="todo_status">
<option value="a">Active</option>
<option value="p">Passive</option>
</select>
</div>

<div class="form-group">
<label for="progress">Progress</label>
<input type="range" value="0" min="0" max="100" class="form-control" id="progress" name="todo_progress">
</div>

<div class="form-group">
<label for="color">Color</label>
<input type="color" class="form-control" id="color" name="todo_color" value="#007bff">
</div>

<div class="form-group">
<label for="start_date">Start Date</label>
<div class="row">
<input type="date" class="form-control col-8" id="start_date" name="todo_start_date">
<input type="time" class="form-control col-4" id="start_date_time" name="todo_start_date_time">
</div>

</div>

<div class="form-group">
<label for="end_date">End Date</label>
<div class="row">
<input type="date" class="form-control col-8" id="end_date" name="todo_end_date">
<input type="time" class="form-control col-4" id="end_date_time" name="todo_end_date_time">
</div>
</div>

</div>
<!-- /.card-body -->

Expand Down Expand Up @@ -106,10 +125,11 @@
<script src="<?= assets('plugins/jquery/jquery.min.js'); ?>"></script>
<!-- Bootstrap 4 -->
<script src="<?= assets('plugins/bootstrap/js/bootstrap.bundle.min.js'); ?>"></script>
<!-- Sweetalert -->
<script src="<?= assets('plugins/sweetalert2/sweetalert2.all.min.js'); ?>"></script>
<!-- AdminLTE App -->
<script src="<?= assets('js/adminlte.min.js'); ?>"></script>
<!-- Sweetalert -->
<script src="<?= assets('plugins/sweetalert2/sweetalert2.all.min.js'); ?>"></script>
<!-- Axios -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.7.2/axios.min.js" integrity="sha512-JSCFHhKDilTRRXe9ak/FJ28dcpOJxzQaCd3Xg8MyF6XFjODhy/YMCM8HW0TFDckNHWUewW+kfvhin43hKtJxAw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
const todo = document.getElementById('todo');
Expand All @@ -124,6 +144,8 @@
let end_date = document.getElementById('end_date').value;
let end_date_time = document.getElementById('end_date_time').value;
let category_id = document.getElementById('category_id').value;
let status = document.getElementById('status').value;
let progress = document.getElementById('progress').value;

let formData = new FormData();
formData.append('title', title);
Expand All @@ -134,6 +156,8 @@
formData.append('end_date', end_date);
formData.append('end_date_time', end_date_time);
formData.append('category_id', category_id);
formData.append('status', status);
formData.append('progress', progress);

axios.post('<?= url('api/addtodo'); ?>', formData).then(res => {

Expand Down
Loading

0 comments on commit faf8432

Please sign in to comment.