- Code design

- Passing data from users table
This commit is contained in:
Gregorio Chiko Putra 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 \Core\View;
use App\Models\Post; use App\Models\Post;
use App\Models\Access;
use \Core\Token; use \Core\Token;
use \Core\Session; use \Core\Session;
class Posts class Posts
{ {
private $model; private $post,
$access;
public function __construct() public function __construct()
{ {
$this->model = new Post(); $this->post = new Post();
$this->access = new Access();
} }
public function index() 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 // echo "This is index of posts."; // Nanti di replace sama twig view ke App\Views\Data\pengumuman.html
View::render('Data/pengumuman.html', [ View::render('Data/pengumuman.html', [
'posts' => $posts 'posts' => $posts
@ -27,7 +30,7 @@ class Posts
public function entry() 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 // 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', [ View::render('Data/entry_pengumuman.html', [
'categories' => $categories, 'categories' => $categories,
@ -36,12 +39,15 @@ class Posts
return true; return true;
} }
public function edit($id = []) public function edit($id)
{ {
if ($id) { if ($id) {
if (is_array($id)) {
$id = implode('', $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(); $date = new \DateTime();
$timestamp = $date->format("Y/m/d H:i:s"); $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 // 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, 'posts' => $posts,
'categories' => $categories, 'categories' => $categories,
'users' => $users,
'timestamp' => $timestamp, 'timestamp' => $timestamp,
'token' => Token::generate() 'token' => Token::generate()
] ]
@ -63,7 +70,7 @@ class Posts
public function post($args = []) public function post($args = [])
{ {
$table = 'pengumuman'; $table = 'pengumuman';
if ($this->model->entry($table, $args)) { if ($this->post->entry($table, $args)) {
Session::flash('info', 'Data successfuly uploaded'); Session::flash('info', 'Data successfuly uploaded');
return $this->index(); return $this->index();
} }
@ -76,7 +83,7 @@ class Posts
unset($args['id']); unset($args['id']);
if ($this->post->update($table, $args, $id)) { if ($this->post->update($table, $args, $id)) {
Session::flash('info', 'Data successfuly updated'); Session::flash('info', 'Data successfuly updated');
return $this->edit(); return $this->edit($id);
} }
} }
@ -86,7 +93,7 @@ class Posts
$id = $args['id']; $id = $args['id'];
if ($this->post->delete($table, $id)) { if ($this->post->delete($table, $id)) {
Session::flash('info', 'Data successfuly removed'); Session::flash('info', 'Data successfuly removed');
return \Core\Redirect::to('/'); return $this->edit($id);
} }
} }
} }

View File

@ -11,6 +11,7 @@ class Access extends \Core\Model
'id int(3) NOT NULL AUTO_INCREMENT', 'id int(3) NOT NULL AUTO_INCREMENT',
'username varchar(25) NOT NULL', 'username varchar(25) NOT NULL',
'password char(13)', 'password char(13)',
'name varchar(50)',
'registered_at timestamp DEFAULT CURRENT_TIMESTAMP', 'registered_at timestamp DEFAULT CURRENT_TIMESTAMP',
'PRIMARY KEY (id)' 'PRIMARY KEY (id)'
] ]
@ -22,7 +23,7 @@ class Access extends \Core\Model
try { try {
$db = static::connectDB(); $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)) { if ($stmt = $db->query($sql)) {
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC); $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
@ -39,14 +40,14 @@ class Access extends \Core\Model
try { try {
$db = static::connectDB(); $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); $query = $db->prepare($sql);
if ($query->execute([$id])) { if ($query->execute([$id])) {
if ($query->rowCount() === 1) { if ($query->rowCount() === 1) {
$result = $query->fetchAll(\PDO::FETCH_ASSOC); $result = $query->fetchAll(\PDO::FETCH_ASSOC);
return true; return $result;
} }
} }
return false; return false;

View File

@ -37,10 +37,9 @@ class Post extends \Core\Model
try { try {
$db = static::connectDB(); $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 = $db->prepare($sql);
$query->bindValue(1, 1);
if ($query->execute()) { if ($query->execute()) {
if ($query->rowCount() != 0) { if ($query->rowCount() != 0) {

View File

@ -3,14 +3,20 @@
{% block title %}Edit Pengumuman{% endblock %} {% block title %}Edit Pengumuman{% endblock %}
{% block body %} {% block body %}
<form method="post">
{% for post in posts %} {% for post in posts %}
<form method="post">
<h3>Pengumuman {{ post.id }}</h3> <h3>Pengumuman {{ post.id }}</h3>
<label for="category">Kategori: </label> <label for="category">Kategori: </label>
<select name="category"> <select name="category">
{% for cat in categories %} {% 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 %} {% endfor %}
</select> </select>
@ -22,17 +28,27 @@
<br> <br>
<label>Dibuat pada: </label> <label>Dibuat pada: </label>
<span>{{ post.created_at }}</span> <span>
{{ post.created_at }}
</span>
<br> <br>
<label>Dibuat oleh: </label> <label>Dibuat oleh: </label>
<span>{{ post.creator }}</span> <span>
{% for user in users %}
{% if post.creator == user.id %}
{{ user.name }}
{% endif %}
{% endfor %}
</span>
<br> <br>
<label for="expired_at">Berlaku sampai: </label> <label for="expired_at">Berlaku sampai: </label>
<span>{{ post.expired_at }}</span> <span>
{{ post.expired_at }}
</span>
<br> <br>
@ -53,27 +69,48 @@
{% if post.editor == "0" %} {% if post.editor == "0" %}
- -
{% else %} {% else %}
{{ post.editor }} {% for user in users %}
{% if post.editor == user.id %}
{{ user.name }}
{% endif %}
{% endfor %}
{% endif %} {% endif %}
</span> </span>
<input type="hidden" name="editor" value="3"><!-- User --> <input type="hidden" name="editor" value="3"><!-- User -->
<br>
<label>Status: </label>
<span>
{% if post.status == 0 %}
Nonaktif
{% else %}
Aktif
{% endif %}
</span>
<!-- Current Timestamp --> <!-- Current Timestamp -->
<input type="hidden" name="_currts" value="{{ timestamp }}" disabled> <input type="hidden" name="_currts" value="{{ timestamp }}" disabled>
{% endfor %}
<!-- Method --> <!-- Method -->
<input type="hidden" name="_method" value="put"> <input type="hidden" name="_method" value="put">
<!-- Token --> <!-- Token -->
<input type="hidden" name="_token" value={{ token }}> <input type="hidden" name="_token" value="{{ token }}">
<br> <br>
<button type="submit">Edit</button> <button type="submit"
</form> {% if post.status == 0 %}
disabled
{% endif %}
>
Edit
</button>
</form>
<form method="post"> {% if post.status == 1 %}
<form method="post">
<input type="hidden" name="id" value="{{ post.id }}"> <input type="hidden" name="id" value="{{ post.id }}">
<!-- Method --> <!-- Method -->
@ -83,6 +120,7 @@
<input type="hidden" name="_token" value="{{ token }}"> <input type="hidden" name="_token" value="{{ token }}">
<button type="submit">Nonaktif</button> <button type="submit">Nonaktif</button>
</form>
{% endif %}
{% endfor %} {% endfor %}
</form>
{% endblock %} {% endblock %}

View File

@ -7,7 +7,14 @@
{% for post in posts %} {% for post in posts %}
<h3>Pengumuman {{ post.id }}</h3> <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> <p>{{ post.content }}</p>
{% endfor %} {% endfor %}

View File

@ -8,8 +8,9 @@ class Redirect
if($url) if($url)
{ {
$url = htmlspecialchars($url); $url = htmlspecialchars($url);
$url = rtrim($url, '/'); $url = rtrim($url, '/');
$url = substr_replace($url, '', 0, 1);
var_dump($url);
header("Location:$url"); header("Location:$url");
return true; return true;