Skip to content

Commit

Permalink
refactor: moved open questions to Twig (#2791)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Apr 26, 2024
1 parent 88db232 commit d2b1efb
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 147 deletions.
2 changes: 1 addition & 1 deletion phpmyfaq/admin/index.php
Expand Up @@ -275,7 +275,7 @@
require 'record.delatt.php';
break;
case 'question':
require 'record.questions.php';
require 'open-questions.php';
break;
case 'comments':
require 'record.comments.php';
Expand Down
90 changes: 90 additions & 0 deletions phpmyfaq/admin/open-questions.php
@@ -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 <thorsten@phpmyfaq.de>
* @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';
}
144 changes: 0 additions & 144 deletions phpmyfaq/admin/record.questions.php

This file was deleted.

1 change: 0 additions & 1 deletion phpmyfaq/admin/statistics.admin-log.php
Expand Up @@ -22,7 +22,6 @@
use phpMyFAQ\Filter;
use phpMyFAQ\Pagination;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Strings;
use phpMyFAQ\Template\TwigWrapper;
use phpMyFAQ\Template\UserNameTwigExtension;
use phpMyFAQ\Translation;
Expand Down
78 changes: 78 additions & 0 deletions phpmyfaq/assets/templates/admin/content/open-questions.twig
@@ -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&amp;id={{ question.answerId }}&amp;lang={{ currentLocale }}"
class="btn btn-success">
{{ msg2answerFAQ }}
</a>
{% else %}
<a href="?action=takequestion&amp;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>
2 changes: 1 addition & 1 deletion phpmyfaq/src/phpMyFAQ/Question.php
Expand Up @@ -156,7 +156,7 @@ public function getAll(bool $showAll = true): array
->setEmail($row->email)
->setCategoryId($row->category_id)
->setQuestion($row->question)
->setCreated($row->created)
->setCreated(Date::createIsoDate($row->created))
->setAnswerId($row->answer_id)
->setIsVisible($row->is_visible === 'Y');

Expand Down
26 changes: 26 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Template/CategoryNameTwigExtension.php
@@ -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();
}
}

0 comments on commit d2b1efb

Please sign in to comment.