Changed database interaction structures
This commit is contained in:
@@ -10,13 +10,11 @@ use Core\Hash;
|
||||
|
||||
class Home
|
||||
{
|
||||
public $model,
|
||||
$table;
|
||||
public $model;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new Access();
|
||||
$this->table = 'user';
|
||||
}
|
||||
|
||||
/* Routes */
|
||||
@@ -31,6 +29,7 @@ class Home
|
||||
if (Session::exists('userid')) {
|
||||
Session::flash('info', 'Anda telah masuk.');
|
||||
Redirect::to('/');
|
||||
die();
|
||||
} else {
|
||||
View::render('Access/login.html', [
|
||||
'token' => Token::generate()
|
||||
@@ -75,8 +74,6 @@ class Home
|
||||
}
|
||||
}
|
||||
|
||||
$table = 'user';
|
||||
|
||||
$date = new \DateTime();
|
||||
$now = $date->format('Y-m-d');
|
||||
$args['registered_at'] = $now;
|
||||
@@ -89,7 +86,7 @@ class Home
|
||||
$args['full_name'] = htmlspecialchars($args['full_name']);
|
||||
$args['username'] = htmlspecialchars($args['username']);
|
||||
|
||||
$data = $this->model->showAll($table);
|
||||
$data = $this->model->showAll();
|
||||
foreach ($data as $users) {
|
||||
if ($args['username'] == $users['username']) {
|
||||
Session::flash('info', 'Username telah digunakan. Silahkan gunakan username lain.');
|
||||
@@ -98,20 +95,21 @@ class Home
|
||||
}
|
||||
}
|
||||
|
||||
$this->model->entry($table, $args);
|
||||
$this->model->entry($args);
|
||||
|
||||
Redirect::to('/');
|
||||
die();
|
||||
}
|
||||
|
||||
public function put($args = [])
|
||||
{
|
||||
$table = 'user';
|
||||
$username = $args['username'];
|
||||
$password = $args['password'];
|
||||
|
||||
$user = $this->model->showAll($table, [
|
||||
$user = $this->model->showAll([
|
||||
['username', '=', $username]
|
||||
]);
|
||||
|
||||
if ($user == false) {
|
||||
$info = "Username/password salah.";
|
||||
} else {
|
||||
@@ -121,11 +119,11 @@ class Home
|
||||
if ($user['max_user'] <= 0) {
|
||||
$info = "Telah mencapai maksimal user yang diizinkan. Silahkan logout pada perangkat lain terlebih dahulu.";
|
||||
} else {
|
||||
if ($this->model->update($table, ['status' => 1], $user['id']) != true) {
|
||||
if ($this->model->update(['status' => 1], $user['id']) != true) {
|
||||
$info = "Terjadi kesalahan. Silahkan coba lagi dalam beberapa saat.";
|
||||
} else {
|
||||
$max_user = $user['max_user'] - 1;
|
||||
if ($this->model->update($table, ['max_user' => $max_user], $user['id']) == true) {
|
||||
if ($this->model->update(['max_user' => $max_user], $user['id']) == true) {
|
||||
Session::put('userid', $user['id']);
|
||||
Session::put('username', $user['username']);
|
||||
Session::put('full_name', $user['full_name']);
|
||||
@@ -139,20 +137,19 @@ class Home
|
||||
}
|
||||
Session::flash('info', $info);
|
||||
Redirect::to('/');
|
||||
die();
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$table = 'user';
|
||||
$userid = Session::get('userid');
|
||||
|
||||
$user = $this->model->showAll($table, [
|
||||
$user = $this->model->showAll([
|
||||
['id', '=', $userid]
|
||||
]);
|
||||
$max_user = $user['max_user'] + 1;
|
||||
|
||||
if ($this->model->update(
|
||||
$table,
|
||||
[
|
||||
'status' => 0,
|
||||
'max_user' => $max_user
|
||||
|
||||
@@ -10,13 +10,11 @@ use \Core\Redirect;
|
||||
|
||||
class Posts
|
||||
{
|
||||
private $post,
|
||||
$table;
|
||||
private $post;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new Post();
|
||||
$this->table = 'pengumuman';
|
||||
}
|
||||
|
||||
public function checkValid()
|
||||
@@ -24,39 +22,48 @@ class Posts
|
||||
$date = new \DateTime();
|
||||
$now = $date->format("Y-m-d");
|
||||
|
||||
$valid = $this->model->showAll($this->table, [
|
||||
$valid = $this->model->showAll([
|
||||
['valid_at', '<=', $now],
|
||||
['status', '!=', 3]
|
||||
]);
|
||||
if ($valid) {
|
||||
foreach ($valid as $fields) {
|
||||
$id = $fields['id'];
|
||||
|
||||
$this->model->update($this->table, ['status' => 1], $id);
|
||||
if (is_array($fields)) {
|
||||
$id = $fields['id'];
|
||||
} else {
|
||||
$id = $valid['id'];
|
||||
}
|
||||
$this->model->update(['status' => 1], $id);
|
||||
}
|
||||
}
|
||||
|
||||
$not_valid = $this->model->showAll($this->table, [
|
||||
$not_valid = $this->model->showAll([
|
||||
['valid_at', '>', $now],
|
||||
['status', '!=', 3]
|
||||
]);
|
||||
if ($not_valid) {
|
||||
foreach ($not_valid as $fields) {
|
||||
$id = $fields['id'];
|
||||
|
||||
$this->model->update($this->table, ['status' => 2], $id);
|
||||
if (is_array($fields)) {
|
||||
$id = $fields['id'];
|
||||
} else {
|
||||
$id = $not_valid['id'];
|
||||
}
|
||||
$this->model->update(['status' => 2], $id);
|
||||
}
|
||||
}
|
||||
|
||||
$expired = $this->model->showAll($this->table, [
|
||||
$expired = $this->model->showAll([
|
||||
['expired_at', '<', $now],
|
||||
['status', '!=', 3]
|
||||
]);
|
||||
if ($expired) {
|
||||
foreach ($expired as $fields) {
|
||||
$id = $fields['id'];
|
||||
|
||||
$this->model->update($this->table, ['status' => 0], $id);
|
||||
if (is_array($fields)) {
|
||||
$id = $fields['id'];
|
||||
} else {
|
||||
$id = $expired['id'];
|
||||
}
|
||||
$this->model->update(['status' => 0], $id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,7 +73,9 @@ class Posts
|
||||
{
|
||||
$this->checkValid();
|
||||
|
||||
$posts = $this->model->showAll($this->table, [
|
||||
$posts = [];
|
||||
|
||||
$post = $this->model->showAll([
|
||||
['status', '=', 1]
|
||||
]);
|
||||
|
||||
@@ -75,19 +84,21 @@ class Posts
|
||||
$status = '';
|
||||
|
||||
if (Session::exists('userid')) {
|
||||
$posts = $this->model->showAll($this->table);
|
||||
$post = $this->model->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']);
|
||||
if (array_key_exists(0, $post)) {
|
||||
$posts = $post;
|
||||
} else {
|
||||
$posts[] = $post;
|
||||
}
|
||||
|
||||
View::render($url, [
|
||||
'posts' => $posts,
|
||||
'status' => $status
|
||||
]);
|
||||
die();
|
||||
}
|
||||
|
||||
public function entry()
|
||||
@@ -96,9 +107,7 @@ class Posts
|
||||
$date = new \DateTime();
|
||||
$now = $date->format("Y-m-d");
|
||||
|
||||
$this->table = 'kategori';
|
||||
|
||||
$categories = $this->model->showAll($this->table);
|
||||
$categories = $this->model->showAll([], 'kategori');
|
||||
|
||||
$user = Session::get('userid');
|
||||
|
||||
@@ -108,8 +117,6 @@ class Posts
|
||||
'user' => $user,
|
||||
'token' => Token::generate()
|
||||
]);
|
||||
|
||||
$this->table = 'pengumuman';
|
||||
} else {
|
||||
throw new \Exception("Page not found", 404);
|
||||
}
|
||||
@@ -123,24 +130,22 @@ class Posts
|
||||
$id = implode('', $id);
|
||||
}
|
||||
|
||||
$categories = $this->model->showCategories();
|
||||
$categories = $this->model->showAll([], 'kategori');
|
||||
|
||||
$post = $this->model->showAll($this->table, [
|
||||
$post = $this->model->showAll([
|
||||
['id', '=', $id]
|
||||
]);
|
||||
$creator = $post['creator'];
|
||||
$editor = $post['editor'];
|
||||
|
||||
$this->table = 'user';
|
||||
$table = 'users';
|
||||
|
||||
$creator = $this->model->showAll($this->table, [
|
||||
$creator = $this->model->showAll([
|
||||
['id', '=', $creator]
|
||||
]);
|
||||
$editor = $this->model->showAll($this->table, [
|
||||
], $table);
|
||||
$editor = $this->model->showAll([
|
||||
['id', '=', $editor]
|
||||
]);
|
||||
|
||||
$this->table = 'pengumuman';
|
||||
], $table);
|
||||
|
||||
$editor_now = Session::get('userid');
|
||||
|
||||
@@ -168,15 +173,12 @@ class Posts
|
||||
public function category()
|
||||
{
|
||||
if (Session::exists('userid')) {
|
||||
$this->table = 'kategori';
|
||||
$categories = $this->model->showAll($this->table);
|
||||
$categories = $this->model->showAll([], 'kategori');
|
||||
|
||||
View::render('Data/kategori.html', [
|
||||
'categories' => $categories,
|
||||
'token' => Token::generate()
|
||||
]);
|
||||
|
||||
$this->table = 'pengumuman';
|
||||
} else {
|
||||
throw new \Exception("Bad request", 400);
|
||||
}
|
||||
@@ -186,34 +188,34 @@ class Posts
|
||||
public function post($args = [])
|
||||
{
|
||||
if (isset($args['_addon'])) {
|
||||
$this->table = $args['_addon'];
|
||||
$table = $args['_addon'];
|
||||
unset($args['_addon']);
|
||||
}
|
||||
|
||||
foreach ($args as $value) {
|
||||
if ($value == '') {
|
||||
Session::flash('info', 'Semua data harus diisi.');
|
||||
if ($this->table == 'pengumuman') {
|
||||
if ($table) {
|
||||
Redirect::to("/posts/category");
|
||||
} else {
|
||||
Redirect::to('/posts/entry');
|
||||
} elseif ($this->table == 'kategori') {
|
||||
Redirect::to('/posts/category');
|
||||
}
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->model->entry($this->table, $args)) {
|
||||
Session::flash('info', 'Data berhasil diunggah.');
|
||||
|
||||
if ($this->table == 'kategori') {
|
||||
if (isset($table)) {
|
||||
if ($this->model->entry($args, $table)) {
|
||||
Session::flash('info', 'Data berhasil diunggah.');
|
||||
Redirect::to('/posts/category');
|
||||
} elseif ($this->table == 'pengumuman') {
|
||||
}
|
||||
} else {
|
||||
if ($this->model->entry($args)) {
|
||||
Session::flash('info', 'Data berhasil diunggah.');
|
||||
Redirect::to('/');
|
||||
}
|
||||
}
|
||||
|
||||
// Return the $table back to default
|
||||
$this->table = 'pengumuman';
|
||||
die();
|
||||
}
|
||||
|
||||
public function put($args = [])
|
||||
@@ -221,10 +223,11 @@ class Posts
|
||||
if (isset($args['_addon'])) {
|
||||
$table = $args['_addon'];
|
||||
|
||||
$this->model->update($table, ['status' => 1], $args['id']);
|
||||
$this->model->update(['status' => 1], $args['id'], $table);
|
||||
|
||||
Session::flash('info', 'Data berhasil diaktifkan.');
|
||||
Redirect::to('/posts/category');
|
||||
die();
|
||||
}
|
||||
$args['content'] = htmlspecialchars($args['content']);
|
||||
|
||||
@@ -258,28 +261,29 @@ class Posts
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->model->update($this->table, $args, $id)) {
|
||||
if ($this->model->update($args, $id)) {
|
||||
Session::flash('info', 'Data berhasil diperbarui.');
|
||||
Redirect::to('/');
|
||||
} else {
|
||||
Session::flash('info', 'Terjadi kesalahan. Silahkan coba lagi dalam beberapa saat.');
|
||||
Redirect::to("./$id");
|
||||
}
|
||||
die();
|
||||
}
|
||||
|
||||
public function delete($args = [])
|
||||
{
|
||||
if (isset($args['_addon'])) {
|
||||
$this->table = $args['_addon'];
|
||||
$table = $args['_addon'];
|
||||
unset($args['_addon']);
|
||||
}
|
||||
|
||||
$id = $args['id'];
|
||||
|
||||
if ($this->table == 'kategori') {
|
||||
$delete = $this->model->delete($this->table, $id, 0);
|
||||
if ($table) {
|
||||
$delete = $this->model->delete($id, 0, $table);
|
||||
} else {
|
||||
$delete = $this->model->delete($this->table, $id);
|
||||
$delete = $this->model->delete($id);
|
||||
}
|
||||
|
||||
if ($delete == true) {
|
||||
@@ -287,15 +291,14 @@ class Posts
|
||||
} else {
|
||||
$info = 'Terjadi kesalahan. Silahkan coba lagi dalam beberapa saat.';
|
||||
}
|
||||
// Return the $table back to default
|
||||
$this->table = 'pengumuman';
|
||||
|
||||
Session::flash('info', $info);
|
||||
|
||||
if ($this->table = 'kategori') {
|
||||
Redirect::to('/posts/category');
|
||||
} elseif ($this->table = 'pengumuman') {
|
||||
if ($table) {
|
||||
Redirect::to("/posts/category");
|
||||
} else {
|
||||
Redirect::to('/');
|
||||
}
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user