195 lines
6.4 KiB
PHP
195 lines
6.4 KiB
PHP
<?php
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
function statpress_dashboard_page() {
|
|
echo '<div class="wrap"><h1>StatPress Dashboard</h1>';
|
|
statpress_render_history_table();
|
|
echo '</div>';
|
|
}
|
|
|
|
function statpress_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 . 'statpress_activities';
|
|
$table_categories = $wpdb->prefix . 'statpress_categories';
|
|
|
|
// --- 1. OBSŁUGA USUWANIA (DELETE) ---
|
|
if ( isset( $_GET['action'], $_GET['id'], $_GET['_wpnonce'] ) && 'statpress_delete' === $_GET['action'] ) {
|
|
$activity_id = intval( $_GET['id'] );
|
|
|
|
// Weryfikacja bezpieczeństwa (Nonce)
|
|
if ( wp_verify_nonce( $_GET['_wpnonce'], 'statpress_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. USTAWIENIA PAGINACJI ---
|
|
$items_per_page = 20; // Ile wpisów na stronę
|
|
$current_page = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
|
|
$offset = ( $current_page - 1 ) * $items_per_page;
|
|
|
|
// --- 3. POBIERANIE DANYCH (SELECT) ---
|
|
// Pobranie całkowitej liczby wpisów do paginacji
|
|
$total_items = $wpdb->get_var( "SELECT COUNT(id) FROM $table_activities" );
|
|
$total_pages = ceil( $total_items / $items_per_page );
|
|
|
|
// Pobieramy wpisy dla bieżącej strony
|
|
$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}statpress_event_types et ON a.event_type_id = et.id
|
|
LEFT JOIN {$wpdb->prefix}statpress_equipment eq ON a.equipment_id = eq.id
|
|
ORDER BY a.date DESC, a.id DESC
|
|
LIMIT %d OFFSET %d
|
|
",
|
|
$items_per_page,
|
|
$offset
|
|
);
|
|
|
|
$activities = $wpdb->get_results( $sql );
|
|
|
|
// --- 4. WIDOK TABELI (HTML) ---
|
|
?>
|
|
<div class="wrap">
|
|
<h2>Historia Aktywności</h2>
|
|
|
|
<?php if ( $total_pages > 1 ) : ?>
|
|
<div class="tablenav top">
|
|
<div class="tablenav-pages">
|
|
<span class="displaying-num"><?php echo esc_html( $total_items ); ?> aktywności</span>
|
|
<?php
|
|
echo paginate_links(
|
|
array(
|
|
'base' => add_query_arg( 'paged', '%#%' ),
|
|
'format' => '',
|
|
'total' => $total_pages,
|
|
'current' => $current_page,
|
|
'prev_text' => '« Poprzednia',
|
|
'next_text' => 'Następna »',
|
|
)
|
|
);
|
|
?>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<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 z zachowaniem paginacji
|
|
$delete_url = wp_nonce_url(
|
|
add_query_arg(
|
|
array(
|
|
'action' => 'statpress_delete',
|
|
'id' => $row->id,
|
|
)
|
|
),
|
|
'statpress_delete_' . $row->id
|
|
);
|
|
|
|
$edit_url = add_query_arg(
|
|
array(
|
|
'page' => 'statpress-edit-activity',
|
|
'id' => $row->id,
|
|
),
|
|
admin_url( 'admin.php' )
|
|
);
|
|
|
|
$details_url = add_query_arg(
|
|
array(
|
|
'page' => 'statpress-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>
|
|
|
|
<?php if ( $total_pages > 1 ) : ?>
|
|
<div class="tablenav bottom">
|
|
<div class="tablenav-pages">
|
|
<span class="displaying-num"><?php echo esc_html( $total_items ); ?> aktywności</span>
|
|
<?php
|
|
echo paginate_links(
|
|
array(
|
|
'base' => add_query_arg( 'paged', '%#%' ),
|
|
'format' => '',
|
|
'total' => $total_pages,
|
|
'current' => $current_page,
|
|
'prev_text' => '« Poprzednia',
|
|
'next_text' => 'Następna »',
|
|
)
|
|
);
|
|
?>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php
|
|
}
|