- Function to store and get worked good

- Token works great
- Session works great
This commit is contained in:
Gregorio Chiko Putra 2017-09-05 09:53:52 +07:00
parent 45dbe24da7
commit c64064321e
5 changed files with 70 additions and 35 deletions

View File

@ -4,6 +4,7 @@ namespace App\Controllers;
use \Core\View; use \Core\View;
use App\Models\Post; use App\Models\Post;
use \Core\Token; use \Core\Token;
use \Core\Session;
class Posts class Posts
{ {
@ -27,34 +28,26 @@ class Posts
public function entry() public function entry()
{ {
$categories = $this->model->showCategories(); $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 // 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,
'timestamp' => $timestamp,
'token' => Token::generate() 'token' => Token::generate()
]); ]);
return true; return true;
} }
public function edit($id = null) public function edit($id = [])
{ {
if ($id) { if ($id) {
$id = implode('', $id);
$posts = $this->model->showSingle($id); $posts = $this->model->showSingle($id);
$date = new \DateTime(); $categories = $this->model->showCategories();
$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
View::render( View::render(
'Data/edit_pengumuman.html', 'Data/edit_pengumuman.html',
[ [
'category' => $posts['category'], 'posts' => $posts,
'content' => $posts['content'], 'categories' => $categories,
'created_at' => $posts['created_at'],
'creator' => $posts['creator'],
'edited_at' => $posts['edited_at'],
'editor' => $posts['editor'],
'timestamp' => $timestamp,
'token' => Token::generate() 'token' => Token::generate()
] ]
); );
@ -62,4 +55,14 @@ class Posts
} }
return false; return false;
} }
// Methods
public function post($args = [])
{
$table = 'pengumuman';
if ($this->model->entry($table, $args)) {
Session::flash('info', 'Data successfuly uploaded');
return $this->index();
}
}
} }

View File

@ -4,43 +4,44 @@
{% block body %} {% block body %}
<form method="post"> <form method="post">
<h3>Pengumuman 1</h3> {% for post in posts %}
<h3>Pengumuman {{ post.id }}</h3>
<label for="category">Kategori: </label> <label for="category">Kategori: </label>
<select name="category"> <select name="category">
<option value="1" {% if category == 1 %}selected{% endif %}>Category 1</option> {% for cat in categories %}
<option value="2" {% if category == 2 %}selected{% endif %}>Category 2</option> <option value="{{ cat.id }}" {% if post.category == cat.id %}selected{% endif %}>{{ cat.category }}</option>
<option value="3" {% if category == 3 %}selected{% endif %}>Category 3</option> {% endfor %}
<option value="4" {% if category == 4 %}selected{% endif %}>Category 4</option>
</select> </select>
<br> <br>
<label for="konten">Konten: </label> <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> <br>
<label for="created_at">Dibuat pada: </label> <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> <br>
<label for="creator">Dibuat oleh: </label> <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> <br>
<label for="edited_at">Diubah pada: </label> <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> <br>
<label for="editor">Diubah oleh: </label> <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 --> <!-- Current Timestamp -->
<input type="hidden" name="_currts" value={{ timestamp }}> <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">

View File

@ -6,8 +6,8 @@
<form method="post"> <form method="post">
<h3>Tambah Pengumuman</h3> <h3>Tambah Pengumuman</h3>
<label for="kategori">Kategori</label> <label for="category">Kategori</label>
<select name="kategori"> <select name="category">
{% for cat in categories %} {% for cat in categories %}
<option value="{{ cat.id }}">{{ cat.category }}</option> <option value="{{ cat.id }}">{{ cat.category }}</option>
{% endfor %} {% endfor %}
@ -15,20 +15,27 @@
<br> <br>
<label for="konten">Konten</label> <label for="expired_at">Berlaku sampai</label>
<textarea name="konten" rows="3" cols="30"></textarea> <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 --> <!-- Current Timestamp -->
<input type="hidden" name="_currts" value={{ timestamp }}> <input type="hidden" name="_currts" value="{{ timestamp }}" disabled>
<!-- Method --> <!-- Method -->
<input type="hidden" name="_method" value="post"> <input type="hidden" name="_method" value="post">
<!-- Token --> <!-- Token -->
<input type="hidden" name="_token" value={{ token }}> <input type="hidden" name="_token" value="{{ token }}">
<br> <br>
<button type="submit" name="entry">Entry</button> <button type="submit">Entry</button>
</form> </form>
{% endblock %} {% endblock %}

View File

@ -66,14 +66,32 @@ class Router
$action = $this->convertToCamelCaps($action); $action = $this->convertToCamelCaps($action);
if (is_callable([$object, $action])) { if (is_callable([$object, $action])) {
if (array_key_exists('id', $this->params)) { // Check if there's input to the current page
$var = $this->params['id']; if (Input::exists('post')) {
return $object->$action($var); $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();
} }
} }
} }
}
return false; return false;
} }

View File

@ -1,7 +1,13 @@
<?php <?php
session_start();
// Autoload // Autoload
require_once dirname(__DIR__).'/vendor/autoload.php'; require_once dirname(__DIR__).'/vendor/autoload.php';
if (Core\Session::exists('info')) {
echo Core\Session::flash('info');
echo "<br>";
}
$router = new Core\Router(); $router = new Core\Router();
$router->add('', ['controller' => 'posts', 'action' => 'index']); $router->add('', ['controller' => 'posts', 'action' => 'index']);