Add initial support for reading candidate data from a CSV file
This commit is contained in:
2
php-template/.vscode/launch.json
vendored
2
php-template/.vscode/launch.json
vendored
@@ -6,7 +6,7 @@
|
|||||||
"type": "php",
|
"type": "php",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/main.php",
|
"program": "${workspaceFolder}/main.php",
|
||||||
"args": ["${workspaceFolder}/example-input"],
|
"args": ["${workspaceFolder}/example-config.json", "${workspaceFolder}/example-candidates.csv"],
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
"port": 9000
|
"port": 9000
|
||||||
}
|
}
|
||||||
|
|||||||
3
php-template/example-candidates.csv
Normal file
3
php-template/example-candidates.csv
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Council,Ward,Candidate Name,Rating
|
||||||
|
Brimbank,Harvester,Joe Blogs,1
|
||||||
|
Brimbank,Harvester,Jane Doe,5
|
||||||
|
17
php-template/example-config.json
Normal file
17
php-template/example-config.json
Normal 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"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"shortName": "Ballarat",
|
|
||||||
"councilName": "Ballarat City Council",
|
|
||||||
"wardNames": [
|
|
||||||
"Alfredton",
|
|
||||||
"Delacombe",
|
|
||||||
"Sebastopol",
|
|
||||||
"Central",
|
|
||||||
"Wendouree",
|
|
||||||
"Golden Point",
|
|
||||||
"North",
|
|
||||||
"Brown Hill",
|
|
||||||
"Buninyong"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -18,8 +18,29 @@ if (json_last_error() !== JSON_ERROR_NONE) {
|
|||||||
exit(1);
|
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();
|
$renderer = new SPLPageRenderer();
|
||||||
$pageContent = $renderer->renderCouncilPage($config);
|
$pageContent = $renderer->renderCouncilPage($config, $candidates);
|
||||||
if ($pageContent === null) {
|
if ($pageContent === null) {
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class SPLPageRenderer {
|
class SPLPageRenderer {
|
||||||
public function renderCouncilPage($config) {
|
public function renderCouncilPage($config, $candidates) {
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
set_error_handler(function($errno, $errstr, $errfile, $errline) {
|
set_error_handler(function($errno, $errstr, $errfile, $errline) {
|
||||||
|
|||||||
@@ -6,14 +6,21 @@
|
|||||||
<!-- wp:heading {"className":"is-style-default"} -->
|
<!-- 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>
|
<h2 class="wp-block-heading is-style-default" id="<?php echo strtolower(str_replace(' ', '-', $wardName)); ?>"><?php echo $wardName; ?></h2>
|
||||||
<!-- /wp:heading -->
|
<!-- /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"}} -->
|
<!-- wp:group {"layout":{"type":"grid"}} -->
|
||||||
<div class="wp-block-group">
|
<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"}} -->
|
<!-- wp:group {"layout":{"type":"flex","orientation":"vertical"}} -->
|
||||||
<div class="wp-block-group">
|
<div class="wp-block-group">
|
||||||
<!-- wp:heading {"fontSize":"medium"} -->
|
<!-- 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:heading -->
|
||||||
|
|
||||||
<!-- wp:image {"aspectRatio":"1","scale":"cover","style":{"color":{}}} -->
|
<!-- wp:image {"aspectRatio":"1","scale":"cover","style":{"color":{}}} -->
|
||||||
@@ -25,7 +32,7 @@
|
|||||||
<!-- /wp:paragraph -->
|
<!-- /wp:paragraph -->
|
||||||
</div>
|
</div>
|
||||||
<!-- /wp:group -->
|
<!-- /wp:group -->
|
||||||
<?php endfor; ?>
|
<?php endforeach; ?>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<!-- /wp:group -->
|
<!-- /wp:group -->
|
||||||
|
|||||||
Reference in New Issue
Block a user