Changed the way same data checker works

This commit is contained in:
Gregorio Chiko Putra 2017-09-07 13:38:48 +07:00
parent cf7d5b4c5a
commit 3de31142e0
2 changed files with 44 additions and 9 deletions

View File

@ -166,20 +166,24 @@ class Posts
// Methods // Methods
public function post($args = []) public function post($args = [])
{ {
foreach ($args as $value) {
if ($value == '') {
Session::flash('info', 'All data must not be empty');
Redirect::to('/');
die();
}
}
$table = 'pengumuman'; $table = 'pengumuman';
if (isset($args['_addon'])) { if (isset($args['_addon'])) {
$table = $args['_addon']; $table = $args['_addon'];
unset($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)) { if ($this->post->entry($table, $args)) {
Session::flash('info', 'Data successfuly uploaded'); Session::flash('info', 'Data successfuly uploaded');
@ -200,11 +204,38 @@ class Posts
$id = $args['id']; $id = $args['id'];
unset($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)) { if ($this->post->update($table, $args, $id)) {
Session::flash('info', 'Data successfuly updated'); Session::flash('info', 'Data successfuly updated');
Redirect::to('/'); Redirect::to('/');
} else { } else {
Session::flash('info', 'Error'); Session::flash('info', 'Data cannot be uploaded');
Redirect::to("./$id"); Redirect::to("./$id");
} }
} }

View File

@ -18,11 +18,13 @@
</option> </option>
{% endfor %} {% endfor %}
</select> </select>
<input type="hidden" name="old_category" value="{{ post.category }}">
<br> <br>
<label for="content">Konten: </label> <label for="content">Konten: </label>
<textarea name="content" rows="3" cols="30" maxlength="255">{{ post.content }}</textarea> <textarea name="content" rows="3" cols="30" maxlength="255">{{ post.content }}</textarea>
<input type="hidden" name="old_content" value="{{ post.content }}">
<br> <br>
@ -42,11 +44,13 @@
<label for="valid_at">Berlaku dari: </label> <label for="valid_at">Berlaku dari: </label>
<input type="date" name="valid_at" value="{{ post.valid_at }}"> <input type="date" name="valid_at" value="{{ post.valid_at }}">
<input type="hidden" name="old_valid_at" value="{{ post.valid_at }}">
<br> <br>
<label for="expired_at">Berlaku sampai: </label> <label for="expired_at">Berlaku sampai: </label>
<input type="date" name="expired_at" value="{{ post.expired_at }}"> <input type="date" name="expired_at" value="{{ post.expired_at }}">
<input type="hidden" name="old_expired_at" value="{{ post.expired_at }}">
<br> <br>