Skip to content

Commit

Permalink
Add 2024 general election information.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed May 24, 2024
1 parent aea839b commit 72a6cf7
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 112 deletions.
4 changes: 2 additions & 2 deletions scripts/mpinfoin.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions www/docs/mp/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
Expand Down
18 changes: 18 additions & 0 deletions www/docs/postcode/address_list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<form>
<input type="hidden" name="pc" value="<?= htmlspecialchars($pc) ?>">

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

<p>
<?php
foreach ($addresses as $address) {
print '<label style="font-size:1rem"><input type="radio" name="address" value="' . $address->slug . '"> ';
print ucwords(strtolower($address->address));
print '</label>';
}
?>

<p>
<input class="button" type="submit" value="Look up">

</form>
221 changes: 127 additions & 94 deletions www/docs/postcode/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,71 @@
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)) {
twfy_debug ('MP', "Can't display an MP because the submitted postcode wasn't of a valid form.");
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);

# ---

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 .= '<h1>' . gettext("Your representatives") . '</h1>';
$out .= '<ul><li>';
$name = $mp['given_name'] . ' ' . $mp['family_name'];
if (isset($mp['former'])) {
$out .= sprintf(gettext('Your former <strong>MP</strong> (Member of Parliament) is <a href="%s">%s</a>, %s'), '/mp/?p=' . $mp['person_id'], $name, gettext($mp['constituency']));
} else {
$out .= sprintf(gettext('Your <strong>MP</strong> (Member of Parliament) is <a href="%s">%s</a>, %s'), '/mp/?p=' . $mp['person_id'], $name, gettext($mp['constituency']));
}
$out .= '</li>';
if ($mcon) {
$name = $mcon['given_name'] . ' ' . $mcon['family_name'];
$out .= '<li>';
if ($house == HOUSE_TYPE_SCOTLAND) {
$url = $urlp . $mcon['person_id'];
$cons = $mcon['constituency'];
if ($current) {
$out .= sprintf(gettext('Your <strong>constituency MSP</strong> (Member of the Scottish Parliament) is <a href="%s">%s</a>, %s'), $url, $name, $cons);
} else {
$out .= sprintf(gettext('Your <strong>constituency MSP</strong> (Member of the Scottish Parliament) was <a href="%s">%s</a>, %s'), $url, $name, $cons);
}
} elseif ($house == HOUSE_TYPE_WALES) {
$url = $urlp . $mcon['person_id'];
$cons = gettext($mcon['constituency']);
if ($current) {
# First %s is URL, second %s is name, third %s is constituency
$out .= sprintf(gettext('Your <strong>constituency MS</strong> (Member of the Senedd) is <a href="%s">%s</a>, %s'), $url, $name, $cons);
} else {
# First %s is URL, second %s is name, third %s is constituency
$out .= sprintf(gettext('Your <strong>constituency MS</strong> (Member of the Senedd) was <a href="%s">%s</a>, %s'), $url, $name, $cons);
}
}
$out .= '</li>';
}
if ($current) {
if ($house == HOUSE_TYPE_NI) {
$out .= '<li>' . sprintf(gettext('Your <strong>%s MLAs</strong> (Members of the Legislative Assembly) are:'), $areas[$area_type]);
} elseif ($house == HOUSE_TYPE_WALES){
$out .= '<li>' . sprintf(gettext('Your <strong>%s region MSs</strong> are:'), gettext($areas[$area_type]));
} else {
$out .= '<li>' . sprintf(gettext('Your <strong>%s %s</strong> are:'), gettext($areas[$area_type]), $member_names['plural']);
}
} else {
if ($house == HOUSE_TYPE_NI) {
$out .= '<li>' . sprintf(gettext('Your <strong>%s MLAs</strong> (Members of the Legislative Assembly) were:'), $areas[$area_type]);
} elseif ($house == HOUSE_TYPE_WALES){
$out .= '<li>' . sprintf(gettext('Your <strong>%s region MSs</strong> were:'), gettext($areas[$area_type]));
} else {
$out .= '<li>' . sprintf(gettext('Your <strong>%s %s</strong> were:'), gettext($areas[$area_type]), $member_names['plural']);
}
}
$out .= '<ul>';
foreach ($mreg as $reg) {
$out .= '<li><a href="' . $urlp . $reg['person_id'] . '">';
$out .= $reg['given_name'] . ' ' . $reg['family_name'];
$out .= '</a>';
}
$out .= '</ul></ul>';
$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' => '<div class="block"><h4>' . gettext('Browse people') . '</h4>
<ul><li><a href="' . $MPSURL->generate() . '">' . gettext('Browse all MPs') . '</a></li>
<li><a href="' . $REGURL->generate() . '">' . $browse_text . '</a></li>
</ul></div>'
));
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) {
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion www/includes/easyparliament/templates/html/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</div>
</div>
<?php } else { ?>
<h1>Find out more about your MP</h1>
<h1>Find out more about your MP, and election candidates</h1>
<div class="row collapse">
<form action="/postcode/" class="mp-search__form" onsubmit="trackFormSubmit(this, 'PostcodeSearch', 'Submit', 'Home'); return false;">
<label for="postcode">Your postcode</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
<?php if ($this_page == "mp" & $current_member[1]) { ?>
<div class="panel">
<h2>General Election 2024</h2>

<p>Find out more about your new constituency and candidates!

<form action="/postcode/">
<label style='font-size:1rem;display:inline' for='postcode'>Postcode:</label>
<input type='text' name='pc' value='' id='postcode' size=8 style="max-width:10em; display: inline; font-size:1rem;">
<input type='submit' value='Look up' class='button'>
</form>

<?php
if ($standing_down_2024) {
echo '<p>This MP is standing down from Parliament at this election.';
}
?>

</div>

<?php if ($this_page == "mp") { ?>

<?php if ($current_member[1]) { ?>

<div class="panel">
<h2>About your Member of Parliament</h2>
<p>
Expand All @@ -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.
</p>
<h2>
What you can do
</h2>
<ul class="rep-actions">
<li>Find out <a href="#profile">more about your MP</a>, including <a href="<?= $member_url ?>/votes">their voting summary</a><?php if ($register_interests) { ?>, <a href="#register">register of interests</a><?php } ?> and <a href="#appearances">recent speeches</a>.</li>
<li><a href="https://www.writetothem.com/">Write to your MP</a>, or find out about your other local representatives <a href="https://www.writetothem.com">on WriteToThem.com</a>.</li>
<li>Find out more about <a href="https://www.localintelligencehub.com/area/WMC/<?= $latest_membership['constituency'] ?>"><?= $latest_membership['constituency'] ?></a> on the <a href="https://www.localintelligencehub.com/">Local Intelligence Hub</a>.</li>
</ul>
</div>
<?php } else if ($this_page == "mp" & !$current_member[1]) { ?>

<?php } else { ?>

<div class="panel">
<h2>About your former Member of Parliament</h2>
<p>
<?= ucfirst($full_name) ?> is a former MP for <?= $latest_membership['constituency'] ?>.
</p>
<?php } ?>

<h2>
What you can do
</h2>
<ul class="rep-actions">
<li>Find out <a href="#profile">more about your MP</a>, including <a href="#appearances">their speeches</a>.</li>
<li>Find out <a href="#profile">more about your MP</a>, including <a href="<?= $member_url ?>/votes">their voting summary</a><?php if ($register_interests) { ?>, <a href="#register">register of interests</a><?php } ?> and <a href="#appearances">recent speeches</a>.</li>
<?php if ($current_member[1]) { ?>
<li><a href="https://www.writetothem.com/">Write to your MP</a>, or find out about your other local representatives <a href="https://www.writetothem.com">on WriteToThem.com</a>.</li>
<?php } ?>
<?php if ($latest_membership['end_date'] == '9999-12-31' || $latest_membership['end_date'] == '2024-05-30') { ?>
<li>Find out more about <a href="https://www.localintelligencehub.com/area/WMC/<?= $latest_membership['constituency'] ?>"><?= $latest_membership['constituency'] ?></a> on the <a href="https://www.localintelligencehub.com/">Local Intelligence Hub</a>.</li>
<?php } ?>
</ul>
</div>
<?php }; ?>

<?php } ?>
Loading

0 comments on commit 72a6cf7

Please sign in to comment.