From a939f4d0d4f97e496e72c7309f1dee952e0e1c45 Mon Sep 17 00:00:00 2001 From: Thorsten Rinne Date: Sat, 30 Nov 2024 11:52:00 +0100 Subject: [PATCH] refactor: migrated report page to controller (#3257) --- phpmyfaq/admin/header.php | 34 +----- phpmyfaq/admin/index.php | 5 - phpmyfaq/admin/report.main.php | 59 --------- .../admin/statistics/report.main.twig | 108 ----------------- .../templates/admin/statistics/report.twig | 112 ++++++++++++++++++ phpmyfaq/src/admin-routes.php | 6 + .../AbstractAdministrationController.php | 6 +- .../Administration/Api/ExportController.php | 2 +- .../Administration/ReportController.php | 53 +++++++++ 9 files changed, 179 insertions(+), 206 deletions(-) delete mode 100644 phpmyfaq/admin/report.main.php delete mode 100644 phpmyfaq/assets/templates/admin/statistics/report.main.twig create mode 100644 phpmyfaq/assets/templates/admin/statistics/report.twig create mode 100644 phpmyfaq/src/phpMyFAQ/Controller/Administration/ReportController.php diff --git a/phpmyfaq/admin/header.php b/phpmyfaq/admin/header.php index c04de149a9..04fc0475d6 100644 --- a/phpmyfaq/admin/header.php +++ b/phpmyfaq/admin/header.php @@ -127,7 +127,12 @@ 'msgAdminElasticsearchStats', 'statistics/search' ); -$secLevelEntries['statistics'] .= $adminHelper->addMenuEntry('reports', 'reports', 'ad_menu_reports'); +$secLevelEntries['statistics'] .= $adminHelper->addMenuEntry( + 'reports', + 'reports', + 'ad_menu_reports', + 'statistics/report' +); $secLevelEntries['imports_exports'] = $adminHelper->addMenuEntry( PermissionType::FAQ_ADD->value, @@ -170,11 +175,6 @@ $secLevelEntries['config'] .= $adminHelper->addMenuEntry('editconfig', 'system', 'ad_system_info', 'system'); switch ($action) { - case 'user': - case 'group': - case 'cookies': - $userPage = true; - break; case 'category-overview': case 'addcategory': case 'savecategory': @@ -206,28 +206,6 @@ case 'stickyfaqs': $contentPage = true; break; - case 'statistics': - case 'viewsessions': - case 'sessionbrowse': - case 'adminlog': - case 'searchstats': - case 'reports': - case 'reportview': - $statisticsPage = true; - break; - case 'export': - case 'importcsv': - $exportsPage = true; - break; - case 'backup': - $backupPage = true; - break; - case 'config': - case 'stopwordsconfig': - case 'upgrade': - case 'instances': - case 'system': - case 'elasticsearch': case 'forms': $configurationPage = true; break; diff --git a/phpmyfaq/admin/index.php b/phpmyfaq/admin/index.php index b31aa37049..617b9ca019 100755 --- a/phpmyfaq/admin/index.php +++ b/phpmyfaq/admin/index.php @@ -314,11 +314,6 @@ case 'glossary': require 'glossary.php'; break; - // Reports - case 'reportview': - case 'reports': - require 'report.main.php'; - break; // Config administration case 'forms': require 'forms.php'; diff --git a/phpmyfaq/admin/report.main.php b/phpmyfaq/admin/report.main.php deleted file mode 100644 index 361563e039..0000000000 --- a/phpmyfaq/admin/report.main.php +++ /dev/null @@ -1,59 +0,0 @@ - - * @author Thorsten Rinne - * @copyright 2011-2024 phpMyFAQ Team - * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 - * @link https://www.phpmyfaq.de - * @since 2011-01-12 - */ - -use phpMyFAQ\Configuration; -use phpMyFAQ\Enums\PermissionType; -use phpMyFAQ\Session\Token; -use phpMyFAQ\Template\TwigWrapper; -use phpMyFAQ\Translation; -use phpMyFAQ\User\CurrentUser; - -if (!defined('IS_VALID_PHPMYFAQ')) { - http_response_code(400); - exit(); -} - -$faqConfig = Configuration::getConfigurationInstance(); -$user = CurrentUser::getCurrentUser($faqConfig); - -if ($user->perm->hasPermission($user->getUserId(), PermissionType::REPORTS->value)) { - $templateVars = [ - 'ad_menu_reports' => Translation::get('ad_menu_reports'), - 'csrfTokenInput' => Token::getInstance($container->get('session'))->getTokenInput('create-report'), - 'ad_stat_report_make_report' => Translation::get('ad_stat_report_make_report'), - 'ad_stat_report_fields' => Translation::get('ad_stat_report_fields'), - 'ad_stat_report_category' => Translation::get('ad_stat_report_category'), - 'ad_stat_report_sub_category' => Translation::get('ad_stat_report_sub_category'), - 'ad_stat_report_translations' => Translation::get('ad_stat_report_translations'), - 'ad_stat_report_language' => Translation::get('ad_stat_report_language'), - 'ad_stat_report_id' => Translation::get('ad_stat_report_id'), - 'ad_stat_report_sticky' => Translation::get('ad_stat_report_sticky'), - 'ad_stat_report_title' => Translation::get('ad_stat_report_title'), - 'ad_stat_report_creation_date' => Translation::get('ad_stat_report_creation_date'), - 'ad_stat_report_owner' => Translation::get('ad_stat_report_owner'), - 'ad_stat_report_last_modified_person' => Translation::get('ad_stat_report_last_modified_person'), - 'ad_stat_report_url' => Translation::get('ad_stat_report_url'), - 'ad_stat_report_visits' => Translation::get('ad_stat_report_visits') - ]; - - $twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates'); - $template = $twig->loadTemplate('@admin/statistics/report.main.twig'); - - echo $template->render($templateVars); -} else { - require __DIR__ . '/no-permission.php'; -} diff --git a/phpmyfaq/assets/templates/admin/statistics/report.main.twig b/phpmyfaq/assets/templates/admin/statistics/report.main.twig deleted file mode 100644 index eae14caee0..0000000000 --- a/phpmyfaq/assets/templates/admin/statistics/report.main.twig +++ /dev/null @@ -1,108 +0,0 @@ -
- {{ csrfTokenInput | raw }} -
-

- {{ ad_menu_reports }} -

-
-
- -
-
-
- -
-
-

{{ ad_stat_report_fields }}

-
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
- -
- - -
-
- - -
-
- - -
-
- - -
-
- -
-
diff --git a/phpmyfaq/assets/templates/admin/statistics/report.twig b/phpmyfaq/assets/templates/admin/statistics/report.twig new file mode 100644 index 0000000000..5f3ebe3597 --- /dev/null +++ b/phpmyfaq/assets/templates/admin/statistics/report.twig @@ -0,0 +1,112 @@ +{% extends '@admin/index.twig' %} + +{% block content %} +
+ {{ csrfTokenInput | raw }} +
+

+ {{ 'ad_menu_reports' | translate }} +

+
+
+ +
+
+
+ +
+
+

{{ 'ad_stat_report_fields' | translate }}

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+{% endblock %} diff --git a/phpmyfaq/src/admin-routes.php b/phpmyfaq/src/admin-routes.php index 82015bc520..19cbcf6a9c 100644 --- a/phpmyfaq/src/admin-routes.php +++ b/phpmyfaq/src/admin-routes.php @@ -26,6 +26,7 @@ use phpMyFAQ\Controller\Administration\InstanceController; use phpMyFAQ\Controller\Administration\PasswordChangeController; use phpMyFAQ\Controller\Administration\RatingController; +use phpMyFAQ\Controller\Administration\ReportController; use phpMyFAQ\Controller\Administration\SessionKeepAliveController; use phpMyFAQ\Controller\Administration\StatisticsSearchController; use phpMyFAQ\Controller\Administration\StatisticsSessionsController; @@ -159,6 +160,11 @@ 'controller' => [RatingController::class, 'index'], 'methods' => 'GET' ], + 'admin.statistics.report' => [ + 'path' => '/statistics/report', + 'controller' => [ReportController::class, 'index'], + 'methods' => 'GET' + ], 'admin.statistics.search' => [ 'path' => '/statistics/search', 'controller' => [StatisticsSearchController::class, 'index'], diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Administration/AbstractAdministrationController.php b/phpmyfaq/src/phpMyFAQ/Controller/Administration/AbstractAdministrationController.php index 2ae8efcd92..f55665834d 100644 --- a/phpmyfaq/src/phpMyFAQ/Controller/Administration/AbstractAdministrationController.php +++ b/phpmyfaq/src/phpMyFAQ/Controller/Administration/AbstractAdministrationController.php @@ -236,11 +236,6 @@ protected function getHeader(Request $request): array case 'stickyfaqs': $contentPage = true; break; - case 'searchstats': - case 'reports': - case 'reportview': - $statisticsPage = true; - break; case 'forms': $configurationPage = true; break; @@ -269,6 +264,7 @@ protected function getHeader(Request $request): array break; case 'admin.statistics.admin-log': case 'admin.statistics.ratings': + case 'admin.statistics.report': case 'admin.statistics.sessions': case 'admin.statistics.session.day': case 'admin.statistics.session.id': diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/ExportController.php b/phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/ExportController.php index 516076fc71..8dcfe40246 100644 --- a/phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/ExportController.php +++ b/phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/ExportController.php @@ -162,7 +162,7 @@ public function exportReport(Request $request): Response $handle = fopen('php://temp', 'r+'); foreach ($text as $row) { - fputcsv($handle, $row); + fputcsv($handle, $row, ',', '"', '\\', PHP_EOL); } rewind($handle); diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Administration/ReportController.php b/phpmyfaq/src/phpMyFAQ/Controller/Administration/ReportController.php new file mode 100644 index 0000000000..c3fcf72cb5 --- /dev/null +++ b/phpmyfaq/src/phpMyFAQ/Controller/Administration/ReportController.php @@ -0,0 +1,53 @@ + + * @copyright 2024 phpMyFAQ Team + * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 + * @link https://www.phpmyfaq.de + * @since 2024-11-30 + */ + +declare(strict_types=1); + +namespace phpMyFAQ\Controller\Administration; + +use phpMyFAQ\Core\Exception; +use phpMyFAQ\Enums\PermissionType; +use phpMyFAQ\Session\Token; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Attribute\Route; +use Twig\Error\LoaderError; + +class ReportController extends AbstractAdministrationController +{ + /** + * @throws LoaderError + * @throws Exception + * @throws \Exception + */ + #[Route('/statistics/report', name: 'admin.statistics.report', methods: ['GET'])] + public function index(Request $request): Response + { + $this->userHasPermission(PermissionType::REPORTS); + + $session = $this->container->get('session'); + + return $this->render( + '@admin/statistics/report.twig', + [ + ... $this->getHeader($request), + ... $this->getFooter(), + 'csrfTokenInput' => Token::getInstance($session)->getTokenInput('create-report'), + ] + ); + } +}