function api_getConstituency_name($constituency) { $constituency = MySociety\TheyWorkForYou\Utility\Constituencies::normaliseConstituencyName($constituency); if (!$constituency) { api_error('Could not find anything with that name'); return; } return _api_getConstituency_name($constituency); }
function api_getBoundary_name($name) { $name = MySociety\TheyWorkForYou\Utility\Constituencies::normaliseConstituencyName($name); if (!$name) { api_error('Name not recognised'); return; } $out = array(); $areas_info = _api_cacheCheck('areas', 'WMC'); $id = null; foreach ($areas_info as $k => $v) { if (MySociety\TheyWorkForYou\Utility\Constituencies::normaliseConstituencyName($v['name']) == $name) { $id = $k; } } if (!$id) { api_error('No data found for name'); return; } header("Location: http://mapit.mysociety.org/area/4326/{$id}.kml"); exit; }
function _api_getGeometry_name($name) { if (!defined('OPTION_MAPIT_URL') || !OPTION_MAPIT_URL) { return null; } $name = MySociety\TheyWorkForYou\Utility\Constituencies::normaliseConstituencyName($name); # Names are currently in ISO-8859-1, but MapIt is in UTF-8 $name_utf8 = iconv('iso-8859-1', 'utf-8//TRANSLIT', $name); $areas_info = _api_cacheCheck('areas', 'WMC'); $ni_geometry = _api_ni_centroids(); foreach ($areas_info as $area_id => $area) { if ($name_utf8 == $area['name']) { if (isset($ni_geometry[$area_id])) { $out = $ni_geometry[$area_id]; } else { $out = mapit_call('area/geometry', $area_id); } $out['name'] = $name; return $out; } } return null; }
function _api_getMP_constituency($constituency) { $db = new ParlDB(); $out = null; if ($constituency == '') { return false; } if ($constituency == 'Orkney ') { $constituency = 'Orkney & Shetland'; } $normalised = MySociety\TheyWorkForYou\Utility\Constituencies::normaliseConstituencyName($constituency); if ($normalised) { $constituency = $normalised; } $q = $db->query("SELECT member.*, p.title, p.given_name, p.family_name, p.lordofname\n FROM member, person_names p\n WHERE constituency = :constituency\n AND member.person_id = p.person_id AND p.type = 'name'\n AND p.start_date <= left_house and left_house <= p.end_date\n AND left_reason = 'still_in_office' AND house=:house", array(':house' => HOUSE_TYPE_COMMONS, ':constituency' => $constituency)); if ($q->rows > 0) { $out = _api_getPerson_row($q->row(0), true); } if (!$out && get_http_var('always_return')) { $q = $db->query("SELECT member.*, p.title, p.given_name, p.family_name, p.lordofname\n FROM member, person_names p\n WHERE house=:house AND constituency = :constituency\n AND member.person_id = p.person_id AND p.type = 'name'\n AND p.start_date <= left_house and left_house <= p.end_date\n ORDER BY left_house DESC LIMIT 1", array(':house' => HOUSE_TYPE_COMMONS, ':constituency' => $constituency)); if ($q->rows > 0) { $out = _api_getPerson_row($q->row(0), true); } } if ($out) { api_output($out, isset($out['lastupdate']) ? strtotime($out['lastupdate']) : null); return true; } return false; }
function pick_multiple($pc, $areas, $area_type, $rep_type) { global $PAGE; $db = new ParlDB(); $q = $db->query("SELECT member.person_id, given_name, family_name, constituency, left_house\n FROM member, person_names pn\n WHERE constituency = :constituency\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)\n AND house = 1 ORDER BY left_house DESC LIMIT 1", array(':constituency' => MySociety\TheyWorkForYou\Utility\Constituencies::normaliseConstituencyName($areas['WMC']))); $mp = array(); if ($q->rows()) { $mp = $q->row(0); if ($mp['left_house'] != '9999-12-31') { $mp['former'] = true; } } $a = array_values($areas); $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 (3,4)"); $current = true; if (!$q->rows()) { # XXX No results implies dissolution, fix for 2011. $current = false; $q = $db->query($query_base . " AND ( (house=3 AND left_house='2011-03-24') OR (house=4 AND left_house='2011-03-23') )"); } $mcon = array(); $mreg = array(); for ($i = 0; $i < $q->rows(); $i++) { $house = $q->field($i, 'house'); $cons = $q->field($i, 'constituency'); if ($house == 3) { $mreg[] = $q->row($i); } elseif ($house == 4) { if ($cons == $areas['SPC']) { $mcon = $q->row($i); } elseif ($cons == $areas['SPE']) { $mreg[] = $q->row($i); } } else { $PAGE->error_message('Odd result returned, please let us know!'); return; } } $out = ''; $out .= '<p>That postcode has multiple results, please pick who you are interested in:</p>'; $out .= '<ul><li>Your '; if (isset($mp['former'])) { $out .= 'former '; } $out .= '<strong>MP</strong> (Member of Parliament) is <a href="/mp/?p=' . $mp['person_id'] . '">'; $out .= $mp['given_name'] . ' ' . $mp['family_name'] . '</a>, ' . $mp['constituency'] . '</li>'; if ($mcon) { $out .= '<li>Your <strong>constituency MSP</strong> (Member of the Scottish Parliament) '; $out .= $current ? 'is' : 'was'; $out .= ' <a href="/msp/?p=' . $mcon['person_id'] . '">'; $out .= $mcon['given_name'] . ' ' . $mcon['family_name'] . '</a>, ' . $mcon['constituency'] . '</li>'; } $out .= '<li>Your <strong>' . $areas[$area_type] . ' ' . $rep_type . 's</strong> '; if ($rep_type == 'MLA') { $out .= '(Members of the Legislative Assembly)'; } $out .= ' ' . ($current ? 'are' : 'were') . ':'; $out .= '<ul>'; foreach ($mreg as $reg) { $out .= '<li><a href="/' . strtolower($rep_type) . '/?p=' . $reg['person_id'] . '">'; $out .= $reg['given_name'] . ' ' . $reg['family_name']; $out .= '</a>'; } $out .= '</ul></ul>'; $MPSURL = new URL('mps'); $REGURL = new URL(strtolower($rep_type) . 's'); $sidebar = array(array('type' => 'html', 'content' => '<div class="block"><h4>Browse people</h4> <ul><li><a href="' . $MPSURL->generate() . '">Browse all MPs</a></li> <li><a href="' . $REGURL->generate() . '">Browse all ' . $rep_type . 's</a></li> </ul></div>')); return array($out, $sidebar); }
public function constituency_to_person_id($constituency, $house = null) { if ($constituency == '') { throw new MySociety\TheyWorkForYou\MemberException('Sorry, no constituency was found.'); } if ($constituency == 'Orkney ') { $constituency = 'Orkney & Shetland'; } $normalised = MySociety\TheyWorkForYou\Utility\Constituencies::normaliseConstituencyName($constituency); if ($normalised) { $constituency = $normalised; } $params = array(); $left = "left_reason = 'still_in_office'"; if (DISSOLUTION_DATE) { $left = "({$left} OR left_house = '" . DISSOLUTION_DATE . "')"; } $query = "SELECT person_id FROM member\n WHERE constituency = :constituency\n AND {$left}"; $params[':constituency'] = $constituency; if ($house) { $query .= ' AND house = :house'; $params[':house'] = $house; } $q = $this->db->query($query, $params); if ($q->rows > 0) { return $q->field(0, 'person_id'); } else { throw new MySociety\TheyWorkForYou\MemberException('Sorry, there is no current member for the "' . _htmlentities(ucwords($constituency)) . '" constituency.'); } }