From c89113d72dc46a6a87d2075e2e48502f72c9350b Mon Sep 17 00:00:00 2001 From: nsavinda Date: Tue, 31 Oct 2023 20:20:49 +0530 Subject: [PATCH] feat: Admin->tips crud --- app/controllers/Admin/Tips.php | 46 +++++++ app/controllers/Tips.php | 2 +- app/core/Database.php | 2 +- app/core/Model.php | 2 +- app/models/Tips.php | 32 ++++- app/views/admin/layout/sidebar.php | 2 +- app/views/admin/tips.view.php | 185 +++++++++++++++++++---------- public/assets/css/admin-style.css | 174 +++++++++++++++++++++++++++ 8 files changed, 372 insertions(+), 73 deletions(-) create mode 100644 app/controllers/Admin/Tips.php diff --git a/app/controllers/Admin/Tips.php b/app/controllers/Admin/Tips.php new file mode 100644 index 00000000..7897dfd7 --- /dev/null +++ b/app/controllers/Admin/Tips.php @@ -0,0 +1,46 @@ +findAll(); + // show($data); + $this->view('admin/tips', $data); + } + + public function update(string $a = '', string $b = '', string $c = ''): void + { + + $tips = new TipsModel(); + $tips->updateTip($_POST); + + redirect('admin/tips'); + } + + public function add(string $a = '', string $b = '', string $c = ''): void + { + + $tips = new TipsModel(); + $tips->addTip($_POST); + // show($tips); + + redirect('admin/tips'); + } + + public function delete(string $a = '', string $b = '', string $c = ''): void + { + + // show($a); + // show($b); + // show($c); + $tips = new TipsModel(); + $tips->delete($a, 'id'); + + redirect('admin/tips'); + } +} \ No newline at end of file diff --git a/app/controllers/Tips.php b/app/controllers/Tips.php index 89dc155a..b06c9197 100644 --- a/app/controllers/Tips.php +++ b/app/controllers/Tips.php @@ -15,6 +15,6 @@ public function update(string $a = '', string $b = '', string $c = ''): void $tips = new TipsModel(); $tips->updateTips($_POST); - redirect('profile'); //methana mokadda? + redirect('profile'); } } diff --git a/app/core/Database.php b/app/core/Database.php index eb608e9a..3b5203c9 100755 --- a/app/core/Database.php +++ b/app/core/Database.php @@ -19,7 +19,7 @@ public function query(string $query, array $data = []): ?array { return $result; } } - return null; + return []; } public function get_row(string $query, array $data = []): ?object { diff --git a/app/core/Model.php b/app/core/Model.php index 5b2592ef..f10cdf28 100755 --- a/app/core/Model.php +++ b/app/core/Model.php @@ -30,7 +30,7 @@ public function findAll():array{ } - public function where(array $data, array $data_not = []): array{ + public function where(array $data, array $data_not = []){ $keys = array_keys($data); $keys_not = array_keys($data_not); diff --git a/app/models/Tips.php b/app/models/Tips.php index 71b0ade5..7600b7fb 100644 --- a/app/models/Tips.php +++ b/app/models/Tips.php @@ -9,6 +9,8 @@ class TipsModel //'id', 'title', 'description', + 'author', + 'id', ]; public function validateCustomerSignup(array $data) @@ -25,18 +27,42 @@ public function validateCustomerSignup(array $data) return empty($this->errors); } - public function updateTips(array $data) + public function updateTip(array $data) { // $user = new UserModel; - $data['id'] = $_SESSION['USER']->id; + // $data['id'] = $_SESSION['USER']->id; + $id = $data['id']; + // alowed column + $data = array_filter($data, function ($key) { + return in_array($key, $this->allowedColumns); + }, ARRAY_FILTER_USE_KEY); + + // show($data); + return $this->update($id, $data, 'id'); + } + + public function addTip(array $data) + { + + // $user = new UserModel; + + // $data['id'] = $_SESSION['USER']->id; + $data['author'] = "admin"; // alowed column $data = array_filter($data, function ($key) { return in_array($key, $this->allowedColumns); }, ARRAY_FILTER_USE_KEY); - return $this->update($_SESSION['USER']->id, $data, 'id'); + return $this->insert($data); + } + + public function findAll(): array + { + $data['author'] = "admin"; + return $this->where($data); + return false; } } diff --git a/app/views/admin/layout/sidebar.php b/app/views/admin/layout/sidebar.php index 3b075063..6309e357 100755 --- a/app/views/admin/layout/sidebar.php +++ b/app/views/admin/layout/sidebar.php @@ -56,7 +56,7 @@