Add initial support for reading candidate data from a CSV file

This commit is contained in:
2024-07-30 23:50:09 +10:00
parent 3f5dafa35c
commit 4ed59223cf
7 changed files with 55 additions and 22 deletions

View File

@@ -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
}

View File

@@ -0,0 +1,3 @@
Council,Ward,Candidate Name,Rating
Brimbank,Harvester,Joe Blogs,1
Brimbank,Harvester,Jane Doe,5
1 Council Ward Candidate Name Rating
2 Brimbank Harvester Joe Blogs 1
3 Brimbank Harvester Jane Doe 5

View File

@@ -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"
]
}

View File

@@ -1,15 +0,0 @@
{
"shortName": "Ballarat",
"councilName": "Ballarat City Council",
"wardNames": [
"Alfredton",
"Delacombe",
"Sebastopol",
"Central",
"Wendouree",
"Golden Point",
"North",
"Brown Hill",
"Buninyong"
]
}

View File

@@ -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);
}

View File

@@ -1,7 +1,7 @@
<?php
class SPLPageRenderer {
public function renderCouncilPage($config) {
public function renderCouncilPage($config, $candidates) {
ob_start();
set_error_handler(function($errno, $errstr, $errfile, $errline) {

View File

@@ -6,14 +6,21 @@
<!-- wp:heading {"className":"is-style-default"} -->
<h2 class="wp-block-heading is-style-default" id="<?php echo strtolower(str_replace(' ', '-', $wardName)); ?>"><?php echo $wardName; ?></h2>
<!-- /wp:heading -->
<?php
$wardCandidates = array_filter($candidates, function ($candidate) use ($wardName) {
return isset($candidate["Ward"]) && $candidate["Ward"] === $wardName;
});
if (count($wardCandidates) == 0) continue;
?>
<!-- wp:group {"layout":{"type":"grid"}} -->
<div class="wp-block-group">
<?php for ($i = 0; $i < 5; $i++): ?>
<?php foreach ($wardCandidates as $index => $candidate): ?>
<!-- wp:group {"layout":{"type":"flex","orientation":"vertical"}} -->
<div class="wp-block-group">
<!-- wp:heading {"fontSize":"medium"} -->
<h2 class="wp-block-heading has-medium-font-size">Candidate <?php echo $i + 1; ?></h2>
<h2 class="wp-block-heading has-medium-font-size"><?php echo $candidate['Candidate Name']; ?></h2>
<!-- /wp:heading -->
<!-- wp:image {"aspectRatio":"1","scale":"cover","style":{"color":{}}} -->
@@ -25,7 +32,7 @@
<!-- /wp:paragraph -->
</div>
<!-- /wp:group -->
<?php endfor; ?>
<?php endforeach; ?>
</div>
<!-- /wp:group -->