From 09ffd40bf78e574c4ae851c4230edbde6d87cebf Mon Sep 17 00:00:00 2001 From: Matt Way Date: Sun, 4 Aug 2024 22:08:11 +1000 Subject: [PATCH] Use named arguments to pass files to template --- make-council-pages.sh | 2 +- php-template/.vscode/launch.json | 2 +- php-template/main.php | 53 +++++++++++++++++--------------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/make-council-pages.sh b/make-council-pages.sh index f4ea1fe..743e915 100644 --- a/make-council-pages.sh +++ b/make-council-pages.sh @@ -21,7 +21,7 @@ function create_or_update_page() { slug=$(echo "$council_block" | jq -r '.slug') - content=$(echo "$council_block" | jq -c | php php-template/main.php "php://stdin" "candidates/$slug.csv") + content=$(echo "$council_block" | jq -c | php php-template/main.php --council-file "php://stdin" --candidates-file "candidates/$slug.csv") if [ $? -eq 0 ]; then diff --git a/php-template/.vscode/launch.json b/php-template/.vscode/launch.json index c5d0472..a16da9d 100644 --- a/php-template/.vscode/launch.json +++ b/php-template/.vscode/launch.json @@ -6,7 +6,7 @@ "type": "php", "request": "launch", "program": "${workspaceFolder}/main.php", - "args": ["${workspaceFolder}/example-config.json", "${workspaceFolder}/example-candidates.csv"], + "args": ["--council-file", "${workspaceFolder}/example-config.json", "--candidates-file", "${workspaceFolder}/example-candidates.csv"], "cwd": "${workspaceFolder}", "port": 9000 } diff --git a/php-template/main.php b/php-template/main.php index 1a170bf..4ec267c 100644 --- a/php-template/main.php +++ b/php-template/main.php @@ -1,46 +1,49 @@ $value) { - $candidate[$value] = $data[$key]; - } - $candidates[] = $candidate; +$candidateData = []; +if (($handle = fopen($candidatesFile, "r")) !== FALSE) { + $headers = fgetcsv($handle); + while (($data = fgetcsv($handle)) !== FALSE) { + $candidate = []; + foreach ($headers as $key => $value) { + $candidate[$value] = $data[$key]; } - fclose($handle); - } else { - error_log('Error opening CSV'); - exit(1); + $candidateData[] = $candidate; } + fclose($handle); +} else { + error_log('Error opening candidates file'); + exit(1); } -// TODO: Check that candidates were read successfully?? - $renderer = new SPLPageRenderer(); -$pageContent = $renderer->renderCouncilPage($config, $candidates); +$pageContent = $renderer->renderCouncilPage($councilData, $candidateData); if ($pageContent === null) { exit(2); }