post = new Post(); $this->access = new Access(); } public function checkValid() { $date = new \DateTime(); $now = $date->format("Y-m-d"); $table = 'pengumuman'; $valid = $this->post->showAll([ ['valid_at', '<=', $now], ['status', '!=', 3] ]); if ($valid !== false) { foreach ($valid as $fields) { $id = $fields['id']; $this->post->update($table, ['status' => 1], $id); } } $not_valid = $this->post->showAll([ ['valid_at', '>', $now], ['status', '!=', 3] ]); if ($not_valid !== false) { foreach ($not_valid as $fields) { $id = $fields['id']; $this->post->update($table, ['status' => 2], $id); } } $expired = $this->post->showAll([ ['expired_at', '<', $now], ['status', '!=', 3] ]); if ($expired !== false) { foreach ($expired as $fields) { $id = $fields['id']; $this->post->update($table, ['status' => 0], $id); } } } public function index() { $this->checkValid(); $posts = $this->post->showAll([ ['status', '=', 1] ]); $url = 'Data/pengumuman.html'; $status = ''; if (Session::exists('userid')) { $posts = $this->post->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]/', "
", $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 { Redirect::to('/'); } } public function edit($id) { if (Session::exists('userid')) { if ($id) { if (is_array($id)) { $id = implode('', $id); } $categories = $this->post->showCategories(); $post = $this->post->showSingle($id); $creator = $post['creator']; $editor = $post['editor']; $creator = $this->access->showSingle($creator); $editor = $this->access->showSingle($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 { Redirect::to('/'); } } public function category() { if (Session::exists('userid')) { $categories = $this->post->showCategories(); View::render('Data/kategori.html', [ 'categories' => $categories, 'token' => Token::generate() ]); } else { Redirect::to('/'); } } // Methods public function post($args = []) { $table = 'pengumuman'; if (isset($args['_addon'])) { $table = $args['_addon']; unset($args['_addon']); } foreach ($args as $value) { if ($value == '') { Session::flash('info', 'All data must not be empty'); if ($table == 'pengumuman') { Redirect::to('/posts/entry'); } elseif ($table == 'kategori') { Redirect::to('/posts/category'); } die(); } } if ($this->post->entry($table, $args)) { Session::flash('info', 'Data successfuly uploaded'); if ($table == 'kategori') { Redirect::to('/posts/category'); } elseif ($table == 'pengumuman') { Redirect::to('/'); } } } public function put($args = []) { $table = 'pengumuman'; $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', 'Data must not be same'); 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($table, $args, $id)) { Session::flash('info', 'Data successfuly updated'); Redirect::to('/'); } else { Session::flash('info', 'Data cannot be uploaded'); Redirect::to("./$id"); } } public function delete($args = []) { $table = 'pengumuman'; if (isset($args['_addon'])) { $table = $args['_addon']; unset($args['_addon']); } $id = $args['id']; if ($this->post->delete($table, $id)) { Session::flash('info', 'Data successfuly removed'); if ($table = 'kategori') { Redirect::to('/posts/category'); } elseif ($table = 'pengumuman') { Redirect::to('/'); } } } }