Skip to content

Commit

Permalink
refactor: move session view to twig (#2791)
Browse files Browse the repository at this point in the history
  • Loading branch information
modelrailroader authored May 1, 2024
1 parent 5ccab23 commit 69dcc9a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 68 deletions.
36 changes: 19 additions & 17 deletions phpmyfaq/admin/assets/src/user/user-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,25 @@ export const handleUserList = () => {
});

const deleteUserConfirm = document.getElementById('pmf-delete-user-yes');
deleteUserConfirm.addEventListener('click', async (event) => {
event.preventDefault();
const source = document.getElementById('source_page');
if (source.value === 'user-list') {
const userId = document.getElementById('pmf-user-id-delete').value;
const csrfToken = document.getElementById('csrf-token-delete-user').value;
const response = await deleteUser(userId, csrfToken);
const json = await response.json();
if (json.success) {
pushNotification(json.success);
const row = document.getElementById('row_user_id_' + userId);
row.remove();
}
if (json.error) {
pushErrorNotification(json.error);
if (deleteUserConfirm) {
deleteUserConfirm.addEventListener('click', async (event) => {
event.preventDefault();
const source = document.getElementById('source_page');
if (source.value === 'user-list') {
const userId = document.getElementById('pmf-user-id-delete').value;
const csrfToken = document.getElementById('csrf-token-delete-user').value;
const response = await deleteUser(userId, csrfToken);
const json = await response.json();
if (json.success) {
pushNotification(json.success);
const row = document.getElementById('row_user_id_' + userId);
row.remove();
}
if (json.error) {
pushErrorNotification(json.error);
}
}
}
});
});
}
}
};
69 changes: 18 additions & 51 deletions phpmyfaq/admin/statistics.show.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Filter;
use phpMyFAQ\Session;
use phpMyFAQ\Strings;
use phpMyFAQ\Template\TwigWrapper;
use phpMyFAQ\Translation;
use Twig\Extension\DebugExtension;

if (!defined('IS_VALID_PHPMYFAQ')) {
http_response_code(400);
Expand All @@ -28,60 +29,26 @@

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

?>

<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>
<?php printf('%s #%d', Translation::get('ad_sess_session'), $sessionId); ?>
</h1>
</div>

<?php
if ($user->perm->hasPermission($user->getUserId(), PermissionType::STATISTICS_VIEWLOGS->value)) {
$session = new Session($faqConfig);
$time = $session->getTimeFromSessionId($sessionId);
$trackingData = explode("\n", file_get_contents(PMF_CONTENT_DIR . '/core/data/tracking' . date('dmY', $time)));
?>
<table class="table table-striped align-middle">
<tfoot>
<tr>
<td colspan="2"><a href="?action=viewsessions"><?= Translation::get('ad_sess_back') ?></a></td>
</tr>
</tfoot>
<tbody>
<?php
$num = 0;
foreach ($trackingData as $line) {
$data = explode(';', $line);
if ($data[0] == $sessionId) {
++$num;
?>
<tr>
<td><?= date('Y-m-d H:i:s', (int)$data[7]) ?></td>
<td><?= $data[1] ?> (<?= $data[2] ?>)</td>
</tr>
<?php if ($num == 1) { ?>
<tr>
<td><?= Translation::get('ad_sess_referer') ?>:</td>
<td><?= Strings::htmlentities(str_replace('?', '? ', $data[5])) ?>
</td>
</tr>
<tr>
<td><?= Translation::get('ad_sess_browser') ?>:</td>
<td><?= Strings::htmlentities($data[6]) ?></td>
</tr>
<tr>
<td><?= Translation::get('ad_sess_ip') ?>:</td>
<td><?= Strings::htmlentities($data[3]) ?></td>
</tr>
<?php }
}
}
?>
</tbody>
</table>
<?php

$templateVars = [
'ad_sess_session' => Translation::get('ad_sess_session'),
'sessionId' => $sessionId,
'ad_sess_back' => Translation::get('ad_sess_back'),
'ad_sess_referer' => Translation::get('ad_sess_referer'),
'ad_sess_browser' => Translation::get('ad_sess_browser'),
'ad_sess_ip' => Translation::get('ad_sess_ip'),
'trackingData' => $trackingData
];

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

echo $template->render($templateVars);
} else {
require __DIR__ . '/no-permission.php';
}
43 changes: 43 additions & 0 deletions phpmyfaq/assets/templates/admin/statistics/statistics.show.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<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-clock-history"></i>
{{ ad_sess_session }} #{{ sessionId }}
</h1>
</div>
<table class="table table-striped align-middle">
<tfoot>
<tr>
<td colspan="2"><a href="?action=viewsessions">{{ ad_sess_back }}</a></td>
</tr>
</tfoot>
<tbody>
{% set num = 0 %}
{% for line in trackingData %}
{% set data = line|split(';') %}
{% if data.0 == sessionId %}
{% set num = num + 1 %}
<tr>
<td>{{ data.7|date('Y-m-d H:i:s') }}</td>
<td>{{ data.1 }} ({{ data.2 }})</td>
</tr>
{% if num == 1 %}
<tr>
<td>{{ ad_sess_referer }}:</td>
<td>
{% set temp = data.5|replace({'?': '? '}) %}
{{ temp|escape }}
</td>
</tr>
<tr>
<td>{{ ad_sess_browser }}:</td>
<td>{{ data.6|escape }}</td>
</tr>
<tr>
<td>{{ ad_sess_ip }}:</td>
<td>{{ data.3|escape }}</td>
</tr>
{% endif %}
{% endif %}
{% endfor %}
</tbody>
</table>

0 comments on commit 69dcc9a

Please sign in to comment.