Initial commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
header('Content-Type: application/json');
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!isset($data['id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing road id']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
$stmt = $db->prepare('DELETE FROM roads WHERE id = ?');
|
||||
$stmt->execute([$data['id']]);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
header('Content-Type: application/json');
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!isset($data['id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing parcel id']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
$stmt = $db->prepare('DELETE FROM land_parcels WHERE id = ?');
|
||||
$stmt->execute([$data['id']]);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
header('Content-Type: application/json');
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
try {
|
||||
$db = getDB();
|
||||
$stmt = $db->query('SELECT id, name, road_type, coordinates, length_meters, created_at, updated_at FROM roads ORDER BY created_at DESC');
|
||||
$roads = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// Decode coordinates JSON for each road
|
||||
foreach ($roads as &$road) {
|
||||
$road['coordinates'] = json_decode($road['coordinates'], true);
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'lines' => $roads
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
header('Content-Type: application/json');
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
try {
|
||||
$db = getDB();
|
||||
$stmt = $db->query('SELECT id, name, certificate_type, coordinates, area_sqm, created_at, updated_at FROM land_parcels ORDER BY created_at DESC');
|
||||
$parcels = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// Decode coordinates JSON for each parcel
|
||||
foreach ($parcels as &$parcel) {
|
||||
$parcel['coordinates'] = json_decode($parcel['coordinates'], true);
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'polygons' => $parcels
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
header('Content-Type: application/json');
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
// Validate required fields
|
||||
if (!isset($data['name']) || !isset($data['road_type']) || !isset($data['coordinates']) || !isset($data['length_meters'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing required fields: name, road_type, coordinates, or length_meters']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate road_type
|
||||
$validTypes = ['Nasional', 'Provinsi', 'Kabupaten'];
|
||||
if (!in_array($data['road_type'], $validTypes)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid road_type. Must be: Nasional, Provinsi, or Kabupaten']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate coordinates array
|
||||
if (!is_array($data['coordinates']) || count($data['coordinates']) < 2) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Coordinates must be an array with at least 2 points']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate each coordinate
|
||||
foreach ($data['coordinates'] as $coord) {
|
||||
if (!is_array($coord) || count($coord) !== 2) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Each coordinate must be [latitude, longitude]']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$lat = floatval($coord[0]);
|
||||
$lng = floatval($coord[1]);
|
||||
|
||||
if (!is_numeric($coord[0]) || !is_numeric($coord[1])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Coordinates must be numeric values']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($lat < -90 || $lat > 90 || $lng < -180 || $lng > 180) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid coordinate range. Latitude: -90 to 90, Longitude: -180 to 180']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// Validate name is not empty
|
||||
if (empty(trim($data['name']))) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Road name cannot be empty']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate length_meters is positive
|
||||
if (!is_numeric($data['length_meters']) || $data['length_meters'] <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Length must be a positive number']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate condition_status if provided
|
||||
$condition_status = isset($data['condition_status']) ? $data['condition_status'] : 'normal';
|
||||
$validConditions = ['normal', 'minor_damage', 'major_damage'];
|
||||
if (!in_array($condition_status, $validConditions)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid condition_status. Must be: normal, minor_damage, or major_damage']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
|
||||
// Store coordinates as JSON
|
||||
$coordinatesJson = json_encode($data['coordinates']);
|
||||
|
||||
$stmt = $db->prepare('INSERT INTO roads (name, road_type, coordinates, length_meters, condition_status) VALUES (?, ?, ?, ?, ?)');
|
||||
$stmt->execute([
|
||||
$data['name'],
|
||||
$data['road_type'],
|
||||
$coordinatesJson,
|
||||
floatval($data['length_meters']),
|
||||
$condition_status
|
||||
]);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => $db->lastInsertId(),
|
||||
'name' => $data['name'],
|
||||
'road_type' => $data['road_type'],
|
||||
'coordinates' => $data['coordinates'],
|
||||
'length_meters' => floatval($data['length_meters']),
|
||||
'condition_status' => $condition_status,
|
||||
'created_at' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
header('Content-Type: application/json');
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
// Validate required fields
|
||||
if (!isset($data['name']) || !isset($data['certificate_type']) || !isset($data['coordinates']) || !isset($data['area_sqm'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing required fields: name, certificate_type, coordinates, or area_sqm']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate certificate_type
|
||||
$validTypes = ['SHM', 'HGB', 'HGU', 'HP'];
|
||||
if (!in_array($data['certificate_type'], $validTypes)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid certificate_type. Must be: SHM, HGB, HGU, or HP']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate coordinates array (minimum 3 for polygon)
|
||||
if (!is_array($data['coordinates']) || count($data['coordinates']) < 3) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Coordinates must be an array with at least 3 points for a polygon']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate each coordinate
|
||||
foreach ($data['coordinates'] as $coord) {
|
||||
if (!is_array($coord) || count($coord) !== 2) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Each coordinate must be [latitude, longitude]']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$lat = floatval($coord[0]);
|
||||
$lng = floatval($coord[1]);
|
||||
|
||||
if (!is_numeric($coord[0]) || !is_numeric($coord[1])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Coordinates must be numeric values']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($lat < -90 || $lat > 90 || $lng < -180 || $lng > 180) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid coordinate range. Latitude: -90 to 90, Longitude: -180 to 180']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// Validate name is not empty
|
||||
if (empty(trim($data['name']))) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Parcel name cannot be empty']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate area_sqm is positive
|
||||
if (!is_numeric($data['area_sqm']) || $data['area_sqm'] <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Area must be a positive number']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
|
||||
// Store coordinates as JSON
|
||||
$coordinatesJson = json_encode($data['coordinates']);
|
||||
|
||||
$stmt = $db->prepare('INSERT INTO land_parcels (name, certificate_type, coordinates, area_sqm) VALUES (?, ?, ?, ?)');
|
||||
$stmt->execute([
|
||||
$data['name'],
|
||||
$data['certificate_type'],
|
||||
$coordinatesJson,
|
||||
floatval($data['area_sqm'])
|
||||
]);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => $db->lastInsertId(),
|
||||
'name' => $data['name'],
|
||||
'certificate_type' => $data['certificate_type'],
|
||||
'coordinates' => $data['coordinates'],
|
||||
'area_sqm' => floatval($data['area_sqm']),
|
||||
'created_at' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
require_once 'config.php';
|
||||
|
||||
try {
|
||||
$db = getDB();
|
||||
|
||||
// Get filter parameters
|
||||
$search = isset($_GET['search']) ? '%' . $_GET['search'] . '%' : '';
|
||||
$from_date = isset($_GET['from_date']) ? $_GET['from_date'] : '';
|
||||
$to_date = isset($_GET['to_date']) ? $_GET['to_date'] : '';
|
||||
$certificate_type = isset($_GET['certificate_type']) ? $_GET['certificate_type'] : '';
|
||||
|
||||
// Build dynamic WHERE clause
|
||||
$where_clauses = [];
|
||||
$params = [];
|
||||
|
||||
if ($search !== '%' && $search !== '%') {
|
||||
$where_clauses[] = '(name LIKE ?)';
|
||||
$params[] = $search;
|
||||
}
|
||||
|
||||
if ($from_date) {
|
||||
$where_clauses[] = '(DATE(created_at) >= ?)';
|
||||
$params[] = $from_date;
|
||||
}
|
||||
|
||||
if ($to_date) {
|
||||
$where_clauses[] = '(DATE(created_at) <= ?)';
|
||||
$params[] = $to_date;
|
||||
}
|
||||
|
||||
if ($certificate_type) {
|
||||
$where_clauses[] = '(certificate_type = ?)';
|
||||
$params[] = $certificate_type;
|
||||
}
|
||||
|
||||
// Build SQL query
|
||||
$sql = 'SELECT id, name, certificate_type, coordinates, area_sqm, created_at, updated_at FROM land_parcels';
|
||||
|
||||
if (count($where_clauses) > 0) {
|
||||
$sql .= ' WHERE ' . implode(' AND ', $where_clauses);
|
||||
}
|
||||
|
||||
$sql .= ' ORDER BY created_at DESC';
|
||||
|
||||
// Execute query
|
||||
$stmt = $db->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$parcels = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// Decode coordinates JSON for each parcel
|
||||
foreach ($parcels as &$parcel) {
|
||||
$parcel['coordinates'] = json_decode($parcel['coordinates'], true);
|
||||
}
|
||||
|
||||
// Count total for query
|
||||
$count_sql = 'SELECT COUNT(*) as total FROM land_parcels' . (count($where_clauses) > 0 ? ' WHERE ' . implode(' AND ', $where_clauses) : '');
|
||||
$count_stmt = $db->prepare($count_sql);
|
||||
$count_stmt->execute($params);
|
||||
$total = $count_stmt->fetch(PDO::FETCH_ASSOC)['total'];
|
||||
|
||||
// Prepare filters applied info
|
||||
$filters_applied = [];
|
||||
if ($search !== '%' && $search !== '%') {
|
||||
$filters_applied['search'] = str_replace('%', '', $search);
|
||||
}
|
||||
if ($from_date || $to_date) {
|
||||
$filters_applied['date_range'] = [];
|
||||
if ($from_date) $filters_applied['date_range'][] = $from_date;
|
||||
if ($to_date) $filters_applied['date_range'][] = $to_date;
|
||||
}
|
||||
if ($certificate_type) {
|
||||
$filters_applied['certificate_type'] = $certificate_type;
|
||||
}
|
||||
|
||||
// Return success response
|
||||
http_response_code(200);
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'polygons' => $parcels,
|
||||
'total' => $total,
|
||||
'filters_applied' => $filters_applied
|
||||
]);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => 'Database error: ' . $e->getMessage()
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(400);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
require_once 'config.php';
|
||||
|
||||
try {
|
||||
$db = getDB();
|
||||
|
||||
// Get filter parameters
|
||||
$search = isset($_GET['search']) ? '%' . $_GET['search'] . '%' : '';
|
||||
$from_date = isset($_GET['from_date']) ? $_GET['from_date'] : '';
|
||||
$to_date = isset($_GET['to_date']) ? $_GET['to_date'] : '';
|
||||
$condition_status = isset($_GET['condition_status']) ? $_GET['condition_status'] : '';
|
||||
|
||||
// Build dynamic WHERE clause
|
||||
$where_clauses = [];
|
||||
$params = [];
|
||||
|
||||
if ($search !== '%' && $search !== '%') {
|
||||
$where_clauses[] = '(name LIKE ?)';
|
||||
$params[] = $search;
|
||||
}
|
||||
|
||||
if ($from_date) {
|
||||
$where_clauses[] = '(DATE(created_at) >= ?)';
|
||||
$params[] = $from_date;
|
||||
}
|
||||
|
||||
if ($to_date) {
|
||||
$where_clauses[] = '(DATE(created_at) <= ?)';
|
||||
$params[] = $to_date;
|
||||
}
|
||||
|
||||
if ($condition_status) {
|
||||
$where_clauses[] = '(condition_status = ?)';
|
||||
$params[] = $condition_status;
|
||||
}
|
||||
|
||||
// Build SQL query
|
||||
$sql = 'SELECT id, name, road_type, coordinates, length_meters, condition_status, created_at, updated_at FROM roads';
|
||||
|
||||
if (count($where_clauses) > 0) {
|
||||
$sql .= ' WHERE ' . implode(' AND ', $where_clauses);
|
||||
}
|
||||
|
||||
$sql .= ' ORDER BY created_at DESC';
|
||||
|
||||
// Execute query
|
||||
$stmt = $db->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$roads = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// Decode coordinates JSON for each road
|
||||
foreach ($roads as &$road) {
|
||||
$road['coordinates'] = json_decode($road['coordinates'], true);
|
||||
}
|
||||
|
||||
// Count total for unfiltered query
|
||||
$count_sql = 'SELECT COUNT(*) as total FROM roads' . (count($where_clauses) > 0 ? ' WHERE ' . implode(' AND ', $where_clauses) : '');
|
||||
$count_stmt = $db->prepare($count_sql);
|
||||
$count_stmt->execute($params);
|
||||
$total = $count_stmt->fetch(PDO::FETCH_ASSOC)['total'];
|
||||
|
||||
// Prepare filters applied info
|
||||
$filters_applied = [];
|
||||
if ($search !== '%' && $search !== '%') {
|
||||
$filters_applied['search'] = str_replace('%', '', $search);
|
||||
}
|
||||
if ($from_date || $to_date) {
|
||||
$filters_applied['date_range'] = [];
|
||||
if ($from_date) $filters_applied['date_range'][] = $from_date;
|
||||
if ($to_date) $filters_applied['date_range'][] = $to_date;
|
||||
}
|
||||
if ($condition_status) {
|
||||
$filters_applied['condition_status'] = $condition_status;
|
||||
}
|
||||
|
||||
// Return success response
|
||||
http_response_code(200);
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'lines' => $roads,
|
||||
'total' => $total,
|
||||
'filters_applied' => $filters_applied
|
||||
]);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => 'Database error: ' . $e->getMessage()
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(400);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
header('Content-Type: application/json');
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
// Validate required fields
|
||||
if (!isset($data['id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing required field: id']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$id = intval($data['id']);
|
||||
$db = getDB();
|
||||
|
||||
// Determine what type of update this is
|
||||
$hasCoordinates = isset($data['coordinates']) && isset($data['length_meters']);
|
||||
$hasName = isset($data['name']);
|
||||
$hasRoadType = isset($data['road_type']);
|
||||
$hasConditionStatus = isset($data['condition_status']);
|
||||
|
||||
// Full edit (multiple fields including name or road_type)
|
||||
if ($hasName || $hasRoadType || $hasCoordinates || $hasConditionStatus) {
|
||||
$updates = [];
|
||||
$params = [];
|
||||
|
||||
if ($hasName) {
|
||||
if (empty($data['name'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Name cannot be empty']);
|
||||
exit;
|
||||
}
|
||||
$updates[] = 'name = ?';
|
||||
$params[] = $data['name'];
|
||||
}
|
||||
|
||||
if ($hasRoadType) {
|
||||
$validTypes = ['Nasional', 'Provinsi', 'Kabupaten'];
|
||||
if (!in_array($data['road_type'], $validTypes)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid road_type. Must be: Nasional, Provinsi, or Kabupaten']);
|
||||
exit;
|
||||
}
|
||||
$updates[] = 'road_type = ?';
|
||||
$params[] = $data['road_type'];
|
||||
}
|
||||
|
||||
if ($hasConditionStatus) {
|
||||
$validConditions = ['normal', 'minor_damage', 'major_damage'];
|
||||
if (!in_array($data['condition_status'], $validConditions)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid condition_status. Must be: normal, minor_damage, or major_damage']);
|
||||
exit;
|
||||
}
|
||||
$updates[] = 'condition_status = ?';
|
||||
$params[] = $data['condition_status'];
|
||||
}
|
||||
|
||||
if ($hasCoordinates) {
|
||||
// Validate coordinates array
|
||||
if (!is_array($data['coordinates']) || count($data['coordinates']) < 2) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Coordinates must be an array with at least 2 points']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate each coordinate
|
||||
foreach ($data['coordinates'] as $coord) {
|
||||
if (!is_array($coord) || count($coord) !== 2 || !is_numeric($coord[0]) || !is_numeric($coord[1])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid coordinate format']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$lat = floatval($coord[0]);
|
||||
$lng = floatval($coord[1]);
|
||||
|
||||
if ($lat < -90 || $lat > 90 || $lng < -180 || $lng > 180) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid coordinate range']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// Validate length_meters is positive
|
||||
if (!is_numeric($data['length_meters']) || $data['length_meters'] <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Length must be a positive number']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$updates[] = 'coordinates = ?';
|
||||
$updates[] = 'length_meters = ?';
|
||||
$params[] = json_encode($data['coordinates']);
|
||||
$params[] = floatval($data['length_meters']);
|
||||
}
|
||||
|
||||
if (empty($updates)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'No fields to update']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Add updated_at and id to params
|
||||
$updates[] = 'updated_at = CURRENT_TIMESTAMP';
|
||||
$params[] = $id;
|
||||
|
||||
$sql = 'UPDATE roads SET ' . implode(', ', $updates) . ' WHERE id = ?';
|
||||
$stmt = $db->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
|
||||
// Fetch updated record
|
||||
$getStmt = $db->prepare('SELECT * FROM roads WHERE id = ?');
|
||||
$getStmt->execute([$id]);
|
||||
$road = $getStmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($road) {
|
||||
$road['coordinates'] = json_decode($road['coordinates'], true);
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'line' => $road
|
||||
]);
|
||||
} else {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'No fields to update']);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 0);
|
||||
header('Content-Type: application/json');
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
// Validate required fields
|
||||
if (!isset($data['id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing required field: id']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$id = intval($data['id']);
|
||||
$db = getDB();
|
||||
|
||||
// Determine what type of update this is
|
||||
$hasCoordinates = isset($data['coordinates']) && isset($data['area_sqm']);
|
||||
$hasName = isset($data['name']);
|
||||
$hasCertificateType = isset($data['certificate_type']);
|
||||
|
||||
// Full edit (multiple fields including name or certificate_type)
|
||||
if ($hasName || $hasCertificateType || $hasCoordinates) {
|
||||
$updates = [];
|
||||
$params = [];
|
||||
|
||||
if ($hasName) {
|
||||
if (empty($data['name'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Name cannot be empty']);
|
||||
exit;
|
||||
}
|
||||
$updates[] = 'name = ?';
|
||||
$params[] = $data['name'];
|
||||
}
|
||||
|
||||
if ($hasCertificateType) {
|
||||
$validTypes = ['SHM', 'HGB', 'HGU', 'HP'];
|
||||
if (!in_array($data['certificate_type'], $validTypes)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid certificate_type. Must be: SHM, HGB, HGU, or HP']);
|
||||
exit;
|
||||
}
|
||||
$updates[] = 'certificate_type = ?';
|
||||
$params[] = $data['certificate_type'];
|
||||
}
|
||||
|
||||
if ($hasCoordinates) {
|
||||
// Validate coordinates array (minimum 3 for polygon)
|
||||
if (!is_array($data['coordinates']) || count($data['coordinates']) < 3) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Coordinates must be an array with at least 3 points']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate each coordinate
|
||||
foreach ($data['coordinates'] as $coord) {
|
||||
if (!is_array($coord) || count($coord) !== 2 || !is_numeric($coord[0]) || !is_numeric($coord[1])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid coordinate format']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$lat = floatval($coord[0]);
|
||||
$lng = floatval($coord[1]);
|
||||
|
||||
if ($lat < -90 || $lat > 90 || $lng < -180 || $lng > 180) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid coordinate range']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// Validate area_sqm is positive
|
||||
if (!is_numeric($data['area_sqm']) || $data['area_sqm'] <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Area must be a positive number']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$updates[] = 'coordinates = ?';
|
||||
$updates[] = 'area_sqm = ?';
|
||||
$params[] = json_encode($data['coordinates']);
|
||||
$params[] = floatval($data['area_sqm']);
|
||||
}
|
||||
|
||||
if (empty($updates)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'No fields to update']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Add updated_at and id to params
|
||||
$updates[] = 'updated_at = CURRENT_TIMESTAMP';
|
||||
$params[] = $id;
|
||||
|
||||
$sql = 'UPDATE land_parcels SET ' . implode(', ', $updates) . ' WHERE id = ?';
|
||||
$stmt = $db->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
|
||||
// Fetch updated record
|
||||
$getStmt = $db->prepare('SELECT * FROM land_parcels WHERE id = ?');
|
||||
$getStmt->execute([$id]);
|
||||
$parcel = $getStmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($parcel) {
|
||||
$parcel['coordinates'] = json_decode($parcel['coordinates'], true);
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'polygon' => $parcel
|
||||
]);
|
||||
} else {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'No fields to update']);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user