Skip to content

Commit 7f86de9

Browse files
committed
refactor: moved search statistics page to Twig (#2791)
1 parent fdc563f commit 7f86de9

File tree

12 files changed

+305
-173
lines changed

12 files changed

+305
-173
lines changed

phpmyfaq/admin/assets/src/api/statistics.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,24 @@ export const deleteAdminLog = async (csrfToken) => {
3333
console.error(error);
3434
}
3535
};
36+
37+
export const truncateSearchTerms = async (csrfToken) => {
38+
try {
39+
const response = await fetch(`./api/statistics/search-terms`, {
40+
method: 'DELETE',
41+
cache: 'no-cache',
42+
headers: {
43+
'Content-Type': 'application/json',
44+
},
45+
body: JSON.stringify({
46+
csrfToken: csrfToken,
47+
}),
48+
redirect: 'follow',
49+
referrerPolicy: 'no-referrer',
50+
});
51+
52+
return await response.json();
53+
} catch (error) {
54+
console.error(error);
55+
}
56+
};

phpmyfaq/admin/assets/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { handleUserList, handleUsers } from './user';
5353
import { handleGroups } from './group';
5454
import { handlePasswordStrength, handlePasswordToggle } from '../../../assets/src/utils';
5555
import { sidebarToggle } from './utils';
56+
import { handleTruncateSearchTerms } from './statistics/search';
5657

5758
document.addEventListener('DOMContentLoaded', async () => {
5859
'use strict';
@@ -114,6 +115,7 @@ document.addEventListener('DOMContentLoaded', async () => {
114115
handleDeleteAdminLog();
115116
handleStatistics();
116117
handleCreateReport();
118+
handleTruncateSearchTerms();
117119

118120
// Configuration → FAQ configuration
119121
await handleConfiguration();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Search Term Handling
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public License,
5+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
6+
* obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* @package phpMyFAQ
9+
* @author Jan Harms <[email protected]>
10+
* @copyright 2024 phpMyFAQ Team
11+
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
12+
* @link https://www.phpmyfaq.de
13+
* @since 2024-05-09
14+
*/
15+
16+
import { truncateSearchTerms } from '../api';
17+
import { pushErrorNotification, pushNotification } from '../utils';
18+
19+
export const handleTruncateSearchTerms = () => {
20+
const buttonTruncateSearchTerms = document.getElementById('pmf-button-truncate-search-terms');
21+
22+
if (buttonTruncateSearchTerms) {
23+
buttonTruncateSearchTerms.addEventListener('click', async (event) => {
24+
event.preventDefault();
25+
26+
const csrf = event.target.getAttribute('data-pmf-csrf-token');
27+
28+
if (confirm('Are you sure?')) {
29+
const response = await truncateSearchTerms(csrf);
30+
31+
if (response.success) {
32+
const tableToDelete = document.getElementById('pmf-table-search-terms');
33+
tableToDelete.remove();
34+
pushNotification(response.success);
35+
} else {
36+
pushErrorNotification(response.error);
37+
}
38+
}
39+
});
40+
}
41+
};

phpmyfaq/admin/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@
326326
break;
327327
case 'clear-statistics':
328328
case 'statistics':
329-
require 'stat.ratings.php';
329+
require 'statistics.ratings.php';
330330
break;
331331
case 'truncatesearchterms':
332332
case 'searchstats':
333-
require 'stat.search.php';
333+
require 'statistics.search.php';
334334
break;
335335
// Reports
336336
case 'reports':

phpmyfaq/admin/stat.search.php

Lines changed: 0 additions & 163 deletions
This file was deleted.
File renamed without changes.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/**
4+
* Frontend for search log statistics.
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 Anatoliy Belsky <[email protected]>
12+
* @author Thorsten Rinne <[email protected]>
13+
* @copyright 2003-2024 phpMyFAQ Team
14+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
15+
* @link https://www.phpmyfaq.de
16+
* @since 2003-03-30
17+
*/
18+
19+
use phpMyFAQ\Configuration;
20+
use phpMyFAQ\Enums\PermissionType;
21+
use phpMyFAQ\Filter;
22+
use phpMyFAQ\Pagination;
23+
use phpMyFAQ\Search;
24+
use phpMyFAQ\Session\Token;
25+
use phpMyFAQ\Template\LanguageCodeTwigExtension;
26+
use phpMyFAQ\Template\TwigWrapper;
27+
use phpMyFAQ\Translation;
28+
use phpMyFAQ\User\CurrentUser;
29+
use Symfony\Component\HttpFoundation\Request;
30+
use Twig\Extension\DebugExtension;
31+
32+
if (!defined('IS_VALID_PHPMYFAQ')) {
33+
http_response_code(400);
34+
exit();
35+
}
36+
37+
$faqConfig = Configuration::getConfigurationInstance();
38+
$user = CurrentUser::getCurrentUser($faqConfig);
39+
40+
$request = Request::createFromGlobals();
41+
42+
if ($user->perm->hasPermission($user->getUserId(), PermissionType::STATISTICS_VIEWLOGS->value)) {
43+
$perPage = 10;
44+
$pages = Filter::filterVar($request->query->get('pages'), FILTER_VALIDATE_INT);
45+
$page = Filter::filterVar($request->query->get('page'), FILTER_VALIDATE_INT, 1);
46+
47+
$search = new Search($faqConfig);
48+
49+
$twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates');
50+
$twig->addExtension(new DebugExtension());
51+
$twig->addExtension(new LanguageCodeTwigExtension());
52+
$template = $twig->loadTemplate('./admin/statistics/search.twig');
53+
54+
$searchesCount = $search->getSearchesCount();
55+
$searchesList = $search->getMostPopularSearches($searchesCount + 1, true);
56+
57+
if (is_null($pages)) {
58+
$pages = round(((is_countable($searchesList) ? count($searchesList) : 0) + ($perPage / 3)) / $perPage, 0);
59+
}
60+
61+
$start = ($page - 1) * $perPage;
62+
$end = $start + $perPage;
63+
64+
$baseUrl = sprintf(
65+
'%sadmin/?action=searchstats&amp;page=%d',
66+
$faqConfig->getDefaultUrl(),
67+
$page
68+
);
69+
70+
// Pagination options
71+
$options = [
72+
'baseUrl' => $request->getUri(),
73+
'total' => is_countable($searchesList) ? count($searchesList) : 0,
74+
'perPage' => $perPage,
75+
'pageParamName' => 'page',
76+
];
77+
$pagination = new Pagination($options);
78+
79+
$templateVars = [
80+
'ad_menu_searchstats' => Translation::get('ad_menu_searchstats'),
81+
'csrfToken' => Token::getInstance()->getTokenString('truncate-search-terms'),
82+
'ad_searchterm_del' => Translation::get('ad_searchterm_del'),
83+
'ad_searchstats_search_term' => Translation::get('ad_searchstats_search_term'),
84+
'ad_searchstats_search_term_count' => Translation::get('ad_searchstats_search_term_count'),
85+
'ad_searchstats_search_term_lang' => Translation::get('ad_searchstats_search_term_lang'),
86+
'ad_searchstats_search_term_percentage' => Translation::get('ad_searchstats_search_term_percentage'),
87+
'pagination' => $pagination->render(),
88+
'searchesCount' => $searchesCount,
89+
'searchesList' => $searchesList,
90+
'csrfTokenDelete' => Token::getInstance()->getTokenString('delete-searchterm'),
91+
'ad_news_delete' => Translation::get('ad_news_delete'),
92+
];
93+
94+
echo $template->render($templateVars);
95+
} else {
96+
require __DIR__ . '/no-permission.php';
97+
}

phpmyfaq/admin/statistics.show.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@
1515
* @since 2003-02-24
1616
*/
1717

18+
use phpMyFAQ\Configuration;
1819
use phpMyFAQ\Enums\PermissionType;
1920
use phpMyFAQ\Filter;
2021
use phpMyFAQ\Session;
2122
use phpMyFAQ\Template\TwigWrapper;
2223
use phpMyFAQ\Translation;
24+
use phpMyFAQ\User\CurrentUser;
2325
use Twig\Extension\DebugExtension;
2426

2527
if (!defined('IS_VALID_PHPMYFAQ')) {
2628
http_response_code(400);
2729
exit();
2830
}
2931

32+
$faqConfig = Configuration::getConfigurationInstance();
33+
$user = CurrentUser::getCurrentUser($faqConfig);
34+
3035
$sessionId = Filter::filterInput(INPUT_GET, 'id', FILTER_VALIDATE_INT);
3136

3237
if ($user->perm->hasPermission($user->getUserId(), PermissionType::STATISTICS_VIEWLOGS->value)) {

0 commit comments

Comments
 (0)