Merge branch 'main' into auto_generic

This commit is contained in:
Kim Taylor
2024-09-22 17:53:38 +10:00
9 changed files with 344 additions and 105 deletions

View File

@@ -77,8 +77,11 @@ if (($handle = fopen($inputFile, "r")) !== FALSE) {
if ($currentWard == "Coastal-promontory") {
$currentWard = "Coastal-Promontory";
}
if ($currentWard == "East Gippsland Shire") {
$currentWard = "Unsubdivided";
}
}
if ($data[0] == "Candidate" || $data[0] == "") {
if (trim($data[0]) == "Candidate" || trim($data[0]) == "") {
if ($currentWard == null) {
error_log("No ward found, skipping data on line " . $currentLine);
continue;

19
make-pledge-page.sh Executable file
View File

@@ -0,0 +1,19 @@
#!/bin/bash
# This script uses the jq, wp, and php commands, make sure they are installed before running this script.
# The folder containing data for each council.
# Includes the list of candidates and any media.
DATA_PATH="../spl-data"
# Iterate over folders in data path
candidates_files=()
for folder in "$DATA_PATH"/*; do
if test -f "$folder"/candidates.csv; then
candidates_files+=("$folder"/candidates.csv)
fi
done
content=$(php pledge-update/pledge-page.php --candidates-files "${candidates_files[*]}")
wp post update 12106 --post_content="$content"

View File

@@ -0,0 +1,42 @@
<?php
require_once("parse_pledge_data.php");
$options = getopt("", ["candidates-files:"]);
if (isset($options['candidates-files'])) {
$candidates_files = $options['candidates-files'];
} else {
error_log("Error: Missing required option '--candidates-files'.");
exit(1);
}
$candidate_data = parse_pledge_data(explode(" ", $candidates_files));
/* Select people who have taken the pledge */
$pledgeCandidates = array_filter($candidate_data, function ($candidate) {
return $candidate['Pledge'] === 'y';
});
/* Select 9 random candidates */
$pledgeKeys = array_rand($pledgeCandidates, 9);
shuffle($pledgeKeys);
$i = 0;
foreach ($pledgeKeys as $key) {
$image_url = $pledgeCandidates[$key]['image_url'];
$image_id = $pledgeCandidates[$key]['image_id'];
echo "s|pledge_img_".$i."|".$image_url."|\n";
echo "s|pledge_id_".$i."|".$image_id."|\n";
echo "s|pledge_string_".$i."|";
echo $pledgeCandidates[$key]['Candidate Name'].
" (".
$pledgeCandidates[$key]['Council'].
") has taken the pledge!|\n";
$i++;
}
exit(0);

View File

@@ -1,100 +0,0 @@
<?php
$options = getopt("", ["pledges-files:"]);
if (isset($options['pledges-files'])) {
$pledges_files = $options['pledges-files'];
} else {
error_log("Error: Missing required option '--pledges-files'.");
exit(1);
}
$files = explode(" ", $pledges_files);
$candidate_data = [];
foreach ($files as $key => $file) {
$config_file = dirname($file)."/config.json";
$config_string = file_get_contents($config_file);
if ($config_string !== FALSE) {
$config = json_decode($config_string, true);
} else {
error_log("Error opening config.json.");
exit(1);
}
$candidate_file = dirname($file)."/candidates.csv";
if (($handle = fopen($candidate_file, "r")) !== FALSE) {
$headers = fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) {
$candidate = [];
$candidate['Pledge'] = 'n';
foreach ($headers as $key => $value) {
$candidate[$value] = $data[$key];
}
$candidate['Council'] = $config['councilName'];
$candidate['Path'] = dirname($file);
$candidate_data[$candidate['Candidate Name']] = $candidate;
}
fclose($handle);
} else {
error_log('Error opening candidates file');
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 */
$pledgeCandidates = array_filter($candidate_data, function ($candidate) {
return $candidate['Pledge'] === 'y';
});
/* Select 9 random candidates */
$pledgeKeys = array_rand($pledgeCandidates, 9);
shuffle($pledgeKeys);
$i = 0;
foreach ($pledgeKeys as $key) {
$media_desc = $pledgeCandidates[$key]['Path']."/".
$pledgeCandidates[$key]['Picture'].".json";
$media_string = file_get_contents($media_desc);
if ($media_string !== FALSE) {
$media = json_decode($media_string, true);
} else {
error_log("Error opening image descriptor.");
exit(1);
}
$image_url = $media['url'];
$image_id = $media['id'];
echo "s|pledge_img_".$i."|".$image_url."|\n";
echo "s|pledge_id_".$i."|".$image_id."|\n";
echo "s|pledge_string_".$i."|";
echo $pledgeCandidates[$key]['Candidate Name'].
" (".
$pledgeCandidates[$key]['Council'].
") has taken the pledge!|\n";
$i++;
}
exit(0);

View File

@@ -0,0 +1,28 @@
<?php
class SPLPageRenderer {
public function renderPledgePage($councils, $candidates) {
ob_start();
$didError = false;
set_error_handler(function($errno, $errstr, $errfile, $errline) use(&$didError) {
$didError = true;
error_log("Error: $errstr in $errfile on line $errline");
return true; // Prevent default error handling
});
require "template.php";
restore_error_handler();
$content = ob_get_clean();
// Explictly return null if we didn't generate any content or if there was an error
if (!empty($content) && !$didError) {
return $content;
} else {
return null;
}
}
}

View File

@@ -0,0 +1,73 @@
<?php
function parse_pledge_data($candidates_files) {
$candidate_data = [];
foreach ($candidates_files as $key => $file) {
$config_file = dirname($file)."/config.json";
$config_string = file_get_contents($config_file);
if ($config_string !== FALSE) {
$config = json_decode($config_string, true);
} else {
error_log("Error opening config.json.");
exit(1);
}
if (($handle = fopen($file, "r")) !== FALSE) {
$headers = fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) {
$candidate = [];
$candidate['Pledge'] = 'n';
$candidate['Picture'] = "";
$candidate['image_url'] = "";
$candidate['image_id'] = "";
foreach ($headers as $key => $value) {
$candidate[$value] = $data[$key];
}
$candidate['Council'] = $config['councilName'];
$candidate['Path'] = dirname($file);
$media_desc = $candidate['Path']."/".
$candidate['Picture'].".json";
if (file_exists($media_desc)) {
$media_string = file_get_contents($media_desc);
if ($media_string !== FALSE) {
$media = json_decode($media_string, true);
} else {
error_log("Error opening image descriptor.");
exit(1);
}
/* Get photo URL and ID */
$candidate['image_url'] = $media['url'];
$candidate['image_id'] = $media['id'];
}
$candidate_data[$candidate['Candidate Name']] = $candidate;
}
fclose($handle);
} else {
error_log('Error opening candidates file');
exit(1);
}
/* Override pledge columns if pledges.csv is present */
$pledges_file = dirname($file)."/pledges.csv";
if (!file_exists($pledges_file)) continue;
if (($handle = fopen($pledges_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);
}
}
return $candidate_data;
}

View File

@@ -0,0 +1,41 @@
<?php
require_once("parse_pledge_data.php");
require_once("page_renderer.php");
$options = getopt("", ["candidates-files:"]);
if (isset($options['candidates-files'])) {
$candidates_files = $options['candidates-files'];
} else {
error_log("Error: Missing required option '--candidates-files'.");
exit(1);
}
$candidate_data = parse_pledge_data(explode(" ", $candidates_files));
/* Select people who have taken the pledge */
$pledgeCandidates = array_filter($candidate_data, function ($candidate) {
return $candidate['Pledge'] === 'y';
});
$renderer = new SPLPageRenderer();
//print_r($pledgeCandidates);
$councils = [];
foreach ($pledgeCandidates as $key => $candidate) {
$councils[] = $candidate['Council'];
}
$councils = array_unique($councils);
asort($councils);
//print_r($councils);
$pageContent = $renderer->renderPledgePage($councils, $pledgeCandidates);
if ($pageContent === null) {
exit(2);
}
echo $pageContent;
exit(0);

133
pledge-update/template.php Normal file
View File

@@ -0,0 +1,133 @@
<?php
function sluggify($input) {
return strtolower(str_replace(' ', '-', $input));
}
?>
<!-- wp:paragraph -->
<p>The Streets People Love campaign offers council candidates the opportunity to take the following pledge:</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"color":{"background":"#10B5B0"}}} -->
<p class="has-background" style="background-color:#10B5B0">If elected Councillor, I pledge to allocate budget and street space to build streets people love, and ensure that residents of all ages and abilities can safely move around our council area, irrespective of whether they choose to walk, cycle, wheel, use public transport or drive.</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Candidates from these councils have taken the pledge:</p>
<!-- /wp:paragraph -->
<?php
$councilCount = count($councils);
?>
<?php if ($councilCount > 1): ?>
<?php
if ($councilCount > 8) {
$councilListChunkSize = ceil($councilCount / 2);
} else {
$councilListChunkSize = $councilCount;
}
$councilChunks = array_chunk($councils, $councilListChunkSize);
?>
<!-- wp:columns {"className":"council-list-columns"} -->
<div class="wp-block-columns council-list-columns">
<?php for ($columnIdx = 0; $columnIdx < 4; $columnIdx++): ?>
<!-- wp:column {"verticalAlignment":"top","style":{"spacing":{"padding":{"top":"0","bottom":"0"}}}} -->
<div class="wp-block-column is-vertically-aligned-top" style="padding-top:0;padding-bottom:0">
<?php if (array_key_exists($columnIdx, $councilChunks)): ?>
<!-- wp:list {"style":{"spacing":{"margin":{"top":"0","right":"0","bottom":"0","left":"0"}}}} -->
<ul style="margin-top:0;margin-right:0;margin-bottom:0;margin-left:0" class="wp-block-list">
<?php foreach($councilChunks[$columnIdx] as $councilName): ?>
<!-- wp:list-item -->
<li><a href="#<?php echo sluggify($councilName); ?>"><?php echo $councilName; ?></a></li>
<!-- /wp:list-item -->
<?php endforeach; ?>
</ul>
<!-- /wp:list -->
<?php endif; ?>
</div>
<!-- /wp:column -->
<?php endfor; ?>
</div>
<!-- /wp:columns -->
<?php else: ?>
<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->
<?php endif; ?>
<?php foreach ($councils as $key => $council): ?>
<!-- wp:heading {"level":3,"className":"is-style-default"} -->
<?php $groupSlug = sluggify($council); ?>
<h3 class="wp-block-heading is-style-default" id="<?php echo $groupSlug; ?>"><a style="text-decoration: none;" href="#<?php echo $groupSlug; ?>"><?php echo htmlspecialchars($council); ?></a></h3>
<!-- /wp:heading -->
<?php
$groupCandidates = array_filter($candidates, function ($candidate) use ($council) {
return $candidate['Council'] === $council;
});
?>
<?php
$columnCount = 4;
$chunkedCouncilCandidates = array_chunk($groupCandidates, $columnCount);
?>
<?php foreach($chunkedCouncilCandidates as $chunk): ?>
<!-- wp:columns -->
<div class="wp-block-columns">
<?php for ($columnIdx = 0; $columnIdx < $columnCount; $columnIdx++): ?>
<!-- wp:column -->
<div class="wp-block-column">
<?php if (array_key_exists($columnIdx, $chunk)): ?>
<?php
$candidate = $chunk[$columnIdx];
if (isset($candidate['Picture']) && strlen($candidate['image_url'])) {
$candidate_image['url'] = $candidate['image_url'];
$candidate_image['id'] = $candidate['image_id'];
} else {
continue;
}
?>
<!-- wp:image {"id":<?php echo $candidate_image['id']; ?>,"width":"200px","height":"200px","scale":"cover","align":"center","style":{"color":{}},"className":"is-resized"} -->
<figure class="wp-block-image aligncenter is-resized"><img src="<?php echo $candidate_image['url']; ?>" alt="" class="wp-image-<?php echo $candidate_image['id']; ?>" style="object-fit:cover;width:200px;height:200px"/></figure>
<!-- /wp:image -->
<!-- wp:heading {"textAlign":"center","className":"wp-block-heading has-text-align-center has-medium-font-size","style":{"spacing":{"margin":{"top":"1rem","bottom":"0.5rem"}}}} -->
<h2 class="wp-block-heading has-text-align-center has-medium-font-size" style="margin-top:1rem;margin-bottom:0.5rem"><strong><?php echo htmlspecialchars($candidate['Candidate Name']); ?></strong></h2>
<!-- /wp:heading -->
<?php endif; ?>
</div>
<!-- /wp:column -->
<?php endfor; ?>
</div>
<!-- /wp:columns -->
<?php endforeach; ?>
<?php endforeach; ?>
<?php if (isset($config['footer'])): ?>
<!-- wp:paragraph -->
<p><?php echo $config['footer']; ?></p>
<!-- /wp:paragraph -->
<?php endif; ?>

View File

@@ -13,14 +13,14 @@
DATA_PATH="../spl-data"
pledges_files=()
candidates_files=()
for folder in "$DATA_PATH"/*; do
if test -f "$folder"/pledges.csv; then
pledges_files+=("$folder"/pledges.csv)
if test -f "$folder"/candidates.csv; then
candidates_files+=("$folder"/candidates.csv)
fi
done
pledge_sed=$(php pledge-update/main.php --pledges-files "${pledges_files[*]}")
pledge_sed=$(php pledge-update/homepage.php --candidates-files "${candidates_files[*]}")
content=$(sed "$pledge_sed" ../spl-data/movie-homepage)