From a06db764fefac06ea40bce3efc16f354f9be83b9 Mon Sep 17 00:00:00 2001 From: Thorsten Rinne Date: Sun, 1 Dec 2024 12:53:35 +0100 Subject: [PATCH] refactor: migrated comments page to controller (#3257) --- nginx.conf | 2 +- phpmyfaq/.htaccess | 2 +- phpmyfaq/admin/comments.php | 62 ----- phpmyfaq/admin/header.php | 4 +- phpmyfaq/admin/index.php | 3 - .../templates/admin/content/comments.twig | 212 +++++++++--------- phpmyfaq/src/admin-routes.php | 6 + .../AbstractAdministrationController.php | 13 +- .../Administration/CommentsController.php | 49 ++++ phpmyfaq/src/services.php | 6 + 10 files changed, 182 insertions(+), 177 deletions(-) delete mode 100644 phpmyfaq/admin/comments.php create mode 100644 phpmyfaq/src/phpMyFAQ/Controller/Administration/CommentsController.php diff --git a/nginx.conf b/nginx.conf index 742a688f56..5ed8a85886 100644 --- a/nginx.conf +++ b/nginx.conf @@ -129,7 +129,7 @@ server { rewrite admin/api/(.*) /admin/api/index.php last; # Administration pages - rewrite admin/(attachments|backup|configuration|elasticsearch|export|glossary|group|import|instance|instances|password|session-keep-alive|statistics|stopwords|system|tags|update|user) /admin/front.php last; + rewrite admin/(attachments|backup|comments|configuration|elasticsearch|export|glossary|group|import|instance|instances|password|session-keep-alive|statistics|stopwords|system|tags|update|user) /admin/front.php last; # REST API v3.0 and v3.1 rewrite ^api/v3\.[01]/(.*) /api/index.php last; diff --git a/phpmyfaq/.htaccess b/phpmyfaq/.htaccess index 995b5312ce..182e6b55ac 100644 --- a/phpmyfaq/.htaccess +++ b/phpmyfaq/.htaccess @@ -143,7 +143,7 @@ Header set Access-Control-Allow-Headers "Content-Type, Authorization" # Administration API RewriteRule ^admin/api/(.*) admin/api/index.php [L,QSA] # Administration pages - RewriteRule ^admin/(attachments|backup|configuration|elasticsearch|export|glossary|group|import|instance|instances|password|session-keep-alive|statistics|stopwords|system|tags|update|user) admin/front.php [L,QSA] + RewriteRule ^admin/(attachments|backup|comments|configuration|elasticsearch|export|glossary|group|import|instance|instances|password|session-keep-alive|statistics|stopwords|system|tags|update|user) admin/front.php [L,QSA] # Private APIs RewriteRule ^api/(autocomplete|bookmark/delete|bookmark/create|user/data/update|user/password/update|user/request-removal|user/remove-twofactor|contact|voting|register|captcha|share|comment/create|faq/create|question/create|webauthn/prepare|webauthn/register|webauthn/prepare-login|webauthn/login) api/index.php [L,QSA] # Setup APIs diff --git a/phpmyfaq/admin/comments.php b/phpmyfaq/admin/comments.php deleted file mode 100644 index a59cc577d4..0000000000 --- a/phpmyfaq/admin/comments.php +++ /dev/null @@ -1,62 +0,0 @@ - - * @copyright 2007-2024 phpMyFAQ Team - * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 - * @link https://www.phpmyfaq.de - * @since 2007-03-04 - */ - -use phpMyFAQ\Comments; -use phpMyFAQ\Configuration; -use phpMyFAQ\Date; -use phpMyFAQ\Entity\CommentType; -use phpMyFAQ\Enums\PermissionType; -use phpMyFAQ\Faq; -use phpMyFAQ\Session\Token; -use phpMyFAQ\Template\Extensions\FaqTwigExtension; -use phpMyFAQ\Template\TwigWrapper; -use phpMyFAQ\User\CurrentUser; -use Twig\Extra\Intl\IntlExtension; - -if (!defined('IS_VALID_PHPMYFAQ')) { - http_response_code(400); - exit(); -} - -$faqConfig = Configuration::getConfigurationInstance(); -$user = CurrentUser::getCurrentUser($faqConfig); -[$currentAdminUser, $currentAdminGroups] = CurrentUser::getCurrentUserGroupId($user); - -$twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates'); -$twig->addExtension(new IntlExtension()); -$twig->addExtension(new FaqTwigExtension()); -$template = $twig->loadTemplate('@admin/content/comments.twig'); - -if ($user->perm->hasPermission($user->getUserId(), PermissionType::COMMENT_DELETE->value)) { - $comment = new Comments($faqConfig); - $faq = new Faq($faqConfig); - $date = new Date($faqConfig); - - $faqComments = $comment->getAllComments(); - $newsComments = $comment->getAllComments(CommentType::NEWS); - - $templateVars = [ - 'currentLocale' => $faqConfig->getLanguage()->getLanguage(), - 'faqComments' => $faqComments, - 'newsComments' => $newsComments, - 'csrfToken' => Token::getInstance($container->get('session'))->getTokenString('delete-comment'), - ]; - - echo $template->render($templateVars); -} else { - require __DIR__ . '/no-permission.php'; -} diff --git a/phpmyfaq/admin/header.php b/phpmyfaq/admin/header.php index 0f8755f29d..7d0e50520a 100644 --- a/phpmyfaq/admin/header.php +++ b/phpmyfaq/admin/header.php @@ -80,7 +80,7 @@ 'stickyRecordsHeader' ); $secLevelEntries['content'] .= $adminHelper->addMenuEntry('delquestion', 'question', 'ad_menu_open'); -$secLevelEntries['content'] .= $adminHelper->addMenuEntry('delcomment', 'comments', 'ad_menu_comments'); +$secLevelEntries['content'] .= $adminHelper->addMenuEntry('delcomment', 'comments', 'ad_menu_comments', 'comments'); $secLevelEntries['content'] .= $adminHelper->addMenuEntry( 'addattachment+editattachment+delattachment', 'attachments', @@ -195,8 +195,6 @@ case 'delete-news': case 'question': case 'takequestion': - case 'comments': - case 'attachments': case 'stickyfaqs': $contentPage = true; break; diff --git a/phpmyfaq/admin/index.php b/phpmyfaq/admin/index.php index 4bebca952b..d90db9c1e0 100755 --- a/phpmyfaq/admin/index.php +++ b/phpmyfaq/admin/index.php @@ -270,9 +270,6 @@ case 'question': require 'open-questions.php'; break; - case 'comments': - require 'comments.php'; - break; case 'stickyfaqs': require 'stickyfaqs.php'; break; diff --git a/phpmyfaq/assets/templates/admin/content/comments.twig b/phpmyfaq/assets/templates/admin/content/comments.twig index a6b92891c6..ab5df92201 100644 --- a/phpmyfaq/assets/templates/admin/content/comments.twig +++ b/phpmyfaq/assets/templates/admin/content/comments.twig @@ -1,114 +1,120 @@ -
-

- - {{ 'adminHeaderComments' | translate }} -

-
+{% extends '@admin/index.twig' %} -
+{% block content %} +
+

+ + {{ 'adminHeaderComments' | translate }} +

+
- -
-
-
- +
- - {% for comment in faqComments %} - - - + + {% endfor %} +
- - -
- -
-
- - {{ comment.date | format_datetime(locale=currentLocale) }} -
-
- - - {{ comment.recordId | faqQuestion | raw }} - + +
+
+ + + + + {% for comment in faqComments %} + + + - - {% endfor %} -
+ + +
+ +
+
+ + {{ comment.date | format_datetime(locale=currentLocale) }} +
+ +
- - - {{ comment.comment }} -
+ {{ comment.comment | striptags }} +
-
-
- + +
+ +
-
-
-
- +
+ + - - {% for comment in newsComments %} - - - + + {% endfor %} +
- - -
- -
-
- - {{ comment.date | format_datetime(locale=currentLocale) }} + + {% for comment in newsComments %} + + + - - {% endfor %} -
+ + +
+ +
+
+ + {{ comment.date | format_datetime(locale=currentLocale) }} +
+
+ + + +
+
-
- - - -
- - - {{ comment.comment }} -
+ {{ comment.comment }} +
- -
- + +
+ +
-
+{% endblock %} diff --git a/phpmyfaq/src/admin-routes.php b/phpmyfaq/src/admin-routes.php index e13be276cd..f9fa398664 100644 --- a/phpmyfaq/src/admin-routes.php +++ b/phpmyfaq/src/admin-routes.php @@ -18,6 +18,7 @@ use phpMyFAQ\Controller\Administration\AdminLogController; use phpMyFAQ\Controller\Administration\AttachmentsController; use phpMyFAQ\Controller\Administration\BackupController; +use phpMyFAQ\Controller\Administration\CommentsController; use phpMyFAQ\Controller\Administration\ConfigurationController; use phpMyFAQ\Controller\Administration\ElasticsearchController; use phpMyFAQ\Controller\Administration\ExportController; @@ -62,6 +63,11 @@ 'controller' => [BackupController::class, 'restore'], 'methods' => 'POST' ], + 'admin.comments' => [ + 'path' => '/comments', + 'controller' => [CommentsController::class, 'index'], + 'methods' => 'GET' + ], 'admin.configuration' => [ 'path' => '/configuration', 'controller' => [ConfigurationController::class, 'index'], diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Administration/AbstractAdministrationController.php b/phpmyfaq/src/phpMyFAQ/Controller/Administration/AbstractAdministrationController.php index bfe7b63fe5..28702a2013 100644 --- a/phpmyfaq/src/phpMyFAQ/Controller/Administration/AbstractAdministrationController.php +++ b/phpmyfaq/src/phpMyFAQ/Controller/Administration/AbstractAdministrationController.php @@ -96,22 +96,26 @@ protected function getHeader(Request $request): array $secLevelEntries['content'] .= $adminHelper->addMenuEntry( 'delcomment', 'comments', - 'ad_menu_comments' + 'ad_menu_comments', + 'comments' ); $secLevelEntries['content'] .= $adminHelper->addMenuEntry( 'addattachment+editattachment+delattachment', 'attachments', - 'ad_menu_attachments' + 'ad_menu_attachments', + 'attachments' ); $secLevelEntries['content'] .= $adminHelper->addMenuEntry( PermissionType::FAQ_EDIT->value, 'tags', - 'ad_entry_tags' + 'ad_entry_tags', + 'tags' ); $secLevelEntries['content'] .= $adminHelper->addMenuEntry( 'addglossary+editglossary+delglossary', 'glossary', - 'ad_menu_glossary' + 'ad_menu_glossary', + 'glossary' ); $secLevelEntries['content'] .= $adminHelper->addMenuEntry( 'addnews+editnews+delnews', @@ -260,6 +264,7 @@ protected function getHeader(Request $request): array $userPage = true; break; case 'admin.attachments': + case 'admin.comments': case 'admin.glossary': case 'admin.tags': $contentPage = true; diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Administration/CommentsController.php b/phpmyfaq/src/phpMyFAQ/Controller/Administration/CommentsController.php new file mode 100644 index 0000000000..b2c45138e9 --- /dev/null +++ b/phpmyfaq/src/phpMyFAQ/Controller/Administration/CommentsController.php @@ -0,0 +1,49 @@ +userHasPermission(PermissionType::COMMENT_DELETE); + + $comment = $this->container->get('phpmyfaq.comments'); + + $faqComments = $comment->getAllComments(); + $newsComments = $comment->getAllComments(CommentType::NEWS); + + $this->addExtension(new IntlExtension()); + $this->addExtension(new FaqTwigExtension()); + return $this->render( + '@admin/content/comments.twig', + [ + ... $this->getHeader($request), + ... $this->getFooter(), + 'currentLocale' => $this->configuration->getLanguage()->getLanguage(), + 'faqComments' => $faqComments, + 'newsComments' => $newsComments, + 'csrfToken' => Token::getInstance($this->container->get('session'))->getTokenString('delete-comment'), + ] + ); + } +} diff --git a/phpmyfaq/src/services.php b/phpmyfaq/src/services.php index 1702b98967..1bb57604fd 100644 --- a/phpmyfaq/src/services.php +++ b/phpmyfaq/src/services.php @@ -27,6 +27,7 @@ use phpMyFAQ\Captcha\Helper\CaptchaHelper; use phpMyFAQ\Category\Order; use phpMyFAQ\Category\Permission; +use phpMyFAQ\Comments; use phpMyFAQ\Configuration; use phpMyFAQ\Database\DatabaseHelper; use phpMyFAQ\Date; @@ -127,6 +128,11 @@ new Reference('phpmyfaq.configuration') ]); + $services->set('phpmyfaq.comments', Comments::class) + ->args([ + new Reference('phpmyfaq.configuration') + ]); + $services->set('phpmyfaq.configuration', Configuration::class) ->factory([Configuration::class, 'getConfigurationInstance']);