User/rework

This commit is contained in:
Luca Haid
2025-03-17 08:56:54 +00:00
parent 720688e346
commit e28dddc2e2
14 changed files with 613 additions and 218 deletions

View File

@@ -0,0 +1,77 @@
<DOCTYPE html>
<html>
<head>
<title>Test XLSX</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
#output { width: 100%; height: 400px; margin-top: 10px; }
</style>
</head>
<body>
<input type="file" id="fileInput" accept=".xlsx">
<script src="/plugins/xlsx/xlsx.min.js"></script>
<script>
document.getElementById('fileInput').addEventListener('change', handleFile);
function handleFile(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (e) => {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, {type: 'array', cellStyles: true});
const jsonData = convertToJson(workbook);
downloadJson(jsonData);
};
reader.readAsArrayBuffer(file);
}
function convertToJson(workbook) {
const result = [];
const headers = ["FLR", "KG", "GST", "EZ", "Vorname", "Nachname", "Straße", "PLZ, Ort"];
// Process first worksheet
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
const range = XLSX.utils.decode_range(worksheet['!ref']);
for(let rowNum = 1; rowNum <= range.e.r; rowNum++) {
const rowData = { fields: {}, bg_colors: {} };
headers.forEach((header, colIdx) => {
const cellAddress = XLSX.utils.encode_cell({r: rowNum, c: colIdx});
const cell = worksheet[cellAddress];
// Get cell value
const value = cell ? cell.v : null;
rowData.fields[header] = value;
// Get background color
let hexColor = 'none';
console.log(cell);
if (cell?.s?.fgColor?.rgb) {
hexColor = `#${cell.s.fgColor.rgb}`; // Remove alpha channel
}
rowData.bg_colors[header] = hexColor;
});
result.push(rowData);
}
return result;
}
function downloadJson(data) {
const blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'output.json';
a.click();
}
</script>
</body>
</html>

View File

@@ -0,0 +1,195 @@
<?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 ?>

View File

@@ -0,0 +1,117 @@
<!DOCTYPE html>
<html>
<head>
<title>JSON Processor</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
#output { width: 100%; height: 400px; margin-top: 10px; }
</style>
</head>
<body>
<input type="file" id="fileInput" accept=".json">
<textarea id="output" readonly></textarea>
<script>
document.getElementById('fileInput').addEventListener('change', function(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
try {
const inputData = JSON.parse(e.target.result);
const transformed = transformData(inputData);
document.getElementById('output').value = JSON.stringify(transformed, null, 2);
} catch (error) {
alert('Error parsing JSON: ' + error.message);
}
};
reader.readAsText(file);
});
function hexToHSL(hex) {
// Remove # if present
let r = parseInt(hex.substring(1, 3), 16)/255;
let g = parseInt(hex.substring(3, 5), 16)/255;
let b = parseInt(hex.substring(5, 7), 16)/255;
let max = Math.max(r, g, b), min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if(max === min) {
h = s = 0; // achromatic
} else {
let d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h *= 60;
}
return { h: Math.round(h), s: Math.round(s*100), l: Math.round(l*100) };
}
function getColorType(hue) {
if((hue >= 0 && hue < 30) || hue >= 330) return 'red';
if(hue >= 180 && hue < 270) return 'blue';
if(hue >= 60 && hue < 180) return 'green';
if(hue >= 45 && hue < 60) return 'yellow';
return 'other';
}
function transformData(data) {
return data.map(item => {
const [plz, ...ortParts] = item.fields["PLZ, Ort"]?.split(' ') || ['', ''];
const ort = ortParts.join(' ');
const colorCounts = {
red: 0,
green: 0,
blue: 0,
yellow: 0,
none: 0
};
Object.values(item.bg_colors).forEach(color => {
if(color === 'none') {
colorCounts.none++;
return;
}
const { h } = hexToHSL(color);
const colorType = getColorType(h);
if(colorCounts[colorType] !== undefined) colorCounts[colorType]++;
});
// Determine status
const statusPriorities = ['red', 'green', 'yellow', 'blue'];
const counts = statusPriorities.map(c => colorCounts[c]);
const maxCount = Math.max(...counts);
let status = 'new';
if(maxCount > 0) {
if(colorCounts.red === maxCount) status = 'disallowed';
else if(colorCounts.green === maxCount) status = 'signed';
else if(colorCounts.yellow === maxCount) status = 'sent';
}
return {
kg: item.fields.KG,
gst: item.fields.GST,
ez: item.fields.EZ,
firstname: item.fields.Vorname,
lastname: item.fields.Nachname || '',
street: item.fields.Straße || '',
plz: plz,
ort: ort,
status: status
};
});
}
</script>
</body>
</html>