diff --git a/App/Controllers/Home.php b/App/Controllers/Home.php index 19d288d..66e6050 100644 --- a/App/Controllers/Home.php +++ b/App/Controllers/Home.php @@ -41,4 +41,11 @@ class Home } } } + + public function logout() + { + if ($this->access->logout()) { + Redirect::to('/'); + } + } } diff --git a/App/Controllers/Posts.php b/App/Controllers/Posts.php index 8580831..701c350 100644 --- a/App/Controllers/Posts.php +++ b/App/Controllers/Posts.php @@ -20,10 +20,17 @@ class Posts public function index() { - $posts = $this->post->showAll(); + $posts = $this->post->showAll(['status' => 1]); + $url = 'Data/pengumuman.html'; + $status = ''; // echo "This is index of posts."; // Nanti di replace sama twig view ke App\Views\Data\pengumuman.html - View::render('Data/pengumuman.html', [ - 'posts' => $posts + if (Session::exists('userid')) { + $posts = $this->post->showAll(); + $status = 'admin'; + } + View::render($url, [ + 'posts' => $posts, + 'status' => $status ]); return true; } diff --git a/App/Models/Access.php b/App/Models/Access.php index 20f0bde..5592481 100644 --- a/App/Models/Access.php +++ b/App/Models/Access.php @@ -99,4 +99,21 @@ class Access extends \Core\Model echo $e->getMessage(); } } + + public function logout() + { + $id = \Core\Session::get('userid'); + if ($this->update( + 'user', + ['status' => 0], + $id + )) { + \Core\Session::delete('userid'); + \Core\Session::delete('username'); + \Core\Session::delete('name'); + + return true; + } + return false; + } } diff --git a/App/Models/Post.php b/App/Models/Post.php index a5197fa..97c4038 100644 --- a/App/Models/Post.php +++ b/App/Models/Post.php @@ -32,15 +32,24 @@ class Post extends \Core\Model ); } - public function showAll() + public function showAll($conds = []) { try { $db = static::connectDB(); - $sql = "SELECT * FROM pengumuman ORDER BY created_at"; + $sql = "SELECT * FROM pengumuman"; + + if ($conds) { + $key = implode('', array_keys($conds)); + $sql .= " WHERE {$key} = ?"; + } $query = $db->prepare($sql); + if ($conds) { + $query->bindValue(1, implode('', array_values($conds))); + } + if ($query->execute()) { if ($query->rowCount() != 0) { $result = $query->fetchAll(\PDO::FETCH_ASSOC); diff --git a/App/Views/Data/pengumuman.html b/App/Views/Data/pengumuman.html index 5c43aaf..1eac1e6 100644 --- a/App/Views/Data/pengumuman.html +++ b/App/Views/Data/pengumuman.html @@ -5,18 +5,23 @@ {% block body %}

List Pengumuman

-{% for post in posts %} -

Pengumuman {{ post.id }}

- -{% if post.status == 1 %} - Edit - -{% else %} - Edit - (Nonaktif) -{% endif %} -

{{ post.content }}

-{% endfor %} + {% for post in posts %} +

Pengumuman {{ post.id }}

+ {% if status %} + + {% if post.status == 1 %} + Edit + + {% else %} + Edit + (Nonaktif) + {% endif %} + {% endif %} +

{{ post.content }}

+ {% endfor %} -+ Tambah Pengumuman + {% if status %} + + Tambah Pengumuman + Logout + {% endif %} {% endblock %}