function regional_list($pc, $area_type, $rep_type) { $constituencies = MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituencies($pc); if ($constituencies == 'CONNECTION_TIMED_OUT') { throw new MySociety\TheyWorkForYou\MemberException('Sorry, we couldn’t check your postcode right now, as our postcode lookup server is under quite a lot of load.'); } elseif (!$constituencies) { throw new MySociety\TheyWorkForYou\MemberException('Sorry, ' . htmlentities($pc) . ' isn’t a known postcode'); } elseif (!isset($constituencies[$area_type])) { throw new MySociety\TheyWorkForYou\MemberException(htmlentities($pc) . ' does not appear to be a valid postcode'); } global $PAGE; $a = array_values($constituencies); $db = new ParlDB(); $query_base = "SELECT member.person_id, given_name, family_name, constituency, house\n FROM member, person_names pn\n WHERE constituency IN ('" . join("','", $a) . "')\n AND member.person_id = pn.person_id AND pn.type = 'name'\n AND pn.end_date = (SELECT MAX(end_date) FROM person_names WHERE person_names.person_id = member.person_id)"; $q = $db->query($query_base . " AND left_reason = 'still_in_office' AND house in (" . HOUSE_TYPE_NI . "," . HOUSE_TYPE_SCOTLAND . ")"); $current = true; if (!$q->rows()) { # XXX No results implies dissolution, fix for 2011. $current = false; $q = $db->query($query_base . " AND ( (house=" . HOUSE_TYPE_NI . " AND left_house='2011-03-24') OR (house=" . HOUSE_TYPE_SCOTLAND . " AND left_house='2011-03-23') )"); } $mcon = array(); $mreg = array(); for ($i = 0; $i < $q->rows(); $i++) { $house = $q->field($i, 'house'); $pid = $q->field($i, 'person_id'); $cons = $q->field($i, 'constituency'); if ($house == HOUSE_TYPE_COMMONS) { continue; } elseif ($house == HOUSE_TYPE_NI) { $mreg[] = $q->row($i); } elseif ($house == HOUSE_TYPE_SCOTLAND) { if ($cons == $constituencies['SPC']) { $mcon = $q->row($i); } elseif ($cons == $constituencies['SPE']) { $mreg[] = $q->row($i); } } else { throw new MySociety\TheyWorkForYou\MemberException('Odd result returned!' . $house); } } if ($rep_type == 'msp') { if ($current) { $data['members_statement'] = '<p>You have one constituency MSP (Member of the Scottish Parliament) and multiple region MSPs.</p>'; $data['members_statement'] .= '<p>Your <strong>constituency MSP</strong> is <a href="/msp/?p=' . $mcon['person_id'] . '">'; $data['members_statement'] .= $mcon['given_name'] . ' ' . $mcon['family_name'] . '</a>, MSP for ' . $mcon['constituency']; $data['members_statement'] .= '.</p> <p>Your <strong>' . $constituencies['SPE'] . ' region MSPs</strong> are:</p>'; } else { $data['members_statement'] = '<p>You had one constituency MSP (Member of the Scottish Parliament) and multiple region MSPs.</p>'; $data['members_statement'] .= '<p>Your <strong>constituency MSP</strong> was <a href="/msp/?p=' . $mcon['person_id'] . '">'; $data['members_statement'] .= $mcon['given_name'] . ' ' . $mcon['family_name'] . '</a>, MSP for ' . $mcon['constituency']; $data['members_statement'] .= '.</p> <p>Your <strong>' . $constituencies['SPE'] . ' region MSPs</strong> were:</p>'; } } else { if ($current) { $data['members_statement'] = '<p>You have multiple MLAs (Members of the Legislative Assembly) who represent you in ' . $constituencies['NIE'] . '. They are:</p>'; } else { $data['members_statement'] = '<p>You had multiple MLAs (Members of the Legislative Assembly) who represented you in ' . $constituencies['NIE'] . '. They were:</p>'; } } foreach ($mreg as $reg) { $data['members'][] = array('url' => '/' . $rep_type . '/?p=' . $reg['person_id'], 'name' => $reg['given_name'] . ' ' . $reg['family_name']); } // Send the output for rendering MySociety\TheyWorkForYou\Renderer::output('mp/regional_list', $data); }
function api_getPerson_postcode($pc, $house) { $pc = preg_replace('#[^a-z0-9 ]#i', '', $pc); $types = array(); if ($house == HOUSE_TYPE_NI) { $types = array('NIE'); } elseif ($house == HOUSE_TYPE_SCOTLAND) { $types = array('SPC', 'SPE'); } if (validate_postcode($pc)) { $constituencies = MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituencies($pc); if ($constituencies == 'CONNECTION_TIMED_OUT') { api_error('Connection timed out'); } elseif ($types && isset($constituencies[$types[0]])) { $constituencies = array_map(function ($c) use($constituencies) { return $constituencies[$c]; }, $types); _api_getPerson_constituency($constituencies, $house); } elseif ($types && isset($constituencies['WMC'])) { api_error('Postcode not in correct region'); } elseif (isset($constituencies['WMC'])) { _api_getMP_constituency($constituencies['WMC']); } else { api_error('Unknown postcode'); } } else { api_error('Invalid postcode'); } }
<?php # For looking up a postcode and redirecting or displaying appropriately include_once '../../includes/easyparliament/init.php'; include_once INCLUDESPATH . 'easyparliament/member.php'; $errors = array(); $pc = get_http_var('pc'); if (!$pc) { postcode_error('Please supply a postcode!'); } $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"); } $out = ''; $sidebars = array(); if (isset($constituencies['SPE']) || isset($constituencies['SPC'])) { $MEMBER = fetch_mp($pc, $constituencies); list($out, $sidebars) = pick_multiple($pc, $constituencies, 'SPE', 'MSP'); } elseif (isset($constituencies['NIE'])) { $MEMBER = fetch_mp($pc, $constituencies); list($out, $sidebars) = pick_multiple($pc, $constituencies, 'NIE', 'MLA'); } else { # Just have an MP, redirect instantly to the canonical page