196 lines
7.0 KiB
PHP
196 lines
7.0 KiB
PHP
<?php
|
|
// Database configuration
|
|
define('DB_HOST', 'test');
|
|
define('DB_NAME', 'test');
|
|
define('DB_USER', 'test');
|
|
define('DB_PASS', 'test');
|
|
|
|
// Authentication
|
|
$auth_code = 'test'; // Set this for basic security
|
|
|
|
// display errors
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
ini_set('error_reporting', E_ALL);
|
|
|
|
// Process form submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['auth']) && $_POST['auth'] === $auth_code) {
|
|
try {
|
|
// Validate file upload
|
|
if (!isset($_FILES['json_file']) || $_FILES['json_file']['error'] !== UPLOAD_ERR_OK) {
|
|
throw new Exception('File upload error');
|
|
}
|
|
|
|
// Verify JSON file
|
|
$file_info = pathinfo($_FILES['json_file']['name']);
|
|
if (strtolower($file_info['extension']) !== 'json') {
|
|
throw new Exception('Invalid file type');
|
|
}
|
|
|
|
// Read and decode JSON
|
|
$json_data = file_get_contents($_FILES['json_file']['tmp_name']);
|
|
$entries = json_decode($json_data, true);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
throw new Exception('Invalid JSON format');
|
|
}
|
|
|
|
// Connect to database
|
|
$pdo = new PDO(
|
|
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=utf8mb4",
|
|
DB_USER,
|
|
DB_PASS,
|
|
[
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
|
]
|
|
);
|
|
|
|
$results = [
|
|
'consents_created' => 0,
|
|
'owners_created' => 0,
|
|
'errors' => []
|
|
];
|
|
|
|
// Process each entry
|
|
foreach ($entries as $index => $entry) {
|
|
try {
|
|
// Normalize data
|
|
$kg = (int)$entry['kg'];
|
|
$gst = (string)$entry['gst'];
|
|
$ez = (string)$entry['ez'];
|
|
$street = substr($entry['street'], 0, 255);
|
|
|
|
// Find or create ConstructionConsent
|
|
$stmt = $pdo->prepare("
|
|
SELECT id FROM ConstructionConsent
|
|
WHERE kg = ? AND gst = ? AND ez = ?
|
|
");
|
|
$stmt->execute([$kg, $gst, $ez]);
|
|
$consent = $stmt->fetch();
|
|
|
|
if (!$consent) {
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO ConstructionConsent (
|
|
constructionconsentproject_id, adb_strasse_id, object_type, name, ez, kg, gst,
|
|
create_by, edit_by, `create`, `edit`
|
|
) VALUES (
|
|
1, ?, ?, ?, ?, ?, ?,
|
|
145, 145, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
|
|
)
|
|
");
|
|
$stmt->execute([137947, "street", "GST: " . $gst, $ez, $kg, $gst]);
|
|
$consent_id = $pdo->lastInsertId();
|
|
$results['consents_created']++;
|
|
} else {
|
|
$consent_id = $consent['id'];
|
|
}
|
|
|
|
// Prepare owner data
|
|
$firstname = !isset($entry['firstname']) ? null : substr($entry['firstname'], 0, 255);
|
|
$lastname = substr($entry['lastname'] ?? '', 0, 255);
|
|
$street = substr($entry['street'], 0, 64);
|
|
$zip = substr($entry['plz'], 0, 32);
|
|
$city = substr($entry['ort'], 0, 64);
|
|
|
|
// Determine owner status and result
|
|
$status = match(strtolower($entry['status'])) {
|
|
'sent' => 'sent',
|
|
'disallowed' => 'returned',
|
|
'signed' => 'returned',
|
|
default => 'new'
|
|
};
|
|
|
|
$result = match(strtolower($entry['status'])) {
|
|
'disallowed' => 'denied',
|
|
'signed' => 'accepted',
|
|
default => 'open'
|
|
};
|
|
|
|
// Check existing owner
|
|
$stmt = $pdo->prepare("
|
|
SELECT id FROM ConstructionConsentOwner
|
|
WHERE constructionconsent_id = ? AND (
|
|
(firstname = ? AND lastname = ? AND street = ?) OR
|
|
(firstname IS NULL AND lastname = ? AND street = ?))
|
|
");
|
|
$stmt->execute([$consent_id, $firstname, $lastname, $street, $lastname, $street]);
|
|
|
|
if (!$stmt->fetch()) {
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO ConstructionConsentOwner (
|
|
constructionconsent_id, firstname, lastname, street,
|
|
zip, city, status, result,
|
|
create_by, edit_by, `create`, `edit`
|
|
) VALUES (
|
|
?, ?, ?, ?,
|
|
?, ?, ?, ?,
|
|
145, 145, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
|
|
)
|
|
");
|
|
$stmt->execute([
|
|
$consent_id, $firstname, $lastname, $street,
|
|
$zip, $city, $status, $result
|
|
]);
|
|
$results['owners_created']++;
|
|
}
|
|
} catch (Exception $e) {
|
|
$results['errors'][] = [
|
|
'index' => $index,
|
|
'message' => $e->getMessage(),
|
|
'entry' => $entry
|
|
];
|
|
}
|
|
}
|
|
|
|
// Show results
|
|
echo "<h3>Processing Results:</h3>";
|
|
echo "<p>New consents created: " . $results['consents_created'] . "</p>";
|
|
echo "<p>New owners created: " . $results['owners_created'] . "</p>";
|
|
if (!empty($results['errors'])) {
|
|
echo "<h4>Errors:</h4>";
|
|
foreach ($results['errors'] as $error) {
|
|
echo "<p style='color: red'>Error at index " . $error['index'] . ": " . $error['message'] . "</p>";
|
|
echo "<pre>" . print_r($error['entry'], true) . "</pre>";
|
|
}
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
echo "<p style='color: red'>Error: " . $e->getMessage() . "</p>";
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Construction Consent Importer</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; margin: 2rem; }
|
|
.container { max-width: 800px; margin: 0 auto; }
|
|
form { border: 1px solid #ccc; padding: 2rem; border-radius: 5px; }
|
|
.form-group { margin-bottom: 1rem; }
|
|
label { display: block; margin-bottom: 0.5rem; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1>Upload Construction Consent JSON</h1>
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<div class="form-group">
|
|
<label>Security Code:
|
|
<input type="password" name="auth" required>
|
|
</label>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>JSON File:
|
|
<input type="file" name="json_file" accept=".json" required>
|
|
</label>
|
|
</div>
|
|
<button type="submit">Process File</button>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
|
|
<?php exit ?>
|