Skip to content

Commit 809c50e

Browse files
author
Daniel Neto
committed
Feature: Add functionality to view and manage all posted comments for admins
1 parent ddfaa78 commit 809c50e

4 files changed

Lines changed: 160 additions & 8 deletions

File tree

objects/comment.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,58 @@ public static function getTotalCommentsOnMyVideos() {
423423
return $countRow;
424424
}
425425

426+
public static function getAllPostedComments($includeResponses = false) {
427+
global $global;
428+
if (!User::isAdmin()) {
429+
return [];
430+
}
431+
$users_id = User::getId();
432+
$format = 'i';
433+
$values = [$users_id];
434+
$sql = "SELECT c.*, u.name as name, u.user as user, "
435+
. " (SELECT count(id) FROM comments_likes as l where l.comments_id = c.id AND `like` = 1 ) as likes, "
436+
. " (SELECT count(id) FROM comments_likes as l where l.comments_id = c.id AND `like` = -1 ) as dislikes ";
437+
$sql .= ", (SELECT `like` FROM comments_likes as l where l.comments_id = c.id AND users_id = ? ) as myVote ";
438+
$format .= '';
439+
$sql .= " FROM comments c LEFT JOIN users as u ON u.id = c.users_id LEFT JOIN videos as v ON v.id = c.videos_id";
440+
$sql .= " WHERE u.status = 'a' AND c.comments_id_pai IS NULL ";
441+
$sql .= BootGrid::getSqlFromPost(['name']);
442+
$res = sqlDAL::readSql($sql, $format, $values);
443+
$allData = sqlDAL::fetchAllAssoc($res);
444+
sqlDAL::close($res);
445+
$comment = [];
446+
if ($res != false) {
447+
foreach ($allData as $row) {
448+
$row = cleanUpRowFromDatabase($row);
449+
$row['comment'] = str_replace('\n', "\n", $row['comment']);
450+
$row['commentPlain'] = xss_esc_back($row['comment']);
451+
$row['commentHTML'] = markDownToHTML(str_replace('`', "'", $row['commentPlain']));
452+
$row['commentHTML'] = linkifyTimestamps($row['commentHTML']);
453+
$row['responses'] = [];
454+
if ($includeResponses) {
455+
$row['responses'] = self::getAllComments($row['videos_id'], $row['id'], 0, $includeResponses);
456+
}
457+
$comment[] = $row;
458+
}
459+
} else {
460+
$comment = false;
461+
_error_log($sql . ' Error : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
462+
}
463+
return $comment;
464+
}
465+
466+
public static function getTotalAllPostedComments() {
467+
if (!User::isAdmin()) {
468+
return 0;
469+
}
470+
$sql = "SELECT c.id FROM comments c LEFT JOIN users as u ON u.id = c.users_id LEFT JOIN videos as v ON v.id = c.videos_id WHERE u.status = 'a' AND c.comments_id_pai IS NULL ";
471+
$sql .= BootGrid::getSqlSearchFromPost(['name']);
472+
$res = sqlDAL::readSql($sql, "", []);
473+
$countRow = sqlDAL::num_rows($res);
474+
sqlDAL::close($res);
475+
return $countRow;
476+
}
477+
426478
public static function userCanAdminComment($comments_id) {
427479
if (!User::isLogged()) {
428480
return false;

objects/myComments.download.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
}
1010

1111
$type = 'posted';
12-
if (!empty($_GET['type']) && $_GET['type'] === 'received') {
13-
$type = 'received';
12+
if (!empty($_GET['type']) && in_array($_GET['type'], ['received', 'all'], true)) {
13+
$type = $_GET['type'];
14+
}
15+
16+
if ($type === 'all' && !User::isAdmin()) {
17+
forbiddenPage('Permission denied', true);
1418
}
1519

1620
$format = 'html';
@@ -26,7 +30,10 @@
2630

2731
setRowCount(1000000);
2832

29-
if ($type === 'received') {
33+
if ($type === 'all') {
34+
$comments = Comment::getAllPostedComments(true);
35+
$typeLabel = __('All Comments');
36+
} elseif ($type === 'received') {
3037
$comments = Comment::getCommentsOnMyVideos(true);
3138
$typeLabel = __('Comments on My Videos');
3239
} else {

objects/myComments.json.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
}
1717

1818
$type = 'posted';
19-
if (!empty($_REQUEST['type']) && $_REQUEST['type'] === 'received') {
20-
$type = 'received';
19+
if (!empty($_REQUEST['type']) && in_array($_REQUEST['type'], ['received', 'all'], true)) {
20+
$type = $_REQUEST['type'];
2121
}
2222

23-
if ($type === 'received') {
23+
if ($type === 'all') {
24+
if (!User::isAdmin()) {
25+
forbiddenPage('Permission denied', true);
26+
}
27+
$comments = Comment::getAllPostedComments(true);
28+
$total = Comment::getTotalAllPostedComments();
29+
} elseif ($type === 'received') {
2430
$comments = Comment::getCommentsOnMyVideos(true);
2531
$total = Comment::getTotalCommentsOnMyVideos();
2632
} else {

view/myComments.php

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
}
1010
require_once $global['systemRootPath'] . 'objects/comment.php';
1111
$userCanUpload = User::canUpload();
12+
$userIsAdmin = User::isAdmin();
1213
$_page = new Page(array('My Comments'));
1314
$commentTemplate = json_encode(file_get_contents($global['systemRootPath'] . 'view/videoComments_template.php'));
1415
?>
@@ -120,7 +121,7 @@
120121
</h3>
121122
</div>
122123
<div class="panel-body">
123-
<?php if ($userCanUpload) { ?>
124+
<?php if ($userCanUpload || $userIsAdmin) { ?>
124125
<ul class="nav nav-tabs" role="tablist">
125126
<li role="presentation" class="active">
126127
<a href="#postedTab" aria-controls="postedTab" role="tab" data-toggle="tab">
@@ -129,13 +130,24 @@
129130
<span class="badge" id="postedBadge" style="display:none;"></span>
130131
</a>
131132
</li>
133+
<?php if ($userCanUpload) { ?>
132134
<li role="presentation">
133135
<a href="#receivedTab" aria-controls="receivedTab" role="tab" data-toggle="tab">
134136
<i class="fas fa-inbox"></i>
135137
<?php echo __('Comments on My Videos'); ?>
136138
<span class="badge" id="receivedBadge" style="display:none;"></span>
137139
</a>
138140
</li>
141+
<?php } ?>
142+
<?php if ($userIsAdmin) { ?>
143+
<li role="presentation">
144+
<a href="#allTab" aria-controls="allTab" role="tab" data-toggle="tab">
145+
<i class="fas fa-list"></i>
146+
<?php echo __('All Comments'); ?>
147+
<span class="badge" id="allBadge" style="display:none;"></span>
148+
</a>
149+
</li>
150+
<?php } ?>
139151
</ul>
140152
<?php } ?>
141153
<div class="tab-content">
@@ -157,6 +169,16 @@
157169
</div>
158170
</div>
159171
<?php } ?>
172+
<?php if ($userIsAdmin) { ?>
173+
<div role="tabpanel" class="tab-pane" id="allTab">
174+
<div id="allCommentsArea" class="myCommentsTabArea canComment userLogged"></div>
175+
<div class="text-center" style="margin-top: 10px;">
176+
<button class="btn btn-link" onclick="loadAllComments(0, lastAllPage + 1);" id="allLoadMoreBtn" style="display:none;">
177+
<i class="fas fa-chevron-down"></i> <?php echo __('Load More'); ?>
178+
</button>
179+
</div>
180+
</div>
181+
<?php } ?>
160182
</div>
161183
</div>
162184
</div>
@@ -165,7 +187,9 @@
165187
var commentTemplate = <?php echo $commentTemplate; ?>;
166188
var lastPostedPage = 0;
167189
var lastReceivedPage = 0;
190+
var lastAllPage = 0;
168191
var receivedLoaded = false;
192+
var allLoaded = false;
169193

170194
function mcGetCommentTemplate(itemsArray) {
171195
var template = commentTemplate;
@@ -384,10 +408,64 @@ function loadReceivedComments(comments_id, page) {
384408
});
385409
}
386410

411+
function loadAllComments(comments_id, page) {
412+
var url = webSiteRootURL + 'objects/myComments.json.php?type=all';
413+
url = addQueryStringParameter(url, 'comments_id', comments_id);
414+
url = addQueryStringParameter(url, 'current', page);
415+
lastAllPage = page;
416+
if (empty(comments_id) && page <= 1 && typeof avideoSetContainerLoading === 'function') {
417+
avideoSetContainerLoading('allCommentsArea', true, {clear: true, items: 4});
418+
}
419+
modal.showPleaseWait();
420+
$.ajax({
421+
url: url,
422+
success: function(response) {
423+
modal.hidePleaseWait();
424+
if (empty(comments_id) && typeof avideoSetContainerLoading === 'function') {
425+
avideoSetContainerLoading('allCommentsArea', false);
426+
}
427+
if (response.error) {
428+
avideoAlertError(response.msg);
429+
} else {
430+
if (empty(comments_id) && page <= 1) {
431+
$('#allCommentsArea').empty();
432+
}
433+
if (response.rows && response.rows.length > 0) {
434+
$.each(response.rows, function(i, row) {
435+
mcAddComment(row, '#allCommentsArea', 0, true);
436+
});
437+
} else if (page <= 1) {
438+
$('#allCommentsArea').html('<div class="my-comments-empty"><i class="fas fa-comments fa-2x"></i><?php echo __('No comments found.'); ?></div>');
439+
}
440+
if (response.rows && response.rows.length > 0 && (response.current * response.rowCount) < response.total) {
441+
$('#allLoadMoreBtn').show();
442+
} else {
443+
$('#allLoadMoreBtn').hide();
444+
}
445+
if (page <= 1) {
446+
$('#allBadge').text(response.total).show();
447+
}
448+
}
449+
},
450+
error: function() {
451+
modal.hidePleaseWait();
452+
if (empty(comments_id) && typeof avideoSetContainerLoading === 'function') {
453+
avideoSetContainerLoading('allCommentsArea', false);
454+
}
455+
avideoAlertError(<?php printJSString('Error loading comments') ?>);
456+
}
457+
});
458+
}
459+
387460
function openDownloadCommentsModal() {
388461
// detect active tab
389462
var activeTab = $('#myCommentsPanel .nav-tabs li.active a').attr('href');
390-
var type = (activeTab === '#receivedTab') ? 'received' : 'posted';
463+
var type = 'posted';
464+
if (activeTab === '#receivedTab') {
465+
type = 'received';
466+
} else if (activeTab === '#allTab') {
467+
type = 'all';
468+
}
391469
$('#downloadCommentsType').val(type);
392470
$('#downloadCommentsModal').modal('show');
393471
}
@@ -414,6 +492,12 @@ function doDownloadComments() {
414492
loadReceivedComments(0, 1);
415493
}
416494
});
495+
$('a[href="#allTab"]').on('shown.bs.tab', function() {
496+
if (!allLoaded) {
497+
allLoaded = true;
498+
loadAllComments(0, 1);
499+
}
500+
});
417501
});
418502
</script>
419503

@@ -435,6 +519,9 @@ function doDownloadComments() {
435519
<?php if ($userCanUpload): ?>
436520
<option value="received"><?php echo __('Comments on My Videos'); ?></option>
437521
<?php endif; ?>
522+
<?php if ($userIsAdmin): ?>
523+
<option value="all"><?php echo __('All Comments'); ?></option>
524+
<?php endif; ?>
438525
</select>
439526
</div>
440527
<div class="form-group">

0 commit comments

Comments
 (0)