diff --git a/App/Controllers/Home.php b/App/Controllers/Home.php index a243dc2..02736d0 100644 --- a/App/Controllers/Home.php +++ b/App/Controllers/Home.php @@ -31,10 +31,6 @@ class Home Session::flash('info', 'Anda telah masuk'); Redirect::to('/'); die(); - } else { - View::render('Access/login.html', [ - 'token' => Token::generate() - ]); } // Login @@ -69,17 +65,21 @@ class Home Session::put('userid', $user['id']); Session::put('username', $user['username']); Session::put('full_name', $user['full_name']); - Session::put('privilage', $user['privilage']); + Session::put('privilege', $user['privilege']); $info = "Berhasil masuk"; + Session::flash('info', $info); + Redirect::to('/'); + die(); } } } } } - Session::flash('info', $info); - Redirect::to('/'); - die(); + } else { + View::render('Access/login.html', [ + 'token' => Token::generate() + ]); } } @@ -90,7 +90,7 @@ class Home Session::delete('userid'); Session::delete('username'); Session::delete('full_name'); - Session::delete('privilage'); + Session::delete('privilege'); $info = "Berhasil keluar"; } @@ -100,7 +100,7 @@ class Home public function register() { - if (Session::exists('userid') && Session::get('privilage') == 1) { + if (Session::exists('userid') && Session::get('privilege') == 1) { View::render('Access/registrasi.html', [ 'token' => Token::generate() ]); diff --git a/App/Controllers/Posts.php b/App/Controllers/Posts.php index 71fbd00..d099c7c 100644 --- a/App/Controllers/Posts.php +++ b/App/Controllers/Posts.php @@ -25,7 +25,7 @@ class Posts $valid = $this->model->showAll([ ['valid_at', '<=', $now], - ['status', '!=', 3] + ['status', '!=', 0] ]); if ($valid) { foreach ($valid as $fields) { @@ -40,7 +40,7 @@ class Posts $not_valid = $this->model->showAll([ ['valid_at', '>', $now], - ['status', '!=', 3] + ['status', '!=', 0] ]); if ($not_valid) { foreach ($not_valid as $fields) { @@ -55,7 +55,7 @@ class Posts $expired = $this->model->showAll([ ['expired_at', '<', $now], - ['status', '!=', 3] + ['status', '!=', 0] ]); if ($expired) { foreach ($expired as $fields) { @@ -70,7 +70,7 @@ class Posts } /* Routes */ - public function index() + public function index($args = '') { $this->checkValid(); @@ -83,11 +83,16 @@ class Posts $url = 'Data/pengumuman.html'; $status = ''; - $privilage = ''; + $privilege = ''; if (Session::exists('userid')) { $post = $this->model->showAll(); - $privilage = Session::get('privilage'); + if ($args != '') { + $post = $this->model->showAll([ + ['status', '=', $args] + ]); + } + $privilege = Session::get('privilege'); $status = 'loggedin'; } @@ -106,7 +111,8 @@ class Posts View::render($url, [ 'posts' => $posts, 'status' => $status, - 'privilage' => $privilage + 'privilege' => $privilege, + 'token' => Token::generate() ]); } @@ -204,7 +210,7 @@ class Posts public function category() { if (Session::exists('userid')) { - if (Session::get('privilage') != 1) { + if (Session::get('privilege') != 1) { Session::flash('info', 'Hanya admin yang bisa mengatur kategori'); Redirect::to('/'); die(); @@ -266,12 +272,13 @@ class Posts die(); } - public function put($args = []) + public function put($args) { if (isset($args['_addon'])) { $table = $args['_addon']; + unset($args['_addon']); - $this->model->update(['status' => 1], $args['id'], $table); + $this->model->update($args, $args['id'], $table); Session::flash('info', 'Data berhasil diaktifkan'); Redirect::to('/posts/category'); @@ -285,32 +292,37 @@ class Posts unset($args['id']); // Check if data same with old data - $old_data = [ - $args['old_category'], - $args['old_content'], - $args['old_valid_at'], - $args['old_expired_at'] - ]; - $new_data = [ - $args['category'], - $args['content'], - $args['valid_at'], - $args['expired_at'] - ]; - - if ($old_data == $new_data) { - Session::flash('info', 'Tidak ada data yang diubah'); - Redirect::to("./$id"); - die(); - } - $keys = array_keys($args); + $old_data = []; + if ($matches = preg_grep('/^old_/', $keys)) { foreach ($matches as $match) { + $old_data[] = $args[$match]; unset($args[$match]); } + $new_data = [ + $args['category'], + $args['content'], + $args['valid_at'], + $args['expired_at'] + ]; + if ($old_data == $new_data) { + Session::flash('info', 'Tidak ada data yang diubah'); + Redirect::to("./$id"); + die(); + } } + foreach ($args as $key => $val) { + if (strpos($val, "##date##") !== false) { + $date = new \DateTime(); + $now = $date->format("Y-m-d"); + $args[$key] = $now; + } + } + + var_dump($args); + if ($this->model->update($args, $id)) { Session::flash('info', 'Data berhasil diperbarui'); Redirect::to('/'); @@ -329,11 +341,12 @@ class Posts } $id = $args['id']; + unset($args['id']); - if ($table) { - $delete = $this->model->delete($id, 0, $table); + if (isset($table)) { + $delete = $this->model->update($args, $id, $table); } else { - $delete = $this->model->delete($id); + $delete = $this->model->update($args, $id); } if ($delete == true) { @@ -344,7 +357,7 @@ class Posts Session::flash('info', $info); - if ($table) { + if (isset($table)) { Redirect::to("/posts/category"); } else { Redirect::to('/'); diff --git a/App/Models/Access.php b/App/Models/Access.php index a1c90fd..a5dcb74 100644 --- a/App/Models/Access.php +++ b/App/Models/Access.php @@ -13,7 +13,7 @@ class Access extends \Core\Model 'salt char(23) NOT NULL', 'full_name varchar(50) NOT NULL', 'registered_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP', - 'privilage tinyint(1) NOT NULL DEFAULT 0', + 'privilege tinyint(1) NOT NULL DEFAULT 0', 'status tinyint(1) NOT NULL DEFAULT 0', 'max_user int(1) NOT NULL DEFAULT 5', 'PRIMARY KEY (id)' diff --git a/App/Models/Post.php b/App/Models/Post.php index 2836ceb..b4d6e59 100644 --- a/App/Models/Post.php +++ b/App/Models/Post.php @@ -130,31 +130,22 @@ class Post extends \Core\Model $x = 1; foreach ($args as $field) { - // Setting the query for multiple rows - if (is_array($field)) { - foreach ($field as $fields) { - $values .= '(?)'; - if ($x < count($field)) { - $values .= ', '; - } - $x++; - } - } else { - if ($x === 1) { - $values .= '('; - } - $values .= '?'; - if ($x < count($args)) { - $values .= ', '; - } else { - $values .= ')'; - } - $x++; + if ($x === 1) { + $values .= '('; } + $values .= '?'; + if ($x < count($args)) { + $values .= ', '; + } else { + $values .= ')'; + } + $x++; } try { $sql = "INSERT INTO {$table} ({$keys}) VALUES {$values}"; + var_dump($sql); + var_dump($args); $db = static::connectDB(); diff --git a/App/Views/Access/login.html b/App/Views/Access/login.html index 2e7bb21..20c65c0 100644 --- a/App/Views/Access/login.html +++ b/App/Views/Access/login.html @@ -19,7 +19,9 @@