37138-vm/templates/teachers.php
Flatlogic Bot bb0884a9fc SIAKAD 1.0
2025-12-24 04:07:02 +00:00

166 lines
8.0 KiB
PHP

<?php
// templates/teachers.php
// Handle form submissions and actions
$feedback = handle_guru_action();
// Get the current action from URL, default to 'list'
$action = $_GET['action'] ?? 'list';
$id = $_GET['id'] ?? null;
// Get data for a single teacher if editing
$guru = null;
if ($action === 'edit' && $id) {
$guru = get_guru_by_id($id);
if (!$guru) {
// If teacher not found, redirect to list with an error
header("Location: index.php?page=teachers&status=not_found");
exit();
}
}
?>
<main class="main-content">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Manajemen Guru</h1>
<?php if ($action === 'list'): ?>
<a href="index.php?page=teachers&action=add" class="btn btn-primary">Tambah Guru</a>
<?php else: ?>
<a href="index.php?page=teachers" class="btn btn-outline-secondary">Kembali ke Daftar</a>
<?php endif; ?>
</div>
<?php if (isset($feedback['message'])): ?>
<div class="alert alert-<?php echo $feedback['success'] ? 'success' : 'danger'; ?>">
<?php echo htmlspecialchars($feedback['message']); ?>
</div>
<?php endif; ?>
<?php if (isset($_GET['status']) && $_GET['status'] == 'deleted'): ?>
<div class="alert alert-success">Data guru berhasil dihapus.</div>
<?php endif; ?>
<?php if (isset($_GET['status']) && $_GET['status'] == 'error'): ?>
<div class="alert alert-danger">Terjadi kesalahan saat menghapus data.</div>
<?php endif; ?>
<?php if (isset($_GET['status']) && $_GET['status'] == 'not_found'): ?>
<div class="alert alert-warning">Data guru tidak ditemukan.</div>
<?php endif; ?>
<?php if ($action === 'add' || $action === 'edit'): ?>
<div class="card">
<div class="card-header">
<h5 class="card-title"><?php echo $action === 'add' ? 'Tambah Guru Baru' : 'Ubah Data Guru'; ?></h5>
</div>
<div class="card-body">
<form action="index.php?page=teachers" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="<?php echo $action; ?>">
<?php if ($id): ?>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<?php endif; ?>
<div class="row">
<div class="col-md-6 mb-3">
<label for="nip" class="form-label">NIP</label>
<input type="text" class="form-control" id="nip" name="nip" value="<?php echo htmlspecialchars($guru['nip'] ?? ''); ?>" required>
</div>
<div class="col-md-6 mb-3">
<label for="nama" class="form-label">Nama Lengkap</label>
<input type="text" class="form-control" id="nama" name="nama" value="<?php echo htmlspecialchars($guru['nama'] ?? ''); ?>" required>
</div>
</div>
<div class="mb-3">
<label for="alamat" class="form-label">Alamat</label>
<textarea class="form-control" id="alamat" name="alamat" rows="3"><?php echo htmlspecialchars($guru['alamat'] ?? ''); ?></textarea>
</div>
<div class="mb-3">
<label for="telepon" class="form-label">No. Telepon</label>
<input type="tel" class="form-control" id="telepon" name="telepon" value="<?php echo htmlspecialchars($guru['telepon'] ?? ''); ?>">
</div>
<hr>
<h5 class="mb-3">Foto dan Lokasi</h5>
<div class="mb-3">
<label for="foto" class="form-label">Foto Guru</label>
<input class="form-control" type="file" id="foto" name="foto" accept="image/jpeg, image/png">
<?php if (!empty($guru['foto'])): ?>
<div class="mt-2">
<img src="assets/uploads/guru/<?php echo htmlspecialchars($guru['foto']); ?>" alt="Foto saat ini" style="max-height: 100px; border-radius: 4px;">
<input type="hidden" name="foto_existing" value="<?php echo htmlspecialchars($guru['foto']); ?>">
</div>
<?php endif; ?>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="latitude" class="form-label">Latitude</label>
<input type="text" class="form-control" id="latitude" name="latitude" value="<?php echo htmlspecialchars($guru['latitude'] ?? ''); ?>">
</div>
<div class="col-md-6 mb-3">
<label for="longitude" class="form-label">Longitude</label>
<input type="text" class="form-control" id="longitude" name="longitude" value="<?php echo htmlspecialchars($guru['longitude'] ?? ''); ?>">
</div>
</div>
<div id="map" style="height: 300px;" class="mb-3"></div>
<p class="form-text text-muted">Klik pada peta untuk mengatur koordinat Latitude dan Longitude.</p>
<button type="submit" class="btn btn-success">Simpan Data</button>
</form>
</div>
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead class="table-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Foto</th>
<th scope="col">NIP</th>
<th scope="col">Nama</th>
<th scope="col">Aksi</th>
</tr>
</thead>
<tbody>
<?php
$all_guru = get_all_guru();
if (count($all_guru) > 0):
$i = 1;
foreach ($all_guru as $g):
$foto_path = 'assets/uploads/guru/' . ($g['foto'] ?? 'default.png');
if (empty($g['foto']) || !file_exists($foto_path)) {
$foto_path = 'assets/images/placeholder.png';
}
?>
<tr>
<td><?php echo $i++; ?></td>
<td>
<img src="<?php echo htmlspecialchars($foto_path); ?>" alt="Foto <?php echo htmlspecialchars($g['nama']); ?>" style="width: 50px; height: 50px; object-fit: cover; border-radius: 50%;">
</td>
<td><?php echo htmlspecialchars($g['nip']); ?></td>
<td><?php echo htmlspecialchars($g['nama']); ?></td>
<td>
<a href="index.php?page=teachers&action=edit&id=<?php echo $g['id']; ?>" class="btn btn-sm btn-warning">Ubah</a>
<a href="index.php?page=teachers&action=delete&id=<?php echo $g['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Apakah Anda yakin ingin menghapus data ini?');">Hapus</a>
</td>
</tr>
<?php
endforeach;
else:
?>
<tr>
<td colspan="5" class="text-center">Belum ada data guru.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</main>