Changed model structure
This commit is contained in:
parent
882e635081
commit
210c96e150
@ -10,12 +10,12 @@ use Core\Hash;
|
||||
|
||||
class Home
|
||||
{
|
||||
public $access,
|
||||
public $model,
|
||||
$table;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->access = new Access();
|
||||
$this->model = new Access();
|
||||
$this->table = 'user';
|
||||
}
|
||||
|
||||
@ -89,7 +89,7 @@ class Home
|
||||
$args['full_name'] = htmlspecialchars($args['full_name']);
|
||||
$args['username'] = htmlspecialchars($args['username']);
|
||||
|
||||
$data = $this->access->showAll($table);
|
||||
$data = $this->model->showAll($table);
|
||||
foreach ($data as $users) {
|
||||
if ($args['username'] == $users['username']) {
|
||||
Session::flash('info', 'Username telah digunakan. Silahkan gunakan username lain.');
|
||||
@ -98,7 +98,7 @@ class Home
|
||||
}
|
||||
}
|
||||
|
||||
$this->access->entry($table, $args);
|
||||
$this->model->entry($table, $args);
|
||||
|
||||
Redirect::to('/');
|
||||
}
|
||||
@ -109,7 +109,7 @@ class Home
|
||||
$username = $args['username'];
|
||||
$password = $args['password'];
|
||||
|
||||
$user = $this->access->showAll($table, [
|
||||
$user = $this->model->showAll($table, [
|
||||
['username', '=', $username]
|
||||
]);
|
||||
if ($user == false) {
|
||||
@ -121,11 +121,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->access->update($table, ['status' => 1], $user['id']) != true) {
|
||||
if ($this->model->update($table, ['status' => 1], $user['id']) != true) {
|
||||
$info = "Terjadi kesalahan. Silahkan coba lagi dalam beberapa saat.";
|
||||
} else {
|
||||
$max_user = $user['max_user'] - 1;
|
||||
if ($this->access->update($table, ['max_user' => $max_user], $user['id']) == true) {
|
||||
if ($this->model->update($table, ['max_user' => $max_user], $user['id']) == true) {
|
||||
Session::put('userid', $user['id']);
|
||||
Session::put('username', $user['username']);
|
||||
Session::put('full_name', $user['full_name']);
|
||||
@ -146,12 +146,12 @@ class Home
|
||||
$table = 'user';
|
||||
$userid = Session::get('userid');
|
||||
|
||||
$user = $this->access->showAll($table, [
|
||||
$user = $this->model->showAll($table, [
|
||||
['id', '=', $userid]
|
||||
]);
|
||||
$max_user = $user['max_user'] + 1;
|
||||
|
||||
if ($this->access->update(
|
||||
if ($this->model->update(
|
||||
$table,
|
||||
[
|
||||
'status' => 0,
|
||||
|
@ -11,13 +11,11 @@ use \Core\Redirect;
|
||||
class Posts
|
||||
{
|
||||
private $post,
|
||||
$access,
|
||||
$table;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->post = new Post();
|
||||
$this->access = new Access();
|
||||
$this->model = new Post();
|
||||
$this->table = 'pengumuman';
|
||||
}
|
||||
|
||||
@ -26,7 +24,7 @@ class Posts
|
||||
$date = new \DateTime();
|
||||
$now = $date->format("Y-m-d");
|
||||
|
||||
$valid = $this->post->showAll($this->table, [
|
||||
$valid = $this->model->showAll($this->table, [
|
||||
['valid_at', '<=', $now],
|
||||
['status', '!=', 3]
|
||||
]);
|
||||
@ -34,11 +32,11 @@ class Posts
|
||||
foreach ($valid as $fields) {
|
||||
$id = $fields['id'];
|
||||
|
||||
$this->post->update($this->table, ['status' => 1], $id);
|
||||
$this->model->update($this->table, ['status' => 1], $id);
|
||||
}
|
||||
}
|
||||
|
||||
$not_valid = $this->post->showAll($this->table, [
|
||||
$not_valid = $this->model->showAll($this->table, [
|
||||
['valid_at', '>', $now],
|
||||
['status', '!=', 3]
|
||||
]);
|
||||
@ -46,11 +44,11 @@ class Posts
|
||||
foreach ($not_valid as $fields) {
|
||||
$id = $fields['id'];
|
||||
|
||||
$this->post->update($this->table, ['status' => 2], $id);
|
||||
$this->model->update($this->table, ['status' => 2], $id);
|
||||
}
|
||||
}
|
||||
|
||||
$expired = $this->post->showAll($this->table, [
|
||||
$expired = $this->model->showAll($this->table, [
|
||||
['expired_at', '<', $now],
|
||||
['status', '!=', 3]
|
||||
]);
|
||||
@ -58,7 +56,7 @@ class Posts
|
||||
foreach ($expired as $fields) {
|
||||
$id = $fields['id'];
|
||||
|
||||
$this->post->update($this->table, ['status' => 0], $id);
|
||||
$this->model->update($this->table, ['status' => 0], $id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -68,7 +66,7 @@ class Posts
|
||||
{
|
||||
$this->checkValid();
|
||||
|
||||
$posts = $this->post->showAll($this->table, [
|
||||
$posts = $this->model->showAll($this->table, [
|
||||
['status', '=', 1]
|
||||
]);
|
||||
|
||||
@ -77,7 +75,7 @@ class Posts
|
||||
$status = '';
|
||||
|
||||
if (Session::exists('userid')) {
|
||||
$posts = $this->post->showAll($this->table);
|
||||
$posts = $this->model->showAll($this->table);
|
||||
$status = 'admin';
|
||||
}
|
||||
|
||||
@ -98,7 +96,9 @@ class Posts
|
||||
$date = new \DateTime();
|
||||
$now = $date->format("Y-m-d");
|
||||
|
||||
$categories = $this->post->showCategories();
|
||||
$this->table = 'kategori';
|
||||
|
||||
$categories = $this->model->showAll($this->table);
|
||||
|
||||
$user = Session::get('userid');
|
||||
|
||||
@ -111,6 +111,7 @@ class Posts
|
||||
} else {
|
||||
throw new \Exception("Page not found", 404);
|
||||
}
|
||||
$this->table = 'pengumuman';
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
@ -121,21 +122,25 @@ class Posts
|
||||
$id = implode('', $id);
|
||||
}
|
||||
|
||||
$categories = $this->post->showCategories();
|
||||
$categories = $this->model->showCategories();
|
||||
|
||||
$post = $this->post->showAll($this->table, [
|
||||
$post = $this->model->showAll($this->table, [
|
||||
['id', '=', $id]
|
||||
]);
|
||||
$creator = $post['creator'];
|
||||
$editor = $post['editor'];
|
||||
|
||||
$creator = $this->access->showAll($this->table, [
|
||||
$this->table = 'user';
|
||||
|
||||
$creator = $this->model->showAll($this->table, [
|
||||
['id', '=', $creator]
|
||||
]);
|
||||
$editor = $this->access->showAll($this->table, [
|
||||
$editor = $this->model->showAll($this->table, [
|
||||
['id', '=', $editor]
|
||||
]);
|
||||
|
||||
$this->table = 'pengumuman';
|
||||
|
||||
$editor_now = Session::get('userid');
|
||||
|
||||
$date = new \DateTime();
|
||||
@ -162,7 +167,8 @@ class Posts
|
||||
public function category()
|
||||
{
|
||||
if (Session::exists('userid')) {
|
||||
$categories = $this->post->showCategories();
|
||||
$this->table = 'kategori';
|
||||
$categories = $this->model->showAll($this->table);
|
||||
|
||||
View::render('Data/kategori.html', [
|
||||
'categories' => $categories,
|
||||
@ -171,6 +177,8 @@ class Posts
|
||||
} else {
|
||||
throw new \Exception("Bad request", 400);
|
||||
}
|
||||
|
||||
$this->table = 'pengumuman';
|
||||
}
|
||||
|
||||
/* Methods */
|
||||
@ -193,7 +201,7 @@ class Posts
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->post->entry($this->table, $args)) {
|
||||
if ($this->model->entry($this->table, $args)) {
|
||||
Session::flash('info', 'Data berhasil diunggah.');
|
||||
|
||||
if ($this->table == 'kategori') {
|
||||
@ -212,7 +220,7 @@ class Posts
|
||||
if (isset($args['_addon'])) {
|
||||
$table = $args['_addon'];
|
||||
|
||||
$this->post->update($table, ['status' => 1], $args['id']);
|
||||
$this->model->update($table, ['status' => 1], $args['id']);
|
||||
|
||||
Session::flash('info', 'Data berhasil diaktifkan.');
|
||||
Redirect::to('/posts/category');
|
||||
@ -249,7 +257,7 @@ class Posts
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->post->update($this->table, $args, $id)) {
|
||||
if ($this->model->update($this->table, $args, $id)) {
|
||||
Session::flash('info', 'Data berhasil diperbarui.');
|
||||
Redirect::to('/');
|
||||
} else {
|
||||
@ -268,9 +276,9 @@ class Posts
|
||||
$id = $args['id'];
|
||||
|
||||
if ($this->table == 'kategori') {
|
||||
$delete = $this->post->delete($this->table, $id, 0);
|
||||
$delete = $this->model->delete($this->table, $id, 0);
|
||||
} else {
|
||||
$delete = $this->post->delete($this->table, $id);
|
||||
$delete = $this->model->delete($this->table, $id);
|
||||
}
|
||||
|
||||
if ($delete == true) {
|
||||
|
@ -34,24 +34,4 @@ class Post extends \Core\Model
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function showCategories()
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "SELECT * FROM kategori";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute()) {
|
||||
if ($query->rowCount() > 1) {
|
||||
$results = $query->fetchAll(\PDO::FETCH_ASSOC);
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user