lepisi-pengumuman/App/Controllers/Posts.php

100 lines
2.7 KiB
PHP

<?php
namespace App\Controllers;
use \Core\View;
use App\Models\Post;
use App\Models\Access;
use \Core\Token;
use \Core\Session;
class Posts
{
private $post,
$access;
public function __construct()
{
$this->post = new Post();
$this->access = new Access();
}
public function index()
{
$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
]);
return true;
}
public function entry()
{
$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,
'token' => Token::generate()
]);
return true;
}
public function edit($id)
{
if ($id) {
if (is_array($id)) {
$id = implode('', $id);
}
$post = $this->post->showSingle($id);
$categories = $this->post->showCategories();
$user = $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
View::render(
'Data/edit_pengumuman.html',
[
'post' => $post,
'categories' => $categories,
'user' => $user,
'timestamp' => $timestamp,
'token' => Token::generate()
]
);
return true;
}
return false;
}
// Methods
public function post($args = [])
{
$table = 'pengumuman';
if ($this->post->entry($table, $args)) {
Session::flash('info', 'Data successfuly uploaded');
return $this->index();
}
}
public function put($args = [])
{
$table = 'pengumuman';
$id = $args['id'];
unset($args['id']);
if ($this->post->update($table, $args, $id)) {
Session::flash('info', 'Data successfuly updated');
return $this->edit($id);
}
}
public function delete($args = [])
{
$table = 'pengumuman';
$id = $args['id'];
if ($this->post->delete($table, $id)) {
Session::flash('info', 'Data successfuly removed');
return $this->edit($id);
}
}
}