- Code design

- Passing data from users table
This commit is contained in:
2017-09-05 11:30:11 +07:00
parent 1381593163
commit 47d455063f
6 changed files with 139 additions and 86 deletions

View File

@@ -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);
}
}
}