Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
StartpageController
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
2 / 2
4
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%
43 / 43
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Start page 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 2002-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     2002-08-23
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Frontend;
21
22use phpMyFAQ\Category\Startpage;
23use phpMyFAQ\Core\Exception;
24use phpMyFAQ\Faq;
25use phpMyFAQ\Faq\Statistics;
26use phpMyFAQ\Filter;
27use phpMyFAQ\Language\Plurals;
28use phpMyFAQ\News;
29use phpMyFAQ\Tags;
30use phpMyFAQ\Translation;
31use phpMyFAQ\Twig\Extensions\TagNameTwigExtension;
32use phpMyFAQ\User\CurrentUser;
33use Symfony\Component\HttpFoundation\Request;
34use Symfony\Component\HttpFoundation\Response;
35use Symfony\Component\Routing\Attribute\Route;
36use Twig\Extension\AttributeExtension;
37
38final class StartpageController extends AbstractFrontController
39{
40    public function __construct(
41        private readonly Plurals $plurals,
42        private readonly Faq $faq,
43        private readonly Tags $tags,
44    ) {
45        parent::__construct();
46    }
47
48    /**
49     * Displays the start page with categories, Top 10, and latest messages.
50     *
51     * @throws Exception
52     * @throws \Exception
53     */
54    #[Route(path: '/', name: 'public.index', methods: ['GET', 'POST'])]
55    public function index(Request $request): Response
56    {
57        $news = new News($this->configuration);
58
59        [$currentUser, $currentGroups] = CurrentUser::getCurrentUserGroupId($this->currentUser);
60
61        $faqStatistics = new Statistics($this->configuration);
62        $faqStatistics->setUser($currentUser)->setGroups($currentGroups);
63
64        $startpage = new Startpage($this->configuration);
65        $startpage
66            ->setLanguage($this->configuration->getLanguage()->getLanguage())
67            ->setUser($currentUser)
68            ->setGroups($currentGroups);
69
70        $startPageCategories = $startpage->getCategories();
71
72        // Get top ten parameter
73        $param = $this->configuration->get('records.orderingPopularFaqs') === 'visits' ? 'visits' : 'voted';
74
75        $this->faq->setUser($currentUser);
76        $this->faq->setGroups($currentGroups);
77
78        $this->tags->setUser($currentUser)->setGroups($currentGroups);
79
80        Filter::filterVar($request->query->get('cat'), FILTER_VALIDATE_INT, 0);
81
82        $this->addExtension(new AttributeExtension(TagNameTwigExtension::class));
83        return $this->render('startpage.twig', [
84            ...$this->getHeader($request),
85            'baseHref' => $this->faqSystem->getSystemUri($this->configuration),
86            'title' => $this->configuration->getTitle(),
87            'pageHeader' => $this->configuration->getTitle(),
88            'startPageCategories' => (is_countable($startPageCategories) ? count($startPageCategories) : 0) > 0,
89            'startPageCategoryDecks' => $startPageCategories,
90            'stickyRecordsHeader' => Translation::get(key: 'stickyRecordsHeader'),
91            'stickyRecordsList' => $this->faq->getStickyFaqsData(),
92            'writeTopTenHeader' => Translation::get(key: 'msgTopTen'),
93            'topRecordsList' => $faqStatistics->getTopTen($param),
94            'errorMsgTopTen' => Translation::get(key: 'err_noTopTen'),
95            'writeNewestHeader' => Translation::get(key: 'msgLatestArticles'),
96            'latestRecordsList' => $faqStatistics->getLatest(),
97            'errorMsgLatest' => Translation::get(key: 'msgErrorNoRecords'),
98            'msgTrendingFAQs' => Translation::get(key: 'msgTrendingFAQs'),
99            'trendingRecordsList' => $faqStatistics->getTrending(),
100            'errorMsgTrendingFaqs' => Translation::get(key: 'msgErrorNoRecords'),
101            'msgNewsHeader' => Translation::get(key: 'newsArchive'),
102            'newsList' => $news->getAll(),
103            'writeNumberOfArticles' => $this->plurals->get(
104                'plmsgHomeArticlesOnline',
105                $faqStatistics->totalFaqs($this->configuration->getLanguage()->getLanguage()),
106            ),
107            'msgTags' => Translation::get(key: 'msgPopularTags'),
108            'tagsList' => $this->tags->getPopularTags(12),
109        ]);
110    }
111}