276 lines
7.3 KiB
PHP
276 lines
7.3 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,
|
|
$table;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->post = new Post();
|
|
$this->access = new Access();
|
|
$this->table = 'pengumuman';
|
|
}
|
|
|
|
public function checkValid()
|
|
{
|
|
$date = new \DateTime();
|
|
$now = $date->format("Y-m-d");
|
|
|
|
$valid = $this->post->showAll($this->table, [
|
|
['valid_at', '<=', $now],
|
|
['status', '!=', 3]
|
|
]);
|
|
if ($valid) {
|
|
foreach ($valid as $fields) {
|
|
$id = $fields['id'];
|
|
|
|
$this->post->update($this->table, ['status' => 1], $id);
|
|
}
|
|
}
|
|
|
|
$not_valid = $this->post->showAll($this->table, [
|
|
['valid_at', '>', $now],
|
|
['status', '!=', 3]
|
|
]);
|
|
if ($not_valid) {
|
|
foreach ($not_valid as $fields) {
|
|
$id = $fields['id'];
|
|
|
|
$this->post->update($this->table, ['status' => 2], $id);
|
|
}
|
|
}
|
|
|
|
$expired = $this->post->showAll($this->table, [
|
|
['expired_at', '<', $now],
|
|
['status', '!=', 3]
|
|
]);
|
|
if ($expired) {
|
|
foreach ($expired as $fields) {
|
|
$id = $fields['id'];
|
|
|
|
$this->post->update($this->table, ['status' => 0], $id);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Routes */
|
|
public function index()
|
|
{
|
|
$this->checkValid();
|
|
|
|
$posts = $this->post->showAll($this->table, [
|
|
['status', '=', 1]
|
|
]);
|
|
|
|
$url = 'Data/pengumuman.html';
|
|
|
|
$status = '';
|
|
|
|
if (Session::exists('userid')) {
|
|
$posts = $this->post->showAll($this->table);
|
|
$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 {
|
|
throw new \Exception("Page not found", 404);
|
|
}
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
if (Session::exists('userid')) {
|
|
if ($id) {
|
|
if (is_array($id)) {
|
|
$id = implode('', $id);
|
|
}
|
|
|
|
$categories = $this->post->showCategories();
|
|
|
|
$post = $this->post->showAll($this->table, [
|
|
['id', '=', $id]
|
|
]);
|
|
$creator = $post['creator'];
|
|
$editor = $post['editor'];
|
|
|
|
$creator = $this->access->showAll($this->table, [
|
|
['id', '=', $creator]
|
|
]);
|
|
$editor = $this->access->showAll($this->table, [
|
|
['id', '=', $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 {
|
|
throw new \Exception("Page not found", 404);
|
|
}
|
|
}
|
|
|
|
public function category()
|
|
{
|
|
if (Session::exists('userid')) {
|
|
$categories = $this->post->showCategories();
|
|
|
|
View::render('Data/kategori.html', [
|
|
'categories' => $categories,
|
|
'token' => Token::generate()
|
|
]);
|
|
} else {
|
|
throw new \Exception("Page not found", 404);
|
|
}
|
|
}
|
|
|
|
/* Methods */
|
|
public function post($args = [])
|
|
{
|
|
if (isset($args['_addon'])) {
|
|
$this->table = $args['_addon'];
|
|
unset($args['_addon']);
|
|
}
|
|
|
|
foreach ($args as $value) {
|
|
if ($value == '') {
|
|
Session::flash('info', 'All data must not be empty');
|
|
if ($this->table == 'pengumuman') {
|
|
Redirect::to('/posts/entry');
|
|
} elseif ($this->table == 'kategori') {
|
|
Redirect::to('/posts/category');
|
|
}
|
|
die();
|
|
}
|
|
}
|
|
|
|
if ($this->post->entry($this->table, $args)) {
|
|
Session::flash('info', 'Data successfuly uploaded');
|
|
|
|
if ($this->table == 'kategori') {
|
|
Redirect::to('/posts/category');
|
|
} elseif ($this->table == 'pengumuman') {
|
|
Redirect::to('/');
|
|
}
|
|
}
|
|
|
|
// Return the $table back to default
|
|
$this->table = 'pengumuman';
|
|
}
|
|
|
|
public function put($args = [])
|
|
{
|
|
$args['content'] = htmlspecialchars($args['content']);
|
|
|
|
$id = $args['id'];
|
|
unset($args['id']);
|
|
|
|
// Check if data same with old data
|
|
$old_data = [
|
|
$args['old_category'],
|
|
$args['old_content'],
|
|
$args['old_valid_at'],
|
|
$args['old_expired_at']
|
|
];
|
|
$new_data = [
|
|
$args['category'],
|
|
$args['content'],
|
|
$args['valid_at'],
|
|
$args['expired_at']
|
|
];
|
|
|
|
if ($old_data == $new_data) {
|
|
Session::flash('info', 'Data must not be same');
|
|
Redirect::to("./$id");
|
|
die();
|
|
}
|
|
|
|
$keys = array_keys($args);
|
|
if ($matches = preg_grep('/^old_/', $keys)) {
|
|
foreach ($matches as $match) {
|
|
unset($args[$match]);
|
|
}
|
|
}
|
|
|
|
if ($this->post->update($this->table, $args, $id)) {
|
|
Session::flash('info', 'Data successfuly updated');
|
|
Redirect::to('/');
|
|
} else {
|
|
Session::flash('info', 'Data cannot be uploaded');
|
|
Redirect::to("./$id");
|
|
}
|
|
}
|
|
|
|
public function delete($args = [])
|
|
{
|
|
if (isset($args['_addon'])) {
|
|
$this->table = $args['_addon'];
|
|
unset($args['_addon']);
|
|
}
|
|
|
|
$id = $args['id'];
|
|
|
|
if ($this->post->delete($this->table, $id)) {
|
|
Session::flash('info', 'Data successfuly removed');
|
|
|
|
if ($this->table = 'kategori') {
|
|
Redirect::to('/posts/category');
|
|
} elseif ($this->table = 'pengumuman') {
|
|
Redirect::to('/');
|
|
}
|
|
}
|
|
|
|
// Return the $table back to default
|
|
$this->table = 'pengumuman';
|
|
}
|
|
}
|