Changed database interaction structures

This commit is contained in:
2017-09-09 11:21:51 +07:00
parent 1de9cb6447
commit 59d8c11d9b
5 changed files with 499 additions and 296 deletions

View File

@@ -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