Skip to content

Commit

Permalink
add ajax to comments method
Browse files Browse the repository at this point in the history
  • Loading branch information
SaputraBR committed Oct 24, 2022
1 parent d50c11e commit a425484
Show file tree
Hide file tree
Showing 128 changed files with 358 additions and 141 deletions.
3 changes: 2 additions & 1 deletion app/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');
$routes->get('/post/(:any)', 'Home::post/$1');
$routes->add('/komen', 'Home::comment_add');
$routes->group('', ['filter' => 'adminLog'], static function($routes){
$routes->get('/cms', 'Admin::admin',);
$routes->get('/cms/(:segment)/preview', 'Admin::preview/$1');
Expand All @@ -48,7 +49,7 @@
$routes->get('/lainnya', 'Page::lainnya');
$routes->get('/about', 'Page::about');
$routes->get('/credit', 'Page::credit');
$routes->get('/faqs', 'Page::faqs');
$routes->get('/faqs', 'Page::faqs', ['as' => 'postingan']);
$routes->get('/search', 'Home::search');
$routes->get('/logout', 'login::logout');
$routes->get('/login', 'Login::index');
Expand Down
14 changes: 8 additions & 6 deletions app/Controllers/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function index()
$data['pager'] = $home->pager;
$data['page'] = $this->request->getPost('page') ? $this->request->getPost('page') : 1;

echo view('index', $data);
echo view('home', $data);
}

public function post($slug)
Expand Down Expand Up @@ -59,11 +59,11 @@ function comment_add() {

if ($resp != NULL) {
foreach ($resp as $row) {
$date = mysql_to_php_date($row->dibuat);
echo "<li class='col-12 col-lg-12 col-md-12 col-sm-12 pb-2 pt-2 mb-3 ml-auto bg-komentar' id='li_comment_{$row->id_comment}>".
"<p class='border-bottom'>{$row->nama}(<small><i>{$row->email}</i></small>) <small class='float-right'>{$date}}</small></p>".
"<p class='ml-2'>{$row->message}</p>".
"<a href='#' class='ml-2 balas' id='{$row->id_comment}'><button class='btn btn-primary'>Balas</button></a>".
$date = php_date($row->dibuat);
echo "<li class='mt-1 mb-1 ml-auto' id='li_comment_{$row->id_comment}'>".
"<div class='border-bottom'>{$row->nama}(<small><i>{$row->email}</i></small>) <small class='float-right'>{$date}</small></div>".
"<div class='ml-2'>{$row->message}</div>".
"<a href='#' class='balas' id='{$row->id_comment}'><button class='btn btn-primary ml-2 mt-1'>Balas</button></a>".
"</li>";
}
} else {
Expand All @@ -72,6 +72,8 @@ function comment_add() {
} else {
echo 'Error: Please enter your comment';
}
$pn = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
return redirect($pn, 'refresh');
}


Expand Down
9 changes: 9 additions & 0 deletions app/Helpers/custom_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,14 @@ function php_date($mysql_date)
return $format;
}
}

if (!function_exists('substrack'))
{
function substrack($format)
{
$format = substr("", 0, 15);
return $format;
}
}

?>
1 change: 0 additions & 1 deletion app/Models/AdminModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ class AdminModel extends Model
protected $allowedFields = ['judul', 'content', 'author', 'status', 'gambar', 'kategori', 'slug'];
}


?>
26 changes: 14 additions & 12 deletions app/Models/MessageModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function comment_post($slug)
return $comments;
}

return '<ul class="row-cols" style="list-style-type: none"></ul>';
return '<main class="row-cols" style="list-style-type: none" id="komen_main"></main>';
}

function comment_add($data)
Expand Down Expand Up @@ -75,7 +75,7 @@ private function format_comments($comments)
$parent_stack = array();


$html[] = '<ul class="row-cols" style="list-style-type: none">';
$html[] = '<ul class="row-cols" style="list-style-type: none; padding-left: 0" id="komen_main">';


while ($loop && ( ( $option = an_each($children[$parent]) ) || ( $parent > $default_id ) ))
Expand All @@ -90,31 +90,33 @@ private function format_comments($comments)
$tab = str_repeat("\t", ( count($parent_stack) + 1 ) * 2 - 1);

$html[] = sprintf(
'%1$s<li class="col-12 col-lg-12 col-md-12 col-sm-12 pb-2 pt-2 mb-3 ml-auto bg-komentar border-bottom" id="li_comment_%2$s>'.
'%1$s%1$s<p class="border-bottom">%3$s(<small class="border-bottom"><i>%4$s</i></small>) <small class="float-right border-bottom">%5$s</small></p>'.
'%1$s%1$s<p class="ml-2">%6$s</p>'.
'%1$s%1$s<a href="#" class="ml-2 balas" id="%2$s"><button class="btn btn-primary">Balas</button></a>', $tab, // %1$s = tabulation
'%1$s<li class="mt-1 mb-1 ml-auto" id="li_comment_%2$s">'.
'%1$s%1$s<div class="border-bottom">%3$s(<small><i>%4$s</i></small>) <small class="float-right">%5$s</small></div>'.
'%1$s%1$s<div class="ml-2">%6$s</div>'.
'%1$s%1$s<a href="#" class="balas" id="%2$s"><button class="btn btn-primary ml-2 mt-1">Balas</button></a>',
$tab, // %1$s = tabulation
$option['value']->id_comment, //%2$s = id comment
$option['value']->nama, // %3$s = username
$option['value']->email, //%4$s = email
substr($option['value']->email, 0, 27), //%4$s = email
php_date($option['value']->dibuat), // %5$s = comment created_date
$option['value']->message // %6$s = comment
);


//$check_status = "";
$html[] = $tab . '\t' . '<ul class="row-cols" style="list-style-type: none">';
$html[] = $tab . "\t" . '<ul class="row-cols" style="list-style-type: none;">';

array_push($parent_stack, $option['value']->id_main);
$parent = $option['value']->id_comment;

} else {
// HTML for comment item with no children (aka "leaf")
$html[] = sprintf(
'%1$s<li class="col-12 col-lg-12 col-md-12 col-sm-12 pb-2 pt-2 mb-3 ml-auto bg-komentar" id="li_comment_%2$s">'.
'%1$s%1$s<p class="border-bottom">%3$s(<small><i>%4$s</i></small>) <small class="float-right">%5$s</small></p>'.
'%1$s%1$s<p class="ml-2">%6$s</p>'.
'%1$s%1$s<a href="#" class="ml-2 balas" id="%2$s"><button class="btn btn-primary">Balas</button></a>'.
//'%1$s<ul class="row-cols" style="list-style-type: none">'.
'%1$s<li class="mt-1 mb-1 ml-auto" id="li_comment_%2$s">'.
'%1$s%1$s<div class="border-bottom">%3$s(<small><i>%4$s</i></small>) <small class="float-right">%5$s</small></div>'.
'%1$s%1$s<div class="ml-2">%6$s</div>'.
'%1$s%1$s<a href="#" class="balas" id="%2$s"><button class="btn btn-primary ml-2 mt-1">Balas</button></a>'.
'%1$s</li>', str_repeat("\t", ( count($parent_stack) + 1 ) * 2 - 1), // %1$s = tabulation
$option['value']->id_comment, //%2$s = id comment
$option['value']->nama, // %3$s = username
Expand Down
16 changes: 8 additions & 8 deletions app/Views/faqs.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
<link rel="icon" href="img/fullBackground-icon.png">
<title>Belajar</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-grid.min.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-reboot.min.css" />
<link rel="stylesheet" type="text/css" href="custom.css" />
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="/css/bootstrap-grid.min.css" />
<link rel="stylesheet" type="text/css" href="/css/bootstrap-reboot.min.css" />
<link rel="stylesheet" type="text/css" href="/css/custom.css" />
</head>

<body class="bg-light">
Expand All @@ -31,9 +31,9 @@
<?= $this->include('layout/footer.php') ?>
</body>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="custom.js"></script>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="/js/custom.js"></script>

</html>
File renamed without changes.
5 changes: 3 additions & 2 deletions app/Views/layout/navbar.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<aside class="sidenav d-sm-flex d-lg-none" id="sideNav" onclick="">
<aside class="sidenav d-sm-flex d-lg-none" id="sideNav">
<div class="sidenav2" id="sideNav2">
<header class="border-bottom d-flex p-3">
<h5 class="font-weight-bold">MENU</h5>
<a href="javascript:void()" class="ml-auto order-1" style="color: black;" role="button" id="close" onclick="closeNav()">
<a href="javascript:void(0)" class="ml-auto order-1" style="color: black;" role="button" id="close" onclick="closeNav()">
<svg xmlns="http://www.w3.org/2000/svg" width="23" height="23" fill="currentColor" class="bi bi-x-circle-fill" viewBox="0 0 16 16">
<path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM5.354 4.646a.5.5 0 1 0-.708.708L7.293 8l-2.647 2.646a.5.5 0 0 0 .708.708L8 8.707l2.646 2.647a.5.5 0 0 0 .708-.708L8.707 8l2.647-2.646a.5.5 0 0 0-.708-.708L8 7.293 5.354 4.646z"/>
</svg>
Expand Down Expand Up @@ -49,6 +49,7 @@
<p class="mt-3"><small>Copyright &copy; 2022 BELAJAR.COM</small></p>
</footer>
</div>
<div style="width: 35%;; background-color: transparent;" onclick="closeNav()"></div>
</aside>

<nav class="navbar navbar-expand-lg bg-white sticky-top shadow">
Expand Down
51 changes: 44 additions & 7 deletions app/Views/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<link rel="icon" href="/img/fullBackground-icon.png">
<title>Belajar</title>
<link rel="stylesheet" type="text/css" href="/css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="/css/bootstrap-grid.css"/>
<link rel="stylesheet" type="text/css" href="/css/bootstrap-reboot.css"/>
<link rel="stylesheet" type="text/css" href="/css/custom.css"/>
<link rel="stylesheet" type="text/css" href="/css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="/css/bootstrap-grid.css"/>
<link rel="stylesheet" type="text/css" href="/css/bootstrap-reboot.css"/>
<link rel="stylesheet" type="text/css" href="/css/custom.css"/>
</head>

<body class="bg-light">
Expand Down Expand Up @@ -85,7 +85,7 @@
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea type="text" id="message" name="message" class="form-control form-control2" id="" cols="30" rows="6" required></textarea>
<textarea type="text" id="message" name="message" class="form-control form-control2" id="" cols="30" rows="6"></textarea>
</div>
<div class="form-group" hidden>
<input type='hidden' name='id_post' value="<?= $belajar['id_post'] ?>" id='id_post'/>
Expand All @@ -99,10 +99,47 @@
</div>
</section>

<section class="container rounded-lg shadow p-4 mt-3 mb-5 bg-white" id="komentar">
<section class="container rounded-lg shadow p-5 mt-3 mb-5 bg-white" id="komentar">
<?= $komen ?>
</section>

<!--<div class="modal fade" id="modalContactForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="modal-title w-100 font-weight-bold">Write to us</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body mx-3">
<div class="md-form">
<label data-error="wrong" data-success="right" for="form34">Nama</label>
<input type="text" id="form34" class="form-control validate">
</div>

<div class="md-form">
<label data-error="wrong" data-success="right" for="form29">Email</label>
<input type="email" id="form29" class="form-control validate">
</div>

<div class="md-form">
<label data-error="wrong" data-success="right" for="form8">Message</label>
<textarea type="text" id="message" name="message" class="md-textarea form-control" rows="4"></textarea>
</div>
</div>

<div class="modal-footer d-flex justify-content-center">
<button class="btn btn-unique">Send <i class="fas fa-paper-plane-o ml-1"></i></button>
</div>
</div>
</div>
</div>

<div class="text-center">
<a href="" class="btn btn-default btn-rounded mb-4" data-toggle="modal" data-target="#modalContactForm">Launch
Modal Contact Form</a>
</div>-->
</div>

<aside class="col-sm-12 col-lg-4 mb-5 ">
Expand Down Expand Up @@ -156,7 +193,7 @@
<script type="text/javascript" src="/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="/js/custom.js"></script>
<script type="text/javascript" src="/js/comment_post.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.blockUI/2.70/jquery.blockUI.js" integrity="sha512-QSb5le+VXUEVEQbfljCv8vPnfSbVoBF/iE+c6MqDDqvmzqnr4KL04qdQMCm0fJvC3gCWMpoYhmvKBFqm1Z4c9A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="/js/jquery-blockUI.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" crossorigin="anonymous"></script>

Expand Down
2 changes: 2 additions & 0 deletions public/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
background: linear-gradient(to right, #3a7bd5, #00d2ff); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}


.card:hover {
box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.175) !important;
}
Expand Down Expand Up @@ -334,6 +335,7 @@
transition: 0.2s;
background-color: #f8f9fa;
display: none;
z-index: 10;
}

.title-header {
Expand Down
23 changes: 10 additions & 13 deletions public/js/comment_post.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,36 @@ $(function () {
$(this).hide();
});

$("#comment_form").bind("submit", function (event) {
$("#comment_form").bind("submit", function(event) {
event.preventDefault();

if ($("#message").val() == "") {
alert("Please enter your comment");
return false;
}
};

$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>/Home/comment_add",
url: "http://belajar.local/komen",
data: $('#comment_form').serialize(),
dataType: "html",
success: function (comment) {
var reply_id = $("#id_main").val();
if (reply_id == "") {
$("#comment_wrapper ul:first").prepend(comment);
if (comment.toLowerCase().indexOf("error") >= 0) {
$("#comment_resp_err").attr("value", comment);
}
$("#komentar ul:first").prepend(comment);
}
else {
if ($("#li_comment_" + reply_id).find('ul').length > 0) {
$("#li_comment_" + reply_id + " ul:first").prepend(comment);
$("#li_comment_" + reply_id + "ul:first").prepend(comment);
}
else {
$("#li_comment_" + reply_id).append('<ul class="row-cols" style="list-style-type: none">' + comment + '</ul>');
$("#li_comment_" + reply_id).append('<ul class="row-cols">' + comment + '</ul');
}
}
$("#message").attr("value", "");
$("#Id_main").attr("value", "");
$("#id_main").attr("value", "");
$("#cancel-comment-reply-link").hide();
$("#comment_wrapper").prepend($("#comment_form_wrapper"));
$('#comment_wrapper').unblock();
}
});
});
})
});
1 change: 0 additions & 1 deletion public/js/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
}



/**
* fungsi untuk dark mode
*/
Expand Down
1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993242.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993283.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993334.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993335.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993394.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993396.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993457.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993470.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993472.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993474.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993479.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993480.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993498.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993507.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993509.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993510.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993513.json

This file was deleted.

1 change: 0 additions & 1 deletion writable/debugbar/debugbar_1665993515.json

This file was deleted.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666597758.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666597759.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666597764.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666597768.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666597896.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666597929.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666597937.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666597949.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666597954.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666597965.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666598011.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666598107.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666598108.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666598120.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666598154.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666598156.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666598171.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions writable/debugbar/debugbar_1666598173.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions writable/logs/log-2022-10-18.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CRITICAL - 2022-10-18 02:02:14 --> Too few arguments to function App\Models\MessageModel::comment_post(), 0 passed in C:\xampp\htdocs\belajar\app\Controllers\Home.php on line 32 and exactly 1 expected
#0 C:\xampp\htdocs\belajar\app\Controllers\Home.php(32): App\Models\MessageModel->comment_post()
#1 C:\xampp\htdocs\belajar\system\CodeIgniter.php(824): App\Controllers\Home->post('perjuangan-dan-...')
#2 C:\xampp\htdocs\belajar\system\CodeIgniter.php(410): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Home))
#3 C:\xampp\htdocs\belajar\system\CodeIgniter.php(318): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#4 C:\xampp\htdocs\belajar\public\index.php(37): CodeIgniter\CodeIgniter->run()
#5 {main}
Loading

0 comments on commit a425484

Please sign in to comment.