- Function to store and get worked good
- Token works great - Session works great
This commit is contained in:
parent
45dbe24da7
commit
c64064321e
@ -4,6 +4,7 @@ namespace App\Controllers;
|
||||
use \Core\View;
|
||||
use App\Models\Post;
|
||||
use \Core\Token;
|
||||
use \Core\Session;
|
||||
|
||||
class Posts
|
||||
{
|
||||
@ -27,34 +28,26 @@ class Posts
|
||||
public function entry()
|
||||
{
|
||||
$categories = $this->model->showCategories();
|
||||
$date = new \DateTime();
|
||||
$timestamp = $date->format('Y/m/d H:i:s');
|
||||
// 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,
|
||||
'timestamp' => $timestamp,
|
||||
'token' => Token::generate()
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function edit($id = null)
|
||||
public function edit($id = [])
|
||||
{
|
||||
if ($id) {
|
||||
$id = implode('', $id);
|
||||
$posts = $this->model->showSingle($id);
|
||||
$date = new \DateTime();
|
||||
$timestamp = $date->format('Y/m/d H:i:s');
|
||||
$categories = $this->model->showCategories();
|
||||
// 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',
|
||||
[
|
||||
'category' => $posts['category'],
|
||||
'content' => $posts['content'],
|
||||
'created_at' => $posts['created_at'],
|
||||
'creator' => $posts['creator'],
|
||||
'edited_at' => $posts['edited_at'],
|
||||
'editor' => $posts['editor'],
|
||||
'timestamp' => $timestamp,
|
||||
'posts' => $posts,
|
||||
'categories' => $categories,
|
||||
'token' => Token::generate()
|
||||
]
|
||||
);
|
||||
@ -62,4 +55,14 @@ class Posts
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Methods
|
||||
public function post($args = [])
|
||||
{
|
||||
$table = 'pengumuman';
|
||||
if ($this->model->entry($table, $args)) {
|
||||
Session::flash('info', 'Data successfuly uploaded');
|
||||
return $this->index();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,43 +4,44 @@
|
||||
|
||||
{% block body %}
|
||||
<form method="post">
|
||||
<h3>Pengumuman 1</h3>
|
||||
{% for post in posts %}
|
||||
<h3>Pengumuman {{ post.id }}</h3>
|
||||
|
||||
<label for="category">Kategori: </label>
|
||||
<select name="category">
|
||||
<option value="1" {% if category == 1 %}selected{% endif %}>Category 1</option>
|
||||
<option value="2" {% if category == 2 %}selected{% endif %}>Category 2</option>
|
||||
<option value="3" {% if category == 3 %}selected{% endif %}>Category 3</option>
|
||||
<option value="4" {% if category == 4 %}selected{% endif %}>Category 4</option>
|
||||
{% for cat in categories %}
|
||||
<option value="{{ cat.id }}" {% if post.category == cat.id %}selected{% endif %}>{{ cat.category }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
|
||||
<br>
|
||||
|
||||
<label for="konten">Konten: </label>
|
||||
<textarea name="konten" rows="3" cols="30">{{ content }}</textarea>
|
||||
<textarea name="konten" rows="3" cols="30">{{ post.content }}</textarea>
|
||||
|
||||
<br>
|
||||
|
||||
<label for="created_at">Dibuat pada: </label>
|
||||
<input type="text" name="created_at" value={{ created_at }} disabled>
|
||||
<input type="text" name="created_at" value="{{ post.created_at }}" readonly>
|
||||
|
||||
<br>
|
||||
|
||||
<label for="creator">Dibuat oleh: </label>
|
||||
<input type="text" name="creator" value="User {{ creator }}" disabled>
|
||||
<input type="text" name="creator" value="{{ post.creator }}" readonly>
|
||||
|
||||
<br>
|
||||
|
||||
<label for="edited_at">Diubah pada: </label>
|
||||
<input type="text" name="edited_at" value={{ edited_at }} disabled>
|
||||
<input type="text" name="edited_at" value="{{ post.edited_at }}" readonly>
|
||||
|
||||
<br>
|
||||
|
||||
<label for="editor">Diubah oleh: </label>
|
||||
<input type="text" name="editor" value="User {{ editor }}" disabled>
|
||||
<input type="text" name="editor" value="{{ post.editor }}" readonly>
|
||||
|
||||
<!-- Current Timestamp -->
|
||||
<input type="hidden" name="_currts" value={{ timestamp }}>
|
||||
<input type="hidden" name="_currts" value="{{ timestamp }}" disabled>
|
||||
{% endfor %}
|
||||
|
||||
<!-- Method -->
|
||||
<input type="hidden" name="_method" value="put">
|
||||
|
@ -6,8 +6,8 @@
|
||||
<form method="post">
|
||||
<h3>Tambah Pengumuman</h3>
|
||||
|
||||
<label for="kategori">Kategori</label>
|
||||
<select name="kategori">
|
||||
<label for="category">Kategori</label>
|
||||
<select name="category">
|
||||
{% for cat in categories %}
|
||||
<option value="{{ cat.id }}">{{ cat.category }}</option>
|
||||
{% endfor %}
|
||||
@ -15,20 +15,27 @@
|
||||
|
||||
<br>
|
||||
|
||||
<label for="konten">Konten</label>
|
||||
<textarea name="konten" rows="3" cols="30"></textarea>
|
||||
<label for="expired_at">Berlaku sampai</label>
|
||||
<input type="date" name="expired_at" value="">
|
||||
|
||||
<br>
|
||||
|
||||
<label for="content">Konten</label>
|
||||
<textarea name="content" rows="3" cols="30"></textarea>
|
||||
|
||||
<input type="hidden" name="creator" value="1"> <!-- Nanti diganti user id -->
|
||||
|
||||
<!-- Current Timestamp -->
|
||||
<input type="hidden" name="_currts" value={{ timestamp }}>
|
||||
<input type="hidden" name="_currts" value="{{ timestamp }}" disabled>
|
||||
|
||||
<!-- Method -->
|
||||
<input type="hidden" name="_method" value="post">
|
||||
|
||||
<!-- Token -->
|
||||
<input type="hidden" name="_token" value={{ token }}>
|
||||
<input type="hidden" name="_token" value="{{ token }}">
|
||||
|
||||
<br>
|
||||
|
||||
<button type="submit" name="entry">Entry</button>
|
||||
<button type="submit">Entry</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
@ -66,11 +66,29 @@ class Router
|
||||
$action = $this->convertToCamelCaps($action);
|
||||
|
||||
if (is_callable([$object, $action])) {
|
||||
if (array_key_exists('id', $this->params)) {
|
||||
$var = $this->params['id'];
|
||||
return $object->$action($var);
|
||||
// Check if there's input to the current page
|
||||
if (Input::exists('post')) {
|
||||
$var = $_POST;
|
||||
// Check the token
|
||||
if (Token::check($var['_token'])) {
|
||||
// Get the method
|
||||
$action = $var['_method'];
|
||||
} else {
|
||||
// Token invalid
|
||||
$flash = Session::flash('info', 'Token invalid, try again');
|
||||
die($flash);
|
||||
}
|
||||
unset($var['_token']);
|
||||
unset($var['_method']);
|
||||
}
|
||||
if (array_key_exists('id', $this->params)) {
|
||||
$var[] = $this->params['id'];
|
||||
}
|
||||
if (isset($var)) {
|
||||
return $object->$action($var);
|
||||
} else {
|
||||
return $object->$action();
|
||||
}
|
||||
return $object->$action();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,13 @@
|
||||
<?php
|
||||
session_start();
|
||||
// Autoload
|
||||
require_once dirname(__DIR__).'/vendor/autoload.php';
|
||||
|
||||
if (Core\Session::exists('info')) {
|
||||
echo Core\Session::flash('info');
|
||||
echo "<br>";
|
||||
}
|
||||
|
||||
$router = new Core\Router();
|
||||
|
||||
$router->add('', ['controller' => 'posts', 'action' => 'index']);
|
||||
|
Loading…
Reference in New Issue
Block a user