function api_getDivisions_postcode($pc) { $pc = preg_replace('#[^0-9]#i', '', $pc); $output = array(); if (is_postcode($pc)) { $constituency = postcode_to_constituency($pc); if ($constituency == 'CONNECTION_TIMED_OUT') { api_error('Connection timed out'); } elseif ($constituency) { if (is_array($constituency)) { $constituencies = $constituency; } else { $constituencies = array($constituency); } foreach ($constituencies as $c) { $output[] = array('name' => html_entity_decode($c)); } } else { api_error('Unknown postcode'); } } else { api_error('Invalid postcode'); } api_output($output); }
function api_getRepresentatives_postcode($pc) { $pc = preg_replace('#[^0-9]#i', '', $pc); if (is_postcode($pc)) { $constituency = postcode_to_constituency($pc); if ($constituency == 'CONNECTION_TIMED_OUT') { api_error('Connection timed out'); } elseif ($constituency) { if (is_array($constituency)) { $constituencies = $constituency; } else { $constituencies = array($constituency); } $output = array(); foreach ($constituencies as $c) { $output[] = _api_getMP_constituency($c); } api_output($output); } else { api_error('Unknown postcode'); } } else { api_error('Invalid postcode'); } }
function api_getconstituency_postcode($pc) { $pc = preg_replace('#[^a-z0-9 ]#i', '', $pc); if (validate_postcode($pc)) { $constituency = postcode_to_constituency($pc); if ($constituency == 'CONNECTION_TIMED_OUT') { api_error('Connection timed out'); } elseif ($constituency) { $output['name'] = html_entity_decode($constituency); api_output($output); } else { api_error('Unknown postcode'); } } else { api_error('Invalid postcode'); } }
function api_getMP_postcode($pc) { $pc = preg_replace('#[^0-9]#i', '', $pc); if (is_postcode($pc)) { $constituency = postcode_to_constituency($pc); if ($constituency == 'CONNECTION_TIMED_OUT') { api_error('Connection timed out'); } elseif ($constituency) { $person = _api_getMP_constituency($constituency); $output = $person; api_output($output, strtotime($output['lastupdate'])); } else { api_error('Unknown postcode'); } } else { api_error('Invalid postcode'); } }
function api_getConstituency_postcode($pc) { $pc = preg_replace('#[^a-z0-9 ]#i', '', $pc); if (!validate_postcode($pc)) { api_error('Invalid postcode'); return; } $constituency = postcode_to_constituency($pc, true); if ($constituency == 'CONNECTION_TIMED_OUT') { api_error('Connection timed out'); return; } if (!$constituency) { api_error('Unknown postcode'); return; } return _api_getConstituency_name($constituency); }
function api_getconstituency_postcode($pc) { $pc = preg_replace('#[^a-z0-9 ]#i', '', $pc); if (get_http_var('future')) { $new_areas = mapit_get_voting_areas($pc, 13); # Magic number 13 if (is_object($new_areas)) { # rabx_is_error throws Notice api_error('Unknown postcode, or problem with lookup'); } elseif (!isset($new_areas['WMC'])) { api_error('Unknown postcode, or problem with lookup'); } else { $new_info = mapit_get_voting_area_info($new_areas['WMC']); $output['name'] = $new_info['name']; api_output($output); } } else { if (validate_postcode($pc)) { $constituency = postcode_to_constituency($pc); if ($constituency == 'CONNECTION_TIMED_OUT') { api_error('Connection timed out'); } elseif ($constituency) { $db = new ParlDB; $q = $db->query("select constituency, data_key, data_value from consinfo where constituency = '" . mysql_real_escape_string($constituency) . "'"); if ($q->rows()) { for ($i=0; $i<$q->rows(); $i++) { $data_key = $q->field($i, 'data_key'); $output[$data_key] = $q->field($i, 'data_value'); } ksort($output); } $output['name'] = $constituency; api_output($output); } else { api_error('Unknown postcode'); } } else { api_error('Invalid postcode'); } } }
function api_getconstituency_postcode($pc) { $pc = preg_replace('#[^a-z0-9 ]#i', '', $pc); if (!validate_postcode($pc)) { api_error('Invalid postcode'); return; } if (get_http_var('future')) { $xml = simplexml_load_string(file_get_contents(POSTCODE_API_URL . urlencode($pc))); if (!$xml || $xml->error) { api_error('Unknown postcode, or problem with lookup'); return; } $output['name'] = iconv('utf-8', 'iso-8859-1//TRANSLIT', (string) $xml->future_constituency); api_output($output); } else { $constituency = postcode_to_constituency($pc); if ($constituency == 'CONNECTION_TIMED_OUT') { api_error('Connection timed out'); return; } if (!$constituency) { api_error('Unknown postcode'); return; } $db = new ParlDB(); $q = $db->query("select constituency, data_key, data_value from consinfo\n where constituency = '" . mysql_real_escape_string($constituency) . "'"); if ($q->rows()) { for ($i = 0; $i < $q->rows(); $i++) { $data_key = $q->field($i, 'data_key'); $output[$data_key] = $q->field($i, 'data_value'); } ksort($output); } $output['name'] = $constituency; api_output($output); } }
function postcode_to_person_id($postcode) { twfy_debug('MP', "postcode_to_person_id converting postcode to person"); $constituency = strtolower(postcode_to_constituency($postcode)); return $this->constituency_to_person_id($constituency); }
function search_constituencies_by_query($searchterm) { if (validate_postcode($searchterm)) { // Looks like a postcode - can we find the constituency? $constituency = postcode_to_constituency($searchterm); if ($constituency) { return array(array($constituency), true); } } // No luck so far - let's see if they're searching for a constituency. $try = strtolower($searchterm); $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 :try\n and from_date <= date(now()) and date(now()) <= to_date"; $db = new ParlDB(); $q = $db->query($query, array(':try' => '%' . $try . '%')); $constituencies = array(); for ($n = 0; $n < $q->rows(); $n++) { $constituencies[] = $q->field($n, 'name'); } return array($constituencies, false); }
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>'; } }
<?php # Given a postcode, return a person ID include_once 'min-init.php'; include_once INCLUDESPATH . 'easyparliament/member.php'; $pc = $_GET['pc']; $pc = preg_replace('#[^a-z0-9 ]#i', '', $pc); if (validate_postcode($pc)) { $constituency = postcode_to_constituency($pc); if ($constituency == 'CONNECTION_TIMED_OUT') { error('Connection timed out'); } elseif ($constituency) { $pid = get_person_id($constituency); echo 'pid,', $pid; } else { error('Unknown postcode'); } } else { error('Invalid postcode'); } function error($s) { echo 'error,', $s; } function get_person_id($c) { $db = new ParlDB(); if ($c == '') { return false; } if ($c == 'Orkney ') {
?> <h2>General election constituency boundaries</h2> <div id="boundaries"> <? function create_map_filename($c) { $c = str_replace(array(',', '(', ')', "'"), '', $c); $c = str_replace('&', 'and', $c); $c = str_replace("\xf4", 'o', $c); $c = rawurlencode(strtolower($c)); return $c; } $pc = get_http_var('pc'); if ($pc) { $current = postcode_to_constituency($pc); $current_disp = str_replace('&', 'and', $current); if ($current == "connection_timed_out") { print "Sorry, we couldn't check your postcode right now, as our postcode lookup server is under quite a lot of load."; } elseif ($current == "") { print "Sorry, ".htmlentities($pc) ." isn't a known postcode"; } $map_url_current = create_map_filename($current); $new_areas = mapit_get_voting_areas($pc, 13); # Magic number 13 if (isset($new_areas['WMC'])) { $new_info = mapit_get_voting_area_info($new_areas['WMC']); $new = $new_info['name']; $map_url_new = create_map_filename($new); }
<?php include_once "../../includes/easyparliament/init.php"; include_once INCLUDESPATH . "easyparliament/member.php"; include_once INCLUDESPATH . "postcode.inc"; $pc = get_http_var('pc'); $pc = preg_replace('#[^a-z0-9 ]#i', '', $pc); if (!$pc) { exit; } if (validate_postcode($pc)) { $constituency = strtolower(postcode_to_constituency($pc)); if ($constituency == "CONNECTION_TIMED_OUT") { $errors['pc'] = "Sorry, we couldn't check your postcode right now. Please use the 'All Mps' link above to browse MPs"; } elseif ($constituency == "") { $errors['pc'] = "Sorry, " . htmlentities($pc) . " isn't a known postcode"; twfy_debug('MP', "Can't display an MP, as submitted postcode didn't match a constituency"); } else { $MEMBER = new MEMBER(array('constituency' => $constituency)); if ($MEMBER->person_id()) { // This will cookie the postcode. $THEUSER->set_postcode_cookie($pc); $THEUSER->set_constituency_cookie($constituency); } if ($MEMBER->person_id()) { header('Location: http://' . DOMAIN . '/rss/mp/' . $MEMBER->person_id() . '.rdf'); } } } else { $errors['pc'] = "Sorry, " . htmlentities($pc) . " isn't a valid postcode"; twfy_debug('MP', "Can't display an MP because the submitted postcode wasn't of a valid form.");
/** * Test converting a broken postcode to a constituency, make sure we get an empty string */ public function testBrokenPostcodeToConstituency() { $this->assertEquals('', postcode_to_constituency('ZZ00 ABC')); }
function set_users_mp() { // Is this MP THEUSER's MP? global $THEUSER; if (is_object($THEUSER) && $THEUSER->postcode_is_set() && $this->current_member(1)) { $pc = $THEUSER->postcode(); twfy_debug('MP', "set_users_mp converting postcode to person"); $constituency = strtolower(postcode_to_constituency($pc)); if ($constituency == strtolower($this->constituency())) { $this->the_users_mp = true; } } }
function get_person_by_postcode($pc) { global $THEUSER; $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."); throw new MySociety\TheyWorkForYou\MemberException('Sorry, ' . _htmlentities($pc) . ' isn’t a valid postcode'); } twfy_debug('MP', "MP lookup by postcode"); $constituency = strtolower(postcode_to_constituency($pc)); if ($constituency == "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 ($constituency == "") { twfy_debug('MP', "Can't display an MP, as submitted postcode didn't match a constituency"); throw new MySociety\TheyWorkForYou\MemberException('Sorry, ' . _htmlentities($pc) . ' isn’t a known postcode'); } else { // Redirect to the canonical MP page, with a person id. $MEMBER = new MySociety\TheyWorkForYou\Member(array('constituency' => $constituency, 'house' => HOUSE_TYPE_COMMONS)); if ($MEMBER->person_id()) { // This will cookie the postcode. $THEUSER->set_postcode_cookie($pc); } member_redirect($MEMBER, 302); } }
if (preg_match("/^(.*);([0-4]{7})\$/", $qstring, $matches)) { $_GET = array(); $_GET['submit'] = "1"; $_GET['mppc'] = $matches[1]; $c = 0; foreach ($issues as $issue) { $dreamid = $issue[0]; $_GET["i{$dreamid}"] = floatval(substr($matches[2], $c, 1)) / 4.0; $c++; } $shorter_url = true; } // Validate if a submit $errors = array(); if ($_GET['submit']) { $constituency = postcode_to_constituency($db, $_GET['mppc'], "2001"); $row = $db->query_onez_row_assoc("select * from pw_mp where constituency = '{$constituency}' \n and entered_house <= '2005-04-11' and '2005-04-11' <= left_house"); if ($row) { $mpid = $row['mp_id']; } $mpattr = get_mpid_attr($mpid, false, 1, null); if ($mpattr == null) { $errors[] = "Your MP wasn't found. Please check you\n entered the postcode correctly."; } foreach ($issues as $issue) { $dreamid = $issue[0]; if (!array_key_exists("i{$dreamid}", $_GET) || $_GET["i{$dreamid}"] < 0) { $errors[] = "Please select your opinion on " . $issue[1] . "."; } } }
$_GET['submit'] = "1"; $_GET['mppc'] = $matches[1]; $c = 0; foreach ($issues as $issue) { $dreamid = $issue[0]; $_GET["i$dreamid"] = floatval(substr($matches[2], $c, 1)) / 4.0; $c++; } $shorter_url = true; } */ // Validate if a submit $errors = array(); if ($_GET['submit']) { $mpattr = null; $constituency = postcode_to_constituency($db, $_GET['mppc'], $postcode_year); $constituency = "Kirkcaldy & Cowdenbeath"; // XXX temp if ($constituency) { $row = $db->query_onez_row_assoc("select * from pw_mp where constituency = '{$constituency}'\n and ({$date_clause})"); if ($row) { $mpid = $row['mp_id']; } $mpattr = get_mpid_attr($mpid, false, 1, null); if ($mpattr == null) { $errors[] = "Your MP wasn't found. Please check you\n entered the postcode correctly."; } } } // Redirect to shorter URL /*if ($_GET['submit'] and !$errors and !$shorter_url) {
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); }