- Added database validation

- Added valid date for posts
- Fix passing data to htmls conflicts
This commit is contained in:
2017-09-06 10:40:05 +07:00
parent 57dc4ca0b9
commit 71fb5e4933
8 changed files with 126 additions and 46 deletions

View File

@@ -6,6 +6,8 @@ use App\Models\Post;
use App\Models\Access;
use \Core\Token;
use \Core\Session;
use \Core\Redirect;
use \Core\Validate;
class Posts
{
@@ -18,31 +20,73 @@ class Posts
$this->access = new Access();
}
public function checkExpired()
{
$date = new \DateTime();
$now = $date->format("Y-m-d");
if ($expired = $this->post->showAll('expired_at', '<', $now)) {
foreach ($expired as $value) {
$this->post->update('pengumuman', ['status' => 0], $value['id']);
}
}
}
public function checkValid()
{
$date = new \DateTime();
$date = $date->setTime(0,0);
$now = $date->format("Y-m-d");
if ($not_valid = $this->post->showAll('valid_at', '>', $now)) {
foreach ($not_valid as $value) {
$this->post->update('pengumuman', ['status' => 0], $value['id']);
}
}
}
public function index()
{
$posts = $this->post->showAll(['status' => 1]);
$this->checkValid();
$this->checkExpired();
$posts = $this->post->showAll('status', '=', 1);
$url = 'Data/pengumuman.html';
$status = '';
// echo "This is index of posts."; // Nanti di replace sama twig view ke App\Views\Data\pengumuman.html
if (Session::exists('userid')) {
$posts = $this->post->showAll();
$status = 'admin';
}
// $x = 0;
for ($i=0; $i < count($posts); $i++) {
$posts[$i]['content'] = preg_replace('/[\r]/', '', $posts[$i]['content']);
$posts[$i]['content'] = preg_replace('/[\n]/', "<br/>", $posts[$i]['content']);
}
View::render($url, [
'posts' => $posts,
'status' => $status
]);
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
$user = Session::get('userid');
View::render('Data/entry_pengumuman.html', [
'categories' => $categories,
'user' => $user,
'token' => Token::generate()
]);
return true;
}
@@ -52,55 +96,71 @@ class Posts
if (is_array($id)) {
$id = implode('', $id);
}
$post = $this->post->showSingle($id);
$categories = $this->post->showCategories();
$user = $this->access->showSingle($id);
$post = $this->post->showSingle($id);
$creator = $post['creator'];
$editor = $post['editor'];
$creator = $this->access->showSingle($creator);
$editor = $this->access->showSingle($editor);
$editor_now = Session::get('userid');
$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,
'creator' => $creator,
'editor' => $editor,
'editor_now' => $editor_now,
'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();
Redirect::to('/');
}
}
public function put($args = [])
{
$table = 'pengumuman';
$args['content'] = htmlspecialchars($args['content']);
$id = $args['id'];
unset($args['id']);
if ($this->post->update($table, $args, $id)) {
Session::flash('info', 'Data successfuly updated');
return $this->edit($id);
Redirect::to('/');
}
}
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);
Redirect::to('/');
}
}
}