Add ability to override generic survey data.

This commit is contained in:
Kim Taylor
2024-10-01 23:27:48 +10:00
parent bac9be7003
commit 6eef04e89f

View File

@@ -64,6 +64,7 @@ foreach ($lga_list as $lga) {
$dir = dirname($lga['config-file']); $dir = dirname($lga['config-file']);
$dir_files = scandir($dir); $dir_files = scandir($dir);
$output_file = $dir."/candidates-generic.csv"; $output_file = $dir."/candidates-generic.csv";
$override_file = $dir."/candidates-override.csv";
if (($handle = fopen($output_file, "w")) === FALSE) { if (($handle = fopen($output_file, "w")) === FALSE) {
error_log('Error opening output file'); error_log('Error opening output file');
@@ -75,6 +76,7 @@ foreach ($lga_list as $lga) {
exit(3); exit(3);
} }
$lines = [];
foreach ($lga_candidates as $candidate) { foreach ($lga_candidates as $candidate) {
/* Add extension to photo hash */ /* Add extension to photo hash */
if (strlen($candidate['Photo'])) { if (strlen($candidate['Photo'])) {
@@ -86,19 +88,52 @@ foreach ($lga_list as $lga) {
} }
} }
$fields = [ $lines[] = [
$candidate['match_ward'], $candidate['match_ward'],
$candidate['Name'], $candidate['Name'],
$candidate['Score'], $candidate['Score'],
$candidate['Pledge'], $candidate['Pledge'],
$candidate['Photo'], $candidate['Photo'],
]; ];
}
if (fputcsv($handle, $fields) === FALSE) { /* Apply overrides if they exist */
$overrides = [];
if (file_exists($override_file)) {
if (($ovr_handle = fopen($override_file, "r")) !== FALSE) {
$headers = fgetcsv($ovr_handle);
while (($data = fgetcsv($ovr_handle)) !== FALSE) {
$override = [];
foreach ($headers as $key => $value) {
$override[$value] = $data[$key];
}
$overrides[] = $override;
}
fclose($ovr_handle);
} else {
error_log('Error opening overrides file');
exit(3);
}
}
foreach ($overrides as $override) {
foreach ($lines as $line_key => $line) {
foreach ($header as $index => $field) {
if (($override['Field'] === $field) &&
($line[$index] === $override['Old'])) {
$lines[$line_key][$index] = $override['New'];
}
}
}
}
foreach ($lines as $line) {
if (fputcsv($handle, $line) === FALSE) {
error_log('Error writing candidate to output file'); error_log('Error writing candidate to output file');
exit(3); exit(3);
} }
} }
fclose($handle);
} }
exit(0); exit(0);