lepisi-pengumuman/App/Controllers/Posts.php

213 lines
5.4 KiB
PHP

<?php
namespace App\Controllers;
use \Core\View;
use App\Models\Post;
use App\Models\Access;
use \Core\Token;
use \Core\Session;
use \Core\Redirect;
use \Core\Validate;
class Posts
{
private $post,
$access;
public function __construct()
{
$this->post = new Post();
$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()
{
$this->checkValid();
$this->checkExpired();
$posts = $this->post->showAll('status', '=', 1);
$url = 'Data/pengumuman.html';
$status = '';
if (Session::exists('userid')) {
$posts = $this->post->showAll();
$status = 'admin';
}
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()
{
if (Session::exists('userid')) {
$categories = $this->post->showCategories();
$user = Session::get('userid');
View::render('Data/entry_pengumuman.html', [
'categories' => $categories,
'user' => $user,
'token' => Token::generate()
]);
return true;
} else {
Redirect::to('/');
}
}
public function edit($id)
{
if (Session::exists('userid')) {
if ($id) {
if (is_array($id)) {
$id = implode('', $id);
}
$categories = $this->post->showCategories();
$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");
View::render(
'Data/edit_pengumuman.html',
[
'post' => $post,
'categories' => $categories,
'creator' => $creator,
'editor' => $editor,
'editor_now' => $editor_now,
'timestamp' => $timestamp,
'token' => Token::generate()
]
);
}
} else {
Redirect::to('/');
}
}
public function category()
{
if (Session::exists('userid')) {
$categories = $this->post->showCategories();
View::render('Data/kategori.html', [
'categories' => $categories,
'token' => Token::generate()
]);
} else {
Redirect::to('/');
}
}
// Methods
public function post($args = [])
{
$table = 'pengumuman';
if (isset($args['_addon'])) {
$table = $args['_addon'];
unset($args['_addon']);
}
foreach ($args as $value) {
if ($value == '') {
Session::flash('info', 'All data must not be empty');
Redirect::to('./entry');
}
}
if ($this->post->entry($table, $args)) {
Session::flash('info', 'Data successfuly uploaded');
if ($table = 'kategori') {
Redirect::to('/posts/category');
} elseif ($table = 'pengumuman') {
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');
Redirect::to('/');
}
}
public function delete($args = [])
{
$table = 'pengumuman';
if (isset($args['_addon'])) {
$table = $args['_addon'];
unset($args['_addon']);
}
$id = $args['id'];
if ($this->post->delete($table, $id)) {
Session::flash('info', 'Data successfuly removed');
if ($table = 'kategori') {
Redirect::to('/posts/category');
} elseif ($table = 'pengumuman') {
Redirect::to('/');
}
}
}
}