Files
wp-cycling-stats/moje-statystyki.php
T
2026-01-27 13:16:42 +01:00

1538 lines
72 KiB
PHP

<?php
/**
* Plugin Name: WordPress Activity Stats
* Description: Wtyczka do śledzenia statystyk sportowych (Rower, Bieganie, itp.).
* Version: 1.0
* Author: Jacek Fefliński
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// --- 1. INSTALACJA BAZY DANYCH (AKTYWACJA) ---
register_activation_hook( __FILE__, 'mystat_activate' );
function mystat_activate() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_categories = $wpdb->prefix . 'mystat_categories';
$table_activities = $wpdb->prefix . 'mystat_activities';
$table_event_types = $wpdb->prefix . 'mystat_event_types';
$table_equipment = $wpdb->prefix . 'mystat_equipment';
// SQL dla Kategorii
$sql_cat = "CREATE TABLE $table_categories (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
icon varchar(50) NOT NULL,
color varchar(20) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
// SQL dla Typów Wydarzeń
$sql_event_types = "CREATE TABLE $table_event_types (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
// SQL dla Sprzętu
$sql_equipment = "CREATE TABLE $table_equipment (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
// SQL dla Aktywności
$sql_act = "CREATE TABLE $table_activities (
id bigint(20) NOT NULL AUTO_INCREMENT,
category_id mediumint(9) NOT NULL,
date date NOT NULL,
title varchar(255) DEFAULT '' NOT NULL,
distance decimal(10,2) DEFAULT 0.00,
duration time DEFAULT '00:00:00',
calories int(11) DEFAULT 0,
comment text,
strava_url varchar(255) DEFAULT NULL,
avg_heart_rate smallint(5) UNSIGNED DEFAULT NULL,
max_heart_rate smallint(5) UNSIGNED DEFAULT NULL,
avg_speed decimal(5,2) DEFAULT NULL,
max_speed decimal(5,2) DEFAULT NULL,
avg_cadence smallint(5) UNSIGNED DEFAULT NULL,
max_cadence smallint(5) UNSIGNED DEFAULT NULL,
total_elevation_gain int(11) DEFAULT NULL,
total_elevation_loss int(11) DEFAULT NULL,
min_altitude int(11) DEFAULT NULL,
max_altitude int(11) DEFAULT NULL,
equipment_id mediumint(9) DEFAULT NULL,
gpx_url varchar(255) DEFAULT NULL,
event_type_id mediumint(9) DEFAULT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql_equipment );
dbDelta( $sql_cat );
dbDelta( $sql_event_types );
dbDelta( $sql_act );
// Dodanie domyślnych kategorii, jeśli tabela jest pusta
if ( $wpdb->get_var( "SELECT COUNT(*) FROM $table_categories" ) == 0 ) {
$wpdb->insert( $table_categories, array( 'name' => 'Rower', 'icon' => 'dashicons-buddicons-groups', 'color' => '#3498db' ) );
$wpdb->insert( $table_categories, array( 'name' => 'Bieganie', 'icon' => 'dashicons-businessman', 'color' => '#e74c3c' ) );
}
// Dodanie domyślnych typów wydarzeń, jeśli tabela jest pusta
if ( $wpdb->get_var( "SELECT COUNT(*) FROM $table_event_types" ) == 0 ) {
$default_event_types = ['Bez kategorii', 'Fitness', 'Geocaching', 'Podróżowanie', 'Rekreacyjny', 'Specjalne zdarzenie', 'Transport', 'Trening', 'Wyścig'];
foreach ($default_event_types as $type_name) {
$wpdb->insert( $table_event_types, array( 'name' => $type_name ) );
}
// Ustawienie domyślnego typu "Trening" dla istniejących aktywności, które go nie mają
$training_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $table_event_types WHERE name = %s", 'Trening' ) );
if ($training_id) {
$wpdb->query( $wpdb->prepare( "UPDATE $table_activities SET event_type_id = %d WHERE event_type_id IS NULL OR event_type_id = 0", $training_id ) );
}
}
// Dodanie domyślnego sprzętu, jeśli tabela jest pusta
if ( $wpdb->get_var( "SELECT COUNT(*) FROM $table_equipment" ) == 0 ) {
$default_equipment = ['Giant Revolt', 'Cube LTD', 'Author Agang', 'Liv Tempt 4', 'Cube Acid 24', 'Mongoose BMX', 'Nextbike - Miejski'];
foreach ($default_equipment as $eq_name) {
$wpdb->insert( $table_equipment, array( 'name' => $eq_name ) );
}
}
}
// --- 2. MENU ADMINA I DASHBOARD ---
add_action( 'admin_menu', 'mystat_add_admin_menu' );
/**
* Set up admin-specific hooks.
*/
function mystat_admin_init_setup() {
add_filter( 'upload_mimes', 'mystat_add_gpx_mime_type' );
add_filter( 'wp_check_filetype_and_ext', 'mystat_fix_gpx_upload_permission', 10, 4 );
}
add_action( 'admin_init', 'mystat_admin_init_setup' );
/**
* Add GPX support to WordPress Media Library.
*
* @param array $mimes Allowed mime types.
* @return array Modified mime types.
*/
function mystat_add_gpx_mime_type( $mimes ) {
$mimes['gpx'] = 'application/gpx+xml';
return $mimes;
}
/**
* Bypasses WordPress's strict file type check for GPX files.
* This is needed because WordPress can be overly cautious with XML-based files.
*
* @param array $data File data.
* @param string $file Full path to the file.
* @param string $filename The filename.
* @param array $mimes Mime types.
* @return array Modified file data.
*/
function mystat_fix_gpx_upload_permission( $data, $file, $filename, $mimes ) {
if ( strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ) === 'gpx' ) {
$data['ext'] = 'gpx';
$data['type'] = 'application/gpx+xml';
}
return $data;
}
function mystat_add_admin_menu() {
add_menu_page(
'Moje Statystyki', // Tytuł strony
'Statystyki', // Tytuł w menu
'manage_options', // Wymagane uprawnienia
'moje-statystyki', // Slug menu
'mystat_dashboard_page', // Funkcja renderująca stronę główną (dashboard)
'dashicons-chart-line', // Ikona
6 // Pozycja
);
add_submenu_page(
'moje-statystyki', // Slug rodzica
'Dodaj Nowy Trening', // Tytuł strony
'Nowy trening', // Tytuł w podmenu
'manage_options', // Wymagane uprawnienia
'mystat-nowy-trening', // Slug podmenu
'mystat_add_new_page' // Funkcja renderująca stronę dodawania
);
add_submenu_page(
'moje-statystyki',
'Typy Wydarzeń',
'Typy wydarzeń',
'manage_options',
'mystat-event-types',
'mystat_event_types_page'
);
add_submenu_page(
'moje-statystyki',
'Sprzęt',
'Sprzęt',
'manage_options',
'mystat-equipment',
'mystat_equipment_page'
);
add_submenu_page(
null, // Ukryta strona, nie pojawia się w menu
'Szczegóły Treningu', // Tytuł strony
'Szczegóły Treningu', // Tytuł w menu (nieistotny)
'manage_options', // Wymagane uprawnienia
'mystat-view-activity', // Slug podmenu
'mystat_view_activity_page' // Funkcja renderująca
);
add_submenu_page(
null, // Ukryta strona
'Edytuj Trening', // Tytuł strony
'Edytuj Trening', // Tytuł w menu (nieistotny)
'manage_options', // Wymagane uprawnienia
'mystat-edit-activity', // Slug podmenu
'mystat_edit_activity_page' // Funkcja renderująca
);
add_submenu_page(
'moje-statystyki', // Slug rodzica
'Podsumowanie Roczne', // Tytuł strony
'Podsumowanie Roczne', // Tytuł w podmenu
'manage_options', // Wymagane uprawnienia
'mystat-yearly-summary', // Slug podmenu
'mystat_yearly_summary_page'// Funkcja renderująca
);
add_submenu_page(
'moje-statystyki', // Slug rodzica
'Infografika', // Tytuł strony
'Infografika', // Tytuł w podmenu
'manage_options', // Wymagane uprawnienia
'mystat-infographic', // Slug podmenu
'mystat_infographic_page' // Funkcja renderująca
);
}
function mystat_dashboard_page() {
echo '<div class="wrap"><h1>Moje Statystyki Sportowe</h1>';
mystat_render_history_table();
echo '</div>';
}
function mystat_add_new_page() {
echo '<div class="wrap"><h1>Dodaj Nowy Trening</h1>';
// Obsługa zapisu formularza (musi być przed renderowaniem, aby wyświetlić komunikat)
mystat_handle_activity_form_submission();
// Formularz dodawania
mystat_render_add_form();
echo '</div>';
}
function mystat_event_types_page() {
global $wpdb;
$table_event_types = $wpdb->prefix . 'mystat_event_types';
$message = '';
$notice_class = '';
// Handle POST requests (add/update)
if ( isset( $_POST['submit'] ) && check_admin_referer( 'mystat_manage_event_type' ) ) {
$name = sanitize_text_field( $_POST['event_type_name'] );
$type_id = isset( $_POST['event_type_id'] ) ? intval( $_POST['event_type_id'] ) : 0;
if ( ! empty( $name ) ) {
if ( $type_id > 0 ) { // Update
$wpdb->update( $table_event_types, [ 'name' => $name ], [ 'id' => $type_id ] );
$message = 'Typ wydarzenia zaktualizowany.';
$notice_class = 'notice-success';
} else { // Insert
$wpdb->insert( $table_event_types, [ 'name' => $name ] );
$message = 'Typ wydarzenia dodany.';
$notice_class = 'notice-success';
}
} else {
$message = 'Nazwa typu wydarzenia nie może być pusta.';
$notice_class = 'notice-error';
}
}
// Handle GET requests (delete)
if ( isset( $_GET['action'], $_GET['id'], $_GET['_wpnonce'] ) && $_GET['action'] === 'delete' ) {
if ( wp_verify_nonce( $_GET['_wpnonce'], 'mystat_delete_event_type_' . $_GET['id'] ) ) {
$wpdb->delete( $table_event_types, [ 'id' => intval( $_GET['id'] ) ] );
$message = 'Typ wydarzenia usunięty.';
$notice_class = 'notice-success';
}
}
// Prepare for form (for editing)
$item_to_edit = null;
if ( isset( $_GET['action'], $_GET['id'] ) && $_GET['action'] === 'edit' ) {
$item_to_edit = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_event_types WHERE id = %d", intval( $_GET['id'] ) ) );
}
$event_types = $wpdb->get_results( "SELECT * FROM $table_event_types ORDER BY name ASC" );
?>
<div class="wrap">
<h1>Typy Wydarzeń</h1>
<?php if ( ! empty( $message ) ) : ?>
<div class="notice <?php echo esc_attr( $notice_class ); ?> is-dismissible"><p><?php echo esc_html( $message ); ?></p></div>
<?php endif; ?>
<div id="col-container" class="wp-clearfix">
<div id="col-left">
<div class="col-wrap">
<div class="form-wrap">
<h2><?php echo $item_to_edit ? 'Edytuj typ wydarzenia' : 'Dodaj nowy typ wydarzenia'; ?></h2>
<form method="post">
<input type="hidden" name="event_type_id" value="<?php echo $item_to_edit ? esc_attr( $item_to_edit->id ) : '0'; ?>">
<?php wp_nonce_field( 'mystat_manage_event_type' ); ?>
<div class="form-field">
<label for="event_type_name">Nazwa</label>
<input type="text" name="event_type_name" id="event_type_name" value="<?php echo $item_to_edit ? esc_attr( $item_to_edit->name ) : ''; ?>" required>
</div>
<?php submit_button( $item_to_edit ? 'Zaktualizuj' : 'Dodaj' ); ?>
</form>
</div>
</div>
</div>
<div id="col-right">
<div class="col-wrap">
<table class="wp-list-table widefat fixed striped">
<thead><tr><th>Nazwa</th><th style="width: 100px;">Akcje</th></tr></thead>
<tbody>
<?php foreach ( $event_types as $type ) : ?>
<tr><td><?php echo esc_html( $type->name ); ?></td><td><a href="<?php echo esc_url( add_query_arg( [ 'action' => 'edit', 'id' => $type->id ] ) ); ?>">Edytuj</a> | <a href="<?php echo esc_url( wp_nonce_url( add_query_arg( [ 'action' => 'delete', 'id' => $type->id ] ), 'mystat_delete_event_type_' . $type->id ) ); ?>" onclick="return confirm('Czy na pewno chcesz usunąć ten typ?')" style="color: #a00;">Usuń</a></td></tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php
}
function mystat_equipment_page() {
global $wpdb;
$table_equipment = $wpdb->prefix . 'mystat_equipment';
$message = '';
$notice_class = '';
// Handle POST requests (add/update)
if ( isset( $_POST['submit'] ) && check_admin_referer( 'mystat_manage_equipment' ) ) {
$name = sanitize_text_field( $_POST['equipment_name'] );
$item_id = isset( $_POST['equipment_id'] ) ? intval( $_POST['equipment_id'] ) : 0;
if ( ! empty( $name ) ) {
if ( $item_id > 0 ) { // Update
$wpdb->update( $table_equipment, [ 'name' => $name ], [ 'id' => $item_id ] );
$message = 'Sprzęt zaktualizowany.';
$notice_class = 'notice-success';
} else { // Insert
$wpdb->insert( $table_equipment, [ 'name' => $name ] );
$message = 'Sprzęt dodany.';
$notice_class = 'notice-success';
}
} else {
$message = 'Nazwa sprzętu nie może być pusta.';
$notice_class = 'notice-error';
}
}
// Handle GET requests (delete)
if ( isset( $_GET['action'], $_GET['id'], $_GET['_wpnonce'] ) && $_GET['action'] === 'delete' ) {
if ( wp_verify_nonce( $_GET['_wpnonce'], 'mystat_delete_equipment_' . $_GET['id'] ) ) {
$wpdb->delete( $table_equipment, [ 'id' => intval( $_GET['id'] ) ] );
$message = 'Sprzęt usunięty.';
$notice_class = 'notice-success';
}
}
// Prepare for form (for editing)
$item_to_edit = null;
if ( isset( $_GET['action'], $_GET['id'] ) && $_GET['action'] === 'edit' ) {
$item_to_edit = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_equipment WHERE id = %d", intval( $_GET['id'] ) ) );
}
$equipment_list = $wpdb->get_results( "SELECT * FROM $table_equipment ORDER BY name ASC" );
?>
<div class="wrap">
<h1>Zarządzaj Sprzętem</h1>
<?php if ( ! empty( $message ) ) : ?>
<div class="notice <?php echo esc_attr( $notice_class ); ?> is-dismissible"><p><?php echo esc_html( $message ); ?></p></div>
<?php endif; ?>
<div id="col-container" class="wp-clearfix">
<div id="col-left">
<div class="col-wrap">
<div class="form-wrap">
<h2><?php echo $item_to_edit ? 'Edytuj sprzęt' : 'Dodaj nowy sprzęt'; ?></h2>
<form method="post">
<input type="hidden" name="equipment_id" value="<?php echo $item_to_edit ? esc_attr( $item_to_edit->id ) : '0'; ?>">
<?php wp_nonce_field( 'mystat_manage_equipment' ); ?>
<div class="form-field">
<label for="equipment_name">Nazwa</label>
<input type="text" name="equipment_name" id="equipment_name" value="<?php echo $item_to_edit ? esc_attr( $item_to_edit->name ) : ''; ?>" required>
</div>
<?php submit_button( $item_to_edit ? 'Zaktualizuj' : 'Dodaj' ); ?>
</form>
</div>
</div>
</div>
<div id="col-right">
<div class="col-wrap">
<table class="wp-list-table widefat fixed striped">
<thead><tr><th>Nazwa</th><th style="width: 100px;">Akcje</th></tr></thead>
<tbody>
<?php foreach ( $equipment_list as $item ) : ?>
<tr><td><?php echo esc_html( $item->name ); ?></td><td><a href="<?php echo esc_url( add_query_arg( [ 'action' => 'edit', 'id' => $item->id ] ) ); ?>">Edytuj</a> | <a href="<?php echo esc_url( wp_nonce_url( add_query_arg( [ 'action' => 'delete', 'id' => $item->id ] ), 'mystat_delete_equipment_' . $item->id ) ); ?>" onclick="return confirm('Czy na pewno chcesz usunąć ten sprzęt?')" style="color: #a00;">Usuń</a></td></tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php
}
function mystat_yearly_summary_page() {
global $wpdb;
$table_activities = $wpdb->prefix . 'mystat_activities';
$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 = [current_time('Y')]; // Domyślny rok, jeśli brak danych
}
// Zapytanie SQL do grupowania danych miesięcznie
$sql = $wpdb->prepare("
SELECT
MONTH(date) as month_num,
SUM(distance) as total_distance,
SUM(calories) as total_calories,
SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) as total_duration
FROM $table_activities
WHERE YEAR(date) = %d
GROUP BY month_num
ORDER BY month_num ASC
", $current_year);
$monthly_summary = $wpdb->get_results( $sql, OBJECT_K ); // OBJECT_K zwróci tablicę z kluczami będącymi numerami miesięcy
// Przygotowanie danych dla wszystkich 12 miesięcy
$full_year_summary = [];
$total_year_distance = 0;
$total_year_calories = 0;
$total_year_seconds = 0;
for ( $i = 1; $i <= 12; $i++ ) {
$month_name = date_i18n( 'F', mktime( 0, 0, 0, $i, 10 ) ); // Nazwa miesiąca
$data = isset( $monthly_summary[$i] ) ? $monthly_summary[$i] : null;
$full_year_summary[$i] = (object) [
'month_name' => $month_name,
'total_distance' => $data ? $data->total_distance : 0,
'total_calories' => $data ? $data->total_calories : 0,
'total_duration' => $data ? $data->total_duration : '00:00:00',
];
$total_year_distance += $full_year_summary[$i]->total_distance;
$total_year_calories += $full_year_summary[$i]->total_calories;
if ( $data && ! empty( $data->total_duration ) ) {
list($h, $m, $s) = explode(':', $data->total_duration);
$total_year_seconds += $h * 3600 + $m * 60 + $s;
}
}
$total_year_hours = floor($total_year_seconds / 3600);
$total_year_minutes = floor(($total_year_seconds % 3600) / 60);
$total_year_duration_formatted = sprintf('%d godz. %d min.', $total_year_hours, $total_year_minutes);
// Przygotowanie danych dla wykresu
$chart_labels = [];
$chart_data_distance = [];
foreach ($full_year_summary as $month_data) {
$chart_labels[] = $month_data->month_name;
$chart_data_distance[] = round((float)$month_data->total_distance, 2);
}
// Włączenie skryptów dla Chart.js
wp_enqueue_script('chart-js', 'https://cdn.jsdelivr.net/npm/chart.js', [], null, true);
wp_register_script('mystat-chart-loader', false);
wp_enqueue_script('mystat-chart-loader');
wp_add_inline_script('mystat-chart-loader', '
document.addEventListener("DOMContentLoaded", function() {
const ctx = document.getElementById("mystatYearlyChart");
if (!ctx) return;
new Chart(ctx, {
type: "bar",
data: {
labels: ' . json_encode($chart_labels) . ',
datasets: [{
label: "Dystans (km)",
data: ' . json_encode($chart_data_distance) . ',
backgroundColor: "rgba(52, 152, 219, 0.5)",
borderColor: "rgba(52, 152, 219, 1)",
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
title: { display: true, text: "Kilometry" }
}
}
}
});
});
');
?>
<div class="wrap">
<h1>Podsumowanie Roczne</h1>
<div class="tablenav top">
<div class="alignleft actions">
<label for="filter-by-year" class="screen-reader-text">Filtruj według roku</label>
<form method="get" style="display: flex; gap: 5px; align-items: center;">
<input type="hidden" name="page" value="mystat-yearly-summary">
<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 ); ?>
</form>
</div>
</div>
<div class="postbox" style="margin-bottom: 20px;">
<div class="postbox-header"><h2 class="hndle">Wykres Dystansu w <?php echo esc_html($current_year); ?></h2></div>
<div class="inside">
<div style="position: relative; height:40vh; width:100%;">
<canvas id="mystatYearlyChart"></canvas>
</div>
</div>
</div>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr><th>Miesiąc</th><th>Dystans (km)</th><th>Kalorie (kcal)</th><th>Czas</th></tr>
</thead>
<tbody>
<?php foreach ( $full_year_summary as $month_data ) : ?>
<tr>
<td><?php echo esc_html( $month_data->month_name ); ?></td>
<td><?php echo number_format( $month_data->total_distance, 2, ',', ' ' ); ?></td>
<td><?php echo number_format( $month_data->total_calories, 0, ',', ' ' ); ?></td>
<td><?php echo esc_html( $month_data->total_duration ); ?></td>
</tr>
<?php endforeach; ?>
<tr class="alternate">
<th>SUMA ROCZNA</th>
<th><?php echo number_format( $total_year_distance, 2, ',', ' ' ); ?></th>
<th><?php echo number_format( $total_year_calories, 0, ',', ' ' ); ?></th>
<th><?php echo esc_html( $total_year_duration_formatted ); ?></th>
</tr>
</tbody>
</table>
</div>
<?php
}
/**
* Fetches and parses a GPX file from a URL to extract track points.
*
* @param string $gpx_url The URL of the GPX file.
* @return array An array of [lat, lon] coordinates, or an empty array on failure.
*/
function mystat_get_track_from_gpx_url( $gpx_url ) {
if ( empty( $gpx_url ) ) {
return [];
}
$response = wp_remote_get( $gpx_url, [ 'timeout' => 20 ] );
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
return [];
}
$gpx_content = wp_remote_retrieve_body( $response );
if ( empty( $gpx_content ) ) {
return [];
}
libxml_use_internal_errors( true );
$gpx = simplexml_load_string( $gpx_content );
libxml_clear_errors();
if ( $gpx === false ) {
return [];
}
$points = [];
if ( isset( $gpx->trk ) ) {
foreach ( $gpx->trk->trkseg as $trkseg ) {
foreach ( $trkseg->trkpt as $trkpt ) {
if ( isset( $trkpt['lat'] ) && isset( $trkpt['lon'] ) ) {
$points[] = [ (float) $trkpt['lat'], (float) $trkpt['lon'] ];
}
}
}
}
return $points;
}
function mystat_infographic_page() {
global $wpdb;
$table_activities = $wpdb->prefix . 'mystat_activities';
$table_categories = $wpdb->prefix . 'mystat_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 = [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 = [];
$chart_data = [];
$chart_colors = [];
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', [], null, true);
wp_register_script('mystat-infographic-chart-loader', false);
wp_enqueue_script('mystat-infographic-chart-loader');
wp_add_inline_script('mystat-infographic-chart-loader', '
document.addEventListener("DOMContentLoaded", function() {
const ctx = document.getElementById("mystatCategoryPieChart");
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",
},
title: {
display: true,
text: "Dystans wg kategorii w ' . esc_js($current_year) . '"
}
}
}
});
});
');
?>
<div class="wrap">
<h1>Infografika Statystyk Sportowych</h1>
<div class="tablenav top">
<div class="alignleft actions">
<form method="get" style="display: flex; gap: 5px; align-items: center;">
<input type="hidden" name="page" value="mystat-infographic">
<label for="filter-by-year" class="screen-reader-text">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 ); ?>
</form>
</div>
</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 mystat-infographic-grid">
<div class="mystat-infographic-card"><h3>Dystans</h3><p><?php echo number_format($overall_stats->total_distance, 2, ',', ' '); ?> km</p></div>
<div class="mystat-infographic-card"><h3>Czas</h3><p><?php echo esc_html($overall_stats->total_duration); ?></p></div>
<div class="mystat-infographic-card"><h3>Wznios</h3><p><?php echo number_format($overall_stats->total_elevation_gain, 0, ',', ' '); ?> m</p></div>
<div class="mystat-infographic-card"><h3>Aktywności</h3><p><?php echo number_format($overall_stats->total_activities, 0, ',', ' '); ?></p></div>
</div>
</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 mystat-infographic-grid">
<div class="mystat-infographic-card"><h3>Dystans</h3><p><?php echo number_format($yearly_stats->total_distance, 2, ',', ' '); ?> km</p></div>
<div class="mystat-infographic-card"><h3>Czas</h3><p><?php echo esc_html($yearly_stats->total_duration); ?></p></div>
<div class="mystat-infographic-card"><h3>Wznios</h3><p><?php echo number_format($yearly_stats->total_elevation_gain, 0, ',', ' '); ?> m</p></div>
<div class="mystat-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="mystatCategoryPieChart"></canvas>
</div>
</div>
</div>
</div>
<style>
.mystat-infographic-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; }
.mystat-infographic-card { background: #fff; border: 1px solid #e0e0e0; border-radius: 5px; padding: 15px; text-align: center; box-shadow: 0 2px 4px rgba(0,0,0,0.05); }
.mystat-infographic-card h3 { margin-top: 0; color: #555; font-size: 1.1em; }
.mystat-infographic-card p { font-size: 1.8em; font-weight: bold; color: #333; margin-bottom: 0; }
</style>
<?php
}
function mystat_edit_activity_page() {
global $wpdb;
$activity_id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0;
if ( $activity_id === 0 ) {
echo '<div class="wrap"><h1>Błąd</h1><p>Nie podano ID aktywności do edycji.</p></div>';
return;
}
// Handle form submission for update
mystat_handle_activity_form_submission();
$table_activities = $wpdb->prefix . 'mystat_activities';
$activity = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_activities WHERE id = %d", $activity_id ) );
if ( ! $activity ) {
echo '<div class="wrap"><h1>Błąd</h1><p>Nie znaleziono aktywności o podanym ID.</p></div>';
return;
}
echo '<div class="wrap"><h1>Edytuj Trening</h1>';
mystat_render_add_form( $activity );
echo '</div>';
}
function mystat_view_activity_page() {
global $wpdb;
$activity_id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0;
if ( $activity_id === 0 ) {
echo '<div class="wrap"><h1>Błąd</h1><p>Nie podano ID aktywności.</p></div>';
return;
}
$table_activities = $wpdb->prefix . 'mystat_activities';
$table_categories = $wpdb->prefix . 'mystat_categories';
$table_event_types = $wpdb->prefix . 'mystat_event_types';
$table_equipment = $wpdb->prefix . 'mystat_equipment';
$sql = $wpdb->prepare("
SELECT a.*, c.name as category_name, c.icon, c.color, et.name as event_type_name, eq.name as equipment_name
FROM $table_activities a
LEFT JOIN $table_categories c ON a.category_id = c.id
LEFT JOIN $table_event_types et ON a.event_type_id = et.id
LEFT JOIN $table_equipment eq ON a.equipment_id = eq.id
WHERE a.id = %d
", $activity_id);
$activity = $wpdb->get_row( $sql );
if ( ! $activity ) {
echo '<div class="wrap"><h1>Błąd</h1><p>Nie znaleziono aktywności o podanym ID.</p></div>';
return;
}
// Funkcja pomocnicza do wyświetlania wiersza, jeśli wartość istnieje
$render_row = function($label, $value, $unit = '') {
if ( ! is_null($value) && $value !== '' ) {
echo '<tr>';
echo '<th scope="row">' . esc_html($label) . '</th>';
echo '<td>' . esc_html($value) . ($unit ? ' ' . esc_html($unit) : '') . '</td>';
echo '</tr>';
}
};
// Prepare map data if GPX exists
$track_points = [];
if ( ! empty( $activity->gpx_url ) ) {
// Note: For performance, you might want to cache the parsed points in post meta
// rather than parsing the file on every page load.
$track_points = mystat_get_track_from_gpx_url( $activity->gpx_url );
if ( ! empty( $track_points ) ) {
// Enqueue Leaflet assets. For simplicity, this is done here.
// A better approach is to use the 'admin_enqueue_scripts' hook.
wp_enqueue_style('leaflet-css', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css');
wp_enqueue_script('leaflet-js', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js', [], '1.9.4', true);
// Pass track data to a script
wp_register_script('mystat-map-loader', false);
wp_enqueue_script('mystat-map-loader');
wp_add_inline_script('mystat-map-loader',
'const mystat_track_points = ' . json_encode($track_points) . ';' .
'document.addEventListener("DOMContentLoaded", function() {
if (typeof L === "undefined" || typeof mystat_track_points === "undefined" || mystat_track_points.length === 0) return;
const map = L.map("mystat-activity-map");
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: \'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors\'
}).addTo(map);
const polyline = L.polyline(mystat_track_points, {color: "#' . esc_js( ltrim($activity->color, '#') ) . '"}).addTo(map);
map.fitBounds(polyline.getBounds().pad(0.1));
L.marker(mystat_track_points[0]).addTo(map).bindPopup("Start");
L.marker(mystat_track_points[mystat_track_points.length - 1]).addTo(map).bindPopup("Koniec");
});'
);
}
}
?>
<div class="wrap">
<h1>
Szczegóły treningu: <?php echo esc_html( $activity->title ); ?>
<a href="<?php echo esc_url( add_query_arg( ['page' => 'mystat-edit-activity', 'id' => $activity->id], admin_url('admin.php') ) ); ?>" class="page-title-action">
Edytuj
</a>
</h1>
<p><a href="<?php echo esc_url( admin_url('admin.php?page=moje-statystyki') ); ?>">&larr; Powrót do listy aktywności</a></p>
<div class="postbox" style="margin-top: 20px;">
<div class="postbox-header"><h2 class="hndle">Podsumowanie</h2></div>
<div class="inside">
<div id="mystat-details-container" class="wp-clearfix">
<div class="mystat-details-col">
<h3>Główne dane</h3>
<table class="form-table">
<?php $render_row('Kategoria', $activity->category_name); ?>
<?php $render_row('Data', $activity->date); ?>
<?php $render_row('Dystans', number_format($activity->distance, 2, ',', ' '), 'km'); ?>
<?php $render_row('Czas trwania', $activity->duration); ?>
<?php $render_row('Spalone kalorie', $activity->calories, 'kcal'); ?>
<?php $render_row('Typ wydarzenia', $activity->event_type_name); ?>
<?php $render_row('Sprzęt', $activity->equipment_name); ?>
</table>
</div>
<div class="mystat-details-col">
<h3>Dane szczegółowe</h3>
<table class="form-table">
<?php $render_row('Średnia prędkość', number_format($activity->avg_speed, 1, ',', ' '), 'km/h'); ?>
<?php $render_row('Maksymalna prędkość', number_format($activity->max_speed, 1, ',', ' '), 'km/h'); ?>
<?php $render_row('Średnie tętno', $activity->avg_heart_rate, 'bpm'); ?>
<?php $render_row('Maksymalne tętno', $activity->max_heart_rate, 'bpm'); ?>
<?php $render_row('Średni rytm', $activity->avg_cadence, 'rpm'); ?>
<?php $render_row('Maksymalny rytm', $activity->max_cadence, 'rpm'); ?>
<?php $render_row('Suma wzniosów', $activity->total_elevation_gain, 'm'); ?>
<?php $render_row('Suma spadków', $activity->total_elevation_loss, 'm'); ?>
<?php $render_row('Minimalna wysokość', $activity->min_altitude, 'm n.p.m.'); ?>
<?php $render_row('Maksymalna wysokość', $activity->max_altitude, 'm n.p.m.'); ?>
</table>
</div>
</div>
<hr>
<h3>Notatki i linki</h3>
<table class="form-table">
<?php $render_row('Komentarz', nl2br($activity->comment)); ?>
<?php if ( ! empty( $activity->strava_url ) ) : ?>
<tr><th scope="row">Strava</th><td><a href="<?php echo esc_url( $activity->strava_url ); ?>" target="_blank" rel="noopener noreferrer">Zobacz aktywność na Strava</a></td></tr>
<?php endif; ?>
<?php if ( ! empty( $activity->gpx_url ) ) : ?>
<tr><th scope="row">Plik GPX</th><td><a href="<?php echo esc_url( $activity->gpx_url ); ?>" target="_blank">Pobierz plik GPX</a></td></tr>
<?php endif; ?>
</table>
<?php if ( ! empty( $track_points ) ) : ?>
<hr>
<h3>Mapa Trasy</h3>
<div id="mystat-activity-map" style="height: 450px; width: 100%; border: 1px solid #ddd;"></div>
<?php elseif ( ! empty( $activity->gpx_url ) ) : ?>
<hr>
<h3>Mapa Trasy</h3>
<div class="notice notice-warning inline">
<p>Nie udało się wczytać danych z pliku GPX lub plik jest uszkodzony/pusty.</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
<style>
.mystat-details-col { float: left; width: 49%; }
.mystat-details-col:first-child { margin-right: 2%; }
@media screen and (max-width: 782px) { .mystat-details-col { float: none; width: 100%; margin-right: 0; } }
</style>
<?php
}
/**
* Obsługa zapisu nowego lub edytowanego wpisu do bazy danych
*/
function mystat_handle_activity_form_submission() {
global $wpdb;
// Sprawdź czy formularz został wysłany
if ( ! isset( $_POST['mystat_submit_activity'] ) ) {
return;
}
$activity_id = isset( $_POST['activity_id'] ) ? intval( $_POST['activity_id'] ) : 0;
$nonce_action = $activity_id > 0 ? 'mystat_edit_entry_' . $activity_id : 'mystat_add_entry';
// Weryfikacja bezpieczeństwa (Nonce)
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], $nonce_action ) ) {
echo '<div class="notice notice-error"><p>Błąd weryfikacji bezpieczeństwa formularza.</p></div>';
return;
}
$table_activities = $wpdb->prefix . 'mystat_activities';
// Przygotowanie danych (zamiana przecinka na kropkę w dystansie)
$distance = isset($_POST['distance']) ? floatval( str_replace( ',', '.', $_POST['distance'] ) ) : 0;
// Funkcja pomocnicza do zamiany pustych wartości na NULL, aby poprawnie zapisać je w bazie
$null_if_empty = function($value) {
return $value !== '' ? $value : null;
};
$data = array(
'category_id' => intval( $_POST['category_id'] ),
'date' => sanitize_text_field( $_POST['date'] ),
'title' => sanitize_text_field( $_POST['title'] ),
'distance' => $distance,
'duration' => sanitize_text_field( $_POST['duration'] ),
'calories' => intval( $_POST['calories'] ),
'comment' => sanitize_textarea_field( $_POST['comment'] ),
'strava_url' => $null_if_empty( esc_url_raw( $_POST['strava_url'] ) ),
'avg_heart_rate' => $null_if_empty( intval( $_POST['avg_heart_rate'] ) ),
'max_heart_rate' => $null_if_empty( intval( $_POST['max_heart_rate'] ) ),
'avg_speed' => $null_if_empty( floatval( str_replace( ',', '.', $_POST['avg_speed'] ) ) ),
'max_speed' => $null_if_empty( floatval( str_replace( ',', '.', $_POST['max_speed'] ) ) ),
'avg_cadence' => $null_if_empty( intval( $_POST['avg_cadence'] ) ),
'max_cadence' => $null_if_empty( intval( $_POST['max_cadence'] ) ),
'total_elevation_gain' => $null_if_empty( intval( $_POST['total_elevation_gain'] ) ),
'total_elevation_loss' => $null_if_empty( intval( $_POST['total_elevation_loss'] ) ),
'min_altitude' => $null_if_empty( intval( $_POST['min_altitude'] ) ),
'max_altitude' => $null_if_empty( intval( $_POST['max_altitude'] ) ),
'equipment_id' => $null_if_empty( intval( $_POST['equipment_id'] ) ),
'gpx_url' => $null_if_empty( esc_url_raw( $_POST['gpx_url'] ) ),
'event_type_id' => $null_if_empty( intval( $_POST['event_type_id'] ) ),
);
// Format danych dla $wpdb->insert
$format = array(
'%d', '%s', '%s', '%f', '%s', '%d', '%s', // Pola podstawowe
'%s', '%d', '%d', '%f', '%f', '%d', '%d', // Tętno, prędkość, kadencja
'%d', '%d', '%d', '%d', '%d', '%s', '%d' // Wysokość, sprzęt, linki, typ wydarzenia
);
if ( $activity_id > 0 ) {
// UPDATE
$result = $wpdb->update( $table_activities, $data, [ 'id' => $activity_id ], $format, [ '%d' ] );
$message = 'Trening zaktualizowany pomyślnie!';
} else {
// INSERT
$result = $wpdb->insert( $table_activities, $data, $format );
$message = 'Trening dodany pomyślnie!';
}
if ( $result !== false ) {
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( $message ) . '</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>Wystąpił błąd podczas zapisu do bazy.</p></div>';
}
}
/**
* Renderowanie formularza HTML
*/
function mystat_render_add_form( $activity = null ) {
// Enqueue media scripts for the uploader
wp_enqueue_media();
global $wpdb;
$table_categories = $wpdb->prefix . 'mystat_categories';
$table_event_types = $wpdb->prefix . 'mystat_event_types';
$table_equipment = $wpdb->prefix . 'mystat_equipment';
$categories = $wpdb->get_results( "SELECT * FROM $table_categories ORDER BY name ASC" );
$event_types = $wpdb->get_results( "SELECT * FROM $table_event_types ORDER BY name ASC" );
$equipment_list = $wpdb->get_results( "SELECT * FROM $table_equipment ORDER BY name ASC" );
$is_edit_mode = ! is_null( $activity );
$nonce_action = $is_edit_mode ? 'mystat_edit_entry_' . $activity->id : 'mystat_add_entry';
$form_title = $is_edit_mode ? 'Edytuj Aktywność' : 'Dodaj Nową Aktywność';
$button_text = $is_edit_mode ? 'Zaktualizuj Trening' : 'Zapisz Trening';
?>
<div class="postbox">
<div class="postbox-header"><h2 class="hndle"><?php echo esc_html( $form_title ); ?></h2></div>
<div class="inside">
<form method="post" action="">
<input type="hidden" name="activity_id" value="<?php echo $is_edit_mode ? esc_attr( $activity->id ) : '0'; ?>">
<?php wp_nonce_field( $nonce_action, '_wpnonce' ); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="title">Tytuł</label></th>
<td><input type="text" name="title" id="title" class="large-text" placeholder="np. Poranny trening w lesie" value="<?php echo $is_edit_mode ? esc_attr( $activity->title ) : ''; ?>" required></td>
</tr>
<tr>
<th scope="row"><label for="category_id">Kategoria</label></th>
<td>
<select name="category_id" id="category_id" required>
<?php foreach ( $categories as $cat ) : ?>
<option value="<?php echo esc_attr( $cat->id ); ?>" <?php echo $is_edit_mode ? selected( $activity->category_id, $cat->id, false ) : selected( $cat->name, 'Rower', false ); ?>><?php echo esc_html( $cat->name ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th scope="row"><label for="date">Data</label></th>
<td><input type="date" name="date" id="date" value="<?php echo $is_edit_mode ? esc_attr( $activity->date ) : current_time( 'Y-m-d' ); ?>" class="regular-text" required></td>
</tr>
<tr>
<th scope="row"><label for="distance">Dystans (km)</label></th>
<td><input type="text" name="distance" id="distance" class="regular-text" placeholder="0,00" value="<?php echo $is_edit_mode ? esc_attr( number_format( (float)$activity->distance, 2, ',', '' ) ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="duration">Czas (HH:MM:SS)</label></th>
<td><input type="time" name="duration" id="duration" step="1" value="<?php echo $is_edit_mode ? esc_attr( $activity->duration ) : '00:00:00'; ?>" class="regular-text"></td>
</tr>
<tr>
<th scope="row"><label for="calories">Kalorie (kcal)</label></th>
<td><input type="number" name="calories" id="calories" class="regular-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->calories ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="event_type_id">Typ wydarzenia</label></th>
<td>
<select name="event_type_id" id="event_type_id">
<option value="">-- Wybierz --</option>
<?php foreach ( $event_types as $type ) : ?>
<option value="<?php echo esc_attr( $type->id ); ?>" <?php echo $is_edit_mode ? selected( $activity->event_type_id, $type->id, false ) : selected( $type->name, 'Trening', false ); ?>><?php echo esc_html( $type->name ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th scope="row"><label for="equipment_id">Sprzęt</label></th>
<td>
<select name="equipment_id" id="equipment_id">
<option value="">-- Wybierz --</option>
<?php foreach ( $equipment_list as $item ) : ?>
<option value="<?php echo esc_attr( $item->id ); ?>" <?php if ( $is_edit_mode ) selected( $activity->equipment_id, $item->id ); ?>><?php echo esc_html( $item->name ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr class="form-field">
<td colspan="2"><hr><h4>Dane szczegółowe (opcjonalne)</h4></td>
</tr>
<tr>
<th scope="row"><label for="avg_speed">Śr. prędkość (km/h)</label></th>
<td><input type="text" name="avg_speed" id="avg_speed" class="small-text" placeholder="0,0" value="<?php echo $is_edit_mode ? esc_attr( number_format( (float)$activity->avg_speed, 2, ',', '' ) ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="max_speed">Maks. prędkość (km/h)</label></th>
<td><input type="text" name="max_speed" id="max_speed" class="small-text" placeholder="0,0" value="<?php echo $is_edit_mode ? esc_attr( number_format( (float)$activity->max_speed, 2, ',', '' ) ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="avg_heart_rate">Śr. tętno</label></th>
<td><input type="number" name="avg_heart_rate" id="avg_heart_rate" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->avg_heart_rate ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="max_heart_rate">Maks. tętno</label></th>
<td><input type="number" name="max_heart_rate" id="max_heart_rate" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->max_heart_rate ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="avg_cadence">Śr. rytm pedałowania</label></th>
<td><input type="number" name="avg_cadence" id="avg_cadence" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->avg_cadence ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="max_cadence">Maks. rytm pedałowania</label></th>
<td><input type="number" name="max_cadence" id="max_cadence" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->max_cadence ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="total_elevation_gain">Całkowity wznios (m)</label></th>
<td><input type="number" name="total_elevation_gain" id="total_elevation_gain" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->total_elevation_gain ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="total_elevation_loss">Całkowity spadek (m)</label></th>
<td><input type="number" name="total_elevation_loss" id="total_elevation_loss" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->total_elevation_loss ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="min_altitude">Min. wysokość (m)</labe></th>
<td><input type="number" name="min_altitude" id="min_altitude" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->min_altitude ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="max_altitude">Maks. wysokość (m)</label></th>
<td><input type="number" name="max_altitude" id="max_altitude" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->max_altitude ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="comment">Komentarz</label></th>
<td><textarea name="comment" id="comment" rows="3" class="large-text"><?php echo $is_edit_mode ? esc_textarea( $activity->comment ) : ''; ?></textarea></td>
</tr>
<tr class="form-field">
<td colspan="2"><hr><h4>Linki zewnętrzne (opcjonalne)</h4></td>
</tr>
<tr>
<th scope="row"><label for="strava_url">Link do Strava</label></th>
<td><input type="url" name="strava_url" id="strava_url" class="large-text" placeholder="https://www.strava.com/activities/..." value="<?php echo $is_edit_mode ? esc_attr( $activity->strava_url ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="gpx_url">Link do pliku GPX</label></th>
<td>
<input type="text" name="gpx_url" id="gpx_url" class="large-text" placeholder="Wklej URL lub wgraj plik..." value="<?php echo $is_edit_mode ? esc_attr( $activity->gpx_url ) : ''; ?>">
<button type="button" class="button" id="upload_gpx_button" style="margin-top: 5px;">Wgraj lub wybierz plik</button>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" name="mystat_submit_activity" id="submit" class="button button-primary" value="<?php echo esc_attr( $button_text ); ?>">
</p>
</form>
<script>
jQuery(document).ready(function($) {
$('#upload_gpx_button').click(function(e) {
e.preventDefault();
var gpx_uploader = wp.media({
title: 'Wybierz plik GPX',
button: { text: 'Użyj tego pliku' },
multiple: false,
library: { type: ['application/gpx+xml', 'application/xml', 'text/plain'] }
})
.on('select', function() {
var attachment = gpx_uploader.state().get('selection').first().toJSON();
$('#gpx_url').val(attachment.url).trigger('change');
}).open();
});
});
</script>
</div>
</div>
<?php
}
function mystat_render_history_table() {
global $wpdb;
// Definicje nazw tabel (z uwzględnieniem prefixu WP, jeśli był użyty przy tworzeniu)
// Zakładam, że tabele nazywają się dokładnie tak jak w dokumentacji, ale dobrą praktyką jest $wpdb->prefix
// Jeśli tabele są "sztywne" (bez prefixu wp_), usuń $wpdb->prefix.
$table_activities = $wpdb->prefix . 'mystat_activities';
$table_categories = $wpdb->prefix . 'mystat_categories';
// --- 1. OBSŁUGA USUWANIA (DELETE) ---
if ( isset( $_GET['action'], $_GET['id'], $_GET['_wpnonce'] ) && $_GET['action'] === 'mystat_delete' ) {
$activity_id = intval( $_GET['id'] );
// Weryfikacja bezpieczeństwa (Nonce)
if ( wp_verify_nonce( $_GET['_wpnonce'], 'mystat_delete_' . $activity_id ) ) {
$result = $wpdb->delete(
$table_activities,
array( 'id' => $activity_id ),
array( '%d' )
);
if ( $result ) {
echo '<div class="notice notice-success is-dismissible"><p>Aktywność została usunięta.</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>Wystąpił błąd podczas usuwania.</p></div>';
}
} else {
echo '<div class="notice notice-error is-dismissible"><p>Błąd weryfikacji bezpieczeństwa (Nonce).</p></div>';
}
}
// --- 2. POBIERANIE DANYCH (SELECT) ---
// Pobieramy ostatnie 10 wpisów, łącząc z tabelą kategorii, aby mieć nazwę i ikonę
$sql = $wpdb->prepare("
SELECT a.*, c.name as category_name, c.icon, c.color, et.name as event_type_name, eq.name as equipment_name
FROM $table_activities a
LEFT JOIN $table_categories c ON a.category_id = c.id
LEFT JOIN {$wpdb->prefix}mystat_event_types et ON a.event_type_id = et.id
LEFT JOIN {$wpdb->prefix}mystat_equipment eq ON a.equipment_id = eq.id
ORDER BY a.date DESC, a.id DESC
LIMIT %d
", 10);
$activities = $wpdb->get_results( $sql );
// --- 3. WIDOK TABELI (HTML) ---
?>
<div class="wrap">
<h2>Ostatnie Aktywności</h2>
<table class="wp-list-table widefat fixed striped table-view-list">
<thead>
<tr>
<th scope="col" style="width: 50px;">Ikona</th>
<th scope="col">Data</th>
<th scope="col">Tytuł</th>
<th scope="col" style="width: 120px;">Kategoria</th>
<th scope="col">Typ</th>
<th scope="col">Sprzęt</th>
<th scope="col">Dystans (km)</th>
<th scope="col">Czas</th>
<th scope="col">Śr. prędkość</th>
<th scope="col" style="width: 180px;">Akcja</th>
</tr>
</thead>
<tbody>
<?php if ( ! empty( $activities ) ) : ?>
<?php foreach ( $activities as $row ) : ?>
<?php
// Generowanie URL-i akcji
$delete_url = add_query_arg( array(
'page' => $_REQUEST['page'], // Zachowaj obecną stronę admina
'action' => 'mystat_delete',
'id' => $row->id,
'_wpnonce' => wp_create_nonce( 'mystat_delete_' . $row->id )
), admin_url( 'admin.php' ) );
$edit_url = add_query_arg( array(
'page' => 'mystat-edit-activity',
'id' => $row->id
), admin_url( 'admin.php' ) );
$details_url = add_query_arg( array(
'page' => 'mystat-view-activity',
'id' => $row->id
), admin_url( 'admin.php' ) );
?>
<tr>
<td>
<?php if ( ! empty( $row->icon ) ) : ?>
<span class="dashicons <?php echo esc_attr( $row->icon ); ?>" style="color: <?php echo esc_attr( $row->color ); ?>;"></span>
<?php endif; ?>
</td>
<td><?php echo esc_html( $row->date ); ?></td>
<td><strong><a href="<?php echo esc_url( $details_url ); ?>"><?php echo esc_html( $row->title ? wp_trim_words( $row->title, 6 ) : '(bez tytułu)' ); ?></a></strong></td>
<td><?php echo esc_html( $row->category_name ); ?></td>
<td><?php echo esc_html( $row->event_type_name ); ?></td>
<td><?php echo esc_html( $row->equipment_name ); ?></td>
<td><?php echo number_format( $row->distance, 2, ',', ' ' ); ?></td>
<td><?php echo esc_html( $row->duration ); ?></td>
<td><?php echo $row->avg_speed ? number_format( $row->avg_speed, 1, ',', ' ' ) . ' km/h' : '-'; ?></td>
<td>
<a href="<?php echo esc_url( $edit_url ); ?>" class="button button-small">Edytuj</a>
<a href="<?php echo esc_url( $details_url ); ?>" class="button button-small">Szczegóły</a>
<a href="<?php echo esc_url( $delete_url ); ?>"
class="button button-small button-link-delete"
onclick="return confirm('Czy na pewno chcesz usunąć ten wpis?');">
Usuń
</a>
</td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="10">Brak zarejestrowanych aktywności. Dodaj pierwszy trening powyżej!</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<?php
}
?>
<?php
// --- 3. SHORTCODE DO WYŚWIETLANIA NA FRONCIE ---
/**
* Rejestruje shortcode [moje_statystyki].
*/
function mystat_register_shortcode() {
add_shortcode( 'moje_statystyki', 'mystat_shortcode_handler' );
add_shortcode( 'moje_statystyki_wpis', 'mystat_single_activity_shortcode_handler' );
}
add_action( 'init', 'mystat_register_shortcode' );
/**
* Funkcja obsługująca shortcode.
* @param array $atts Atrybuty shortcode'u (np. year, month).
* @return string HTML do wyświetlenia.
*/
function mystat_shortcode_handler( $atts ) {
global $wpdb;
// Ustawienie domyślnych atrybutów (bieżący rok i miesiąc)
$atts = shortcode_atts( array(
'year' => current_time( 'Y' ),
'month' => current_time( 'n' ),
), $atts, 'moje_statystyki' );
$year = intval( $atts['year'] );
$month = intval( $atts['month'] );
// Pobieranie danych z bazy
$table_activities = $wpdb->prefix . 'mystat_activities';
$sql = $wpdb->prepare("
SELECT a.*, c.name as category_name, eq.name as equipment_name
FROM $table_activities a
LEFT JOIN {$wpdb->prefix}mystat_categories c ON a.category_id = c.id
LEFT JOIN {$wpdb->prefix}mystat_equipment eq ON a.equipment_id = eq.id
WHERE YEAR(a.date) = %d AND MONTH(a.date) = %d
ORDER BY a.date ASC
", $year, $month);
$activities = $wpdb->get_results( $sql );
// Obliczanie podsumowań
$total_distance = 0;
$total_seconds = 0;
foreach ($activities as $activity) {
$total_distance += $activity->distance;
if ( ! empty( $activity->duration ) ) {
list($h, $m, $s) = explode(':', $activity->duration);
$total_seconds += $h * 3600 + $m * 60 + $s;
}
}
$hours = floor($total_seconds / 3600);
$minutes = floor(($total_seconds % 3600) / 60);
$total_duration_formatted = sprintf('%d godz. %d min.', $hours, $minutes);
// Rozpoczęcie buforowania wyjścia
ob_start();
?>
<div class="mystats-shortcode-container">
<h3>Podsumowanie miesiąca</h3>
<table class="mystats-summary-table">
<thead>
<tr>
<th>Całkowity dystans</th>
<th>Całkowity czas</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo number_format( $total_distance, 2, ',', ' ' ); ?> km</td>
<td><?php echo esc_html( $total_duration_formatted ); ?></td>
</tr>
</tbody>
</table>
<h3>Lista aktywności</h3>
<table class="mystats-activity-table">
<thead>
<tr>
<th>Data</th>
<th>Tytuł</th>
<th>Kategoria</th>
<th>Dystans</th>
<th>Czas</th>
<th>Sprzęt</th>
</tr>
</thead>
<tbody>
<?php if ( ! empty( $activities ) ) : ?>
<?php foreach ( $activities as $row ) : ?>
<tr>
<td><?php echo esc_html( date_i18n( 'd.m.Y', strtotime( $row->date ) ) ); ?></td>
<td><?php echo esc_html( $row->title ); ?></td>
<td><?php echo esc_html( $row->category_name ); ?></td>
<td><?php echo number_format( $row->distance, 2, ',', ' ' ); ?> km</td>
<td><?php echo esc_html( $row->duration ); ?></td>
<td><?php echo esc_html( $row->equipment_name ); ?></td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="6">Brak aktywności w tym miesiącu.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<style>
.mystats-shortcode-container table { width: 100%; border-collapse: collapse; margin-bottom: 2em; }
.mystats-shortcode-container th, .mystats-shortcode-container td { padding: 8px 12px; border: 1px solid #ddd; text-align: left; }
.mystats-shortcode-container th { background-color: #f4f4f4; }
.mystats-activity-table td:nth-child(4), .mystats-activity-table td:nth-child(5) { text-align: right; }
</style>
<?php
// Zwrócenie zawartości bufora
return ob_get_clean();
}
?>
<?php
/**
* Funkcja obsługująca shortcode [moje_statystyki_wpis].
* @param array $atts Atrybuty shortcode'u (np. id).
* @return string HTML do wyświetlenia.
*/
function mystat_single_activity_shortcode_handler( $atts ) {
global $wpdb;
$atts = shortcode_atts( array(
'id' => 0,
), $atts, 'moje_statystyki_wpis' );
$activity_id = intval( $atts['id'] );
if ( $activity_id === 0 ) {
return '<p><strong>Błąd:</strong> Nie podano ID wpisu w shortcode.</p>';
}
// Pobieranie danych z bazy
$table_activities = $wpdb->prefix . 'mystat_activities';
$sql = $wpdb->prepare("
SELECT a.*, c.name as category_name, et.name as event_type_name, eq.name as equipment_name
FROM $table_activities a
LEFT JOIN {$wpdb->prefix}mystat_categories c ON a.category_id = c.id
LEFT JOIN {$wpdb->prefix}mystat_event_types et ON a.event_type_id = et.id
LEFT JOIN {$wpdb->prefix}mystat_equipment eq ON a.equipment_id = eq.id
WHERE a.id = %d
", $activity_id);
$activity = $wpdb->get_row( $sql );
if ( ! $activity ) {
return '<p><strong>Błąd:</strong> Nie znaleziono wpisu o ID ' . esc_html($activity_id) . '.</p>';
}
// Funkcja pomocnicza do wyświetlania wiersza
$render_row = function($label, $value, $unit = '') {
if ( ! is_null($value) && $value !== '' && $value != 0) {
echo '<tr>';
echo '<th>' . esc_html($label) . '</th>';
echo '<td>' . esc_html($value) . ($unit ? ' ' . esc_html($unit) : '') . '</td>';
echo '</tr>';
}
};
ob_start();
// Prepare map data if GPX exists
$track_points = [];
if ( ! empty( $activity->gpx_url ) ) {
$track_points = mystat_get_track_from_gpx_url( $activity->gpx_url );
if ( ! empty( $track_points ) ) {
// Enqueue scripts for the frontend
wp_enqueue_style('leaflet-css', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css');
wp_enqueue_script('leaflet-js', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js', [], '1.9.4', true);
$map_id = 'mystat-map-' . esc_attr( $activity->id );
// Pass track data to a script
wp_register_script('mystat-shortcode-map-loader-' . $activity->id, false);
wp_enqueue_script('mystat-shortcode-map-loader-' . $activity->id);
wp_add_inline_script('mystat-shortcode-map-loader-' . $activity->id,
'(function() {
document.addEventListener("DOMContentLoaded", function() {
if (typeof L === "undefined") return;
const trackPoints = ' . json_encode( $track_points ) . ';
const mapId = "' . esc_js($map_id) . '";
var container = L.DomUtil.get(mapId);
if(container != null) { container._leaflet_id = null; }
const map = L.map(mapId);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: \'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>\'
}).addTo(map);
const polyline = L.polyline(trackPoints, {color: "#3498db"}).addTo(map);
map.fitBounds(polyline.getBounds().pad(0.1));
});
})();'
);
}
}
?>
<div class="mystat-single-activity-shortcode">
<h4><?php echo esc_html( $activity->title ); ?></h4>
<p><em><?php echo esc_html( date_i18n( 'j F Y', strtotime( $activity->date ) ) ); ?></em></p>
<table class="mystat-single-summary-table">
<tbody>
<?php $render_row('Dystans', number_format($activity->distance, 2, ',', ' '), 'km'); ?>
<?php $render_row('Czas trwania', $activity->duration); ?>
<?php $render_row('Średnia prędkość', number_format($activity->avg_speed, 1, ',', ' '), 'km/h'); ?>
<?php $render_row('Suma wzniosów', $activity->total_elevation_gain, 'm'); ?>
<?php $render_row('Kategoria', $activity->category_name); ?>
<?php $render_row('Sprzęt', $activity->equipment_name); ?>
<?php if ( ! empty( $activity->strava_url ) ) : ?>
<tr><th>Strava</th><td><a href="<?php echo esc_url( $activity->strava_url ); ?>" target="_blank" rel="noopener noreferrer">Zobacz aktywność</a></td></tr>
<?php endif; ?>
</tbody>
</table>
<?php if ( ! empty( $track_points ) ) : ?>
<div id="<?php echo $map_id; ?>" style="height: 350px; width: 100%; margin-top: 15px; border-radius: 5px;"></div>
<?php endif; ?>
</div>
<style>
.mystat-single-activity-shortcode { border: 1px solid #eee; padding: 15px; margin-bottom: 1.5em; border-radius: 5px; background: #f9f9f9; }
.mystat-single-activity-shortcode h4 { margin-top: 0; }
.mystat-single-activity-shortcode p em { color: #777; font-size: 0.9em; }
.mystat-single-summary-table { width: 100%; border-collapse: collapse; }
.mystat-single-summary-table th, .mystat-single-summary-table td { padding: 4px 0; border: none; text-align: left; vertical-align: top; }
.mystat-single-summary-table th { font-weight: bold; width: 150px; }
</style>
<?php
return ob_get_clean();
}
?>