Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
36 / 36 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CommentsController | |
100.00% |
36 / 36 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
35 / 35 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The Administration comments Controller |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | * @copyright 2024-2026 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2024-12-01 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Controller\Administration; |
| 21 | |
| 22 | use phpMyFAQ\Comments; |
| 23 | use phpMyFAQ\Core\Exception; |
| 24 | use phpMyFAQ\Entity\CommentType; |
| 25 | use phpMyFAQ\Enums\PermissionType; |
| 26 | use phpMyFAQ\Filter; |
| 27 | use phpMyFAQ\News; |
| 28 | use phpMyFAQ\Pagination; |
| 29 | use phpMyFAQ\Pagination\UrlConfig; |
| 30 | use phpMyFAQ\Session\Token; |
| 31 | use phpMyFAQ\Twig\Extensions\FaqTwigExtension; |
| 32 | use phpMyFAQ\Twig\Extensions\TitleSlugifierTwigExtension; |
| 33 | use Symfony\Component\HttpFoundation\Request; |
| 34 | use Symfony\Component\HttpFoundation\Response; |
| 35 | use Symfony\Component\Routing\Attribute\Route; |
| 36 | use Twig\Error\LoaderError; |
| 37 | use Twig\Extension\AttributeExtension; |
| 38 | use Twig\Extra\Intl\IntlExtension; |
| 39 | |
| 40 | final class CommentsController extends AbstractAdministrationController |
| 41 | { |
| 42 | public function __construct( |
| 43 | private readonly Comments $comments, |
| 44 | private readonly News $news, |
| 45 | ) { |
| 46 | parent::__construct(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @throws LoaderError |
| 51 | * @throws Exception |
| 52 | * @throws \Exception |
| 53 | */ |
| 54 | #[Route(path: '/comments', name: 'admin.comments', methods: ['GET'])] |
| 55 | public function index(Request $request): Response |
| 56 | { |
| 57 | $this->userHasPermission(PermissionType::COMMENT_DELETE); |
| 58 | |
| 59 | $page = Filter::filterVar($request->query->get(key: 'page'), FILTER_VALIDATE_INT, 1); |
| 60 | $page = max(1, $page); |
| 61 | |
| 62 | $itemsPerPage = 10; |
| 63 | $allFaqComments = $this->comments->getAllComments(); |
| 64 | $allNewsComments = $this->comments->getAllComments(CommentType::NEWS); |
| 65 | |
| 66 | $faqComments = array_slice($allFaqComments, ($page - 1) * $itemsPerPage, $itemsPerPage); |
| 67 | $newsComments = array_slice($allNewsComments, ($page - 1) * $itemsPerPage, $itemsPerPage); |
| 68 | |
| 69 | $newsHeader = $this->news->getHeader(); |
| 70 | |
| 71 | $faqCommentsPagination = new Pagination( |
| 72 | baseUrl: $request->getUri(), |
| 73 | total: is_countable($allFaqComments) ? count($allFaqComments) : 0, |
| 74 | perPage: $itemsPerPage, |
| 75 | urlConfig: new UrlConfig(pageParamName: 'page'), |
| 76 | ); |
| 77 | |
| 78 | $newsCommentsPagination = new Pagination( |
| 79 | baseUrl: $request->getUri(), |
| 80 | total: is_countable($allNewsComments) ? count($allNewsComments) : 0, |
| 81 | perPage: $itemsPerPage, |
| 82 | urlConfig: new UrlConfig(pageParamName: 'page'), |
| 83 | ); |
| 84 | |
| 85 | $this->addExtension(new IntlExtension()); |
| 86 | $this->addExtension(new AttributeExtension(FaqTwigExtension::class)); |
| 87 | $this->addExtension(new AttributeExtension(TitleSlugifierTwigExtension::class)); |
| 88 | return $this->render(file: '@admin/content/comments.twig', context: [ |
| 89 | ...$this->getHeader($request), |
| 90 | ...$this->getFooter(), |
| 91 | 'currentLocale' => $this->configuration->getLanguage()->getLanguage(), |
| 92 | 'faqComments' => $faqComments, |
| 93 | 'newsComments' => $newsComments, |
| 94 | 'faqCommentsPagination' => $faqCommentsPagination->render(), |
| 95 | 'newsCommentsPagination' => $newsCommentsPagination->render(), |
| 96 | 'newsHeader' => $newsHeader, |
| 97 | 'csrfToken' => Token::getInstance($this->session)->getTokenString(page: 'delete-comment'), |
| 98 | ]); |
| 99 | } |
| 100 | } |