Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AttachmentsController
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 index
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * The Administration Attachment 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-11-22
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Administration;
21
22use phpMyFAQ\Attachment\AttachmentCollection;
23use phpMyFAQ\Core\Exception;
24use phpMyFAQ\Enums\PermissionType;
25use phpMyFAQ\Filter;
26use phpMyFAQ\Pagination;
27use phpMyFAQ\Pagination\UrlConfig;
28use phpMyFAQ\Session\Token;
29use phpMyFAQ\Translation;
30use phpMyFAQ\Twig\Extensions\FormatBytesTwigExtension;
31use Symfony\Component\HttpFoundation\Request;
32use Symfony\Component\HttpFoundation\Response;
33use Symfony\Component\Routing\Attribute\Route;
34use Twig\Error\LoaderError;
35use Twig\Extension\AttributeExtension;
36
37final class AttachmentsController extends AbstractAdministrationController
38{
39    public function __construct(
40        private readonly AttachmentCollection $attachmentCollection,
41    ) {
42        parent::__construct();
43    }
44
45    /**
46     * @throws Exception
47     * @throws LoaderError
48     * @throws \Exception
49     */
50    #[Route(path: '/attachments', name: 'admin.attachments', methods: ['GET'])]
51    public function index(Request $request): Response
52    {
53        $this->userHasPermission(PermissionType::ATTACHMENT_DELETE);
54
55        $page = Filter::filterVar($request->query->get('page'), FILTER_VALIDATE_INT, 1);
56        $page = max(1, $page);
57
58        $collection = $this->attachmentCollection;
59
60        $itemsPerPage = 24;
61        $allCrumbs = $collection->getBreadcrumbs();
62
63        $crumbs = array_slice($allCrumbs, ($page - 1) * $itemsPerPage, $itemsPerPage);
64
65        $pagination = new Pagination(
66            baseUrl: $request->getUri(),
67            total: is_countable($allCrumbs) ? count($allCrumbs) : 0,
68            perPage: $itemsPerPage,
69            urlConfig: new UrlConfig(pageParamName: 'page'),
70        );
71
72        $this->addExtension(new AttributeExtension(FormatBytesTwigExtension::class));
73        return $this->render('@admin/content/attachments.twig', [
74            ...$this->getHeader($request),
75            ...$this->getFooter(),
76            'adminHeaderAttachments' => Translation::get(key: 'ad_menu_attachment_admin'),
77            'adminMsgAttachmentsFilename' => Translation::get(key: 'msgAttachmentsFilename'),
78            'adminMsgTransToolLanguage' => Translation::get(key: 'msgTransToolLanguage'),
79            'adminMsgAttachmentsFilesize' => Translation::get(key: 'msgAttachmentsFilesize'),
80            'adminMsgAttachmentsMimeType' => Translation::get(key: 'msgAttachmentsMimeType'),
81            'csrfTokenDeletion' => Token::getInstance($this->session)->getTokenString('delete-attachment'),
82            'csrfTokenRefresh' => Token::getInstance($this->session)->getTokenString('refresh-attachment'),
83            'attachments' => $crumbs,
84            'adminMsgButtonDelete' => Translation::get(key: 'ad_gen_delete'),
85            'adminMsgFaqTitle' => Translation::get(key: 'ad_entry_faq_record'),
86            'adminAttachmentPagination' => $pagination->render(),
87        ]);
88    }
89}