This commit is contained in:
2026-01-27 12:46:43 +01:00
parent eba19f46ed
commit 49164a9908
+226 -32
View File
@@ -146,6 +146,24 @@ function mystat_add_admin_menu() {
'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
);
}
function mystat_dashboard_page() {
@@ -157,7 +175,7 @@ function mystat_dashboard_page() {
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_new_entry();
mystat_handle_activity_form_submission();
// Formularz dodawania
mystat_render_add_form();
echo '</div>';
@@ -331,10 +349,141 @@ function mystat_equipment_page() {
<?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>';
}
};
?>
<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>
</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 wpisu do bazy danych
* Obsługa zapisu nowego lub edytowanego wpisu do bazy danych
*/
function mystat_handle_new_entry() {
function mystat_handle_activity_form_submission() {
global $wpdb;
// Sprawdź czy formularz został wysłany
@@ -342,8 +491,11 @@ function mystat_handle_new_entry() {
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'], 'mystat_add_entry' ) ) {
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;
}
@@ -389,10 +541,18 @@ function mystat_handle_new_entry() {
'%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 ) {
echo '<div class="notice notice-success is-dismissible"><p>Trening dodany pomyślnie!</p></div>';
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>';
}
@@ -401,7 +561,7 @@ function mystat_handle_new_entry() {
/**
* Renderowanie formularza HTML
*/
function mystat_render_add_form() {
function mystat_render_add_form( $activity = null ) {
global $wpdb;
$table_categories = $wpdb->prefix . 'mystat_categories';
$table_event_types = $wpdb->prefix . 'mystat_event_types';
@@ -409,62 +569,68 @@ function mystat_render_add_form() {
$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" );
$today = current_time( 'Y-m-d' );
$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">Dodaj Nową Aktywność</h2></div>
<div class="postbox-header"><h2 class="hndle"><?php echo esc_html( $form_title ); ?></h2></div>
<div class="inside">
<form method="post" action="">
<?php wp_nonce_field( 'mystat_add_entry', '_wpnonce' ); ?>
<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" required></td>
<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 selected( $cat->name, 'Rower' ); ?>><?php echo esc_html( $cat->name ); ?></option>
<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 esc_attr( $today ); ?>" class="regular-text" required></td>
<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"></td>
<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="00:00:00" class="regular-text"></td>
<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"></td>
<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">Typ wydarzenia</label></th>
<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 selected( $type->name, 'Trening' ); ?>><?php echo esc_html( $type->name ); ?></option>
<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">Sprzęt</label></th>
<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 echo esc_html( $item->name ); ?></option>
<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>
@@ -474,42 +640,58 @@ function mystat_render_add_form() {
</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"></td>
<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"></td>
<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"></td>
<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"></td>
<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"></td>
<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"></td>
<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"></textarea></td>
<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/..."></td>
<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>
</table>
<p class="submit">
<input type="submit" name="mystat_submit_activity" id="submit" class="button button-primary" value="Zapisz Trening">
<input type="submit" name="mystat_submit_activity" id="submit" class="button button-primary" value="<?php echo esc_attr( $button_text ); ?>">
</p>
</form>
</div>
@@ -579,20 +761,30 @@ function mystat_render_history_table() {
<th scope="col">Dystans (km)</th>
<th scope="col">Czas</th>
<th scope="col">Śr. prędkość</th>
<th scope="col" style="width: 80px;">Akcja</th>
<th scope="col" style="width: 180px;">Akcja</th>
</tr>
</thead>
<tbody>
<?php if ( ! empty( $activities ) ) : ?>
<?php foreach ( $activities as $row ) : ?>
<?php
// Generowanie URL do usuwania z Nonce
// 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>
@@ -601,7 +793,7 @@ function mystat_render_history_table() {
<?php endif; ?>
</td>
<td><?php echo esc_html( $row->date ); ?></td>
<td><strong><?php echo esc_html( wp_trim_words( $row->title, 6 ) ); ?></strong></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>
@@ -609,6 +801,8 @@ function mystat_render_history_table() {
<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?');">