141 lines
4.0 KiB
PHP
141 lines
4.0 KiB
PHP
<?php
|
|
|
|
require_once("parse_generic_csv.php");
|
|
|
|
$options = getopt("", ["generic-csv:", "config-files:"]);
|
|
|
|
if (isset($options['generic-csv'])) {
|
|
$generic_csv = $options['generic-csv'];
|
|
} else {
|
|
error_log("Error: Missing required option '--generic-csv'.");
|
|
exit(1);
|
|
}
|
|
|
|
if (isset($options['config-files'])) {
|
|
$config_files = $options['config-files'];
|
|
} else {
|
|
error_log("Error: Missing required option '--config-files'.");
|
|
exit(1);
|
|
}
|
|
|
|
$config_files = explode(" ", $config_files);
|
|
$candidate_data = parse_generic_csv($generic_csv);
|
|
|
|
$lga_list = [];
|
|
/* Generate dictionary of LGAs and Wards */
|
|
foreach ($config_files as $config_file) {
|
|
$config_string = file_get_contents($config_file);
|
|
if ($config_string !== FALSE) {
|
|
$config = json_decode($config_string, true);
|
|
} else {
|
|
error_log("Error opening config.json.");
|
|
exit(1);
|
|
}
|
|
$config['config-file'] = $config_file;
|
|
$lga_list[] = $config;
|
|
}
|
|
|
|
/* Match user typed LGA/Ward to our database */
|
|
match_lga($candidate_data, $lga_list);
|
|
|
|
/* Calculate score for candidate */
|
|
foreach ($candidate_data as $key => $candidate) {
|
|
$score = 0;
|
|
|
|
if ($candidate['Pledge'] === "y") $score++;
|
|
if ($candidate['q1'] === "Yes") $score++;
|
|
if ($candidate['q3'] === "Yes") $score++;
|
|
if ($candidate['q4'] === "Yes") $score++;
|
|
if ($candidate['q7'] === "Yes") $score++;
|
|
|
|
$candidate_data[$key]['Score'] = $score;
|
|
}
|
|
|
|
$header = ["Ward", "Candidate Name", "Rating", "Pledge", "Picture"];
|
|
|
|
/* Generate candidates-generic.csv */
|
|
foreach ($lga_list as $lga) {
|
|
$lga_candidates = array_filter($candidate_data, function ($candidate) use ($lga) {
|
|
return $candidate['match_lga'] === $lga['slug'];
|
|
});
|
|
|
|
if (count($lga_candidates) === 0) continue;
|
|
|
|
remove_duplicates($lga_candidates);
|
|
|
|
$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');
|
|
exit(1);
|
|
}
|
|
|
|
if (fputcsv($handle, $header) === FALSE) {
|
|
error_log('Error writing headers to output file');
|
|
exit(3);
|
|
}
|
|
|
|
$lines = [];
|
|
foreach ($lga_candidates as $candidate) {
|
|
/* Add extension to photo hash */
|
|
if (strlen($candidate['Photo'])) {
|
|
foreach ($dir_files as $file) {
|
|
if (preg_match("/\.json$/", $file)) continue;
|
|
if (strstr($file, $candidate['Photo'])) {
|
|
$candidate['Photo'] = $file;
|
|
}
|
|
}
|
|
}
|
|
|
|
$lines[] = [
|
|
$candidate['match_ward'],
|
|
$candidate['Name'],
|
|
$candidate['Score'],
|
|
$candidate['Pledge'],
|
|
$candidate['Photo'],
|
|
];
|
|
}
|
|
|
|
/* 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) {
|
|
$match_index = array_search($override['Match Field'], $header);
|
|
$replace_index = array_search($override['Replace Field'], $header);
|
|
if ($line[$match_index] === $override['Match Value']) {
|
|
$lines[$line_key][$replace_index] = $override['Replace Value'];
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($lines as $line) {
|
|
if (fputcsv($handle, $line) === FALSE) {
|
|
error_log('Error writing candidate to output file');
|
|
exit(3);
|
|
}
|
|
}
|
|
fclose($handle);
|
|
}
|
|
|
|
exit(0);
|