Separate file for pledge data.

This commit is contained in:
Kim Taylor
2024-09-14 14:40:52 +10:00
parent b0985b6d60
commit 1ab14ba8c9
2 changed files with 33 additions and 14 deletions

View File

@@ -1,17 +1,17 @@
<?php <?php
$options = getopt("", ["candidates-files:"]); $options = getopt("", ["pledges-files:"]);
if (isset($options['candidates-files'])) { if (isset($options['pledges-files'])) {
$candidatesFiles = $options['candidates-files']; $pledges_files = $options['pledges-files'];
} else { } else {
error_log("Error: Missing required option '--candidates-files'."); error_log("Error: Missing required option '--pledges-files'.");
exit(1); exit(1);
} }
$files = explode(" ", $candidatesFiles); $files = explode(" ", $pledges_files);
$candidateData = []; $candidate_data = [];
foreach ($files as $key => $file) { foreach ($files as $key => $file) {
$config_file = dirname($file)."/config.json"; $config_file = dirname($file)."/config.json";
$config_string = file_get_contents($config_file); $config_string = file_get_contents($config_file);
@@ -23,7 +23,9 @@ foreach ($files as $key => $file) {
exit(1); exit(1);
} }
if (($handle = fopen($file, "r")) !== FALSE) { $candidate_file = dirname($file)."/candidates.csv";
if (($handle = fopen($candidate_file, "r")) !== FALSE) {
$headers = fgetcsv($handle); $headers = fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) { while (($data = fgetcsv($handle)) !== FALSE) {
$candidate = []; $candidate = [];
@@ -33,17 +35,33 @@ foreach ($files as $key => $file) {
} }
$candidate['Council'] = $config['councilName']; $candidate['Council'] = $config['councilName'];
$candidate['Path'] = dirname($file); $candidate['Path'] = dirname($file);
$candidateData[] = $candidate; $candidate_data[$candidate['Candidate Name']] = $candidate;
} }
fclose($handle); fclose($handle);
} else { } else {
error_log('Error opening candidates file'); error_log('Error opening candidates file');
exit(1); exit(1);
} }
if (($handle = fopen($file, "r")) !== FALSE) {
$headers = fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) {
$candidate = [];
foreach ($headers as $key => $value) {
$candidate[$value] = $data[$key];
}
$candidate_data[$candidate['Candidate Name']]['Pledge'] =
$candidate['Pledge'];
}
fclose($handle);
} else {
error_log('Error opening pledges file');
exit(1);
}
} }
/* Select people who have taken the pledge */ /* Select people who have taken the pledge */
$pledgeCandidates = array_filter($candidateData, function ($candidate) { $pledgeCandidates = array_filter($candidate_data, function ($candidate) {
return $candidate['Pledge'] === 'y'; return $candidate['Pledge'] === 'y';
}); });

View File

@@ -1,6 +1,7 @@
#!/bin/bash #!/bin/bash
# If this runs as a cron job - might want to limit the number of revisions wordpress stores: # If this runs as a cron job - might want to limit the number of revisions
# wordpress stores:
# wp-config.php: # wp-config.php:
# define( 'WP_POST_REVISIONS', 3 ); # define( 'WP_POST_REVISIONS', 3 );
@@ -12,14 +13,14 @@
DATA_PATH="../spl-data" DATA_PATH="../spl-data"
candidates_files=() pledges_files=()
for folder in "$DATA_PATH"/*; do for folder in "$DATA_PATH"/*; do
if test -f "$folder"/candidates.csv; then if test -f "$folder"/pledges.csv; then
candidates_files+=("$folder"/candidates.csv) pledges_files+=("$folder"/pledges.csv)
fi fi
done done
pledge_sed=$(php pledge-update/main.php --candidates-files "${candidates_files[*]}") pledge_sed=$(php pledge-update/main.php --pledges-files "${pledges_files[*]}")
content=$(sed "$pledge_sed" ../spl-data/movie-homepage) content=$(sed "$pledge_sed" ../spl-data/movie-homepage)