From 8594e61b60b26abeba9fdf5b14e22e0b81357211 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Fri, 24 May 2024 12:38:45 +0100 Subject: [PATCH] Add 2024 general election information. --- conf/general-example | 2 + scripts/mpinfoin.pl | 4 +- www/docs/mp/index.php | 1 + www/docs/postcode/address_list.php | 18 ++ www/docs/postcode/index.php | 221 ++++++++++-------- .../easyparliament/templates/html/index.php | 2 +- .../templates/html/mp/_chamber_info_panel.php | 51 ++-- .../templates/html/postcode/ge2024.php | 107 +++++++++ .../templates/html/postcode/index.php | 96 ++++++++ .../templates/html}/postcode/repexplain.php | 4 +- 10 files changed, 394 insertions(+), 112 deletions(-) create mode 100644 www/docs/postcode/address_list.php create mode 100644 www/includes/easyparliament/templates/html/postcode/ge2024.php create mode 100644 www/includes/easyparliament/templates/html/postcode/index.php rename www/{docs => includes/easyparliament/templates/html}/postcode/repexplain.php (92%) diff --git a/conf/general-example b/conf/general-example index 8fd538b984..894de7bcac 100644 --- a/conf/general-example +++ b/conf/general-example @@ -161,6 +161,8 @@ define('OPTION_HEARFROMYOURMP_BASE_URL', ''); if (!defined('OPTION_MAPIT_URL')) { define('OPTION_MAPIT_URL', ''); } +define('OPTION_DEMOCRACYCLUB_TOKEN', ''); +define('OPTION_MAPIT_UPRN_LOOKUP', ''); // For seeing if someone is in New Zealand. define('OPTION_GAZE_URL', ''); diff --git a/scripts/mpinfoin.pl b/scripts/mpinfoin.pl index cfdfda0e24..5f66dcea16 100755 --- a/scripts/mpinfoin.pl +++ b/scripts/mpinfoin.pl @@ -87,8 +87,8 @@ $twig->parsefile($pwmembers . "wikipedia-commons.xml", ErrorContext => 2); print " Lords \"\n" if $verbose; $twig->parsefile($pwmembers . "wikipedia-lords.xml", ErrorContext => 2); - #print " MPs standing down\n" if $verbose; - #$twig->parsefile($pwmembers . "wikipedia-standingdown.xml", ErrorContext => 2); + print " MPs standing down\n" if $verbose; + $twig->parsefile($pwmembers . "wikipedia-standingdown.xml", ErrorContext => 2); print " Bishops\n" if $verbose; $twig->parsefile($pwmembers . "diocese-bishops.xml", ErrorContext => 2); print " BBC\n" if $verbose; diff --git a/www/docs/mp/index.php b/www/docs/mp/index.php index 9893284370..32bd236413 100644 --- a/www/docs/mp/index.php +++ b/www/docs/mp/index.php @@ -344,6 +344,7 @@ $data['previous_offices'] = $MEMBER->offices('previous'); $data['register_interests'] = person_register_interests($MEMBER, $MEMBER->extra_info); $data['eu_stance'] = $MEMBER->getEUStance(); +$data['standing_down_2024'] = $MEMBER->extra_info['standing_down_2024'] ?? ''; # People who are or were MPs and Lords potentially have voting records, except Sinn Fein MPs $data['has_voting_record'] = ( ($MEMBER->house(HOUSE_TYPE_COMMONS) && $MEMBER->party() != 'Sinn Féin') || $MEMBER->house(HOUSE_TYPE_LORDS) ); diff --git a/www/docs/postcode/address_list.php b/www/docs/postcode/address_list.php new file mode 100644 index 0000000000..b9fc651a10 --- /dev/null +++ b/www/docs/postcode/address_list.php @@ -0,0 +1,18 @@ +
+ + +

This postcode is in more than one constituency; please pick your address from the list below: + +

+ '; + print ucwords(strtolower($address->address)); + print ''; +} +?> + +

+ + +

diff --git a/www/docs/postcode/index.php b/www/docs/postcode/index.php index 259e156d05..77618c5e5a 100644 --- a/www/docs/postcode/index.php +++ b/www/docs/postcode/index.php @@ -5,12 +5,14 @@ include_once '../../includes/easyparliament/init.php'; include_once INCLUDESPATH . 'easyparliament/member.php'; +$data = array(); $errors = array(); $pc = get_http_var('pc'); if (!$pc) { postcode_error('Please supply a postcode!'); } +$data['pc'] = $pc; $pc = preg_replace('#[^a-z0-9]#i', '', $pc); if (!validate_postcode($pc)) { @@ -18,39 +20,56 @@ postcode_error("Sorry, " . _htmlentities($pc) . " isn't a valid postcode"); } -$constituencies = MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituencies($pc); -if ($constituencies == 'CONNECTION_TIMED_OUT') { - postcode_error("Sorry, we couldn't check your postcode right now, as our postcode lookup server is under quite a lot of load."); -} elseif (!$constituencies) { - postcode_error("Sorry, " . _htmlentities($pc) . " isn't a known postcode"); +# 2024 ELECTION EXTRA + +$address = get_http_var('address'); +if ($address) { + $dc_data = democracy_club_address($address); + $constituencies = mapit_address($address); +} else { + $dc_data = democracy_club_postcode($pc); + if ($dc_data->address_picker) { + show_address_list($pc, $dc_data->addresses); + exit; + } + $constituencies = mapit_postcode($pc); +} + +$data['ballot'] = null; +foreach ($dc_data->dates as $date) { + if ($date->date != '2024-07-04') continue; + foreach ($date->ballots as $b) { + if ($b->election_id != 'parl.2024-07-04') continue; + $data['ballot'] = $b; + } } -$out = ''; $sidebars = array(); if (isset($constituencies['SPE']) || isset($constituencies['SPC'])) { - $multi = "scotland"; + $data['multi'] = "scotland"; $MEMBER = fetch_mp($pc, $constituencies); - list($out, $sidebars) = pick_multiple($pc, $constituencies, 'SPE', HOUSE_TYPE_SCOTLAND); + pick_multiple($pc, $constituencies, 'SPE', HOUSE_TYPE_SCOTLAND); } elseif (isset($constituencies['WAE']) || isset($constituencies['WAC'])) { - $multi = "wales"; + $data['multi'] = "wales"; $MEMBER = fetch_mp($pc, $constituencies); - list($out, $sidebars) = pick_multiple($pc, $constituencies, 'WAE', HOUSE_TYPE_WALES); + pick_multiple($pc, $constituencies, 'WAE', HOUSE_TYPE_WALES); } elseif (isset($constituencies['NIE'])) { - $multi = "northern-ireland"; + $data['multi'] = "northern-ireland"; $MEMBER = fetch_mp($pc, $constituencies); - list($out, $sidebars) = pick_multiple($pc, $constituencies, 'NIE', HOUSE_TYPE_NI); + pick_multiple($pc, $constituencies, 'NIE', HOUSE_TYPE_NI); } else { - # Just have an MP, redirect instantly to the canonical page - $multi = "uk"; + $data['multi'] = "uk"; $MEMBER = fetch_mp($pc, $constituencies, 1); - member_redirect($MEMBER); + $data['mp'] = [ + 'name' => $MEMBER->full_name(), + 'person_id' => $MEMBER->person_id(), + 'constituency' => $MEMBER->constituency(), + 'former' => !$MEMBER->current_member(HOUSE_TYPE_COMMONS), + ]; + $data['MPSURL'] = new \MySociety\TheyWorkForYou\Url('mps'); } -$PAGE->page_start(); -$PAGE->stripe_start(); -echo $out; -include("repexplain.php"); -$PAGE->stripe_end($sidebars); -$PAGE->page_end(); +$data['mapit_ids'] = $mapit_ids; +MySociety\TheyWorkForYou\Renderer::output('postcode/index', $data); # --- @@ -83,7 +102,7 @@ function fetch_mp($pc, $constituencies, $house=null) { } function pick_multiple($pc, $areas, $area_type, $house) { - global $PAGE; + global $PAGE, $data; $db = new ParlDB; $member_names = \MySociety\TheyWorkForYou\Utility\House::house_to_members($house); @@ -108,9 +127,7 @@ function pick_multiple($pc, $areas, $area_type, $house) { $mp = array(); if ($q) { $mp = $q; - if ($mp['left_house'] != '9999-12-31') { - $mp['former'] = true; - } + $mp['former'] = ($mp['left_house'] != '9999-12-31'); } $a = array_values($areas); @@ -151,77 +168,21 @@ function pick_multiple($pc, $areas, $area_type, $house) { return; } } + $data['mcon'] = $mcon; + $data['mreg'] = $mreg; + $data['house'] = $house; + $data['urlp'] = $urlp; + $data['current'] = $current; + $data['areas'] = $areas; + $data['area_type'] = $area_type; + $data['member_names'] = $member_names; - $out = ''; - $out .= '

' . gettext("Your representatives") . '

'; - $out .= ''; + $mp['name'] = $mp['given_name'] . ' ' . $mp['family_name']; + $data['mp'] = $mp; - $MPSURL = new \MySociety\TheyWorkForYou\Url('mps'); - $REGURL = new \MySociety\TheyWorkForYou\Url($urlpl); - $browse_text = sprintf(gettext('Browse all %s'), $member_names['plural']); - $sidebar = array(array( - 'type' => 'html', - 'content' => '

' . gettext('Browse people') . '

-
' - )); - return array($out, $sidebar); + $data['MPSURL'] = new \MySociety\TheyWorkForYou\Url('mps'); + $data['REGURL'] = new \MySociety\TheyWorkForYou\Url($urlpl); + $data['browse_text'] = sprintf(gettext('Browse all %s'), $member_names['plural']); } function member_redirect(&$MEMBER) { @@ -231,3 +192,75 @@ function member_redirect(&$MEMBER) { exit; } } + +function democracy_club_postcode($pc) { + $pc = urlencode($pc); + $data = web_lookup("https://developers.democracyclub.org.uk/api/v1/postcode/$pc/?auth_token=" . OPTION_DEMOCRACYCLUB_TOKEN); + $data = json_decode($data); + return $data; +} + +function democracy_club_address($address) { + $address = urlencode($address); + $data = web_lookup("https://developers.democracyclub.org.uk/api/v1/address/$address/?auth_token=" . OPTION_DEMOCRACYCLUB_TOKEN); + $data = json_decode($data); + return $data; +} + +function mapit_postcode($postcode) { + $filename = 'postcode/' . rawurlencode($postcode); + return mapit_lookup('postcode', $filename); +} + +function mapit_address($address) { + $address = urlencode($address); + $url = str_replace('{s}', $address, OPTION_MAPIT_UPRN_LOOKUP); + $file = web_lookup($url); + $r = json_decode($file); + $filename = 'point/4326/' . $r->wgs84_lon . ',' . $r->wgs84_lat; + return mapit_lookup('point', $filename); +} + +function mapit_lookup($type, $filename) { + global $mapit_ids; + $file = web_lookup(OPTION_MAPIT_URL . $filename); + $r = json_decode($file); + if (isset($r->error)) return ''; + if ($type == 'postcode' && !isset($r->areas)) return ''; + + $input = ($type == 'postcode') ? $r->areas : $r; + $areas = array(); + foreach ($input as $row) { + if (in_array($row->type, array('WMC', 'WMCF', 'SPC', 'SPE', 'NIE', 'WAC', 'WAE'))) + $areas[$row->type] = $row->name; + if ($row->type == 'WMC') { + $mapit_ids['old'] = $row->id; + } + if ($row->type == 'WMCF') { + $mapit_ids['new'] = $row->id; + } + } + if (!isset($areas['WMC'])) { + return ''; + } + return $areas; +} + +function show_address_list($pc, $addresses) { + global $PAGE; + $PAGE->page_start(); + $PAGE->stripe_start(); + include("address_list.php"); + $PAGE->page_end(); +} + +function web_lookup($url) { + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); + $file = curl_exec($ch); + curl_close($ch); + return $file; +} diff --git a/www/includes/easyparliament/templates/html/index.php b/www/includes/easyparliament/templates/html/index.php index b98f9c206d..a86103e12c 100644 --- a/www/includes/easyparliament/templates/html/index.php +++ b/www/includes/easyparliament/templates/html/index.php @@ -20,7 +20,7 @@ -

Find out more about your MP

+

Find out more about your MP, and election candidates

diff --git a/www/includes/easyparliament/templates/html/mp/_chamber_info_panel.php b/www/includes/easyparliament/templates/html/mp/_chamber_info_panel.php index a479060a1f..3366b1a11b 100644 --- a/www/includes/easyparliament/templates/html/mp/_chamber_info_panel.php +++ b/www/includes/easyparliament/templates/html/mp/_chamber_info_panel.php @@ -1,4 +1,26 @@ - +
+

General Election 2024

+ +

Find out more about your new constituency and candidates! + + + + + +

+ +This MP is standing down from Parliament at this election.'; +} +?> + +
+ + + + +

About your Member of Parliament

@@ -11,25 +33,28 @@ In the constituency, their focus is on supporting local people and championing local issues. They have a small staff team who help with casework, maintain their diaries, and monitor their inbox.

-

- What you can do -

- -
- + + +

About your former Member of Parliament

is a former MP for .

+ +

What you can do

- + + diff --git a/www/includes/easyparliament/templates/html/postcode/ge2024.php b/www/includes/easyparliament/templates/html/postcode/ge2024.php new file mode 100644 index 0000000000..20f3ed1f2e --- /dev/null +++ b/www/includes/easyparliament/templates/html/postcode/ge2024.php @@ -0,0 +1,107 @@ + + + + +

General election

+ +

See your current representatives + +

There is a UK general election on 4th July 2024 + 1) { + echo "($datediff days away)"; +} elseif ($datediff > 0) { + echo '(tomorrow!)'; +} elseif ($datediff > -86400) { + echo '(today!)'; +} +?>. + +

For this election, you will be in the +post_name ?> +constituency. +You can see statistics and information for your new constituency at the +Local Intelligence Hub. + +

+The people standing in your constituency +candidates_verified) { echo '(not yet finalised or verified)'; } +?> are: + +

+ +

+Democracy Club +For more information visit WhoCanIVoteFor. +This data has been provided by Democracy Club, thanks to them. + +

Many constituency boundaries have changed for this election. +Here's a map of your new constituency (pink) with your +existing constituency in grey: + +

+
+
+
+ + + + + diff --git a/www/includes/easyparliament/templates/html/postcode/index.php b/www/includes/easyparliament/templates/html/postcode/index.php new file mode 100644 index 0000000000..2f13beb372 --- /dev/null +++ b/www/includes/easyparliament/templates/html/postcode/index.php @@ -0,0 +1,96 @@ +
+
+
+
+
+ + +
+ +
+
+
+
diff --git a/www/docs/postcode/repexplain.php b/www/includes/easyparliament/templates/html/postcode/repexplain.php similarity index 92% rename from www/docs/postcode/repexplain.php rename to www/includes/easyparliament/templates/html/postcode/repexplain.php index 5045945eb6..4f625e47d3 100644 --- a/www/docs/postcode/repexplain.php +++ b/www/includes/easyparliament/templates/html/postcode/repexplain.php @@ -1,4 +1,4 @@ -

+

@@ -18,6 +18,6 @@

WriteToThem.com.'), $url) ?>