function api_getBoundary_name($name) { $name = html_entity_decode(normalise_constituency_name($name)); # XXX if (!$name) { api_error('Name not recognised'); return; } $out = array(); $areas = _api_cacheCheck('mapit_get_areas_by_type', 'WMC'); $areas_info = _api_cacheCheck('mapit_get_voting_areas_info', $areas); $id = null; foreach ($areas_info as $k => $v) { if (html_entity_decode(normalise_constituency_name($v['name'])) == $name) { $id = $k; } } if (!$id) { api_error('No data found for name'); return; } $out = mapit_get_voting_area_geometry($id, 'wgs84'); if (isset($out['polygon'])) { api_output($out['polygon']); } else { api_error('Nothing returned from get_voting_areas_geometry'); } }
function api_getConstituency_name($constituency) { $constituency = normalise_constituency_name($constituency); if (!$constituency) { api_error('Could not find anything with that name'); return; } return _api_getConstituency_name($constituency); }
function get_person_id($c) { $db = new ParlDB; if ($c == '') return false; if ($c == 'Orkney ') $c = 'Orkney & Shetland'; $n = normalise_constituency_name($c); if ($n) $c = $n; $q = $db->query("SELECT person_id FROM member WHERE constituency = '" . mysql_real_escape_string($c) . "' AND left_reason = 'still_in_office' AND house=1"); if ($q->rows > 0) return $q->field(0, 'person_id'); return false; }
function _api_getGeometry_name($name) { $geometry = _api_getGeometry(); if (!get_http_var('future')) { $name = normalise_constituency_name($name); } $out = array(); foreach ($geometry['data'] as $n => $data) { if ($n == $name) return $data; } return null; }
function _api_getGeometry_name($name) { $geometry = _api_getGeometry(); $name = html_entity_decode(normalise_constituency_name($name)); # XXX $out = array(); foreach ($geometry['data'] as $n => $data) { if ($n == $name) { return $data; } } return null; }
function api_getBoundary_name($name) { $name = normalise_constituency_name($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 (normalise_constituency_name($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 = normalise_constituency_name($name); $areas = _api_cacheCheck('mapit_get_areas_by_type', 'WMC'); $ni_geometry = _api_ni_centroids(); $areas_info = _api_cacheCheck('mapit_get_voting_areas_info', $areas); foreach ($areas_info as $area_id => $area) { if ($name == $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_getGeometry_name($name) { if (!defined('OPTION_MAPIT_URL') || !OPTION_MAPIT_URL) { return null; } $name = normalise_constituency_name($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; if ($constituency == '') return false; if ($constituency == 'Orkney ') $constituency = 'Orkney & Shetland'; $normalised = normalise_constituency_name($constituency); if ($normalised) $constituency = $normalised; $q = $db->query("SELECT * FROM member WHERE constituency = '" . mysql_real_escape_string($constituency) . "' AND left_reason = 'still_in_office' AND house=1"); if ($q->rows > 0) return _api_getPerson_row($q->row(0), true); if (get_http_var('always_return')) { $q = $db->query("SELECT * FROM member WHERE house=1 AND constituency = '".mysql_real_escape_string($constituency)."' ORDER BY left_house DESC LIMIT 1"); if ($q->rows > 0) return _api_getPerson_row($q->row(0), true); } return false; }
function _api_getMP_constituency($constituency) { $db = new ParlDB(); $out = null; if ($constituency == '') { return false; } if ($constituency == 'Orkney ') { $constituency = 'Orkney & Shetland'; } $normalised = normalise_constituency_name($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; }
<link href="quiz/quiz.css" type="text/css" rel="stylesheet"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <?php if ($_GET['submit'] and !$errors) { # See if MP is standing again $mpattr = $mpattr['mpprops'][0]; $constituency = str_replace("&", "&", $mpattr['constituency']); $house = $mpattr['house']; $standing_again = false; if ($mpattr['leftreason'] == "general_election_standing") { $standing_again = true; } # Regional parties $consid = normalise_constituency_name(strtolower($constituency), $house, "2001"); if (!$consid) { print "<div class=\"error\">Constituency '{$constituency}' not found, please <a href=\"team@publicwhip.org.uk\">let us know</a>.</div>"; exit; } if (array_key_exists($consid, $wales_constituencies)) { $parties = array_merge($parties, $wales_parties); } if (array_key_exists($consid, $scotland_constituencies)) { $parties = array_merge($parties, $scotland_parties); } if (array_key_exists($consid, $northern_ireland_constituencies)) { $parties = $northern_ireland_parties; } if ($consid == "uk.org.publicwhip/cons/655") { // Wyre Forest, Richard Taylor (Ind)
function constituency_to_person_id($constituency, $house = null) { global $PAGE; if ($constituency == '') { $PAGE->error_message("Sorry, no constituency was found."); return false; } if ($constituency == 'Orkney ') { $constituency = 'Orkney & Shetland'; } $normalised = normalise_constituency_name($constituency); if ($normalised) { $constituency = $normalised; } $q = $this->db->query("SELECT person_id FROM member \n\t\t\t\t\tWHERE constituency = '" . mysql_real_escape_string($constituency) . "' \n\t\t\t\t\tAND left_reason = 'still_in_office'" . ($house ? ' AND house=' . mysql_real_escape_string($house) : '')); if ($q->rows > 0) { return $q->field(0, 'person_id'); } else { #$q = $this->db->query("SELECT person_id FROM member WHERE constituency = '".mysql_real_escape_string($constituency) . "'" # . ($house ? ' AND house='.mysql_real_escape_string($house) : '') . ' ORDER BY left_house DESC LIMIT 1'); #if ($q->rows > 0) { # return $q->field(0, 'person_id'); #} else { $PAGE->error_message("Sorry, there is no current member for the '" . htmlentities($constituency) . "' constituency."); return false; #} } }
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 = normalise_constituency_name($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.'); } }
function search_constituencies_by_query($searchterm) { $constituencies = array(); $constituency = ''; $validpostcode = false; if (validate_postcode($searchterm)) { // Looks like a postcode - can we find the constituency? $constituency = postcode_to_constituency($searchterm); if ($constituency != '') { $validpostcode = true; } } if ($constituency == '' && $searchterm) { // No luck so far - let's see if they're searching for a constituency. $try = strtolower($searchterm); if (normalise_constituency_name($try)) { $constituency = normalise_constituency_name($try); } else { $query = "select distinct\n (select name from constituency where cons_id = o.cons_id and main_name) as name \n from constituency AS o where name like '%" . mysql_real_escape_string($try) . "%'\n and from_date <= date(now()) and date(now()) <= to_date"; $db = new ParlDB(); $q = $db->query($query); for ($n = 0; $n < $q->rows(); $n++) { $constituencies[] = $q->field($n, 'name'); } } } else { if ($constituency) { $constituencies[] = $constituency; } } return array($constituencies, $validpostcode); }
function find_constituency($args) { // We see if the user is searching for a postcode or constituency. global $PAGE, $db; if ($args['s'] != '') { $searchterm = $args['s']; } else { $PAGE->error_message('No search string'); return false; } $constituencies = array(); $constituency = ''; $validpostcode = false; if (validate_postcode($searchterm)) { // Looks like a postcode - can we find the constituency? $constituencies = postcode_to_constituency($searchterm); if ($constituencies == '') { $constituencies = array(); } else { $validpostcode = true; } if (!is_array($constituencies)) { $constituencies = array($constituencies); } } if ($constituencies == array() && $searchterm) { // No luck so far - let's see if they're searching for a constituency. $try = strtolower($searchterm); if (normalise_constituency_name($try)) { $constituency = normalise_constituency_name($try); } else { $query = "select distinct\n (select name from constituency where cons_id = o.cons_id and main_name) as name \n from constituency AS o where name like '%" . mysql_real_escape_string($try) . "%'\n and from_date <= date(now()) and date(now()) <= to_date"; $q = $db->query($query); for ($n = 0; $n < $q->rows(); $n++) { $constituencies[] = $q->field($n, 'name'); } } } if (count($constituencies) == 1) { $constituency = $constituencies[0]; } if ($constituency != '') { // Got a match, display.... $MEMBER = new MEMBER(array('constituency' => $constituency)); $URL = new URL('mp'); if ($MEMBER->valid) { $URL->insert(array('m' => $MEMBER->member_id())); print '<h3>MP for ' . preg_replace("#{$searchterm}#i", '<span class="hi">$0</span>', $constituency); if ($validpostcode) { // Display the postcode the user searched for. print ' (' . htmlentities(strtoupper($args['s'])) . ')'; } ?> </h3> <p><a href="<?php echo $URL->generate(); ?> "><strong><?php echo htmlentities($MEMBER->first_name()) . ' ' . htmlentities($MEMBER->last_name()); ?> </strong></a> (<?php echo $MEMBER->party(); ?> )</p> <?php } } elseif (count($constituencies)) { print "<h3>MPs in constituencies matching '" . htmlentities($searchterm) . "'</h3><ul>"; foreach ($constituencies as $constituency) { $MEMBER = new MEMBER(array('constituency' => $constituency)); $URL = new URL('mp'); if ($MEMBER->valid) { $URL->insert(array('m' => $MEMBER->member_id())); } print '<li><a href="' . $URL->generate() . '"><strong>' . htmlentities($MEMBER->first_name()) . ' ' . htmlentities($MEMBER->last_name()) . '</strong></a> (' . preg_replace("#{$searchterm}#i", '<span class="hi">$0</span>', $constituency) . ', ' . $MEMBER->party() . ')</li>'; } print '</ul>'; } }
function name_to_person_id($name, $const = '') { global $PAGE, $this_page; if ($name == '') { $PAGE->error_message('Sorry, no name was found.'); return false; } # Matthew made this change, but I don't know why. It broke # Iain Duncan Smith, so I've put it back. FAI 2005-03-14 # $success = preg_match('#^(.*? .*?) (.*?)$#', $name, $m); $q = "SELECT DISTINCT person_id,constituency FROM member WHERE "; #if ($this_page=='peer') { # $success = preg_match('#^(.*?) (.*?) of (.*?)$#', $name, $m); # if (!$success) # $success = preg_match('#^(.*?)() of (.*?)$#', $name, $m); # if (!$success) # $success = preg_match('#^(.*?) (.*?)()$#', $name, $m); # if (!$success) { # $PAGE->error_message('Sorry, that name was not recognised.'); # return false; # } # $title = mysql_real_escape_string($m[1]); # $last_name = mysql_real_escape_string($m[2]); # $const = $m[3]; # $q .= "house = 2 AND title = '$title' AND last_name='$last_name'"; #} if ($this_page == 'msp') { $success = preg_match('#^(.*?) (.*?) (.*?)$#', $name, $m); if (!$success) { $success = preg_match('#^(.*?)() (.*)$#', $name, $m); } if (!$success) { $PAGE->error_message('Sorry, that name was not recognised.'); return false; } $first_name = mysql_real_escape_string($m[1]); $middle_name = mysql_real_escape_string($m[2]); $last_name = mysql_real_escape_string($m[3]); $q .= "house = 4 AND ("; $q .= "(first_name='{$first_name} {$middle_name}' AND last_name='{$last_name}')"; $q .= " or (first_name='{$first_name}' AND last_name='{$middle_name} {$last_name}') )"; } elseif ($this_page == 'mla') { $success = preg_match('#^(.*?) (.*?) (.*?)$#', $name, $m); if (!$success) { $success = preg_match('#^(.*?)() (.*)$#', $name, $m); } if (!$success) { $PAGE->error_message('Sorry, that name was not recognised.'); return false; } $first_name = mysql_real_escape_string($m[1]); $middle_name = mysql_real_escape_string($m[2]); $last_name = mysql_real_escape_string($m[3]); $q .= "house = 3 AND ("; $q .= "(first_name='{$first_name} {$middle_name}' AND last_name='{$last_name}')"; $q .= " or (first_name='{$first_name}' AND last_name='{$middle_name} {$last_name}') )"; } elseif (strstr($this_page, 'mp') || $this_page == 'peer') { $success = preg_match('#^(.*?) (.*?) (.*?)$#', $name, $m); if (!$success) { $success = preg_match('#^(.*?)() (.*)$#', $name, $m); } if (!$success) { $PAGE->error_message('Sorry, that name was not recognised.'); return false; } $first_name = $m[1]; $middle_name = $m[2]; $last_name = $m[3]; if (strstr($this_page, 'mp')) { $house = 1; } else { $house = 2; } # if ($title) $q .= 'title = \'' . mysql_real_escape_string($title) . '\' AND '; $q .= "house = " . $house . " AND ((first_name='" . mysql_real_escape_string($first_name . " " . $middle_name) . "' AND last_name='" . mysql_real_escape_string($last_name) . "') OR " . "(first_name='" . mysql_real_escape_string($first_name) . "' AND last_name='" . mysql_real_escape_string($middle_name . " " . $last_name) . "'))"; if ($const) { $normalised = normalise_constituency_name($const); if ($normalised && strtolower($normalised) != strtolower($const)) { $this->canonical = false; $const = $normalised; } } } elseif ($this_page == 'royal') { $q .= ' house = 0'; } if ($const) { $q .= ' AND constituency=\'' . mysql_real_escape_string($const) . "'"; } $q .= ' ORDER BY left_house DESC'; $q = $this->db->query($q); if ($q->rows > 1) { # Hacky as a very hacky thing that's graduated in hacking from the University of Hacksville # Anyone who wants to do it properly, feel free $person_ids = array(); $consts = array(); for ($i = 0; $i < $q->rows(); ++$i) { $pid = $q->field($i, 'person_id'); if (!in_array($pid, $person_ids)) { $person_ids[] = $pid; $consts[] = $q->field($i, 'constituency'); } } if (sizeof($person_ids) == 1) { return $person_ids[0]; } $this->constituency = $consts; return $person_ids; } elseif ($q->rows > 0) { return $q->field(0, 'person_id'); } elseif ($const && $this_page != 'peer') { $this->canonical = false; return $this->name_to_person_id($name); } else { $PAGE->error_message("Sorry, there is no current member with that name."); 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' => normalise_constituency_name($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); }