233 lines
5.8 KiB
PHP
233 lines
5.8 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;
|
|
|
|
class Posts
|
|
{
|
|
private $post,
|
|
$access;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->post = new Post();
|
|
$this->access = new Access();
|
|
}
|
|
|
|
public function checkValid()
|
|
{
|
|
$date = new \DateTime();
|
|
$now = $date->format("Y-m-d");
|
|
|
|
$table = 'pengumuman';
|
|
|
|
$valid = $this->post->showAll([
|
|
['valid_at', '<=', $now]
|
|
]);
|
|
if ($valid !== false) {
|
|
foreach ($valid as $fields) {
|
|
$id = $fields['id'];
|
|
|
|
$this->post->update($table, ['status' => 1], $id);
|
|
}
|
|
}
|
|
|
|
$not_valid = $this->post->showAll([
|
|
['valid_at', '>', $now]
|
|
]);
|
|
if ($not_valid !== false) {
|
|
foreach ($not_valid as $fields) {
|
|
$id = $fields['id'];
|
|
|
|
$this->post->update($table, ['status' => 2], $id);
|
|
}
|
|
}
|
|
|
|
$expired = $this->post->showAll([
|
|
['expired_at', '<', $now]
|
|
]);
|
|
if ($expired !== false) {
|
|
foreach ($expired as $fields) {
|
|
$id = $fields['id'];
|
|
|
|
$this->post->update($table, ['status' => 0], $id);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->checkValid();
|
|
|
|
$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
|
|
]);
|
|
}
|
|
|
|
public function entry()
|
|
{
|
|
if (Session::exists('userid')) {
|
|
$date = new \DateTime();
|
|
$now = $date->format("Y-m-d");
|
|
|
|
$categories = $this->post->showCategories();
|
|
|
|
$user = Session::get('userid');
|
|
|
|
View::render('Data/entry_pengumuman.html', [
|
|
'categories' => $categories,
|
|
'timestamp' => $now,
|
|
'user' => $user,
|
|
'token' => Token::generate()
|
|
]);
|
|
} 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 = [])
|
|
{
|
|
foreach ($args as $value) {
|
|
if ($value == '') {
|
|
Session::flash('info', 'All data must not be empty');
|
|
Redirect::to('/');
|
|
die();
|
|
}
|
|
}
|
|
|
|
$table = 'pengumuman';
|
|
if (isset($args['_addon'])) {
|
|
$table = $args['_addon'];
|
|
unset($args['_addon']);
|
|
}
|
|
|
|
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('/');
|
|
} else {
|
|
Session::flash('info', 'Error');
|
|
Redirect::to("./$id");
|
|
}
|
|
}
|
|
|
|
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('/');
|
|
}
|
|
}
|
|
}
|
|
}
|