250 lines
9.6 KiB
PHP
250 lines
9.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Statistics plugin: toont analytics-statistieken in het beheer.
|
|
*
|
|
* Systeem-plugin die analytics-gegevens toont, zoals totaal aantal views,
|
|
* unieke bezoekers, landen en top-pagina's.
|
|
*
|
|
* @since 2.6.4
|
|
*/
|
|
class Statistics
|
|
{
|
|
/**
|
|
* Plugin-API instantie voor communicatie met de CMS-core.
|
|
*
|
|
* @since 2.6.4
|
|
* @var PluginAPIInterface|null
|
|
*/
|
|
private ?PluginAPIInterface $api = null;
|
|
|
|
/**
|
|
* Stelt de Plugin-API instantie in.
|
|
*
|
|
* @since 2.6.4
|
|
* @param PluginAPIInterface $api De Plugin-API instantie.
|
|
* @return void
|
|
*/
|
|
public function setAPI(PluginAPIInterface $api): void
|
|
{
|
|
$this->api = $api;
|
|
}
|
|
|
|
/**
|
|
* Geeft de plugin-configuratie terug.
|
|
*
|
|
* @since 2.6.4
|
|
* @return array<string, mixed> Plugin-configuratie met title, viewable en type.
|
|
*/
|
|
public function getConfig(): array
|
|
{
|
|
return [
|
|
'title' => 'Statistics',
|
|
'viewable' => false,
|
|
'type' => 'system',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Geeft de admin-menu-items voor deze plugin terug.
|
|
*
|
|
* @since 2.6.4
|
|
* @return array<int, array<string, mixed>> Lijst met admin-menu-item configuraties.
|
|
*/
|
|
public function getAdminMenu(): array
|
|
{
|
|
$config = $this->getPluginConfig();
|
|
$requiredRoles = $config['required_roles'] ?? ['bi-manager', 'site-admin', 'admin'];
|
|
|
|
return [
|
|
[
|
|
'plugin' => 'Statistics',
|
|
'route' => 'statistics',
|
|
'label' => 'Statistieken',
|
|
'label_key' => 'menu_label',
|
|
'icon' => 'bi-bar-chart',
|
|
'section' => 'general',
|
|
'permission' => 'statistics',
|
|
'required_roles' => $requiredRoles,
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Haalt de runtime-configuratie van deze plugin op (standaardwaarden plus overrides).
|
|
*
|
|
* Leest standaardwaarden uit plugin.json en overschrijft deze met config.json.
|
|
*
|
|
* @since 2.6.4
|
|
* @return array<string, mixed> De samengevoegde plugin-configuratie.
|
|
*/
|
|
private function getPluginConfig(): array
|
|
{
|
|
$pluginDir = dirname(__DIR__);
|
|
$pluginJsonFile = $pluginDir . '/plugin.json';
|
|
$configJsonFile = $pluginDir . '/config.json';
|
|
|
|
$defaults = [];
|
|
if (file_exists($pluginJsonFile)) {
|
|
$pluginJson = json_decode(file_get_contents($pluginJsonFile), true) ?? [];
|
|
foreach ($pluginJson['settings'] ?? [] as $setting) {
|
|
if (isset($setting['key'])) {
|
|
$defaults[$setting['key']] = $setting['default'] ?? null;
|
|
}
|
|
}
|
|
}
|
|
|
|
$overrides = [];
|
|
if (file_exists($configJsonFile)) {
|
|
$overrides = json_decode(file_get_contents($configJsonFile), true) ?? [];
|
|
}
|
|
|
|
return array_merge($defaults, $overrides);
|
|
}
|
|
|
|
/**
|
|
* Verwerkt een admin-route en toont de statistieken-pagina.
|
|
*
|
|
* Toont analytics-gegevens zoals views, unieke bezoekers, landen en
|
|
* top-pagina's, mits analytics ingeschakeld is.
|
|
*
|
|
* @since 2.6.4
|
|
* @param string $action De uit te voeren actie.
|
|
* @return string|null De HTML-uitvoer van de statistieken-pagina, of null.
|
|
*/
|
|
public function handleAdminRoute(string $action): ?string
|
|
{
|
|
// System plugin: translations resolve against the admin language.
|
|
$t = $this->api ? $this->api->getPluginTranslations('Statistics') : [];
|
|
$tr = function (string $key) use ($t): string {
|
|
return $t[$key] ?? $key;
|
|
};
|
|
|
|
$analytics = $this->getAnalytics();
|
|
if ($analytics === null) {
|
|
return '<div class="alert alert-warning">' . htmlspecialchars($tr('analytics_disabled')) . '</div>';
|
|
}
|
|
|
|
$config = $this->getPluginConfig();
|
|
$showCountries = $config['show_countries'] ?? true;
|
|
$showTopPages = $config['show_top_pages'] ?? true;
|
|
|
|
$stats = $analytics->getStats();
|
|
$totals = $stats['totals'] ?? ['views' => 0, 'uniques' => 0, 'pages' => []];
|
|
$countries = $stats['countries'] ?? [];
|
|
$topPages = $stats['top_pages'] ?? $totals['pages'] ?? [];
|
|
|
|
ob_start();
|
|
?>
|
|
<h2 class="mb-4"><i class="bi bi-bar-chart"></i> <?= htmlspecialchars($tr('page_title')) ?></h2>
|
|
|
|
<div class="row g-4 mb-4">
|
|
<div class="col-md-4">
|
|
<div class="card stat-card shadow-sm">
|
|
<div class="card-body d-flex justify-content-between align-items-center">
|
|
<div>
|
|
<h6 class="text-muted mb-1"><?= htmlspecialchars($tr('total_views')) ?></h6>
|
|
<h3 class="mb-0"><?= number_format($totals['views'] ?? 0, 0, ',', '.') ?></h3>
|
|
</div>
|
|
<i class="bi bi-eye stat-icon text-primary"></i>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card stat-card shadow-sm">
|
|
<div class="card-body d-flex justify-content-between align-items-center">
|
|
<div>
|
|
<h6 class="text-muted mb-1"><?= htmlspecialchars($tr('unique_visitors')) ?></h6>
|
|
<h3 class="mb-0"><?= number_format($totals['uniques'] ?? 0, 0, ',', '.') ?></h3>
|
|
</div>
|
|
<i class="bi bi-people stat-icon text-success"></i>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card stat-card shadow-sm">
|
|
<div class="card-body d-flex justify-content-between align-items-center">
|
|
<div>
|
|
<h6 class="text-muted mb-1"><?= htmlspecialchars($tr('page_views')) ?></h6>
|
|
<h3 class="mb-0"><?= number_format($totals['page_views'] ?? ($totals['views'] ?? 0), 0, ',', '.') ?></h3>
|
|
</div>
|
|
<i class="bi bi-file-earmark-text stat-icon text-info"></i>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($showCountries || $showTopPages): ?>
|
|
<div class="row g-4">
|
|
<?php if ($showCountries): ?>
|
|
<div class="col-md-6">
|
|
<div class="card shadow-sm">
|
|
<div class="card-header"><i class="bi bi-globe"></i> <?= htmlspecialchars($tr('countries')) ?></div>
|
|
<div class="card-body">
|
|
<?php if (empty($countries)): ?>
|
|
<p class="text-muted mb-0"><?= htmlspecialchars($tr('no_data')) ?></p>
|
|
<?php else: ?>
|
|
<table class="table table-sm mb-0">
|
|
<thead><tr><th><?= htmlspecialchars($tr('col_country')) ?></th><th><?= htmlspecialchars($tr('col_views')) ?></th></tr></thead>
|
|
<tbody>
|
|
<?php foreach ($countries as $country => $count): ?>
|
|
<tr>
|
|
<td><?= GeoIP::getCountryFlagEmoji($country) ?> <?= htmlspecialchars(GeoIP::getCountryName($country)) ?></td>
|
|
<td><?= number_format($count, 0, ',', '.') ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if ($showTopPages): ?>
|
|
<div class="col-md-6">
|
|
<div class="card shadow-sm">
|
|
<div class="card-header"><i class="bi bi-file-earmark-bar"></i> <?= htmlspecialchars($tr('top_pages')) ?></div>
|
|
<div class="card-body">
|
|
<?php if (empty($topPages)): ?>
|
|
<p class="text-muted mb-0"><?= htmlspecialchars($tr('no_data')) ?></p>
|
|
<?php else: ?>
|
|
<table class="table table-sm mb-0">
|
|
<thead><tr><th><?= htmlspecialchars($tr('col_page')) ?></th><th><?= htmlspecialchars($tr('col_views')) ?></th></tr></thead>
|
|
<tbody>
|
|
<?php foreach ($topPages as $page => $count): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($page) ?></td>
|
|
<td><?= number_format($count, 0, ',', '.') ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* Haalt de Analytics-instantie op basis van de analytics-configuratie.
|
|
*
|
|
* @since 2.6.4
|
|
* @return Analytics|null De Analytics-instantie, of null als analytics uitgeschakeld is.
|
|
*/
|
|
private function getAnalytics(): ?Analytics
|
|
{
|
|
if ($this->api === null) {
|
|
return null;
|
|
}
|
|
$analyticsConfig = $this->api->getConfig('analytics', []);
|
|
if (!is_array($analyticsConfig) || empty($analyticsConfig['enabled'])) {
|
|
return null;
|
|
}
|
|
return new Analytics($analyticsConfig);
|
|
}
|
|
}
|