Skip to content

Commit 2b00d3c

Browse files
committed
refactor: migrated ask question page (#3834)
1 parent cba5067 commit 2b00d3c

File tree

8 files changed

+606
-175
lines changed

8 files changed

+606
-175
lines changed

phpmyfaq/ask.php

Lines changed: 0 additions & 99 deletions
This file was deleted.

phpmyfaq/src/phpMyFAQ/Controller/Frontend/OpenQuestionsController.php

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
3+
/**
4+
* Open Questions 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-17
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace phpMyFAQ\Controller\Frontend;
21+
22+
use phpMyFAQ\Category;
23+
use phpMyFAQ\Core\Exception;
24+
use phpMyFAQ\Enums\PermissionType;
25+
use phpMyFAQ\Faq\QuestionService;
26+
use phpMyFAQ\Filter;
27+
use phpMyFAQ\Helper\QuestionHelper;
28+
use phpMyFAQ\Translation;
29+
use Symfony\Component\HttpFoundation\RedirectResponse;
30+
use Symfony\Component\HttpFoundation\Request;
31+
use Symfony\Component\HttpFoundation\Response;
32+
use Symfony\Component\Routing\Attribute\Route;
33+
use Twig\TwigFilter;
34+
35+
final class QuestionsController extends AbstractFrontController
36+
{
37+
/**
38+
* Displays the open questions page.
39+
*
40+
* @throws Exception
41+
* @throws \Exception
42+
*/
43+
#[Route(path: '/open-questions.html', name: 'public.open-questions')]
44+
public function index(Request $request): Response
45+
{
46+
$faqSession = $this->container->get('phpmyfaq.user.session');
47+
$faqSession->setCurrentUser($this->currentUser);
48+
$faqSession->userTracking('open_questions', 0);
49+
50+
$category = new Category($this->configuration);
51+
$questionHelper = new QuestionHelper();
52+
$questionHelper->setConfiguration($this->configuration)->setCategory($category);
53+
54+
return $this->render('open-questions.twig', [
55+
...$this->getHeader($request),
56+
'title' => sprintf('%s - %s', Translation::get(key: 'msgOpenQuestions'), $this->configuration->getTitle()),
57+
'metaDescription' => sprintf(
58+
Translation::get(key: 'msgOpenQuestionsMetaDesc'),
59+
$this->configuration->getTitle(),
60+
),
61+
'pageHeader' => Translation::get(key: 'msgOpenQuestions'),
62+
'msgQuestionText' => Translation::get(key: 'msgQuestionText'),
63+
'msgDate_User' => Translation::get(key: 'msgDate_User'),
64+
'msgQuestion2' => Translation::get(key: 'msgQuestion2'),
65+
'openQuestions' => $questionHelper->getOpenQuestions(),
66+
'isCloseQuestionEnabled' => $this->configuration->get('records.enableCloseQuestion'),
67+
'userHasPermissionToAnswer' => $this->currentUser->perm->hasPermission(
68+
$this->currentUser->getUserId(),
69+
PermissionType::FAQ_ADD->value,
70+
),
71+
'msgQuestionsWaiting' => Translation::get(key: 'msgQuestionsWaiting'),
72+
'msgNoQuestionsAvailable' => Translation::get(key: 'msgNoQuestionsAvailable'),
73+
'msg2answerFAQ' => Translation::get(key: 'msg2answerFAQ'),
74+
'msg2answer' => Translation::get(key: 'msg2answer'),
75+
]);
76+
}
77+
78+
/**
79+
* Displays the form to ask a new question
80+
*
81+
* @throws \Exception
82+
*/
83+
#[Route(path: '/ask.html', name: 'public.question.ask', methods: ['GET'])]
84+
public function ask(Request $request): Response
85+
{
86+
$faqSession = $this->container->get('phpmyfaq.user.session');
87+
$faqSession->setCurrentUser($this->currentUser);
88+
$faqSession->userTracking('ask_question', 0);
89+
90+
// Get current groups
91+
$currentGroups = $this->currentUser->perm->getUserGroups($this->currentUser->getUserId());
92+
93+
$questionService = new QuestionService($this->configuration, $this->currentUser, $currentGroups);
94+
95+
if (!$questionService->canUserAskQuestion()) {
96+
return new RedirectResponse($this->configuration->getDefaultUrl() . 'login');
97+
}
98+
99+
$categoryId = Filter::filterVar($request->query->get('category_id'), FILTER_VALIDATE_INT, 0);
100+
101+
$questionData = $questionService->prepareAskQuestionData($categoryId);
102+
103+
$captcha = $this->container->get('phpmyfaq.captcha');
104+
$captchaHelper = $this->container->get('phpmyfaq.captcha.helper.captcha_helper');
105+
106+
// Add Twig filter
107+
$this->addFilter(new TwigFilter('repeat', static fn($string, $times): string => str_repeat(
108+
(string) $string,
109+
$times,
110+
)));
111+
112+
// Prepare template variables
113+
$templateVars = [
114+
...$this->getHeader($request),
115+
'title' => sprintf('%s - %s', Translation::get(key: 'msgQuestion'), $this->configuration->getTitle()),
116+
'metaDescription' => sprintf(
117+
Translation::get(key: 'msgQuestionMetaDesc'),
118+
$this->configuration->getTitle(),
119+
),
120+
'msgMatchingQuestions' => Translation::get(key: 'msgMatchingQuestions'),
121+
'msgFinishSubmission' => Translation::get(key: 'msgFinishSubmission'),
122+
'lang' => $this->configuration->getLanguage()->getLanguage(),
123+
'defaultContentMail' => $questionService->getDefaultUserEmail(),
124+
'defaultContentName' => $questionService->getDefaultUserName(),
125+
'selectedCategory' => $questionData['selectedCategory'],
126+
'categories' => $questionData['categories'],
127+
'captchaFieldset' => $captchaHelper->renderCaptcha(
128+
$captcha,
129+
'ask',
130+
Translation::get(key: 'msgCaptcha'),
131+
$this->currentUser->isLoggedIn(),
132+
),
133+
'msgNewContentSubmit' => Translation::get(key: 'msgNewContentSubmit'),
134+
'noCategories' => $questionData['noCategories'],
135+
'msgFormDisabledDueToMissingCategories' => Translation::get(key: 'msgFormDisabledDueToMissingCategories'),
136+
];
137+
138+
// Collect data for displaying form
139+
foreach ($questionData['formData'] as $input) {
140+
if ((int) $input->input_active !== 0) {
141+
$label = sprintf('id%d_label', (int) $input->input_id);
142+
$required = sprintf('id%d_required', (int) $input->input_id);
143+
$templateVars[$label] = $input->input_label;
144+
$templateVars[$required] = (int) $input->input_required !== 0 ? 'required' : '';
145+
}
146+
}
147+
148+
return $this->render('ask.twig', $templateVars);
149+
}
150+
}

0 commit comments

Comments
 (0)