diff --git a/App/Controllers/Posts.php b/App/Controllers/Posts.php index 701c350..1c18c14 100644 --- a/App/Controllers/Posts.php +++ b/App/Controllers/Posts.php @@ -6,6 +6,8 @@ use App\Models\Post; use App\Models\Access; use \Core\Token; use \Core\Session; +use \Core\Redirect; +use \Core\Validate; class Posts { @@ -18,31 +20,73 @@ class Posts $this->access = new Access(); } + public function checkExpired() + { + $date = new \DateTime(); + $now = $date->format("Y-m-d"); + + if ($expired = $this->post->showAll('expired_at', '<', $now)) { + foreach ($expired as $value) { + $this->post->update('pengumuman', ['status' => 0], $value['id']); + } + } + } + + public function checkValid() + { + $date = new \DateTime(); + $date = $date->setTime(0,0); + $now = $date->format("Y-m-d"); + + if ($not_valid = $this->post->showAll('valid_at', '>', $now)) { + foreach ($not_valid as $value) { + $this->post->update('pengumuman', ['status' => 0], $value['id']); + } + } + } + public function index() { - $posts = $this->post->showAll(['status' => 1]); + $this->checkValid(); + $this->checkExpired(); + + $posts = $this->post->showAll('status', '=', 1); + $url = 'Data/pengumuman.html'; + $status = ''; - // echo "This is index of posts."; // Nanti di replace sama twig view ke App\Views\Data\pengumuman.html + if (Session::exists('userid')) { $posts = $this->post->showAll(); $status = 'admin'; } + + // $x = 0; + 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 ]); + return true; } public function entry() { $categories = $this->post->showCategories(); - // echo "You can entry new data here."; // Nanti di replace sama twig view ke App\Views\Data\entry_pengumuman.html + + $user = Session::get('userid'); + View::render('Data/entry_pengumuman.html', [ 'categories' => $categories, + 'user' => $user, 'token' => Token::generate() ]); + return true; } @@ -52,55 +96,71 @@ class Posts if (is_array($id)) { $id = implode('', $id); } - $post = $this->post->showSingle($id); + $categories = $this->post->showCategories(); - $user = $this->access->showSingle($id); + + $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 H:i:s"); - // echo "You can edit exists data with id $id here"; // Nanti di replace sama twig view ke App\Views\Data\edit_pengumuman.html + View::render( 'Data/edit_pengumuman.html', [ 'post' => $post, 'categories' => $categories, - 'user' => $user, + 'creator' => $creator, + 'editor' => $editor, + 'editor_now' => $editor_now, 'timestamp' => $timestamp, 'token' => Token::generate() ] ); - return true; } - return false; } // Methods public function post($args = []) { $table = 'pengumuman'; + if ($this->post->entry($table, $args)) { Session::flash('info', 'Data successfuly uploaded'); - return $this->index(); + Redirect::to('/'); } } public function put($args = []) { $table = 'pengumuman'; + + $args['content'] = htmlspecialchars($args['content']); + $id = $args['id']; unset($args['id']); + if ($this->post->update($table, $args, $id)) { Session::flash('info', 'Data successfuly updated'); - return $this->edit($id); + Redirect::to('/'); } } public function delete($args = []) { $table = 'pengumuman'; + $id = $args['id']; + if ($this->post->delete($table, $id)) { Session::flash('info', 'Data successfuly removed'); - return $this->edit($id); + Redirect::to('/'); } } } diff --git a/App/Models/Access.php b/App/Models/Access.php index 5592481..59f649d 100644 --- a/App/Models/Access.php +++ b/App/Models/Access.php @@ -10,11 +10,11 @@ class Access extends \Core\Model [ 'id int(3) NOT NULL AUTO_INCREMENT', 'username varchar(25) NOT NULL', - 'password char(13)', - 'salt char(23)', - 'name varchar(50)', - 'registered_at timestamp DEFAULT CURRENT_TIMESTAMP', - 'status tinyint DEFAULT 1', + 'password char(13) NOT NULL', + 'salt char(23) NOT NULL', + 'name varchar(50) NOT NULL', + 'registered_at date NOT NULL DEFAULT CURRENT_TIMESTAMP', + 'status tinyint NOT NULL DEFAULT 1', 'PRIMARY KEY (id)' ] ); diff --git a/App/Models/Post.php b/App/Models/Post.php index 97c4038..5ad387e 100644 --- a/App/Models/Post.php +++ b/App/Models/Post.php @@ -11,12 +11,14 @@ class Post extends \Core\Model [ 'id int(3) NOT NULL AUTO_INCREMENT', 'category int(3) NOT NULL', - 'created_at timestamp DEFAULT CURRENT_TIMESTAMP', - 'expired_at timestamp NOT NULL', + 'created_at date NOT NULL DEFAULT CURRENT_TIMESTAMP', + 'valid_at date NOT NULL DEFAULT CURRENT_TIMESTAMP', + 'expired_at date NOT NULL', 'creator int(3) NOT NULL', - 'edited_at timestamp', - 'editor timestamp', + 'edited_at date', + 'editor date', 'content varchar(255) NOT NULL', + 'status tinyint NOT NULL DEFAULT 1', 'PRIMARY KEY (id)' ] ); @@ -32,22 +34,21 @@ class Post extends \Core\Model ); } - public function showAll($conds = []) + public function showAll($key = '', $operator = '', $cond = '') { try { $db = static::connectDB(); $sql = "SELECT * FROM pengumuman"; - if ($conds) { - $key = implode('', array_keys($conds)); - $sql .= " WHERE {$key} = ?"; + if ($key && $operator && $cond) { + $sql .= " WHERE {$key} {$operator} ?"; } $query = $db->prepare($sql); - if ($conds) { - $query->bindValue(1, implode('', array_values($conds))); + if ($key && $operator && $cond) { + $query->bindValue(1, $cond); } if ($query->execute()) { @@ -56,6 +57,7 @@ class Post extends \Core\Model return $result; } } + return false; } catch (PDOException $e) { echo $e->getMessage(); } diff --git a/App/Views/Data/edit_pengumuman.html b/App/Views/Data/edit_pengumuman.html index 3450b78..9f64aab 100644 --- a/App/Views/Data/edit_pengumuman.html +++ b/App/Views/Data/edit_pengumuman.html @@ -35,9 +35,7 @@ - {% if post.creator == user.id %} - {{ user.name }} - {% endif %} + {{ creator.name }}
@@ -66,12 +64,10 @@ {% if post.editor == "0" %} - {% else %} - {% if post.editor == user.id %} - {{ user.name }} - {% endif %} + {{ editor.name }} {% endif %} - +
diff --git a/App/Views/Data/entry_pengumuman.html b/App/Views/Data/entry_pengumuman.html index 547d4c0..31a8e1c 100644 --- a/App/Views/Data/entry_pengumuman.html +++ b/App/Views/Data/entry_pengumuman.html @@ -15,6 +15,11 @@
+ + + +
+ @@ -23,7 +28,7 @@ - + diff --git a/App/Views/Data/pengumuman.html b/App/Views/Data/pengumuman.html index 1eac1e6..19981cd 100644 --- a/App/Views/Data/pengumuman.html +++ b/App/Views/Data/pengumuman.html @@ -6,18 +6,20 @@

List Pengumuman

{% for post in posts %} -

Pengumuman {{ post.id }}

- {% if status %} - - {% if post.status == 1 %} - Edit - - {% else %} - Edit - (Nonaktif) + {% if post.id %} +

Pengumuman {{ post.id }}

+ {% if status %} + + {% if post.status == 1 %} + Edit + + {% elseif post.status == 0 %} + Edit + (Nonaktif) + {% endif %} {% endif %} +

{{ post.content|raw }}

{% endif %} -

{{ post.content }}

{% endfor %} {% if status %} diff --git a/Core/Router.php b/Core/Router.php index 45bda5a..c139dc4 100644 --- a/Core/Router.php +++ b/Core/Router.php @@ -76,7 +76,8 @@ class Router } else { // Token invalid $flash = Session::flash('info', 'Token invalid, try again'); - die($flash); + $error = Session::flash('info'); + die($error); } unset($var['_token']); unset($var['_method']); diff --git a/Core/Validate.php b/Core/Validate.php new file mode 100644 index 0000000..7ac6277 --- /dev/null +++ b/Core/Validate.php @@ -0,0 +1,14 @@ +$/', '', $item); + return true; + } + return false; + } +}