Skip to content

Commit 46d6686

Browse files
committed
feat: added pagination for comment administration
1 parent 53ea7f2 commit 46d6686

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

phpmyfaq/assets/templates/admin/content/comments.twig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
</td>
6464
</tr>
6565
{% endfor %}
66+
<tfoot>
67+
<tr>
68+
<td colspan="2">{{ faqCommentsPagination | raw }}</td>
69+
</tr>
70+
</tfoot>
6671
</table>
6772

6873
</form>
@@ -107,6 +112,11 @@
107112
</td>
108113
</tr>
109114
{% endfor %}
115+
<tfoot>
116+
<tr>
117+
<td colspan="2">{{ newsCommentsPagination | raw }}</td>
118+
</tr>
119+
</tfoot>
110120
</table>
111121

112122
</form>

phpmyfaq/src/phpMyFAQ/Controller/Administration/CommentsController.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use phpMyFAQ\Core\Exception;
2323
use phpMyFAQ\Entity\CommentType;
2424
use phpMyFAQ\Enums\PermissionType;
25+
use phpMyFAQ\Filter;
26+
use phpMyFAQ\Pagination;
2527
use phpMyFAQ\Session\Token;
2628
use phpMyFAQ\Twig\Extensions\FaqTwigExtension;
2729
use Symfony\Component\HttpFoundation\Request;
@@ -43,20 +45,48 @@ public function index(Request $request): Response
4345
{
4446
$this->userHasPermission(PermissionType::COMMENT_DELETE);
4547

48+
$page = Filter::filterVar($request->query->get(key: 'page'), FILTER_VALIDATE_INT);
49+
$page = max(1, $page);
50+
4651
$comment = $this->container->get(id: 'phpmyfaq.comments');
4752

48-
$faqComments = $comment->getAllComments();
49-
$newsComments = $comment->getAllComments(CommentType::NEWS);
53+
$itemsPerPage = 10;
54+
$allFaqComments = $comment->getAllComments();
55+
$allNewsComments = $comment->getAllComments(CommentType::NEWS);
56+
57+
$faqComments = array_slice($allFaqComments, ($page - 1) * $itemsPerPage, $itemsPerPage);
58+
$newsComments = array_slice($allNewsComments, ($page - 1) * $itemsPerPage, $itemsPerPage);
59+
60+
$baseUrl = sprintf('%sadmin/comments?page=%d', $this->configuration->getDefaultUrl(), $page);
61+
62+
$faqCommentsPagination = new Pagination([
63+
'baseUrl' => $baseUrl,
64+
'total' => is_countable($allFaqComments) ? count($allFaqComments) : 0,
65+
'perPage' => $itemsPerPage,
66+
]);
67+
68+
$newsCommentsPagination = new Pagination([
69+
'baseUrl' => $baseUrl,
70+
'total' => is_countable($allNewsComments) ? count($allNewsComments) : 0,
71+
'perPage' => $itemsPerPage,
72+
]);
5073

5174
$this->addExtension(new IntlExtension());
5275
$this->addExtension(new AttributeExtension(FaqTwigExtension::class));
53-
return $this->render('@admin/content/comments.twig', [
54-
...$this->getHeader($request),
55-
...$this->getFooter(),
56-
'currentLocale' => $this->configuration->getLanguage()->getLanguage(),
57-
'faqComments' => $faqComments,
58-
'newsComments' => $newsComments,
59-
'csrfToken' => Token::getInstance($this->container->get(id: 'session'))->getTokenString('delete-comment'),
60-
]);
76+
return $this->render(
77+
file: '@admin/content/comments.twig',
78+
context: [
79+
...$this->getHeader($request),
80+
...$this->getFooter(),
81+
'currentLocale' => $this->configuration->getLanguage()->getLanguage(),
82+
'faqComments' => $faqComments,
83+
'newsComments' => $newsComments,
84+
'faqCommentsPagination' => $faqCommentsPagination->render(),
85+
'newsCommentsPagination' => $newsCommentsPagination->render(),
86+
'csrfToken' => Token::getInstance($this->container->get(id: 'session'))->getTokenString(
87+
page: 'delete-comment',
88+
),
89+
],
90+
);
6191
}
6292
}

0 commit comments

Comments
 (0)