From ccf210572963ed00a45d0b6693c10f6168422efb Mon Sep 17 00:00:00 2001 From: kutluhan Date: Tue, 9 Jul 2024 02:38:59 +0300 Subject: [PATCH] Add Todos, SweetAlert and Axios --- Controller/api.php | 78 ++++++++++++++++++++++ Controller/todo.php | 92 ++++++++++++++++++++++++++ View/todo/add.php | 158 ++++++++++++++++++++++++++++++++++++++++++++ View/todo/edit.php | 81 +++++++++++++++++++++++ View/todo/home.php | 141 +++++++++++++++++++++++++++++++++++++++ View/todo/list.php | 121 +++++++++++++++++++++++++++++++++ 6 files changed, 671 insertions(+) create mode 100644 Controller/api.php create mode 100644 Controller/todo.php create mode 100644 View/todo/add.php create mode 100644 View/todo/edit.php create mode 100644 View/todo/home.php create mode 100644 View/todo/list.php diff --git a/Controller/api.php b/Controller/api.php new file mode 100644 index 0000000..cc5641f --- /dev/null +++ b/Controller/api.php @@ -0,0 +1,78 @@ + $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("INSERT INTO todos SET todo_title = ?, todo_description = ?, todo_color = ?, todo_start_date = ?, todo_end_date = ?, category_id = ?, user_id = ?"); + $insert = $q -> execute([ + $post['title'], + $post['description'], + $post['color'] ?? '#007bff', + $start_date, + $end_date, + $post['category_id'] ?? 0, + get_session('user_id') + ]); + + if ($insert) { + $status = 'success'; + $title = 'Success!'; + $msg = 'The todo has been added 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 adding the todo.'; + echo json_encode(['status' => $status, 'title' => $title, 'msg' => $msg]); + exit(); + } + +} \ No newline at end of file diff --git a/Controller/todo.php b/Controller/todo.php new file mode 100644 index 0000000..5f1caa3 --- /dev/null +++ b/Controller/todo.php @@ -0,0 +1,92 @@ + $email, + 'password' => $password + ], 'login'); + + 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'] ?? '' + ]); + } + } + */ + + view('categories/home'); + +} elseif (route(0) == 'todo' && route(1) == 'add') { + + $return = model('categories', [], 'list'); + + view('todo/add', $return['data']); + +} elseif (route(0) == 'categories' && 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; + + $category_title = post('category_title'); + $category_id = post('category_id'); + + $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'); + + view('categories/edit', $return['data']); + +} elseif (route(0) == 'categories' && route(1) == 'remove' && is_numeric(route(2))) { + + $return = model('categories', ['category_id' => route(2)], 'remove'); + + redirect('categories/list/?type='. $return['type'].'&message='.$return['message']); +} \ No newline at end of file diff --git a/View/todo/add.php b/View/todo/add.php new file mode 100644 index 0000000..185fc59 --- /dev/null +++ b/View/todo/add.php @@ -0,0 +1,158 @@ + + +
+ + + + + + + + + +
+ +
+
+
+
+
+
+

Add ToDo

+
+ + + + ' . $_SESSION['error']['message'] . '
' : null; + ?> + +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ + +
+ +
+
+ +
+ + +
+
+
+ + + + +
+
+
+ +
+ +
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/View/todo/edit.php b/View/todo/edit.php new file mode 100644 index 0000000..89e3f9b --- /dev/null +++ b/View/todo/edit.php @@ -0,0 +1,81 @@ + + +
+ + + + + + + + + +
+ +
+
+
+
+
+
+

Edit Category

+
+ + + ' . $_SESSION['error']['message'] . '
' : null; + ?> + +
+ +
+
+ + + +
+
+ + + + +
+
+
+ +
+ +
+
+ +
+ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/View/todo/home.php b/View/todo/home.php new file mode 100644 index 0000000..991554f --- /dev/null +++ b/View/todo/home.php @@ -0,0 +1,141 @@ + + +
+ + + + + + + + + +
+ +
+
+
+
+

TO DO

+
+
+
Card title
+ +

+ Some quick example text to build on the card title and make up the bulk of the card's + content. +

+ + Card link + Another link +
+
+ +
+
+
Card title
+ +

+ Some quick example text to build on the card title and make up the bulk of the card's + content. +

+ Card link + Another link +
+
+
+ +
+

DOING

+
+
+
Card title
+ +

+ Some quick example text to build on the card title and make up the bulk of the card's + content. +

+ + Card link + Another link +
+
+ +
+
+
Card title
+ +

+ Some quick example text to build on the card title and make up the bulk of the card's + content. +

+ Card link + Another link +
+
+
+ +
+

DONE

+
+
+
Card title
+ +

+ Some quick example text to build on the card title and make up the bulk of the card's + content. +

+ + Card link + Another link +
+
+ +
+
+
Card title
+ +

+ Some quick example text to build on the card title and make up the bulk of the card's + content. +

+ Card link + Another link +
+
+
+ +
+ +
+
+ +
+ + + + + +
+ + + + + + + + + + + + \ No newline at end of file diff --git a/View/todo/list.php b/View/todo/list.php new file mode 100644 index 0000000..5de3eb2 --- /dev/null +++ b/View/todo/list.php @@ -0,0 +1,121 @@ + + +
+ + + + + + + + + +
+ +
+
+
+
+ +
+
+

Category List

+ +
+ + + +
+
+ +
+ ' . get('message') . '
' : null; + ?> + + + + + + + + + + + + $value): ?> + + + + + + + + + +
#TitleCreated DateUpdated DateProcess
. + + + + + +
+
+ + + +
+
+ +
+ +
+
+ +
+ + + + + + + + + + + + + + + + + + \ No newline at end of file