197 lines
7.5 KiB
PHP
197 lines
7.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/includes/db_init.php';
|
|
require_once __DIR__ . '/includes/layout.php';
|
|
|
|
ensure_tables();
|
|
|
|
$pdo = db();
|
|
$errors = [];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$tanggal = $_POST['tanggal_surat'] ?? '';
|
|
$pengirim = trim($_POST['pengirim'] ?? '');
|
|
$perihal = trim($_POST['perihal'] ?? '');
|
|
$departemen = trim($_POST['departemen_tujuan'] ?? '');
|
|
$status = $_POST['status'] ?? 'baru';
|
|
$nomor = trim($_POST['nomor_surat'] ?? '');
|
|
|
|
if ($tanggal === '') { $errors[] = 'Tanggal surat wajib diisi.'; }
|
|
if ($pengirim === '') { $errors[] = 'Pengirim wajib diisi.'; }
|
|
if ($perihal === '') { $errors[] = 'Perihal wajib diisi.'; }
|
|
if ($departemen === '') { $errors[] = 'Departemen tujuan wajib diisi.'; }
|
|
|
|
$filePath = null;
|
|
if (!empty($_FILES['lampiran']['name'])) {
|
|
if ($_FILES['lampiran']['error'] === UPLOAD_ERR_OK) {
|
|
if ($_FILES['lampiran']['size'] <= 8 * 1024 * 1024) {
|
|
$ext = strtolower(pathinfo($_FILES['lampiran']['name'], PATHINFO_EXTENSION));
|
|
$allowed = ['pdf', 'doc', 'docx', 'jpg', 'jpeg', 'png'];
|
|
if (in_array($ext, $allowed, true)) {
|
|
$safeName = preg_replace('/[^a-zA-Z0-9_-]/', '_', pathinfo($_FILES['lampiran']['name'], PATHINFO_FILENAME));
|
|
$targetDir = __DIR__ . '/uploads';
|
|
if (!is_dir($targetDir)) {
|
|
mkdir($targetDir, 0775, true);
|
|
}
|
|
$fileName = 'masuk_' . time() . '_' . $safeName . '.' . $ext;
|
|
$targetPath = $targetDir . '/' . $fileName;
|
|
if (move_uploaded_file($_FILES['lampiran']['tmp_name'], $targetPath)) {
|
|
$filePath = 'uploads/' . $fileName;
|
|
} else {
|
|
$errors[] = 'Gagal mengunggah lampiran.';
|
|
}
|
|
} else {
|
|
$errors[] = 'Format lampiran tidak didukung (pdf/doc/jpg/png).';
|
|
}
|
|
} else {
|
|
$errors[] = 'Ukuran lampiran maksimal 8 MB.';
|
|
}
|
|
} else {
|
|
$errors[] = 'Lampiran gagal diunggah.';
|
|
}
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
$stmt = $pdo->prepare("INSERT INTO surat_masuk (nomor_surat, tanggal_surat, pengirim, perihal, departemen_tujuan, status, file_path) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([
|
|
$nomor !== '' ? $nomor : null,
|
|
$tanggal,
|
|
$pengirim,
|
|
$perihal,
|
|
$departemen,
|
|
$status,
|
|
$filePath
|
|
]);
|
|
header('Location: /surat_masuk.php?success=1');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$search = trim($_GET['q'] ?? '');
|
|
$where = '';
|
|
$params = [];
|
|
if ($search !== '') {
|
|
$where = "WHERE nomor_surat LIKE ? OR pengirim LIKE ? OR perihal LIKE ? OR departemen_tujuan LIKE ?";
|
|
$like = '%' . $search . '%';
|
|
$params = [$like, $like, $like, $like];
|
|
}
|
|
|
|
$stmt = $pdo->prepare("SELECT id, nomor_surat, tanggal_surat, pengirim, perihal, departemen_tujuan, status FROM surat_masuk $where ORDER BY tanggal_surat DESC, id DESC LIMIT 50");
|
|
$stmt->execute($params);
|
|
$rows = $stmt->fetchAll();
|
|
|
|
render_header('Surat Masuk', 'masuk');
|
|
?>
|
|
|
|
<?php if (!empty($_GET['success'])): ?>
|
|
<div class="toast-container position-fixed top-0 end-0 p-3">
|
|
<div class="toast text-bg-dark border-0" role="alert" data-bs-delay="3500">
|
|
<div class="toast-body">Surat masuk berhasil disimpan.</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="row g-3">
|
|
<div class="col-lg-4">
|
|
<div class="card">
|
|
<div class="card-header">Catat Surat Masuk</div>
|
|
<div class="card-body">
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<ul class="mb-0">
|
|
<?php foreach ($errors as $error): ?>
|
|
<li><?= h($error) ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
<form method="post" enctype="multipart/form-data">
|
|
<div class="mb-3">
|
|
<label class="form-label">Tanggal Surat</label>
|
|
<input type="date" name="tanggal_surat" class="form-control" required value="<?= h($_POST['tanggal_surat'] ?? date('Y-m-d')) ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Nomor Surat (opsional)</label>
|
|
<input type="text" name="nomor_surat" class="form-control" value="<?= h($_POST['nomor_surat'] ?? '') ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Pengirim</label>
|
|
<input type="text" name="pengirim" class="form-control" required value="<?= h($_POST['pengirim'] ?? '') ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Perihal</label>
|
|
<input type="text" name="perihal" class="form-control" required value="<?= h($_POST['perihal'] ?? '') ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Departemen Tujuan</label>
|
|
<input type="text" name="departemen_tujuan" class="form-control" required value="<?= h($_POST['departemen_tujuan'] ?? '') ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Status</label>
|
|
<select name="status" class="form-select">
|
|
<?php foreach (['baru' => 'Baru', 'diproses' => 'Diproses', 'selesai' => 'Selesai'] as $val => $label): ?>
|
|
<option value="<?= h($val) ?>" <?= (($val === ($_POST['status'] ?? 'baru')) ? 'selected' : '') ?>><?= h($label) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Lampiran (PDF/DOC/JPG/PNG)</label>
|
|
<input type="file" name="lampiran" class="form-control">
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">Simpan Surat Masuk</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-lg-8">
|
|
<div class="card">
|
|
<div class="card-header d-flex flex-wrap justify-content-between align-items-center gap-2">
|
|
<span>Daftar Surat Masuk</span>
|
|
<form class="d-flex gap-2" method="get">
|
|
<input type="text" name="q" class="form-control" placeholder="Cari nomor/pengirim/perihal" value="<?= h($search) ?>">
|
|
<button class="btn btn-light border" type="submit">Cari</button>
|
|
</form>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (empty($rows)): ?>
|
|
<div class="empty-state">Belum ada data surat masuk.</div>
|
|
<?php else: ?>
|
|
<div class="table-responsive">
|
|
<table class="table align-middle">
|
|
<thead>
|
|
<tr>
|
|
<th>Tanggal</th>
|
|
<th>Nomor</th>
|
|
<th>Pengirim</th>
|
|
<th>Perihal</th>
|
|
<th>Departemen</th>
|
|
<th>Status</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($rows as $row): ?>
|
|
<tr>
|
|
<td><?= h($row['tanggal_surat']) ?></td>
|
|
<td><?= h($row['nomor_surat'] ?? '-') ?></td>
|
|
<td><?= h($row['pengirim']) ?></td>
|
|
<td>
|
|
<a href="/surat_masuk_view.php?id=<?= h((string)$row['id']) ?>">
|
|
<?= h($row['perihal']) ?>
|
|
</a>
|
|
</td>
|
|
<td><?= h($row['departemen_tujuan']) ?></td>
|
|
<td><span class="<?= h(status_badge($row['status'], 'masuk')) ?>"><?= h($row['status']) ?></span></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php render_footer(); ?>
|