Files
thetool/scripts/constructionconsent/transform-json.php
2025-03-17 08:56:54 +00:00

117 lines
3.7 KiB
PHP

<!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>