- Code design
- Passing data from users table
This commit is contained in:
parent
1381593163
commit
47d455063f
@ -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) {
|
||||
if (is_array($id)) {
|
||||
$id = implode('', $id);
|
||||
$posts = $this->model->showSingle($id);
|
||||
$categories = $this->model->showCategories();
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
@ -3,14 +3,20 @@
|
||||
{% block title %}Edit Pengumuman{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<form method="post">
|
||||
{% for post in posts %}
|
||||
<form method="post">
|
||||
<h3>Pengumuman {{ post.id }}</h3>
|
||||
|
||||
<label for="category">Kategori: </label>
|
||||
<select name="category">
|
||||
{% for cat in categories %}
|
||||
<option value="{{ cat.id }}" {% if post.category == cat.id %}selected{% endif %}>{{ cat.category }}</option>
|
||||
<option value="{{ cat.id }}"
|
||||
{% if post.category == cat.id %}
|
||||
selected
|
||||
{% endif %}
|
||||
>
|
||||
{{ cat.category }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
|
||||
@ -22,17 +28,27 @@
|
||||
<br>
|
||||
|
||||
<label>Dibuat pada: </label>
|
||||
<span>{{ post.created_at }}</span>
|
||||
<span>
|
||||
{{ post.created_at }}
|
||||
</span>
|
||||
|
||||
<br>
|
||||
|
||||
<label>Dibuat oleh: </label>
|
||||
<span>{{ post.creator }}</span>
|
||||
<span>
|
||||
{% for user in users %}
|
||||
{% if post.creator == user.id %}
|
||||
{{ user.name }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</span>
|
||||
|
||||
<br>
|
||||
|
||||
<label for="expired_at">Berlaku sampai: </label>
|
||||
<span>{{ post.expired_at }}</span>
|
||||
<span>
|
||||
{{ post.expired_at }}
|
||||
</span>
|
||||
|
||||
<br>
|
||||
|
||||
@ -53,26 +69,47 @@
|
||||
{% if post.editor == "0" %}
|
||||
-
|
||||
{% else %}
|
||||
{{ post.editor }}
|
||||
{% for user in users %}
|
||||
{% if post.editor == user.id %}
|
||||
{{ user.name }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</span>
|
||||
<input type="hidden" name="editor" value="3"><!-- User -->
|
||||
|
||||
<br>
|
||||
|
||||
<label>Status: </label>
|
||||
<span>
|
||||
{% if post.status == 0 %}
|
||||
Nonaktif
|
||||
{% else %}
|
||||
Aktif
|
||||
{% endif %}
|
||||
</span>
|
||||
|
||||
<!-- Current Timestamp -->
|
||||
<input type="hidden" name="_currts" value="{{ timestamp }}" disabled>
|
||||
{% endfor %}
|
||||
|
||||
<!-- Method -->
|
||||
<input type="hidden" name="_method" value="put">
|
||||
|
||||
<!-- Token -->
|
||||
<input type="hidden" name="_token" value={{ token }}>
|
||||
<input type="hidden" name="_token" value="{{ token }}">
|
||||
|
||||
<br>
|
||||
|
||||
<button type="submit">Edit</button>
|
||||
<button type="submit"
|
||||
{% if post.status == 0 %}
|
||||
disabled
|
||||
{% endif %}
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{% if post.status == 1 %}
|
||||
<form method="post">
|
||||
<input type="hidden" name="id" value="{{ post.id }}">
|
||||
|
||||
@ -83,6 +120,7 @@
|
||||
<input type="hidden" name="_token" value="{{ token }}">
|
||||
|
||||
<button type="submit">Nonaktif</button>
|
||||
{% endfor %}
|
||||
</form>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
|
@ -7,7 +7,14 @@
|
||||
|
||||
{% for post in posts %}
|
||||
<h3>Pengumuman {{ post.id }}</h3>
|
||||
<a href="/posts/edit/{{ post.id }}">Edit</a>
|
||||
<a href="/posts/edit/{{ post.id }}">
|
||||
{% if post.status == 1 %}
|
||||
Edit
|
||||
</a>
|
||||
{% else %}
|
||||
<strike>Edit</strike>
|
||||
</a> (Nonaktif)
|
||||
{% endif %}
|
||||
<p>{{ post.content }}</p>
|
||||
{% endfor %}
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user