Skip to content

Commit

Permalink
refactor: migrated search statistics page to controller (#3257)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Nov 30, 2024
1 parent c967efa commit be99478
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 142 deletions.
3 changes: 2 additions & 1 deletion phpmyfaq/admin/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@
$secLevelEntries['statistics'] .= $adminHelper->addMenuEntry(
PermissionType::STATISTICS_VIEWLOGS->value,
'searchstats',
'msgAdminElasticsearchStats'
'msgAdminElasticsearchStats',
'statistics/search'
);
$secLevelEntries['statistics'] .= $adminHelper->addMenuEntry('reports', 'reports', 'ad_menu_reports');

Expand Down
5 changes: 0 additions & 5 deletions phpmyfaq/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,6 @@
case 'glossary':
require 'glossary.php';
break;
// functions for session administration
case 'truncatesearchterms':
case 'searchstats':
require 'statistics.search.php';
break;
// Reports
case 'reportview':
case 'reports':
Expand Down
95 changes: 0 additions & 95 deletions phpmyfaq/admin/statistics.search.php

This file was deleted.

70 changes: 37 additions & 33 deletions phpmyfaq/assets/templates/admin/statistics/search.twig
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
<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-bar-chart"></i>
{{ msgAdminElasticsearchStats }}
</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group mr-2">
<button class="btn btn-outline-danger" type="button" id="pmf-button-truncate-search-terms"
data-pmf-csrf-token="{{ csrfToken }}">
<i aria-hidden="true" class="bi bi-trash"></i>{{ ad_searchterm_del}}
</button>
{% extends '@admin/index.twig' %}

{% block content %}
<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-bar-chart"></i>
{{ msgAdminElasticsearchStats }}
</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group mr-2">
<button class="btn btn-outline-danger" type="button" id="pmf-button-truncate-search-terms"
data-pmf-csrf-token="{{ csrfToken }}">
<i aria-hidden="true" class="bi bi-trash"></i>{{ ad_searchterm_del }}
</button>
</div>
</div>
</div>
</div>

<div class="row">
<div class="col-lg-12">
<table class="table table-striped align-middle border shadow" id="pmf-table-search-terms">
<thead>
<tr>
<th>{{ ad_searchstats_search_term }}</th>
<th>{{ ad_searchstats_search_term_count }}</th>
<th>{{ ad_searchstats_search_term_lang }}</th>
<th colspan="2">{{ ad_searchstats_search_term_percentage }}</th>
<th>&nbsp;</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="6">{{ pagination | raw }}</td>
</tr>
</tfoot>
<tbody>
<div class="row">
<div class="col-lg-12">
<table class="table table-striped align-middle border shadow" id="pmf-table-search-terms">
<thead>
<tr>
<th>{{ ad_searchstats_search_term }}</th>
<th>{{ ad_searchstats_search_term_count }}</th>
<th>{{ ad_searchstats_search_term_lang }}</th>
<th colspan="2">{{ ad_searchstats_search_term_percentage }}</th>
<th>&nbsp;</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="6">{{ pagination | raw }}</td>
</tr>
</tfoot>
<tbody>
{% for searchItem in searchesList %}
{% set num = searchItem.number / searchesCount * 100 %}
<tr id="row-search-id-{{ searchItem.id }}">
Expand All @@ -50,7 +53,8 @@
</td>
</tr>
{% endfor %}
</tbody>
</table>
</tbody>
</table>
</div>
</div>
</div>
{% endblock %}
6 changes: 6 additions & 0 deletions phpmyfaq/src/admin-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use phpMyFAQ\Controller\Administration\PasswordChangeController;
use phpMyFAQ\Controller\Administration\RatingController;
use phpMyFAQ\Controller\Administration\SessionKeepAliveController;
use phpMyFAQ\Controller\Administration\StatisticsSearchController;
use phpMyFAQ\Controller\Administration\StatisticsSessionsController;
use phpMyFAQ\Controller\Administration\StopWordsController;
use phpMyFAQ\Controller\Administration\SystemInformationController;
Expand Down Expand Up @@ -158,6 +159,11 @@
'controller' => [RatingController::class, 'index'],
'methods' => 'GET'
],
'admin.statistics.search' => [
'path' => '/statistics/search',
'controller' => [StatisticsSearchController::class, 'index'],
'methods' => 'GET'
],
'admin.statistics.sessions' => [
'path' => '/statistics/sessions',
'controller' => [StatisticsSessionsController::class, 'index'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ protected function getHeader(Request $request): array
$secLevelEntries['statistics'] .= $adminHelper->addMenuEntry(
PermissionType::STATISTICS_VIEWLOGS->value,
'searchstats',
'msgAdminElasticsearchStats'
'msgAdminElasticsearchStats',
'statistics/search'
);
$secLevelEntries['statistics'] .= $adminHelper->addMenuEntry('reports', 'reports', 'ad_menu_reports');

Expand Down Expand Up @@ -269,7 +270,9 @@ protected function getHeader(Request $request): array
case 'admin.statistics.admin-log':
case 'admin.statistics.ratings':
case 'admin.statistics.sessions':
case 'admin.statistics.session':
case 'admin.statistics.session.day':
case 'admin.statistics.session.id':
case 'admin.statistics.search':
$statisticsPage = true;
break;
case 'admin.export':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ public function index(Request $request): Response
$itemsPerPage = 15;
$page = Filter::filterVar($request->get('page'), FILTER_VALIDATE_INT, 1);

$baseUrl = sprintf('./statistics/admin-log?page=%d', $page);

// Pagination options
$options = [
'baseUrl' => $baseUrl,
'baseUrl' => $request->getUri(),
'total' => $adminLog->getNumberOfEntries(),
'perPage' => $itemsPerPage,
'pageParamName' => 'page',
Expand All @@ -48,9 +46,6 @@ public function index(Request $request): Response

$loggingData = $adminLog->getAll();

$totalItems = count($loggingData);
$totalPages = ceil($totalItems / $itemsPerPage);

$offset = ($page - 1) * $itemsPerPage;
$currentItems = array_slice($loggingData, $offset, $itemsPerPage);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace phpMyFAQ\Controller\Administration;

use phpMyFAQ\Core\Exception;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Pagination;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Template\Extensions\LanguageCodeTwigExtension;
use phpMyFAQ\Translation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Twig\Error\LoaderError;

class StatisticsSearchController extends AbstractAdministrationController
{
/**
* @throws LoaderError
* @throws Exception
* @throws \Exception
*/
#[Route('/statistics/search', name: 'admin.statistics.search', methods: ['GET'])]
public function index(Request $request): Response
{
$this->userHasPermission(PermissionType::STATISTICS_VIEWLOGS);

$perPage = 10;

$search = $this->container->get('phpmyfaq.search');
$session = $this->container->get('session');

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

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

$this->addExtension(new LanguageCodeTwigExtension());
return $this->render(
'@admin/statistics/search.twig',
[
... $this->getHeader($request),
... $this->getFooter(),
'msgAdminElasticsearchStats' => Translation::get('msgAdminElasticsearchStats'),
'csrfToken' => Token::getInstance($session)->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($session)->getTokenString('delete-searchterm'),
'ad_news_delete' => Translation::get('ad_news_delete'),
]
);
}
}

0 comments on commit be99478

Please sign in to comment.