Skip to content

Commit

Permalink
refactor: moved search statistics page to Twig (#2791)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed May 9, 2024
1 parent fdc563f commit 7f86de9
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 173 deletions.
21 changes: 21 additions & 0 deletions phpmyfaq/admin/assets/src/api/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,24 @@ export const deleteAdminLog = async (csrfToken) => {
console.error(error);
}
};

export const truncateSearchTerms = async (csrfToken) => {
try {
const response = await fetch(`./api/statistics/search-terms`, {
method: 'DELETE',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
csrfToken: csrfToken,
}),
redirect: 'follow',
referrerPolicy: 'no-referrer',
});

return await response.json();
} catch (error) {
console.error(error);
}
};
2 changes: 2 additions & 0 deletions phpmyfaq/admin/assets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { handleUserList, handleUsers } from './user';
import { handleGroups } from './group';
import { handlePasswordStrength, handlePasswordToggle } from '../../../assets/src/utils';
import { sidebarToggle } from './utils';
import { handleTruncateSearchTerms } from './statistics/search';

document.addEventListener('DOMContentLoaded', async () => {
'use strict';
Expand Down Expand Up @@ -114,6 +115,7 @@ document.addEventListener('DOMContentLoaded', async () => {
handleDeleteAdminLog();
handleStatistics();
handleCreateReport();
handleTruncateSearchTerms();

// Configuration → FAQ configuration
await handleConfiguration();
Expand Down
41 changes: 41 additions & 0 deletions phpmyfaq/admin/assets/src/statistics/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Search Term Handling
*
* 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 Jan Harms <[email protected]>
* @copyright 2024 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-05-09
*/

import { truncateSearchTerms } from '../api';
import { pushErrorNotification, pushNotification } from '../utils';

export const handleTruncateSearchTerms = () => {
const buttonTruncateSearchTerms = document.getElementById('pmf-button-truncate-search-terms');

if (buttonTruncateSearchTerms) {
buttonTruncateSearchTerms.addEventListener('click', async (event) => {
event.preventDefault();

const csrf = event.target.getAttribute('data-pmf-csrf-token');

if (confirm('Are you sure?')) {
const response = await truncateSearchTerms(csrf);

if (response.success) {
const tableToDelete = document.getElementById('pmf-table-search-terms');
tableToDelete.remove();
pushNotification(response.success);
} else {
pushErrorNotification(response.error);
}
}
});
}
};
4 changes: 2 additions & 2 deletions phpmyfaq/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,11 @@
break;
case 'clear-statistics':
case 'statistics':
require 'stat.ratings.php';
require 'statistics.ratings.php';
break;
case 'truncatesearchterms':
case 'searchstats':
require 'stat.search.php';
require 'statistics.search.php';
break;
// Reports
case 'reports':
Expand Down
163 changes: 0 additions & 163 deletions phpmyfaq/admin/stat.search.php

This file was deleted.

File renamed without changes.
97 changes: 97 additions & 0 deletions phpmyfaq/admin/statistics.search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* Frontend for search log statistics.
*
* 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 Anatoliy Belsky <[email protected]>
* @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-03-30
*/

use phpMyFAQ\Configuration;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Pagination;
use phpMyFAQ\Search;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Template\LanguageCodeTwigExtension;
use phpMyFAQ\Template\TwigWrapper;
use phpMyFAQ\Translation;
use phpMyFAQ\User\CurrentUser;
use Symfony\Component\HttpFoundation\Request;
use Twig\Extension\DebugExtension;

if (!defined('IS_VALID_PHPMYFAQ')) {
http_response_code(400);
exit();
}

$faqConfig = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($faqConfig);

$request = Request::createFromGlobals();

if ($user->perm->hasPermission($user->getUserId(), PermissionType::STATISTICS_VIEWLOGS->value)) {
$perPage = 10;
$pages = Filter::filterVar($request->query->get('pages'), FILTER_VALIDATE_INT);
$page = Filter::filterVar($request->query->get('page'), FILTER_VALIDATE_INT, 1);

$search = new Search($faqConfig);

$twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates');
$twig->addExtension(new DebugExtension());
$twig->addExtension(new LanguageCodeTwigExtension());
$template = $twig->loadTemplate('./admin/statistics/search.twig');

$searchesCount = $search->getSearchesCount();
$searchesList = $search->getMostPopularSearches($searchesCount + 1, true);

if (is_null($pages)) {
$pages = round(((is_countable($searchesList) ? count($searchesList) : 0) + ($perPage / 3)) / $perPage, 0);
}

$start = ($page - 1) * $perPage;
$end = $start + $perPage;

$baseUrl = sprintf(
'%sadmin/?action=searchstats&amp;page=%d',
$faqConfig->getDefaultUrl(),
$page
);

// Pagination options
$options = [
'baseUrl' => $request->getUri(),
'total' => is_countable($searchesList) ? count($searchesList) : 0,
'perPage' => $perPage,
'pageParamName' => 'page',
];
$pagination = new Pagination($options);

$templateVars = [
'ad_menu_searchstats' => Translation::get('ad_menu_searchstats'),
'csrfToken' => Token::getInstance()->getTokenString('truncate-search-terms'),
'ad_searchterm_del' => Translation::get('ad_searchterm_del'),
'ad_searchstats_search_term' => Translation::get('ad_searchstats_search_term'),
'ad_searchstats_search_term_count' => Translation::get('ad_searchstats_search_term_count'),
'ad_searchstats_search_term_lang' => Translation::get('ad_searchstats_search_term_lang'),
'ad_searchstats_search_term_percentage' => Translation::get('ad_searchstats_search_term_percentage'),
'pagination' => $pagination->render(),
'searchesCount' => $searchesCount,
'searchesList' => $searchesList,
'csrfTokenDelete' => Token::getInstance()->getTokenString('delete-searchterm'),
'ad_news_delete' => Translation::get('ad_news_delete'),
];

echo $template->render($templateVars);
} else {
require __DIR__ . '/no-permission.php';
}
5 changes: 5 additions & 0 deletions phpmyfaq/admin/statistics.show.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@
* @since 2003-02-24
*/

use phpMyFAQ\Configuration;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Session;
use phpMyFAQ\Template\TwigWrapper;
use phpMyFAQ\Translation;
use phpMyFAQ\User\CurrentUser;
use Twig\Extension\DebugExtension;

if (!defined('IS_VALID_PHPMYFAQ')) {
http_response_code(400);
exit();
}

$faqConfig = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($faqConfig);

$sessionId = Filter::filterInput(INPUT_GET, 'id', FILTER_VALIDATE_INT);

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

0 comments on commit 7f86de9

Please sign in to comment.