Skip to content

Commit

Permalink
refactor: migrated sessions page to controller (#3257)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Nov 25, 2024
1 parent 17b768d commit 6aa5eb0
Show file tree
Hide file tree
Showing 17 changed files with 437 additions and 335 deletions.
42 changes: 42 additions & 0 deletions phpmyfaq/admin/assets/src/api/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,45 @@ export const clearRatings = async (csrfToken) => {
console.error(error);
}
};
export const clearVisits = async (csrfToken) => {
try {
const response = await fetch(`./api/statistics/visits/clear`, {
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);
}
};

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

return await response.json();
} catch (error) {
console.error(error);
}
};
4 changes: 4 additions & 0 deletions phpmyfaq/admin/assets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import { getLatestVersion, renderVisitorCharts, renderTopTenCharts, handleVerificationModal } from './dashboard';
import {
handleClearRatings,
handleClearVisits,
handleCreateReport,
handleDeleteAdminLog,
handleDeleteSessions,
handleSessions,
handleStatistics,
} from './statistics';
Expand Down Expand Up @@ -131,6 +133,8 @@ document.addEventListener('DOMContentLoaded', async () => {
handleCreateReport();
handleTruncateSearchTerms();
handleClearRatings();
handleClearVisits();
handleDeleteSessions();

// Configuration → FAQ configuration
await handleConfiguration();
Expand Down
40 changes: 39 additions & 1 deletion phpmyfaq/admin/assets/src/statistics/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* @since 2024-01-14
*/

import { pushErrorNotification } from '../utils';
import { pushErrorNotification, pushNotification } from '../utils';
import { clearVisits, deleteSessions } from '../api/index.js';

export const handleSessions = () => {
const firstHour = document.getElementById('firstHour');
Expand Down Expand Up @@ -67,3 +68,40 @@ export const handleSessions = () => {
});
}
};

export const handleClearVisits = () => {
const buttonClearRatings = document.getElementById('pmf-admin-clear-visits');

if (buttonClearRatings) {
buttonClearRatings.addEventListener('click', async (event) => {
event.preventDefault();
const csrf = event.target.getAttribute('data-pmf-csrf');
const response = await clearVisits(csrf);

if (response.success) {
pushNotification(response.success);
} else {
pushErrorNotification(response.error);
}
});
}
};

export const handleDeleteSessions = () => {
const buttonClearRatings = document.getElementById('pmf-admin-delete-sessions');

if (buttonClearRatings) {
buttonClearRatings.addEventListener('click', async (event) => {
event.preventDefault();
const csrf = document.getElementById('pmf-csrf-token').value;
const month = document.getElementById('month').value;
const response = await deleteSessions(csrf, month);

if (response.success) {
pushNotification(response.success);
} else {
pushErrorNotification(response.error);
}
});
}
};
3 changes: 2 additions & 1 deletion phpmyfaq/admin/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
$secLevelEntries['statistics'] .= $adminHelper->addMenuEntry(
PermissionType::STATISTICS_VIEWLOGS->value,
'viewsessions',
'ad_menu_session'
'ad_menu_session',
'statistics/sessions'
);
$secLevelEntries['statistics'] .= $adminHelper->addMenuEntry(
PermissionType::STATISTICS_ADMINLOG->value,
Expand Down
7 changes: 0 additions & 7 deletions phpmyfaq/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,6 @@
case 'adminlog':
require 'statistics.admin-log.php';
break;
case 'viewsessions':
case 'clear-visits':
require 'statistics.sessions.php';
break;
case 'sessionbrowse':
require 'statistics.sessions.day.php';
break;
case 'viewsession':
require 'statistics.show.php';
break;
Expand Down
62 changes: 0 additions & 62 deletions phpmyfaq/admin/statistics.sessions.day.php

This file was deleted.

112 changes: 0 additions & 112 deletions phpmyfaq/admin/statistics.sessions.php

This file was deleted.

58 changes: 31 additions & 27 deletions phpmyfaq/assets/templates/admin/statistics/sessions.day.twig
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
<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>
{% extends '@admin/index.twig' %}

<div class="row">
<div class="col-lg-12">
<table class="table table-striped align-middle">
<thead>
<tr>
<th>{{ msgIpAddress }}</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>
{% 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-tasks"></i>
{{ adminHeaderSessionsPerDay }} {{ currentDay }}
</h1>
</div>
</div>

<div class="row">
<div class="col-lg-12">
<table class="table table-striped align-middle">
<thead>
<tr>
<th>{{ msgIpAddress }}</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>
{% endblock %}
Loading

0 comments on commit 6aa5eb0

Please sign in to comment.