107 lines
3.2 KiB
PHP
107 lines
3.2 KiB
PHP
<?php
|
|
include 'koneksi.php';
|
|
include 'notification_helper.php';
|
|
header('Content-Type: application/json');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
echo json_encode(["status" => "error", "message" => "Method tidak diizinkan"]);
|
|
exit;
|
|
}
|
|
|
|
// Validasi field wajib
|
|
$required = ['name', 'no_hp', 'pin'];
|
|
foreach ($required as $field) {
|
|
if (!isset($_POST[$field]) || trim($_POST[$field]) === '') {
|
|
echo json_encode(["status" => "error", "message" => "Field $field wajib diisi"]);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$name = trim($_POST['name']);
|
|
$nik_nip = isset($_POST['nik_nip']) ? trim($_POST['nik_nip']) : '';
|
|
$no_hp = trim($_POST['no_hp']);
|
|
$pin = trim($_POST['pin']);
|
|
|
|
// Validasi PIN harus 6 digit angka
|
|
if (!preg_match('/^\d{6}$/', $pin)) {
|
|
echo json_encode(["status" => "error", "message" => "PIN harus 6 digit angka"]);
|
|
exit;
|
|
}
|
|
|
|
// Cek no_hp sudah ada atau belum
|
|
$cek = $pdo->prepare("SELECT COUNT(*) FROM occupant WHERE no_hp = ?");
|
|
$cek->execute([$no_hp]);
|
|
if ($cek->fetchColumn() > 0) {
|
|
echo json_encode(["status" => "error", "message" => "Nomor Handphone sudah digunakan"]);
|
|
exit;
|
|
}
|
|
|
|
// Removed password encryption and hashing logic
|
|
|
|
// Generate 6 digit code_register unik
|
|
function generateCodeRegister($pdo)
|
|
{
|
|
do {
|
|
$code = str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
|
|
$check = $pdo->prepare("SELECT COUNT(*) FROM occupant WHERE code_register = ?");
|
|
$check->execute([$code]);
|
|
} while ($check->fetchColumn() > 0);
|
|
return $code;
|
|
}
|
|
|
|
$code_register = generateCodeRegister($pdo);
|
|
|
|
// Upload foto
|
|
$foto = '';
|
|
if (isset($_FILES['foto']) && $_FILES['foto']['error'] === UPLOAD_ERR_OK) {
|
|
|
|
$allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
|
$ext = strtolower(pathinfo($_FILES['foto']['name'], PATHINFO_EXTENSION));
|
|
|
|
if (!in_array($ext, $allowed)) {
|
|
echo json_encode(["status" => "error", "message" => "Format foto tidak didukung"]);
|
|
exit;
|
|
}
|
|
|
|
if ($_FILES['foto']['size'] > 5 * 1024 * 1024) {
|
|
echo json_encode(["status" => "error", "message" => "Ukuran foto maksimal 5MB"]);
|
|
exit;
|
|
}
|
|
|
|
$uploadDir = __DIR__ . '/uploads/';
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0755, true);
|
|
}
|
|
|
|
$fotoName = 'occ_' . time() . '_' . random_int(1000, 9999) . '.' . $ext;
|
|
$targetPath = $uploadDir . $fotoName;
|
|
|
|
if (move_uploaded_file($_FILES['foto']['tmp_name'], $targetPath)) {
|
|
$foto = 'uploads/' . $fotoName;
|
|
}
|
|
}
|
|
|
|
try {
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO occupant (name, nik_nip, no_hp, pin, foto, code_register, progress, status, access)
|
|
VALUES (?, ?, ?, ?, ?, ?, 'unused', 'rejected', 'suspend')
|
|
");
|
|
$stmt->execute([$name, $nik_nip, $no_hp, $pin, $foto, $code_register]);
|
|
$newId = $pdo->lastInsertId();
|
|
send_notification($pdo, 'mdi-account-multiple-plus', 'Pendaftaran Occupant Baru', "Admin menambahkan occupant: {$name}.", "No HP: {$no_hp}. Status awal: rejected, suspend. Harap verifikasi.", false);
|
|
|
|
echo json_encode([
|
|
"status" => "success",
|
|
"code_register" => $code_register,
|
|
"id_occupant" => $newId
|
|
]);
|
|
|
|
}
|
|
catch (PDOException $e) {
|
|
echo json_encode([
|
|
"status" => "error",
|
|
"message" => "Gagal menyimpan: " . $e->getMessage()
|
|
]);
|
|
}
|
|
?>
|