diff --git a/App/Controllers/Posts.php b/App/Controllers/Posts.php index 2cccbdd..5481b11 100644 --- a/App/Controllers/Posts.php +++ b/App/Controllers/Posts.php @@ -3,21 +3,24 @@ namespace App\Controllers; use \Core\View; use App\Models\Post; +use App\Models\Access; use \Core\Token; use \Core\Session; class Posts { - private $model; + private $post, + $access; public function __construct() { - $this->model = new Post(); + $this->post = new Post(); + $this->access = new Access(); } public function index() { - $posts = $this->model->showAll(); + $posts = $this->post->showAll(); // 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 @@ -27,7 +30,7 @@ class Posts public function entry() { - $categories = $this->model->showCategories(); + $categories = $this->post->showCategories(); // echo "You can entry new data here."; // Nanti di replace sama twig view ke App\Views\Data\entry_pengumuman.html View::render('Data/entry_pengumuman.html', [ 'categories' => $categories, @@ -36,12 +39,15 @@ class Posts return true; } - public function edit($id = []) + public function edit($id) { if ($id) { - $id = implode('', $id); - $posts = $this->model->showSingle($id); - $categories = $this->model->showCategories(); + if (is_array($id)) { + $id = implode('', $id); + } + $posts = $this->post->showSingle($id); + $categories = $this->post->showCategories(); + $users = $this->access->showSingle($id); $date = new \DateTime(); $timestamp = $date->format("Y/m/d H:i:s"); // echo "You can edit exists data with id $id here"; // Nanti di replace sama twig view ke App\Views\Data\edit_pengumuman.html @@ -50,6 +56,7 @@ class Posts [ 'posts' => $posts, 'categories' => $categories, + 'users' => $users, 'timestamp' => $timestamp, 'token' => Token::generate() ] @@ -63,7 +70,7 @@ class Posts public function post($args = []) { $table = 'pengumuman'; - if ($this->model->entry($table, $args)) { + if ($this->post->entry($table, $args)) { Session::flash('info', 'Data successfuly uploaded'); return $this->index(); } @@ -76,7 +83,7 @@ class Posts unset($args['id']); if ($this->post->update($table, $args, $id)) { Session::flash('info', 'Data successfuly updated'); - return $this->edit(); + return $this->edit($id); } } @@ -86,7 +93,7 @@ class Posts $id = $args['id']; if ($this->post->delete($table, $id)) { Session::flash('info', 'Data successfuly removed'); - return \Core\Redirect::to('/'); + return $this->edit($id); } } } diff --git a/App/Models/Access.php b/App/Models/Access.php index 09a9942..3e959ac 100644 --- a/App/Models/Access.php +++ b/App/Models/Access.php @@ -11,6 +11,7 @@ class Access extends \Core\Model 'id int(3) NOT NULL AUTO_INCREMENT', 'username varchar(25) NOT NULL', 'password char(13)', + 'name varchar(50)', 'registered_at timestamp DEFAULT CURRENT_TIMESTAMP', 'PRIMARY KEY (id)' ] @@ -22,7 +23,7 @@ class Access extends \Core\Model try { $db = static::connectDB(); - $sql = "SELECT id, username, registered_at FROM user"; + $sql = "SELECT id, username, name, registered_at FROM user"; if ($stmt = $db->query($sql)) { $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); @@ -39,14 +40,14 @@ class Access extends \Core\Model try { $db = static::connectDB(); - $sql = "SELECT id, username, registered_at FROM user WHERE id = ?"; + $sql = "SELECT id, username, name, registered_at FROM user WHERE id = ?"; $query = $db->prepare($sql); if ($query->execute([$id])) { if ($query->rowCount() === 1) { $result = $query->fetchAll(\PDO::FETCH_ASSOC); - return true; + return $result; } } return false; diff --git a/App/Models/Post.php b/App/Models/Post.php index 3ceaa4f..71341e7 100644 --- a/App/Models/Post.php +++ b/App/Models/Post.php @@ -37,10 +37,9 @@ class Post extends \Core\Model try { $db = static::connectDB(); - $sql = "SELECT * FROM pengumuman WHERE status = ? ORDER BY created_at"; + $sql = "SELECT * FROM pengumuman ORDER BY created_at"; $query = $db->prepare($sql); - $query->bindValue(1, 1); if ($query->execute()) { if ($query->rowCount() != 0) { diff --git a/App/Views/Data/edit_pengumuman.html b/App/Views/Data/edit_pengumuman.html index f06435c..6abf107 100644 --- a/App/Views/Data/edit_pengumuman.html +++ b/App/Views/Data/edit_pengumuman.html @@ -3,86 +3,124 @@ {% block title %}Edit Pengumuman{% endblock %} {% block body %} -
+ + {% if post.status == 1 %} + {% endif %} - - - -{{ post.content }}
{% endfor %} diff --git a/Core/Redirect.php b/Core/Redirect.php index 7f39831..d7f2e60 100644 --- a/Core/Redirect.php +++ b/Core/Redirect.php @@ -8,8 +8,9 @@ class Redirect if($url) { $url = htmlspecialchars($url); - $url = rtrim($url, '/'); + $url = substr_replace($url, '', 0, 1); + var_dump($url); header("Location:$url"); return true;