Skip to content

Commit

Permalink
refactor: rewritten sessions per day (#2791)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Feb 4, 2024
1 parent 8d4f5a2 commit a833432
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 74 deletions.
2 changes: 1 addition & 1 deletion phpmyfaq/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
require 'statistics.sessions.php';
break;
case 'sessionbrowse':
require 'stat.browser.php';
require 'statistics.sessions.day.php';
break;
case 'viewsession':
require 'statistics.show.php';
Expand Down
72 changes: 0 additions & 72 deletions phpmyfaq/admin/stat.browser.php

This file was deleted.

64 changes: 64 additions & 0 deletions phpmyfaq/admin/statistics.sessions.day.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* Sessions per day.
*
* 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 Thorsten Rinne <thorsten@phpmyfaq.de>
* @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-02-24
*/

use phpMyFAQ\Configuration;
use phpMyFAQ\Date;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Session;
use phpMyFAQ\Template\TwigWrapper;
use phpMyFAQ\Translation;
use phpMyFAQ\User\CurrentUser;
use Symfony\Component\HttpFoundation\Request;
use Twig\Extension\CoreExtension;

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 = 50;
$day = Filter::filterVar($request->request->get('day'), FILTER_VALIDATE_INT);
$firstHour = mktime(0, 0, 0, date('m', $day), date('d', $day), date('Y', $day));
$lastHour = mktime(23, 59, 59, date('m', $day), date('d', $day), date('Y', $day));

$session = new Session($faqConfig);
$sessionData = $session->getSessionsByDate($firstHour, $lastHour);
$date = new Date($faqConfig);

$twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates');
$twig->getExtension(CoreExtension::class)->setDateFormat('Y-m-d H:i', '%d days');
$template = $twig->loadTemplate('./admin/statistics/sessions.day.twig');

$templateVars = [
'adminHeaderSessionsPerDay' => Translation::get('ad_sess_session'),
'currentDay' => date('Y-m-d', $day),
'msgIpAdress' => Translation::get('ad_sess_ip'),
'msgSessionDate' => Translation::get('ad_sess_s_date'),
'msgSession' => Translation::get('ad_sess_session'),
'sessionData' => $sessionData,
];

echo $template->render($templateVars);
} else {
require 'no-permission.php';
}
1 change: 0 additions & 1 deletion phpmyfaq/admin/statistics.sessions.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
];

echo $template->render($templateVars);

} else {
echo Alert::danger('err_NotAuth');
}
29 changes: 29 additions & 0 deletions phpmyfaq/assets/templates/admin/statistics/sessions.day.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<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-tasks"></i>
{{ adminHeaderSessionsPerDay }} {{ currentDay }}
</h1>
</div>

<div class="row">
<div class="col-lg-12">
<table class="table table-striped align-middle">
<thead>
<tr>
<th>{{ msgIpAdress }}</th>
<th>{{ msgSessionDate }}</th>
<th>{{ msgSession }}</th>
</tr>
</thead>
<tbody>
{% for sid, data in sessionData %}
<tr>
<td>{{ data.ip }}</td>
<td>{{ data.time | date }}</td>
<td><a href="?action=viewsession&id={{ sid }}">{{ sid }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
5 changes: 5 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Template/TwigWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public function addFunction(TwigFunction $function): void
{
$this->twig->addFunction($function);
}

public function getExtension(string $class): ExtensionInterface
{
return $this->twig->getExtension($class);
}
}

0 comments on commit a833432

Please sign in to comment.