diff --git a/make-pledge-page.sh b/make-pledge-page.sh new file mode 100755 index 0000000..b953b84 --- /dev/null +++ b/make-pledge-page.sh @@ -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[*]}") + +echo $content; diff --git a/pledge-update/homepage.php b/pledge-update/homepage.php new file mode 100644 index 0000000..4c941b2 --- /dev/null +++ b/pledge-update/homepage.php @@ -0,0 +1,42 @@ + $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); diff --git a/pledge-update/page_renderer.php b/pledge-update/page_renderer.php new file mode 100644 index 0000000..f7ce2dc --- /dev/null +++ b/pledge-update/page_renderer.php @@ -0,0 +1,28 @@ + $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; +} diff --git a/pledge-update/pledge-page.php b/pledge-update/pledge-page.php new file mode 100644 index 0000000..e4a222c --- /dev/null +++ b/pledge-update/pledge-page.php @@ -0,0 +1,41 @@ + $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); diff --git a/pledge-update/template.php b/pledge-update/template.php new file mode 100644 index 0000000..0e32c93 --- /dev/null +++ b/pledge-update/template.php @@ -0,0 +1,133 @@ + + + +

The Streets People Love campaign offers council candidates the opportunity to take the following pledge:

+ + + +

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.

+ + + +

Candidates from these councils have taken the pledge:

+ + + + + 1): ?> + + 8) { + $councilListChunkSize = ceil($councilCount / 2); + } else { + $councilListChunkSize = $councilCount; + } + + $councilChunks = array_chunk($councils, $councilListChunkSize); + + ?> + +
+ + + +
+ + + + + + + +
+ + + +
+ + + +

+ + + + $council): ?> + + +

+ + + + + + + + +
+ + + +
+ + + + + +
+ + + +

+ + + + +
+ + + +
+ + + + + + + +

+ + diff --git a/update-pledges.sh b/update-pledges.sh index 9283f8a..54a64fc 100755 --- a/update-pledges.sh +++ b/update-pledges.sh @@ -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)