From 6f65c067dfc043de6a604e213aec853d844454f5 Mon Sep 17 00:00:00 2001 From: Gregorio Chiko Putra Date: Thu, 7 Sep 2017 10:39:54 +0700 Subject: [PATCH] Fix posts status issues --- App/Controllers/Posts.php | 47 +++++++++++++++++++++++---------------- Core/Model.php | 2 +- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/App/Controllers/Posts.php b/App/Controllers/Posts.php index eb1efb1..4579a35 100644 --- a/App/Controllers/Posts.php +++ b/App/Controllers/Posts.php @@ -19,27 +19,37 @@ class Posts $this->access = new Access(); } - public function checkExpired() - { - $date = new \DateTime(); - $now = $date->format("Y-m-d"); - - if ($expired = $this->post->showAll('expired_at', '<', $now)) { - foreach ($expired as $value) { - $this->post->update('pengumuman', ['status' => 0], $value['id']); - } - } - } - public function checkValid() { $date = new \DateTime(); - $date = $date->setTime(0,0); $now = $date->format("Y-m-d"); - if ($not_valid = $this->post->showAll('valid_at', '>', $now)) { - foreach ($not_valid as $value) { - $this->post->update('pengumuman', ['status' => 2], $value['id']); + $table = 'pengumuman'; + + $valid = $this->post->showAll('valid_at', '<=', $now); + if ($valid !== false) { + foreach ($valid as $fields) { + $id = $fields['id']; + + $this->post->update($table, ['status' => 1], $id); + } + } + + $not_valid = $this->post->showAll('valid_at', '>', $now); + if ($not_valid !== false) { + foreach ($not_valid as $fields) { + $id = $fields['id']; + + $this->post->update($table, ['status' => 2], $id); + } + } + + $expired = $this->post->showAll('expired_at', '<', $now); + if ($expired !== false) { + foreach ($expired as $fields) { + $id = $fields['id']; + + $this->post->update($table, ['status' => 0], $id); } } } @@ -47,7 +57,6 @@ class Posts public function index() { $this->checkValid(); - $this->checkExpired(); $posts = $this->post->showAll('status', '=', 1); @@ -162,9 +171,9 @@ class Posts if ($this->post->entry($table, $args)) { Session::flash('info', 'Data successfuly uploaded'); - if ($table = 'kategori') { + if ($table == 'kategori') { Redirect::to('/posts/category'); - } elseif ($table = 'pengumuman') { + } elseif ($table == 'pengumuman') { Redirect::to('/'); } } diff --git a/Core/Model.php b/Core/Model.php index 6140362..314a9b1 100644 --- a/Core/Model.php +++ b/Core/Model.php @@ -144,7 +144,7 @@ abstract class Model try { $db = static::connectDB(); - $result = $this->showAll(); + $result = $this->showAll('id', '=', $id); foreach ($result as $post) { foreach ($post as $key => $val) {