diff --git a/csv-generic/gen-generic.php b/csv-generic/gen-generic.php index 097bc27..71fc214 100644 --- a/csv-generic/gen-generic.php +++ b/csv-generic/gen-generic.php @@ -64,6 +64,7 @@ foreach ($lga_list as $lga) { $dir = dirname($lga['config-file']); $dir_files = scandir($dir); $output_file = $dir."/candidates-generic.csv"; + $override_file = $dir."/candidates-override.csv"; if (($handle = fopen($output_file, "w")) === FALSE) { error_log('Error opening output file'); @@ -75,6 +76,7 @@ foreach ($lga_list as $lga) { exit(3); } + $lines = []; foreach ($lga_candidates as $candidate) { /* Add extension to photo hash */ if (strlen($candidate['Photo'])) { @@ -86,19 +88,52 @@ foreach ($lga_list as $lga) { } } - $fields = [ + $lines[] = [ $candidate['match_ward'], $candidate['Name'], $candidate['Score'], $candidate['Pledge'], $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'); exit(3); } } + fclose($handle); } exit(0);