Changed model structure

This commit is contained in:
Gregorio Chiko Putra 2017-09-08 16:25:24 +07:00
parent 882e635081
commit 210c96e150
3 changed files with 39 additions and 51 deletions

View File

@ -10,12 +10,12 @@ use Core\Hash;
class Home class Home
{ {
public $access, public $model,
$table; $table;
public function __construct() public function __construct()
{ {
$this->access = new Access(); $this->model = new Access();
$this->table = 'user'; $this->table = 'user';
} }
@ -89,7 +89,7 @@ class Home
$args['full_name'] = htmlspecialchars($args['full_name']); $args['full_name'] = htmlspecialchars($args['full_name']);
$args['username'] = htmlspecialchars($args['username']); $args['username'] = htmlspecialchars($args['username']);
$data = $this->access->showAll($table); $data = $this->model->showAll($table);
foreach ($data as $users) { foreach ($data as $users) {
if ($args['username'] == $users['username']) { if ($args['username'] == $users['username']) {
Session::flash('info', 'Username telah digunakan. Silahkan gunakan username lain.'); 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('/'); Redirect::to('/');
} }
@ -109,7 +109,7 @@ class Home
$username = $args['username']; $username = $args['username'];
$password = $args['password']; $password = $args['password'];
$user = $this->access->showAll($table, [ $user = $this->model->showAll($table, [
['username', '=', $username] ['username', '=', $username]
]); ]);
if ($user == false) { if ($user == false) {
@ -121,11 +121,11 @@ class Home
if ($user['max_user'] <= 0) { if ($user['max_user'] <= 0) {
$info = "Telah mencapai maksimal user yang diizinkan. Silahkan logout pada perangkat lain terlebih dahulu."; $info = "Telah mencapai maksimal user yang diizinkan. Silahkan logout pada perangkat lain terlebih dahulu.";
} else { } 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."; $info = "Terjadi kesalahan. Silahkan coba lagi dalam beberapa saat.";
} else { } else {
$max_user = $user['max_user'] - 1; $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('userid', $user['id']);
Session::put('username', $user['username']); Session::put('username', $user['username']);
Session::put('full_name', $user['full_name']); Session::put('full_name', $user['full_name']);
@ -146,12 +146,12 @@ class Home
$table = 'user'; $table = 'user';
$userid = Session::get('userid'); $userid = Session::get('userid');
$user = $this->access->showAll($table, [ $user = $this->model->showAll($table, [
['id', '=', $userid] ['id', '=', $userid]
]); ]);
$max_user = $user['max_user'] + 1; $max_user = $user['max_user'] + 1;
if ($this->access->update( if ($this->model->update(
$table, $table,
[ [
'status' => 0, 'status' => 0,

View File

@ -11,13 +11,11 @@ use \Core\Redirect;
class Posts class Posts
{ {
private $post, private $post,
$access,
$table; $table;
public function __construct() public function __construct()
{ {
$this->post = new Post(); $this->model = new Post();
$this->access = new Access();
$this->table = 'pengumuman'; $this->table = 'pengumuman';
} }
@ -26,7 +24,7 @@ class Posts
$date = new \DateTime(); $date = new \DateTime();
$now = $date->format("Y-m-d"); $now = $date->format("Y-m-d");
$valid = $this->post->showAll($this->table, [ $valid = $this->model->showAll($this->table, [
['valid_at', '<=', $now], ['valid_at', '<=', $now],
['status', '!=', 3] ['status', '!=', 3]
]); ]);
@ -34,11 +32,11 @@ class Posts
foreach ($valid as $fields) { foreach ($valid as $fields) {
$id = $fields['id']; $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], ['valid_at', '>', $now],
['status', '!=', 3] ['status', '!=', 3]
]); ]);
@ -46,11 +44,11 @@ class Posts
foreach ($not_valid as $fields) { foreach ($not_valid as $fields) {
$id = $fields['id']; $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], ['expired_at', '<', $now],
['status', '!=', 3] ['status', '!=', 3]
]); ]);
@ -58,7 +56,7 @@ class Posts
foreach ($expired as $fields) { foreach ($expired as $fields) {
$id = $fields['id']; $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(); $this->checkValid();
$posts = $this->post->showAll($this->table, [ $posts = $this->model->showAll($this->table, [
['status', '=', 1] ['status', '=', 1]
]); ]);
@ -77,7 +75,7 @@ class Posts
$status = ''; $status = '';
if (Session::exists('userid')) { if (Session::exists('userid')) {
$posts = $this->post->showAll($this->table); $posts = $this->model->showAll($this->table);
$status = 'admin'; $status = 'admin';
} }
@ -98,7 +96,9 @@ class Posts
$date = new \DateTime(); $date = new \DateTime();
$now = $date->format("Y-m-d"); $now = $date->format("Y-m-d");
$categories = $this->post->showCategories(); $this->table = 'kategori';
$categories = $this->model->showAll($this->table);
$user = Session::get('userid'); $user = Session::get('userid');
@ -111,6 +111,7 @@ class Posts
} else { } else {
throw new \Exception("Page not found", 404); throw new \Exception("Page not found", 404);
} }
$this->table = 'pengumuman';
} }
public function edit($id) public function edit($id)
@ -121,21 +122,25 @@ class Posts
$id = implode('', $id); $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] ['id', '=', $id]
]); ]);
$creator = $post['creator']; $creator = $post['creator'];
$editor = $post['editor']; $editor = $post['editor'];
$creator = $this->access->showAll($this->table, [ $this->table = 'user';
$creator = $this->model->showAll($this->table, [
['id', '=', $creator] ['id', '=', $creator]
]); ]);
$editor = $this->access->showAll($this->table, [ $editor = $this->model->showAll($this->table, [
['id', '=', $editor] ['id', '=', $editor]
]); ]);
$this->table = 'pengumuman';
$editor_now = Session::get('userid'); $editor_now = Session::get('userid');
$date = new \DateTime(); $date = new \DateTime();
@ -162,7 +167,8 @@ class Posts
public function category() public function category()
{ {
if (Session::exists('userid')) { if (Session::exists('userid')) {
$categories = $this->post->showCategories(); $this->table = 'kategori';
$categories = $this->model->showAll($this->table);
View::render('Data/kategori.html', [ View::render('Data/kategori.html', [
'categories' => $categories, 'categories' => $categories,
@ -171,6 +177,8 @@ class Posts
} else { } else {
throw new \Exception("Bad request", 400); throw new \Exception("Bad request", 400);
} }
$this->table = 'pengumuman';
} }
/* Methods */ /* 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.'); Session::flash('info', 'Data berhasil diunggah.');
if ($this->table == 'kategori') { if ($this->table == 'kategori') {
@ -212,7 +220,7 @@ class Posts
if (isset($args['_addon'])) { if (isset($args['_addon'])) {
$table = $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.'); Session::flash('info', 'Data berhasil diaktifkan.');
Redirect::to('/posts/category'); 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.'); Session::flash('info', 'Data berhasil diperbarui.');
Redirect::to('/'); Redirect::to('/');
} else { } else {
@ -268,9 +276,9 @@ class Posts
$id = $args['id']; $id = $args['id'];
if ($this->table == 'kategori') { if ($this->table == 'kategori') {
$delete = $this->post->delete($this->table, $id, 0); $delete = $this->model->delete($this->table, $id, 0);
} else { } else {
$delete = $this->post->delete($this->table, $id); $delete = $this->model->delete($this->table, $id);
} }
if ($delete == true) { if ($delete == true) {

View File

@ -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);
}
}
} }