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]/', "
", $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("Bad request", 400); } } /* 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', 'Semua data harus diisi.'); 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 berhasil diunggah.'); 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 = []) { if (isset($args['_addon'])) { $table = $args['_addon']; $this->post->update($table, ['status' => 1], $args['id']); Session::flash('info', 'Data berhasil diaktifkan.'); Redirect::to('/posts/category'); } $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', 'Tidak ada data yang diubah.'); 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 berhasil diperbarui.'); Redirect::to('/'); } else { Session::flash('info', 'Terjadi kesalahan. Silahkan coba lagi dalam beberapa saat.'); Redirect::to("./$id"); } } public function delete($args = []) { if (isset($args['_addon'])) { $this->table = $args['_addon']; unset($args['_addon']); } $id = $args['id']; if ($this->table == 'kategori') { $delete = $this->post->delete($this->table, $id, 0); } else { $delete = $this->post->delete($this->table, $id); } if ($delete == true) { $info = 'Data berhasil dinonaktifkan.'; } 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') { Redirect::to('/'); } } }