195 lines
8.7 KiB
PHP
195 lines
8.7 KiB
PHP
<?php
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
function statpress_infographic_page() {
|
|
global $wpdb;
|
|
$table_activities = $wpdb->prefix . 'statpress_activities';
|
|
$table_categories = $wpdb->prefix . 'statpress_categories';
|
|
|
|
$current_year = isset( $_GET['year'] ) ? intval( $_GET['year'] ) : current_time( 'Y' );
|
|
|
|
// Pobierz dostępne lata z bazy danych
|
|
$available_years = $wpdb->get_col( "SELECT DISTINCT YEAR(date) FROM $table_activities ORDER BY YEAR(date) DESC" );
|
|
if ( empty( $available_years ) ) {
|
|
$available_years = array( current_time( 'Y' ) ); // Domyślny rok, jeśli brak danych
|
|
}
|
|
|
|
// --- 1. Statystyki ogólne (wszystkie lata) ---
|
|
$overall_stats = $wpdb->get_row(
|
|
"
|
|
SELECT
|
|
SUM(distance) as total_distance,
|
|
SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) as total_duration,
|
|
SUM(total_elevation_gain) as total_elevation_gain,
|
|
COUNT(id) as total_activities
|
|
FROM $table_activities
|
|
"
|
|
);
|
|
|
|
// --- 2. Statystyki dla wybranego roku ---
|
|
$yearly_stats = $wpdb->get_row(
|
|
$wpdb->prepare(
|
|
"
|
|
SELECT
|
|
SUM(distance) as total_distance,
|
|
SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) as total_duration,
|
|
SUM(total_elevation_gain) as total_elevation_gain,
|
|
COUNT(id) as total_activities
|
|
FROM $table_activities
|
|
WHERE YEAR(date) = %d
|
|
",
|
|
$current_year
|
|
)
|
|
);
|
|
|
|
// --- 3. Dane dla wykresu kołowego (dystans per kategoria dla wybranego roku) ---
|
|
$category_distance_data = $wpdb->get_results(
|
|
$wpdb->prepare(
|
|
"
|
|
SELECT c.name as category_name, c.color, SUM(a.distance) as total_distance
|
|
FROM $table_activities a
|
|
LEFT JOIN $table_categories c ON a.category_id = c.id
|
|
WHERE YEAR(a.date) = %d
|
|
GROUP BY c.name, c.color
|
|
HAVING SUM(a.distance) > 0
|
|
ORDER BY total_distance DESC
|
|
",
|
|
$current_year
|
|
)
|
|
);
|
|
|
|
$chart_labels = array();
|
|
$chart_data = array();
|
|
$chart_colors = array();
|
|
foreach ( $category_distance_data as $data ) {
|
|
$chart_labels[] = $data->category_name;
|
|
$chart_data[] = round( (float) $data->total_distance, 2 );
|
|
$chart_colors[] = $data->color;
|
|
}
|
|
|
|
// Włączenie skryptów dla Chart.js
|
|
wp_enqueue_script( 'chart-js', 'https://cdn.jsdelivr.net/npm/chart.js', array(), null, true );
|
|
wp_enqueue_script( 'html2canvas', 'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js', array(), null, true );
|
|
wp_register_script( 'statpress-infographic-chart-loader', false );
|
|
wp_enqueue_script( 'statpress-infographic-chart-loader' );
|
|
wp_add_inline_script(
|
|
'statpress-infographic-chart-loader',
|
|
'
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
Chart.defaults.font.family = "\'Roboto\', -apple-system, BlinkMacSystemFont, \'Segoe UI\', sans-serif";
|
|
Chart.defaults.color = "#5f6368";
|
|
const ctx = document.getElementById("statpressCategoryPieChart");
|
|
if (!ctx) return;
|
|
new Chart(ctx, {
|
|
type: "pie",
|
|
data: {
|
|
labels: ' . json_encode( $chart_labels ) . ',
|
|
datasets: [{
|
|
data: ' . json_encode( $chart_data ) . ',
|
|
backgroundColor: ' . json_encode( $chart_colors ) . ',
|
|
hoverOffset: 4
|
|
}]
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
plugins: {
|
|
legend: {
|
|
position: "right",
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
// Logika eksportu PNG
|
|
const exportBtn = document.getElementById("statpress-export-btn");
|
|
if (exportBtn) {
|
|
exportBtn.addEventListener("click", function() {
|
|
const originalText = exportBtn.innerText;
|
|
exportBtn.innerText = "Generowanie obrazu...";
|
|
exportBtn.disabled = true;
|
|
|
|
const exportArea = document.getElementById("statpress-export-area");
|
|
html2canvas(exportArea, {
|
|
scale: 2, // 2x lepsza rozdzielczość obrazka (Retina)
|
|
backgroundColor: "#f0f0f1", // Domyślne tło WP Admin
|
|
onclone: function(clonedDoc) {
|
|
// Pokazujemy nasz ukryty tytuł i dodajemy marginesy tylko na obrazku
|
|
const brand = clonedDoc.getElementById("statpress-export-brand");
|
|
if (brand) brand.style.display = "block";
|
|
clonedDoc.getElementById("statpress-export-area").style.padding = "24px";
|
|
}
|
|
}).then(canvas => {
|
|
let link = document.createElement("a");
|
|
link.download = "statpress-podsumowanie-' . esc_js( $current_year ) . '.png";
|
|
link.href = canvas.toDataURL("image/png");
|
|
link.click();
|
|
|
|
exportBtn.innerText = originalText;
|
|
exportBtn.disabled = false;
|
|
});
|
|
});
|
|
}
|
|
});
|
|
'
|
|
);
|
|
|
|
?>
|
|
<div class="wrap">
|
|
<h1>Infografika Statystyk Sportowych</h1>
|
|
<hr class="wp-header-end">
|
|
|
|
<div style="margin-bottom: 20px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px;">
|
|
<form method="get" style="display: flex; gap: 10px; align-items: center;">
|
|
<input type="hidden" name="page" value="statpress-infographic">
|
|
<label for="filter-by-year" style="font-weight: 500; color: #3c4043;">Filtruj według roku:</label>
|
|
<select name="year" id="filter-by-year">
|
|
<?php foreach ( $available_years as $year_option ) : ?>
|
|
<option value="<?php echo esc_attr( $year_option ); ?>" <?php selected( $current_year, $year_option ); ?>><?php echo esc_html( $year_option ); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<?php submit_button( 'Filtruj', 'secondary', 'filter_action', false, array( 'style' => 'margin: 0;' ) ); ?>
|
|
</form>
|
|
<button type="button" id="statpress-export-btn" class="button button-primary">Pobierz PNG</button>
|
|
</div>
|
|
|
|
<div class="postbox" style="margin-bottom: 20px;">
|
|
<div class="postbox-header"><h2 class="hndle">Statystyki Ogólne (wszystkie lata)</h2></div>
|
|
<div class="inside statpress-infographic-grid">
|
|
<div class="statpress-infographic-card"><h3>Dystans</h3><p><?php echo number_format( $overall_stats->total_distance, 2, ',', ' ' ); ?> km</p></div>
|
|
<div class="statpress-infographic-card"><h3>Czas</h3><p><?php echo esc_html( $overall_stats->total_duration ); ?></p></div>
|
|
<div class="statpress-infographic-card"><h3>Wznios</h3><p><?php echo number_format( $overall_stats->total_elevation_gain, 0, ',', ' ' ); ?> m</p></div>
|
|
<div class="statpress-infographic-card"><h3>Aktywności</h3><p><?php echo number_format( $overall_stats->total_activities, 0, ',', ' ' ); ?></p></div>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="statpress-export-area">
|
|
<div id="statpress-export-brand" style="display: none; text-align: center; margin-bottom: 24px;">
|
|
<h2 style="font-size: 28px; margin: 0 0 6px 0; color: #202124;">Podsumowanie Sportowe <?php echo esc_html( $current_year ); ?></h2>
|
|
<p style="margin: 0; color: #5f6368; font-size: 14px;">Wygenerowano z wtyczki StatPress</p>
|
|
</div>
|
|
|
|
<div class="postbox" style="margin-bottom: 20px;">
|
|
<div class="postbox-header"><h2 class="hndle">Statystyki dla <?php echo esc_html( $current_year ); ?></h2></div>
|
|
<div class="inside statpress-infographic-grid">
|
|
<div class="statpress-infographic-card"><h3>Dystans</h3><p><?php echo number_format( $yearly_stats->total_distance, 2, ',', ' ' ); ?> km</p></div>
|
|
<div class="statpress-infographic-card"><h3>Czas</h3><p><?php echo esc_html( $yearly_stats->total_duration ); ?></p></div>
|
|
<div class="statpress-infographic-card"><h3>Wznios</h3><p><?php echo number_format( $yearly_stats->total_elevation_gain, 0, ',', ' ' ); ?> m</p></div>
|
|
<div class="statpress-infographic-card"><h3>Aktywności</h3><p><?php echo number_format( $yearly_stats->total_activities, 0, ',', ' ' ); ?></p></div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="postbox" style="margin-bottom: 20px;">
|
|
<div class="postbox-header"><h2 class="hndle">Rozkład Dystansu wg Kategorii w <?php echo esc_html( $current_year ); ?></h2></div>
|
|
<div class="inside">
|
|
<div style="position: relative; height:40vh; width:100%; max-width: 600px; margin: 0 auto;">
|
|
<canvas id="statpressCategoryPieChart"></canvas>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|