Initial commit
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* WebGIS API - Update Data Endpoint (Improved Version)
|
||||
* Version: 1.0
|
||||
* Method: POST
|
||||
* Description: Update existing feature data
|
||||
*/
|
||||
|
||||
require_once 'api_utils.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
sendError('Method not allowed', 405);
|
||||
}
|
||||
|
||||
$json = file_get_contents('php://input');
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if ($data === null) {
|
||||
sendError('Invalid JSON input', 400);
|
||||
}
|
||||
|
||||
if (!isset($data['id'])) {
|
||||
sendError('ID field is required', 400);
|
||||
}
|
||||
|
||||
$conn = getConnection();
|
||||
|
||||
try {
|
||||
$id = intval($data['id']);
|
||||
|
||||
// Check if record exists
|
||||
$check_sql = "SELECT * FROM data_unified WHERE id = $id";
|
||||
$check_result = $conn->query($check_sql);
|
||||
|
||||
if ($check_result->num_rows === 0) {
|
||||
sendError('Record not found', 404);
|
||||
}
|
||||
|
||||
$old_data = $check_result->fetch_assoc();
|
||||
|
||||
// Build UPDATE query
|
||||
$updates = [];
|
||||
$allowed_fields = [
|
||||
'nama', 'jenis', 'alamat', 'kontak', 'no_wa', 'kegiatan', 'kapasitas',
|
||||
'radius_meter', 'nama_kepala_keluarga', 'jumlah_anggota', 'kondisi_rumah',
|
||||
'sumber_penghasilan', 'kebutuhan_prioritas', 'status_bantuan', 'latitude',
|
||||
'longitude', 'status', 'panjang', 'luas', 'geom'
|
||||
];
|
||||
|
||||
foreach ($allowed_fields as $field) {
|
||||
if (isset($data[$field])) {
|
||||
if ($field === 'geom') {
|
||||
if (is_array($data[$field])) {
|
||||
$geom_json = json_encode($data[$field]);
|
||||
} else {
|
||||
$geom_json = $data[$field];
|
||||
}
|
||||
$updates[] = "$field = '" . $conn->real_escape_string($geom_json) . "'";
|
||||
} else if (is_numeric($data[$field])) {
|
||||
$updates[] = "$field = " . floatval($data[$field]);
|
||||
} else {
|
||||
$updates[] = "$field = '" . $conn->real_escape_string($data[$field]) . "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($updates)) {
|
||||
sendError('No fields to update', 400);
|
||||
}
|
||||
|
||||
$updates[] = "updated_at = NOW()";
|
||||
|
||||
$sql = "UPDATE data_unified SET " . implode(', ', $updates) . " WHERE id = $id";
|
||||
|
||||
if ($conn->query($sql) === TRUE) {
|
||||
// Log activity
|
||||
$new_data = [];
|
||||
foreach ($allowed_fields as $field) {
|
||||
if (isset($data[$field])) {
|
||||
$new_data[$field] = $data[$field];
|
||||
}
|
||||
}
|
||||
|
||||
logActivity($conn, 'UPDATE', [
|
||||
'id' => $id,
|
||||
'old_data' => $old_data,
|
||||
'new_data' => $new_data
|
||||
]);
|
||||
|
||||
sendResponse(true, 'Data updated successfully', ['id' => $id]);
|
||||
} else {
|
||||
sendError('Failed to update data: ' . $conn->error, 500);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
sendError('Server error: ' . $e->getMessage(), 500);
|
||||
} finally {
|
||||
$conn->close();
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user