Files
wp-cycling-stats/includes/admin/pages/page-equipment.php
T
2026-02-05 12:59:40 +01:00

297 lines
16 KiB
PHP

<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
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' ) ) {
$item_id = isset( $_POST['equipment_id'] ) ? intval( $_POST['equipment_id'] ) : 0;
$data = array(
'name' => sanitize_text_field( $_POST['equipment_name'] ),
'type' => sanitize_text_field( $_POST['equipment_type'] ),
'purchase_date' => empty( $_POST['purchase_date'] ) ? null : sanitize_text_field( $_POST['purchase_date'] ),
'initial_cost' => empty( $_POST['initial_cost'] ) ? null : floatval( str_replace( ',', '.', $_POST['initial_cost'] ) ),
'status' => sanitize_text_field( $_POST['status'] ),
'notes' => sanitize_textarea_field( $_POST['notes'] ),
);
if ( ! empty( $data['name'] ) ) {
if ( $item_id > 0 ) { // Update
$result = $wpdb->update( $table_equipment, $data, array( 'id' => $item_id ) );
if ( false !== $result ) {
$message = 'Sprzęt zaktualizowany.';
$notice_class = 'notice-success';
} else {
$message = 'Błąd podczas aktualizacji sprzętu: ' . $wpdb->last_error;
$notice_class = 'notice-error';
}
} else { // Insert
$result = $wpdb->insert( $table_equipment, $data );
if ( false !== $result ) {
$message = 'Sprzęt dodany.';
$notice_class = 'notice-success';
} else {
$message = 'Błąd podczas dodawania sprzętu: ' . $wpdb->last_error;
$notice_class = 'notice-error';
}
}
} 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'] ) && 'delete' === $_GET['action'] ) {
if ( wp_verify_nonce( $_GET['_wpnonce'], 'mystat_delete_equipment_' . $_GET['id'] ) ) {
// Sprawdź, czy sprzęt nie jest używany
$usage_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}mystat_activities WHERE equipment_id = %d", intval( $_GET['id'] ) ) );
if ( 0 == $usage_count ) {
$wpdb->delete( $table_equipment, array( 'id' => intval( $_GET['id'] ) ) );
// Usuń również powiązane wpisy w dzienniku
$wpdb->delete( $wpdb->prefix . 'mystat_equipment_log', array( 'equipment_id' => intval( $_GET['id'] ) ) );
$message = 'Sprzęt usunięty.';
$notice_class = 'notice-success';
} else {
$message = 'Nie można usunąć sprzętu, ponieważ jest przypisany do ' . $usage_count . ' aktywności. Zmień jego status na "wycofany".';
$notice_class = 'notice-error';
}
}
}
// Prepare for form (for editing)
$item_to_edit = null;
$statuses = array( 'aktywny', 'sprzedany', 'wycofany' );
if ( isset( $_GET['action'], $_GET['id'] ) && 'edit' === $_GET['action'] ) {
$item_to_edit = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_equipment WHERE id = %d", intval( $_GET['id'] ) ) );
}
$table_activities = $wpdb->prefix . 'mystat_activities';
$equipment_list = $wpdb->get_results("
SELECT
eq.id,
eq.name,
eq.type,
eq.status,
SUM(a.distance) as total_distance,
COUNT(a.id) as activity_count
FROM
{$table_equipment} eq
LEFT JOIN
{$table_activities} a ON eq.id = a.equipment_id
GROUP BY
eq.id, eq.name, eq.type, eq.status
ORDER BY
eq.status ASC, eq.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 pozycję' : '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 form-required"><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>
<div class="form-field"><label for="equipment_type">Typ</label><input type="text" name="equipment_type" id="equipment_type" value="<?php echo $item_to_edit ? esc_attr( $item_to_edit->type ) : 'Rower'; ?>"></div>
<div class="form-field"><label for="purchase_date">Data zakupu</label><input type="date" name="purchase_date" id="purchase_date" value="<?php echo $item_to_edit ? esc_attr( $item_to_edit->purchase_date ) : ''; ?>"></div>
<div class="form-field"><label for="initial_cost">Koszt zakupu</label><input type="text" name="initial_cost" id="initial_cost" value="<?php echo $item_to_edit ? esc_attr( $item_to_edit->initial_cost ) : ''; ?>" placeholder="0.00"></div>
<div class="form-field"><label for="status">Status</label><select name="status" id="status"><?php foreach ( $statuses as $s ) : ?><option value="<?php echo esc_attr( $s ); ?>" <?php if ( $item_to_edit ) {
selected( $item_to_edit->status, $s );} ?>><?php echo esc_html( ucfirst( $s ) ); ?></option><?php endforeach; ?></select></div>
<div class="form-field"><label for="notes">Notatki</label><textarea name="notes" id="notes" rows="3"><?php echo $item_to_edit ? esc_textarea( $item_to_edit->notes ) : ''; ?></textarea></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 striped">
<thead><tr><th>Nazwa</th><th>Przebieg</th><th>Liczba aktywności</th><th>Status</th><th style="width: 220px;">Akcje</th></tr></thead>
<tbody>
<?php foreach ( $equipment_list as $item ) : ?>
<?php
$details_url = admin_url( 'admin.php?page=mystat-equipment-details&id=' . $item->id );
?>
<tr>
<td><strong><a href="<?php echo esc_url( $details_url ); ?>"><?php echo esc_html( $item->name ); ?></a></strong><br><small><?php echo esc_html( $item->type ); ?></small></td>
<td><?php echo $item->total_distance ? number_format( $item->total_distance, 2, ',', ' ' ) . ' km' : '0 km'; ?></td>
<td><?php echo (int) $item->activity_count; ?></td>
<td><?php echo esc_html( ucfirst( $item->status ) ); ?></td>
<td>
<a href="<?php echo esc_url( $details_url ); ?>" class="button button-small">Dziennik / Serwis</a>
<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit', 'id' => $item->id ) ) ); ?>" class="button button-small">Edytuj</a>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'delete', 'id' => $item->id ) ), 'mystat_delete_equipment_' . $item->id ) ); ?>" onclick="return confirm('Czy na pewno chcesz usunąć ten sprzęt?')" class="button button-small button-link-delete">Usuń</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php
}
function mystat_equipment_details_page() {
global $wpdb;
$equipment_id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0;
if ( 0 === $equipment_id ) {
echo '<div class="wrap"><h1>Błąd</h1><p>Nie podano ID sprzętu.</p></div>';
return;
}
$table_equipment = $wpdb->prefix . 'mystat_equipment';
$table_equipment_log = $wpdb->prefix . 'mystat_equipment_log';
$table_activities = $wpdb->prefix . 'mystat_activities';
$message = '';
$notice_class = '';
// --- Handle Service Log form submissions (add/update/delete) ---
if ( isset( $_POST['submit_log'] ) && check_admin_referer( 'mystat_manage_equipment_log' ) ) {
$log_id = isset( $_POST['log_id'] ) ? intval( $_POST['log_id'] ) : 0;
$log_data = array(
'equipment_id' => $equipment_id,
'log_date' => sanitize_text_field( $_POST['log_date'] ),
'log_type' => sanitize_text_field( $_POST['log_type'] ),
'description' => sanitize_textarea_field( $_POST['description'] ),
'cost' => empty( $_POST['cost'] ) ? null : floatval( str_replace( ',', '.', $_POST['cost'] ) ),
'mileage' => empty( $_POST['mileage'] ) ? null : intval( $_POST['mileage'] ),
);
if ( ! empty( $log_data['log_date'] ) && ! empty( $log_data['log_type'] ) && ! empty( $log_data['description'] ) ) {
if ( $log_id > 0 ) {
$wpdb->update( $table_equipment_log, $log_data, array( 'id' => $log_id ) );
$message = 'Wpis w dzienniku zaktualizowany.';
} else {
$wpdb->insert( $table_equipment_log, $log_data );
$message = 'Wpis dodany do dziennika.';
}
$notice_class = 'notice-success';
} else {
$message = 'Wypełnij wymagane pola (Data, Typ, Opis).';
$notice_class = 'notice-error';
}
}
if ( isset( $_GET['action'], $_GET['log_id'], $_GET['_wpnonce'] ) && 'delete_log' === $_GET['action'] ) {
if ( wp_verify_nonce( $_GET['_wpnonce'], 'mystat_delete_log_' . $_GET['log_id'] ) ) {
$wpdb->delete( $table_equipment_log, array( 'id' => intval( $_GET['log_id'] ) ) );
$message = 'Wpis z dziennika usunięty.';
$notice_class = 'notice-success';
}
}
// --- Get Data ---
$equipment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_equipment WHERE id = %d", $equipment_id ) );
if ( ! $equipment ) {
echo '<div class="wrap"><h1>Błąd</h1><p>Nie znaleziono sprzętu o podanym ID.</p></div>';
return;
}
$total_mileage = $wpdb->get_var( $wpdb->prepare( "SELECT SUM(distance) FROM $table_activities WHERE equipment_id = %d", $equipment_id ) );
$total_service_cost = $wpdb->get_var( $wpdb->prepare( "SELECT SUM(cost) FROM $table_equipment_log WHERE equipment_id = %d", $equipment_id ) );
$service_log = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_equipment_log WHERE equipment_id = %d ORDER BY log_date DESC, id DESC", $equipment_id ) );
$log_to_edit = null;
if ( isset( $_GET['action'], $_GET['log_id'] ) && 'edit_log' === $_GET['action'] ) {
$log_to_edit = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_equipment_log WHERE id = %d", intval( $_GET['log_id'] ) ) );
}
$log_types = array( 'Naprawa', 'Zakup części', 'Przegląd', 'Modyfikacja', 'Inne' );
?>
<div class="wrap">
<h1>Dziennik serwisowy: <?php echo esc_html( $equipment->name ); ?></h1>
<p><a href="<?php echo esc_url( admin_url( 'admin.php?page=mystat-equipment' ) ); ?>">&larr; Powrót do listy sprzętu</a></p>
<?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 class="postbox">
<div class="postbox-header"><h2 class="hndle">Podsumowanie Sprzętu</h2></div>
<div class="inside">
<div class="main">
<p><strong>Całkowity przebieg:</strong> <?php echo number_format( (float) $total_mileage, 2, ',', ' ' ); ?> km</p>
<?php if ( $total_service_cost > 0 ) : ?>
<p><strong>Całkowity koszt serwisu:</strong> <?php echo number_format( $total_service_cost, 2, ',', ' ' ); ?> zł</p>
<?php endif; ?>
<p><strong>Status:</strong> <?php echo esc_html( ucfirst( $equipment->status ) ); ?></p>
<?php if ( $equipment->purchase_date ) : ?><p><strong>Data zakupu:</strong> <?php echo esc_html( $equipment->purchase_date ); ?></p><?php endif; ?>
<?php if ( $equipment->initial_cost ) : ?><p><strong>Koszt zakupu:</strong> <?php echo number_format( $equipment->initial_cost, 2, ',', ' ' ); ?> zł</p><?php endif; ?>
<?php if ( $equipment->notes ) : ?><p><strong>Notatki:</strong><br><?php echo nl2br( esc_html( $equipment->notes ) ); ?></p><?php endif; ?>
</div>
</div>
</div>
<div id="col-container" class="wp-clearfix">
<div id="col-left">
<div class="col-wrap">
<div class="form-wrap">
<h2><?php echo $log_to_edit ? 'Edytuj wpis' : 'Dodaj wpis do dziennika'; ?></h2>
<form method="post">
<input type="hidden" name="log_id" value="<?php echo $log_to_edit ? esc_attr( $log_to_edit->id ) : '0'; ?>">
<?php wp_nonce_field( 'mystat_manage_equipment_log' ); ?>
<div class="form-field form-required"><label for="log_date">Data</label><input type="date" name="log_date" id="log_date" value="<?php echo $log_to_edit ? esc_attr( $log_to_edit->log_date ) : current_time( 'Y-m-d' ); ?>" required></div>
<div class="form-field form-required"><label for="log_type">Typ</label><select name="log_type" id="log_type" required><?php foreach ( $log_types as $type ) : ?><option value="<?php echo esc_attr( $type ); ?>" <?php if ( $log_to_edit ) {
selected( $log_to_edit->log_type, $type );} ?>><?php echo esc_html( $type ); ?></option><?php endforeach; ?></select></div>
<div class="form-field form-required"><label for="description">Opis</label><textarea name="description" id="description" rows="4" required><?php echo $log_to_edit ? esc_textarea( $log_to_edit->description ) : ''; ?></textarea></div>
<div class="form-field"><label for="cost">Koszt (zł)</label><input type="text" name="cost" id="cost" value="<?php echo $log_to_edit ? esc_attr( $log_to_edit->cost ) : ''; ?>" placeholder="0.00"></div>
<div class="form-field"><label for="mileage">Przebieg (km)</label><input type="number" name="mileage" id="mileage" value="<?php echo $log_to_edit ? esc_attr( $log_to_edit->mileage ) : round( (float) $total_mileage ); ?>"><p class="description">Przebieg sprzętu w momencie serwisu. Domyślnie aktualny całkowity przebieg.</p></div>
<?php submit_button( $log_to_edit ? 'Zaktualizuj wpis' : 'Dodaj wpis', 'primary', 'submit_log' ); ?>
<?php if ( $log_to_edit ) : ?><a href="<?php echo esc_url( remove_query_arg( array( 'action', 'log_id' ) ) ); ?>" class="button">Anuluj edycję</a><?php endif; ?>
</form>
</div>
</div>
</div>
<div id="col-right">
<div class="col-wrap">
<table class="wp-list-table widefat fixed striped">
<tfoot>
<tr>
<th colspan="3" style="text-align:right;">Suma kosztów:</th>
<th colspan="3"><strong><?php echo number_format( (float) $total_service_cost, 2, ',', ' ' ); ?> zł</strong></th>
</tr>
</tfoot>
<thead><tr><th>Data</th><th>Typ</th><th>Opis</th><th>Koszt</th><th>Przebieg</th><th style="width: 100px;">Akcje</th></tr></thead>
<tbody>
<?php if ( empty( $service_log ) ) : ?>
<tr><td colspan="6">Brak wpisów w dzienniku.</td></tr>
<?php else : ?>
<?php foreach ( $service_log as $log ) : ?>
<tr>
<td><?php echo esc_html( $log->log_date ); ?></td>
<td><?php echo esc_html( $log->log_type ); ?></td>
<td><?php echo nl2br( esc_html( $log->description ) ); ?></td>
<td><?php echo $log->cost ? number_format( $log->cost, 2, ',', ' ' ) . ' zł' : '-'; ?></td>
<td><?php echo $log->mileage ? number_format( $log->mileage, 0, '', ' ' ) . ' km' : '-'; ?></td>
<td>
<a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit_log', 'log_id' => $log->id ) ) ); ?>">Edytuj</a> |
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'delete_log', 'log_id' => $log->id ) ), 'mystat_delete_log_' . $log->id ) ); ?>" onclick="return confirm('Czy na pewno chcesz usunąć ten wpis?')" style="color: #a00;">Usuń</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php
}