-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: moved open questions to Twig (#2791)
- Loading branch information
Showing
7 changed files
with
196 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/** | ||
* Delete open questions. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* @package phpMyFAQ | ||
* @author Thorsten Rinne <[email protected]> | ||
* @copyright 2003-2024 phpMyFAQ Team | ||
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 | ||
* @link https://www.phpmyfaq.de | ||
* @since 2003-02-24 | ||
*/ | ||
|
||
use phpMyFAQ\Category; | ||
use phpMyFAQ\Configuration; | ||
use phpMyFAQ\Date; | ||
use phpMyFAQ\Enums\PermissionType; | ||
use phpMyFAQ\Filter; | ||
use phpMyFAQ\Question; | ||
use phpMyFAQ\Session\Token; | ||
use phpMyFAQ\Template\CategoryNameTwigExtension; | ||
use phpMyFAQ\Template\TwigWrapper; | ||
use phpMyFAQ\Translation; | ||
use phpMyFAQ\User\CurrentUser; | ||
use Twig\Extension\DebugExtension; | ||
use Twig\Extra\Intl\IntlExtension; | ||
|
||
if (!defined('IS_VALID_PHPMYFAQ')) { | ||
http_response_code(400); | ||
exit(); | ||
} | ||
|
||
$faqConfig = Configuration::getConfigurationInstance(); | ||
$user = CurrentUser::getCurrentUser($faqConfig); | ||
|
||
if ($user->perm->hasPermission($user->getUserId(), PermissionType::QUESTION_DELETE->value)) { | ||
$category = new Category($faqConfig, [], false); | ||
$question = new Question($faqConfig); | ||
$category->setUser($currentAdminUser); | ||
$category->setGroups($currentAdminGroups); | ||
$date = new Date($faqConfig); | ||
|
||
$questionId = Filter::filterInput(INPUT_GET, 'id', FILTER_VALIDATE_INT); | ||
$csrfToken = Filter::filterInput(INPUT_GET, 'csrf', FILTER_SANITIZE_SPECIAL_CHARS); | ||
|
||
if ($csrfToken && Token::getInstance()->verifyToken('toggle-question-visibility', $csrfToken)) { | ||
$csrfChecked = true; | ||
} else { | ||
$csrfChecked = false; | ||
} | ||
|
||
$toggle = Filter::filterInput(INPUT_GET, 'is_visible', FILTER_SANITIZE_SPECIAL_CHARS); | ||
if ($csrfChecked && $toggle === 'toggle') { | ||
$isVisible = $question->getVisibility($questionId); | ||
$question->setVisibility($questionId, ($isVisible == 'N' ? 'Y' : 'N')); | ||
} | ||
|
||
$twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates'); | ||
$twig->addExtension(new IntlExtension()); | ||
$twig->addExtension(new DebugExtension()); | ||
$twig->addExtension(new CategoryNameTwigExtension()); | ||
$template = $twig->loadTemplate('./admin/content/open-questions.twig'); | ||
|
||
$openQuestions = $question->getAll(); | ||
|
||
$templateVars = [ | ||
'msgOpenQuestions' => Translation::get('msgOpenQuestions'), | ||
'csrfTokenDeleteQuestion' => Token::getInstance()->getTokenString('delete-questions'), | ||
'currentLocale' => $faqConfig->getLanguage()->getLanguage(), | ||
'msgAuthor' => Translation::get('ad_entry_author'), | ||
'msgQuestion' => Translation::get('ad_entry_theme'), | ||
'msgVisibility' => Translation::get('ad_entry_visibility'), | ||
'questions' => $openQuestions, | ||
'yes' => Translation::get('ad_gen_yes'), | ||
'no' => Translation::get('ad_gen_no'), | ||
'enableCloseQuestion' => $faqConfig->get('records.enableCloseQuestion'), | ||
'msg2answerFAQ' => Translation::get('msg2answerFAQ'), | ||
'msgTakeQuestion' => Translation::get('ad_ques_take'), | ||
'csrfTokenToggleVisibility' => Token::getInstance()->getTokenString('toggle-question-visibility'), | ||
'msgDeleteAllOpenQuestions' => Translation::get('ad_entry_delete'), | ||
]; | ||
|
||
echo $template->render($templateVars); | ||
} else { | ||
require __DIR__ . '/no-permission.php'; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> | ||
<h1 class="h2"> | ||
<i aria-hidden="true" class="bi bi-question-circle"></i> | ||
{{ msgOpenQuestions }} | ||
</h1> | ||
</div> | ||
|
||
<div class="row"> | ||
<div class="col-lg-12"> | ||
|
||
<div id="returnMessage"></div> | ||
|
||
<form id="phpmyfaq-open-questions" name="phpmyfaq-open-questions" method="post" accept-charset="utf-8"> | ||
<input type="hidden" id="pmf-csrf-token" name="pmf-csrf-token" value="{{ csrfTokenDeleteQuestion }}"> | ||
|
||
<table class="table table-striped align-middle"> | ||
<thead> | ||
<tr> | ||
<th></th> | ||
<th>{{ msgAuthor }}</th> | ||
<th>{{ msgQuestion }}</th> | ||
<th colspan="2">{{ msgVisibility }}?</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for question in questions %} | ||
<tr> | ||
<td> | ||
<label> | ||
<input id="questions[]" name="questions[]" value="{{ question.id }}" type="checkbox"> | ||
</label> | ||
</td> | ||
<td> | ||
{{ question.created | format_datetime(locale=currentLocale) }} | ||
<br> | ||
<a href="mailto:{{ question.email }}"> | ||
{{ question.username }} | ||
</a> | ||
</td> | ||
<td> | ||
<strong> | ||
{{ question.categoryId | categoryName }} | ||
</strong> | ||
<br> | ||
{{ question.question }} | ||
</td> | ||
<td> | ||
<a href="?action=question&id={{ question.id }}&is_visible=toggle&csrf={{ csrfTokenToggleVisibility }}" | ||
class="btn btn-info"> | ||
{{ question.isVisible == 'Y' ? yes : no }} | ||
</a> | ||
</td> | ||
<td> | ||
{% if enableCloseQuestion and question.answerId %} | ||
<a href="?action=editentry&id={{ question.answerId }}&lang={{ currentLocale }}" | ||
class="btn btn-success"> | ||
{{ msg2answerFAQ }} | ||
</a> | ||
{% else %} | ||
<a href="?action=takequestion&id={{ question.id }}" class="btn btn-success"> | ||
{{ msgTakeQuestion }} | ||
</a> | ||
{% endif %} | ||
</td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
|
||
<div class="text-end"> | ||
<button class="btn btn-danger" id="pmf-delete-questions" type="button"> | ||
{{ msgDeleteAllOpenQuestions }} | ||
</button> | ||
</div> | ||
|
||
</form> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
phpmyfaq/src/phpMyFAQ/Template/CategoryNameTwigExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace phpMyFAQ\Template; | ||
|
||
use phpMyFAQ\Category; | ||
use phpMyFAQ\Configuration; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
class CategoryNameTwigExtension extends AbstractExtension | ||
{ | ||
public function getFilters(): array | ||
{ | ||
return [ | ||
new TwigFilter('categoryName', $this->getCategoryName(...)), | ||
]; | ||
} | ||
|
||
private function getCategoryName(int $categoryId): string | ||
{ | ||
$category = new Category(Configuration::getConfigurationInstance()); | ||
|
||
$categoryData = $category->getCategoryData($categoryId); | ||
return $categoryData->getName(); | ||
} | ||
} |