From 4ed59223cf6946cff7b4f49da50e1070e9095e99 Mon Sep 17 00:00:00 2001 From: Matt Way Date: Tue, 30 Jul 2024 23:50:09 +1000 Subject: [PATCH] Add initial support for reading candidate data from a CSV file --- php-template/.vscode/launch.json | 2 +- php-template/example-candidates.csv | 3 +++ php-template/example-config.json | 17 +++++++++++++++++ php-template/example-input | 15 --------------- php-template/main.php | 23 ++++++++++++++++++++++- php-template/page_renderer.php | 2 +- php-template/template.php | 15 +++++++++++---- 7 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 php-template/example-candidates.csv create mode 100644 php-template/example-config.json delete mode 100644 php-template/example-input diff --git a/php-template/.vscode/launch.json b/php-template/.vscode/launch.json index 6362d68..c5d0472 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-input"], + "args": ["${workspaceFolder}/example-config.json", "${workspaceFolder}/example-candidates.csv"], "cwd": "${workspaceFolder}", "port": 9000 } diff --git a/php-template/example-candidates.csv b/php-template/example-candidates.csv new file mode 100644 index 0000000..4fddad0 --- /dev/null +++ b/php-template/example-candidates.csv @@ -0,0 +1,3 @@ +Council,Ward,Candidate Name,Rating +Brimbank,Harvester,Joe Blogs,1 +Brimbank,Harvester,Jane Doe,5 \ No newline at end of file diff --git a/php-template/example-config.json b/php-template/example-config.json new file mode 100644 index 0000000..b925a9a --- /dev/null +++ b/php-template/example-config.json @@ -0,0 +1,17 @@ +{ + "shortName": "Brimbank", + "councilName": "Brimbank City Council", + "wardNames": [ + "Harvester", + "St Albans East", + "Delahey", + "Organ Pipes", + "Grasslands", + "Copernicus", + "Mount Derrimut", + "Horseshoe Bend", + "Albanvale", + "Kororoit Creek", + "Cherry Creek" + ] + } \ No newline at end of file diff --git a/php-template/example-input b/php-template/example-input deleted file mode 100644 index 4ca0136..0000000 --- a/php-template/example-input +++ /dev/null @@ -1,15 +0,0 @@ -{ - "shortName": "Ballarat", - "councilName": "Ballarat City Council", - "wardNames": [ - "Alfredton", - "Delacombe", - "Sebastopol", - "Central", - "Wendouree", - "Golden Point", - "North", - "Brown Hill", - "Buninyong" - ] -} \ No newline at end of file diff --git a/php-template/main.php b/php-template/main.php index 4d4e138..1a170bf 100644 --- a/php-template/main.php +++ b/php-template/main.php @@ -18,8 +18,29 @@ if (json_last_error() !== JSON_ERROR_NONE) { exit(1); } +// Convert CSV into an array of dictionaries. Use the header as the key in the dictionary. +$candidates = []; +if (isset($argv[2])) { + if (($handle = fopen($argv[2], "r")) !== FALSE) { + $headers = fgetcsv($handle); + while (($data = fgetcsv($handle)) !== FALSE) { + $candidate = []; + foreach ($headers as $key => $value) { + $candidate[$value] = $data[$key]; + } + $candidates[] = $candidate; + } + fclose($handle); + } else { + error_log('Error opening CSV'); + exit(1); + } +} + +// TODO: Check that candidates were read successfully?? + $renderer = new SPLPageRenderer(); -$pageContent = $renderer->renderCouncilPage($config); +$pageContent = $renderer->renderCouncilPage($config, $candidates); if ($pageContent === null) { exit(2); } diff --git a/php-template/page_renderer.php b/php-template/page_renderer.php index 402fafa..1c62642 100644 --- a/php-template/page_renderer.php +++ b/php-template/page_renderer.php @@ -1,7 +1,7 @@

- + +
- + $candidate): ?>
-

Candidate

+

@@ -25,7 +32,7 @@
- +