|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * News 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 | + * @author Matteo Scaramuccia <matteo@scaramuccia.com> |
| 13 | + * @copyright 2006-2026 phpMyFAQ Team |
| 14 | + * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 15 | + * @link https://www.phpmyfaq.de |
| 16 | + * @since 2006-07-23 |
| 17 | + */ |
| 18 | + |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +namespace phpMyFAQ\Controller\Frontend; |
| 22 | + |
| 23 | +use phpMyFAQ\Captcha\Helper\CaptchaHelper; |
| 24 | +use phpMyFAQ\Comments; |
| 25 | +use phpMyFAQ\Core\Exception; |
| 26 | +use phpMyFAQ\Entity\CommentType; |
| 27 | +use phpMyFAQ\Filter; |
| 28 | +use phpMyFAQ\Helper\CommentHelper; |
| 29 | +use phpMyFAQ\News\NewsService; |
| 30 | +use phpMyFAQ\Session\Token; |
| 31 | +use phpMyFAQ\Translation; |
| 32 | +use Symfony\Component\HttpFoundation\Request; |
| 33 | +use Symfony\Component\HttpFoundation\Response; |
| 34 | +use Symfony\Component\Routing\Attribute\Route; |
| 35 | + |
| 36 | +final class NewsController extends AbstractFrontController |
| 37 | +{ |
| 38 | + /** |
| 39 | + * Displays a news article with comments. |
| 40 | + * |
| 41 | + * @throws Exception |
| 42 | + * @throws \Exception |
| 43 | + */ |
| 44 | + #[Route(path: '/news/{newsId}/{newsLang}/{slug}.html', name: 'public.news', requirements: [ |
| 45 | + 'newsId' => '\d+', |
| 46 | + 'newsLang' => '[a-z\-_]+', |
| 47 | + ])] |
| 48 | + public function index(Request $request): Response |
| 49 | + { |
| 50 | + $newsId = Filter::filterVar($request->attributes->get('newsId'), FILTER_VALIDATE_INT); |
| 51 | + |
| 52 | + if ($newsId === false || $newsId === null) { |
| 53 | + return $this->render('404.twig', [ |
| 54 | + ...$this->getHeader($request), |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + $faqSession = $this->container->get('phpmyfaq.user.session'); |
| 59 | + $faqSession->setCurrentUser($this->currentUser); |
| 60 | + $faqSession->userTracking('news_view', $newsId); |
| 61 | + |
| 62 | + $newsService = new NewsService($this->configuration, $this->currentUser); |
| 63 | + $news = $newsService->getProcessedNews($newsId); |
| 64 | + |
| 65 | + $captcha = $this->container->get('phpmyfaq.captcha'); |
| 66 | + $captchaHelper = CaptchaHelper::getInstance($this->configuration); |
| 67 | + |
| 68 | + $commentHelper = new CommentHelper(); |
| 69 | + $commentHelper->setConfiguration($this->configuration); |
| 70 | + |
| 71 | + $comment = new Comments($this->configuration); |
| 72 | + $comments = $comment->getCommentsData($newsId, CommentType::NEWS); |
| 73 | + |
| 74 | + $session = $this->container->get('session'); |
| 75 | + |
| 76 | + return $this->render('news.twig', [ |
| 77 | + ...$this->getHeader($request), |
| 78 | + 'writeNewsHeader' => $this->configuration->getTitle() . Translation::get(key: 'msgNews'), |
| 79 | + 'newsHeader' => $news['processedHeader'], |
| 80 | + 'mainPageContent' => $news['processedContent'], |
| 81 | + 'writeDateMsg' => $newsService->formatNewsDate($news), |
| 82 | + 'msgAboutThisNews' => Translation::get(key: 'msgAboutThisNews'), |
| 83 | + 'writeAuthor' => $newsService->getAuthorInfo($news), |
| 84 | + 'editThisEntry' => $newsService->getEditLink($newsId), |
| 85 | + 'writeCommentMsg' => $newsService->getCommentMessage($news), |
| 86 | + 'msgWriteComment' => Translation::get(key: 'newsWriteComment'), |
| 87 | + 'newsId' => $newsId, |
| 88 | + 'newsLang' => $news['lang'], |
| 89 | + 'msgCommentHeader' => Translation::get(key: 'msgCommentHeader'), |
| 90 | + 'msgNewContentName' => Translation::get(key: 'msgNewContentName'), |
| 91 | + 'msgNewContentMail' => Translation::get(key: 'msgNewContentMail'), |
| 92 | + 'defaultContentMail' => $this->currentUser->getUserId() > 0 ? $this->currentUser->getUserData('email') : '', |
| 93 | + 'defaultContentName' => $this->currentUser->getUserId() > 0 |
| 94 | + ? $this->currentUser->getUserData('display_name') |
| 95 | + : '', |
| 96 | + 'msgYourComment' => Translation::get(key: 'msgYourComment'), |
| 97 | + 'csrfInput' => Token::getInstance($session)->getTokenInput('add-comment'), |
| 98 | + 'msgCancel' => Translation::get(key: 'ad_gen_cancel'), |
| 99 | + 'msgNewContentSubmit' => Translation::get(key: 'msgNewContentSubmit'), |
| 100 | + 'captchaFieldset' => $captchaHelper->renderCaptcha( |
| 101 | + $captcha, |
| 102 | + 'writecomment', |
| 103 | + Translation::get(key: 'msgCaptcha'), |
| 104 | + $this->currentUser->isLoggedIn(), |
| 105 | + ), |
| 106 | + 'renderComments' => $commentHelper->getComments($comments), |
| 107 | + ]); |
| 108 | + } |
| 109 | +} |
0 commit comments