diff --git a/moje-statystyki.php b/moje-statystyki.php
index 769bb51..df4301d 100644
--- a/moje-statystyki.php
+++ b/moje-statystyki.php
@@ -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 '
Dodaj Nowy Trening
';
// 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 '';
@@ -331,19 +349,153 @@ function mystat_equipment_page() {
Błąd
Nie podano ID aktywności do edycji.
';
+ 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 'Błąd
Nie znaleziono aktywności o podanym ID.
';
+ return;
+ }
+
+ echo 'Edytuj Trening
';
+ mystat_render_add_form( $activity );
+ echo '';
+}
+
+function mystat_view_activity_page() {
+ global $wpdb;
+
+ $activity_id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0;
+
+ if ( $activity_id === 0 ) {
+ echo 'Błąd
Nie podano ID aktywności.
';
+ 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 'Błąd
Nie znaleziono aktywności o podanym ID.
';
+ return;
+ }
+
+ // Funkcja pomocnicza do wyświetlania wiersza, jeśli wartość istnieje
+ $render_row = function($label, $value, $unit = '') {
+ if ( ! is_null($value) && $value !== '' ) {
+ echo '';
+ echo '| ' . esc_html($label) . ' | ';
+ echo '' . esc_html($value) . ($unit ? ' ' . esc_html($unit) : '') . ' | ';
+ echo '
';
+ }
+ };
+
+ ?>
+
+
+ 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 'Błąd weryfikacji bezpieczeństwa formularza.
';
return;
}
@@ -389,10 +541,18 @@ function mystat_handle_new_entry() {
'%d', '%d', '%d', '%d', '%d', '%s', '%d' // Wysokość, sprzęt, linki, typ wydarzenia
);
- $result = $wpdb->insert( $table_activities, $data, $format );
+ 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 'Trening dodany pomyślnie!
';
+ if ( $result !== false ) {
+ echo '' . esc_html( $message ) . '
';
} else {
echo 'Wystąpił błąd podczas zapisu do bazy.
';
}
@@ -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';
+
?>
-
+
@@ -579,20 +761,30 @@ function mystat_render_history_table() {
Dystans (km) |
Czas |
Śr. prędkość |
-
Akcja |
+
Akcja |
$_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' ) );
?>
|
@@ -601,7 +793,7 @@ function mystat_render_history_table() {
|
date ); ?> |
- title, 6 ) ); ?> |
+ title ? wp_trim_words( $row->title, 6 ) : '(bez tytułu)' ); ?> |
category_name ); ?> |
event_type_name ); ?> |
equipment_name ); ?> |
@@ -609,6 +801,8 @@ function mystat_render_history_table() {
duration ); ?> |
avg_speed ? number_format( $row->avg_speed, 1, ',', ' ' ) . ' km/h' : '-'; ?> |
+ Edytuj
+ Szczegóły
|