|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * FAQ 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 <[email protected]> |
| 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-09-16 |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace phpMyFAQ\Controller\Frontend; |
| 21 | + |
| 22 | +use phpMyFAQ\Faq\FaqService; |
| 23 | +use phpMyFAQ\Filter; |
| 24 | +use phpMyFAQ\System; |
| 25 | +use phpMyFAQ\Translation; |
| 26 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 27 | +use Symfony\Component\HttpFoundation\Request; |
| 28 | +use Symfony\Component\HttpFoundation\Response; |
| 29 | +use Symfony\Component\Routing\Attribute\Route; |
| 30 | +use Twig\TwigFilter; |
| 31 | + |
| 32 | +final class FaqController extends AbstractFrontController |
| 33 | +{ |
| 34 | + /** |
| 35 | + * Displays the form to add a new FAQ |
| 36 | + * |
| 37 | + * @throws \Exception |
| 38 | + */ |
| 39 | + #[Route(path: '/add-faq.html', name: 'public.faq.add', methods: ['GET'])] |
| 40 | + public function add(Request $request): Response |
| 41 | + { |
| 42 | + $faqSession = $this->container->get('phpmyfaq.user.session'); |
| 43 | + $faqSession->setCurrentUser($this->currentUser); |
| 44 | + $faqSession->userTracking('new_entry', 0); |
| 45 | + |
| 46 | + // Get current groups |
| 47 | + $currentGroups = $this->currentUser->perm->getUserGroups($this->currentUser->getUserId()); |
| 48 | + |
| 49 | + $faqService = new FaqService($this->configuration, $this->currentUser, $currentGroups); |
| 50 | + |
| 51 | + if (!$faqService->canUserAddFaq()) { |
| 52 | + if ($this->currentUser->getUserId() === -1) { |
| 53 | + return new RedirectResponse($this->configuration->getDefaultUrl() . 'login'); |
| 54 | + } |
| 55 | + |
| 56 | + return new RedirectResponse($this->configuration->getDefaultUrl()); |
| 57 | + } |
| 58 | + |
| 59 | + $selectedQuestion = Filter::filterVar($request->query->get('question'), FILTER_VALIDATE_INT); |
| 60 | + $selectedCategory = Filter::filterVar($request->query->get('cat'), FILTER_VALIDATE_INT, -1); |
| 61 | + |
| 62 | + $faqData = $faqService->prepareAddFaqData($selectedQuestion, $selectedCategory); |
| 63 | + |
| 64 | + $captcha = $this->container->get('phpmyfaq.captcha'); |
| 65 | + $captchaHelper = $this->container->get('phpmyfaq.captcha.helper.captcha_helper'); |
| 66 | + |
| 67 | + // Add Twig filter |
| 68 | + $this->addFilter(new TwigFilter('repeat', static fn($string, $times): string => str_repeat( |
| 69 | + (string) $string, |
| 70 | + $times, |
| 71 | + ))); |
| 72 | + |
| 73 | + // Prepare template variables |
| 74 | + $templateVars = [ |
| 75 | + ...$this->getHeader($request), |
| 76 | + 'title' => sprintf('%s - %s', Translation::get(key: 'msgAddContent'), $this->configuration->getTitle()), |
| 77 | + 'metaDescription' => sprintf( |
| 78 | + '%s | %s', |
| 79 | + Translation::get(key: 'msgNewContentHeader'), |
| 80 | + $this->configuration->getTitle(), |
| 81 | + ), |
| 82 | + 'msgNewContentHeader' => Translation::get(key: 'msgNewContentHeader'), |
| 83 | + 'msgNewContentAddon' => Translation::get(key: 'msgNewContentAddon'), |
| 84 | + 'lang' => $this->configuration->getLanguage()->getLanguage(), |
| 85 | + 'openQuestionID' => $faqData['selectedQuestion'], |
| 86 | + 'defaultContentMail' => $faqService->getDefaultUserEmail(), |
| 87 | + 'defaultContentName' => $faqService->getDefaultUserName(), |
| 88 | + 'msgNewContentName' => Translation::get(key: 'msgNewContentName'), |
| 89 | + 'msgNewContentMail' => Translation::get(key: 'msgNewContentMail'), |
| 90 | + 'msgNewContentCategory' => Translation::get(key: 'msgNewContentCategory'), |
| 91 | + 'selectedCategory' => $faqData['selectedCategory'], |
| 92 | + 'categories' => $faqData['categories'], |
| 93 | + 'msgNewContentTheme' => Translation::get(key: 'msgNewContentTheme'), |
| 94 | + 'readonly' => $faqData['readonly'], |
| 95 | + 'question' => $faqData['question'], |
| 96 | + 'msgNewContentArticle' => Translation::get(key: 'msgNewContentArticle'), |
| 97 | + 'msgNewContentKeywords' => Translation::get(key: 'msgNewContentKeywords'), |
| 98 | + 'msgNewContentLink' => Translation::get(key: 'msgNewContentLink'), |
| 99 | + 'captchaFieldset' => $captchaHelper->renderCaptcha( |
| 100 | + $captcha, |
| 101 | + 'add', |
| 102 | + Translation::get(key: 'msgCaptcha'), |
| 103 | + $this->currentUser->isLoggedIn(), |
| 104 | + ), |
| 105 | + 'msgNewContentSubmit' => Translation::get(key: 'msgNewContentSubmit'), |
| 106 | + 'enableWysiwygEditor' => $this->configuration->get('main.enableWysiwygEditorFrontend'), |
| 107 | + 'currentTimestamp' => $request->server->get('REQUEST_TIME'), |
| 108 | + 'msgSeparateKeywordsWithCommas' => Translation::get(key: 'msgSeparateKeywordsWithCommas'), |
| 109 | + 'noCategories' => $faqData['noCategories'], |
| 110 | + 'msgFormDisabledDueToMissingCategories' => Translation::get(key: 'msgFormDisabledDueToMissingCategories'), |
| 111 | + 'displayFullForm' => $faqData['displayFullForm'], |
| 112 | + ]; |
| 113 | + |
| 114 | + // Collect data for displaying form |
| 115 | + foreach ($faqData['formData'] as $input) { |
| 116 | + $active = sprintf('id%d_active', (int) $input->input_id); |
| 117 | + $label = sprintf('id%d_label', (int) $input->input_id); |
| 118 | + $required = sprintf('id%d_required', (int) $input->input_id); |
| 119 | + $templateVars[$active] = (bool) $input->input_active; |
| 120 | + $templateVars[$label] = $input->input_label; |
| 121 | + $templateVars[$required] = (int) $input->input_required !== 0 ? 'required' : ''; |
| 122 | + } |
| 123 | + |
| 124 | + return $this->render('add.twig', $templateVars); |
| 125 | + } |
| 126 | +} |
0 commit comments