protected function getMP() { $mp_url = new \URL($this->mp_url); $mp_data = array(); global $THEUSER; if ($THEUSER->has_postcode()) { // User is logged in and has a postcode, or not logged in with a cookied postcode. // (We don't allow the user to search for a postcode if they // already have one set in their prefs.) // this is for people who have e.g. an English postcode looking at the // Scottish homepage try { $constituencies = postcode_to_constituencies($THEUSER->postcode()); if (isset($constituencies[$this->cons_type])) { $constituency = $constituencies[$this->cons_type]; $MEMBER = new Member(array('constituency' => $constituency, 'house' => $this->mp_house)); } } catch (MemberException $e) { return $mp_data; } if (isset($MEMBER) && $MEMBER->valid) { $mp_data['name'] = $MEMBER->full_name(); $left_house = $MEMBER->left_house(); $mp_data['former'] = ''; if ($left_house[$this->mp_house]['date'] != '9999-12-31') { $mp_data['former'] = 'former'; } $mp_data['postcode'] = $THEUSER->postcode(); $mp_data['mp_url'] = $mp_url->generate(); $mp_data['change_url'] = $this->getPostCodeChangeURL(); } } return $mp_data; }
function api_getMSP_postcode($pc) { $pc = preg_replace('#[^a-z0-9 ]#i', '', $pc); if (validate_postcode($pc)) { $constituencies = postcode_to_constituencies($pc); if ($constituencies == 'CONNECTION_TIMED_OUT') { api_error('Connection timed out'); } elseif (isset($constituencies['SPC'])) { _api_getMSP_constituency(array($constituencies['SPC'], $constituencies['SPE'])); } elseif (isset($constituencies['WMC'])) { api_error('Non-Scottish postcode'); } else { api_error('Unknown postcode'); } } else { api_error('Invalid postcode'); } }
# For looking up a postcode and redirecting or displaying appropriately include_once '../../includes/easyparliament/init.php'; include_once INCLUDESPATH . 'easyparliament/member.php'; include_once INCLUDESPATH . 'postcode.inc'; $errors = array(); $pc = get_http_var('pc'); if (!$pc) { $PAGE->error_message('Please supply a postcode!', true); exit; } $pc = preg_replace('#[^a-z0-9]#i', '', $pc); $out = ''; $sidebars = array(); if (validate_postcode($pc)) { $constituencies = postcode_to_constituencies($pc); if ($constituencies == 'CONNECTION_TIMED_OUT') { $errors['pc'] = "Sorry, we couldn't check your postcode right now, as our postcode lookup server is under quite a lot of load."; } elseif (!$constituencies) { $errors['pc'] = "Sorry, " . htmlentities($pc) . " isn't a known postcode"; } elseif (isset($constituencies['SPE']) || isset($constituencies['SPC'])) { $MEMBER = new MEMBER(array('constituency' => $constituencies['WMC'])); if ($MEMBER->person_id()) { $THEUSER->set_postcode_cookie($pc); } list($out, $sidebars) = pick_multiple($pc, $constituencies, 'SPE', 'MSP'); } elseif (isset($constituencies['NIE'])) { $MEMBER = new MEMBER(array('constituency' => $constituencies['WMC'])); if ($MEMBER->person_id()) { $THEUSER->set_postcode_cookie($pc); }
public static function getRegionalList($postcode, $house, $type) { $db = new \ParlDB(); $dissolution_dates = array(3 => '2011-03-24', 4 => '2011-03-23'); $mreg = array(); $constituencies = postcode_to_constituencies($postcode); if (isset($constituencies[$type])) { $cons_name = $constituencies[$type]; $query_base = "SELECT member.person_id, title, lordofname, given_name, family_name, constituency, house\n FROM member, person_names\n WHERE\n member.person_id = person_names.person_id\n AND person_names.type = 'name'\n AND constituency = :cons_name\n AND house = :house\n AND left_house >= start_date\n AND left_house <= end_date"; $q = $db->query("{$query_base} AND left_reason = 'still_in_office'", array(':house' => $house, ':cons_name' => $cons_name)); if (!$q->rows()) { $q = $db->query("{$query_base} AND left_house = :dissolution_date", array(':house' => $house, ':cons_name' => $cons_name, ':dissolution_date' => $dissolution_dates[$house])); } for ($i = 0; $i < $q->rows; $i++) { $name = member_full_name($house, $q->field($i, 'title'), $q->field($i, 'given_name'), $q->field($i, 'family_name'), $q->field($i, 'lordofname')); $mreg[] = array('person_id' => $q->field($i, 'person_id'), 'name' => $name, 'house' => $q->field($i, 'house'), 'constituency' => $q->field($i, 'constituency')); } } return $mreg; }
function regional_list($pc, $area_type, $rep_type) { $constituencies = postcode_to_constituencies($pc); if ($constituencies == 'CONNECTION_TIMED_OUT') { $errors['pc'] = "Sorry, we couldn't check your postcode right now, as our postcode lookup server is under quite a lot of load."; } elseif (!$constituencies) { $errors['pc'] = 'Sorry, ' . htmlentities($pc) . ' isn\'t a known postcode'; } elseif (!isset($constituencies[$area_type])) { $errors['pc'] = htmlentities($pc) . ' does not appear to be a valid postcode'; } global $PAGE; $a = array_values($constituencies); $db = new ParlDB(); $q = $db->query("SELECT person_id, first_name, last_name, constituency, house FROM member \n\t\tWHERE constituency IN ('" . join("','", $a) . "') \n\t\tAND left_reason = 'still_in_office'"); $mcon = array(); $mreg = array(); for ($i = 0; $i < $q->rows(); $i++) { $house = $q->field($i, 'house'); $pid = $q->field($i, 'person_id'); $name = $q->field($i, 'first_name') . ' ' . $q->field($i, 'last_name'); $cons = $q->field($i, 'constituency'); if ($house == 1) { continue; } elseif ($house == 3) { $mreg[] = $q->row($i); } elseif ($house == 4) { if ($cons == $constituencies['SPC']) { $mcon = $q->row($i); } elseif ($cons == $constituencies['SPE']) { $mreg[] = $q->row($i); } } else { $PAGE->error_message('Odd result returned!' . $house); return; } } $PAGE->page_start(); $PAGE->stripe_start(); if ($rep_type == 'msp') { $out = '<p>You have one constituency MSP (Member of the Scottish Parliament) and multiple region MSPs.</p>'; $out .= '<p>Your <strong>constituency MSP</strong> is <a href="/msp/?p=' . $mcon['person_id'] . '">'; $out .= $mcon['first_name'] . ' ' . $mcon['last_name'] . '</a>, MSP for ' . $mcon['constituency']; $out .= '.</p> <p>Your <strong>' . $constituencies['SPE'] . ' region MSPs</strong> are:</p>'; } else { $out = '<p>You have multiple MLAs (Members of the Legislative Assembly) who represent you in ' . $constituencies['NIE'] . '. They are:</p>'; } $out .= '<ul>'; foreach ($mreg as $reg) { $out .= '<li><a href="/' . $rep_type . '/?p=' . $reg['person_id'] . '">'; $out .= $reg['first_name'] . ' ' . $reg['last_name']; $out .= '</a>'; } $out .= '</ul>'; echo $out; $PAGE->stripe_end(); $PAGE->page_end(); }
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 = postcode_to_constituencies($pc, true); 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'); } }
function regional_list($pc, $area_type, $rep_type) { $constituencies = postcode_to_constituencies($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 display_search_form($alert, $details = array(), $errors = array()) { global $this_page, $PAGE; $ACTIONURL = new URL($this_page); $ACTIONURL->reset(); $form_start = '<form action="' . $ACTIONURL->generate() . '" method="post"> <input type="hidden" name="t" value="' . _htmlspecialchars(get_http_var('t')) . '"> <input type="hidden" name="email" value="' . _htmlspecialchars(get_http_var('email')) . '">'; if (isset($details['members']) && $details['members']->rows() > 0) { echo '<ul class="hilites">'; $q = $details['members']; for ($n = 0; $n < $q->rows(); $n++) { echo '<li>'; echo $form_start . '<input type="hidden" name="pid" value="' . $q->field($n, 'person_id') . '">'; echo 'Things by '; $name = member_full_name($q->field($n, 'house'), $q->field($n, 'title'), $q->field($n, 'given_name'), $q->field($n, 'family_name'), $q->field($n, 'lordofname')); if ($q->field($n, 'constituency')) { echo $name . ' (' . $q->field($n, 'constituency') . ') '; } else { echo $name; } echo ' <input type="submit" value="Subscribe"></form>'; echo "</li>\n"; } echo '</ul>'; } if (isset($details['constituencies'])) { echo '<ul class="hilites">'; foreach ($details['constituencies'] as $constituency) { $MEMBER = new MEMBER(array('constituency' => $constituency, 'house' => 1)); echo "<li>"; echo $form_start . '<input type="hidden" name="pid" value="' . $MEMBER->person_id() . '">'; if ($details['valid_postcode']) { echo '<input type="hidden" name="pc" value="' . _htmlspecialchars($details['alertsearch']) . '">'; } echo $MEMBER->full_name(); echo ' (' . _htmlspecialchars($constituency) . ')'; echo ' <input type="submit" value="Subscribe"></form>'; echo "</li>"; } echo '</ul>'; } if ($details['alertsearch']) { echo '<ul class="hilites"><li>'; echo $form_start . '<input type="hidden" name="keyword" value="' . _htmlspecialchars($details['alertsearch']) . '">'; echo 'Mentions of ['; $alertsearch = $details['alertsearch']; if (preg_match('#speaker:(\\d+)#', $alertsearch, $m)) { $MEMBER = new MEMBER(array('person_id' => $m[1])); $alertsearch = str_replace("speaker:{$m['1']}", "speaker:" . $MEMBER->full_name(), $alertsearch); } echo _htmlspecialchars($alertsearch) . '] '; echo ' <input type="submit" value="Subscribe"></form>'; # Use original alertsearch variable here, because name replacement might introduce a comma if (strstr($details['alertsearch'], ',') > -1) { echo '<em class="error">You have used a comma in your search term – are you sure this is what you want? You cannot sign up to multiple search terms using a comma – either use OR, or fill in this form multiple times.</em>'; } if (preg_match('#([A-Z]{1,2}\\d+[A-Z]? ?\\d[A-Z]{2})#i', $alertsearch, $m) && strlen($alertsearch) > strlen($m[1]) && validate_postcode($m[1])) { $scottish_text = ''; $mp_display_text = ''; if (postcode_is_scottish($m[1])) { $mp_display_text = 'your MP, '; $scottish_text = ' or MSP'; } echo '<em class="error">You have used a postcode and something else in your search term – are you sure this is what you want? You will only get an alert if all of these are mentioned in the same debate. Did you mean to get alerts for when your MP' . $scottish_text . ' mentions something instead? If so click subscribe below.</em></li>'; try { $MEMBER = new MEMBER(array('postcode' => $m[1])); // move the postcode to the front just to be tidy $tidy_alertsearch = $m[1] . " " . trim(str_replace("{$m['1']}", "", $alertsearch)); $alertsearch_display = str_replace("{$m['1']} ", "", $tidy_alertsearch); $alertsearch = str_replace("{$m['1']}", "speaker:" . $MEMBER->person_id, $tidy_alertsearch); echo "<li>"; echo $form_start . '<input type="hidden" name="keyword" value="' . _htmlspecialchars($alertsearch) . '">'; echo 'Mentions of ['; echo _htmlspecialchars($alertsearch_display) . '] by ' . $mp_display_text . $MEMBER->full_name(); echo ' <input type="submit" value="Subscribe"></form>'; if ($scottish_text) { $constituencies = postcode_to_constituencies($m[1]); if (isset($constituencies['SPC'])) { $MEMBER = new MEMBER(array('constituency' => $constituencies['SPC'], 'house' => 4)); // move the postcode to the front just to be tidy $alertsearch = str_replace("{$m['1']}", "speaker:" . $MEMBER->person_id, $tidy_alertsearch); echo "</li><li>"; echo $form_start . '<input type="hidden" name="keyword" value="' . _htmlspecialchars($alertsearch) . '">'; echo 'Mentions of ['; echo _htmlspecialchars($alertsearch_display) . '] by your MSP, ' . $MEMBER->full_name(); echo ' <input type="submit" value="Subscribe"></form>'; } } } catch (MySociety\TheyWorkForYou\MemberException $e) { echo '<p>We had a problem looking up your representative.</p>'; } } echo "</li></ul>"; } if ($details['pid']) { $MEMBER = new MEMBER(array('person_id' => $details['pid'])); echo '<ul class="hilites"><li>'; echo "Signing up for things by " . $MEMBER->full_name(); echo ' (' . _htmlspecialchars($MEMBER->constituency()) . ')'; echo "</li></ul>"; } if ($details['keyword']) { echo '<ul class="hilites"><li>'; echo 'Signing up for results from a search for ['; $alertsearch = $details['keyword']; if (preg_match('#speaker:(\\d+)#', $alertsearch, $m)) { $MEMBER = new MEMBER(array('person_id' => $m[1])); $alertsearch = str_replace("speaker:{$m['1']}", "speaker:" . $MEMBER->full_name(), $alertsearch); } echo _htmlspecialchars($alertsearch) . ']'; echo "</li></ul>"; } if (!$details['pid'] && !$details['keyword']) { ?> <p><label for="alertsearch">To sign up to an email alert, enter either your <strong>postcode</strong>, the <strong>name</strong> of who you're interested in, or the <strong>search term</strong> you wish to receive alerts for.</label> To be alerted on an exact <strong>phrase</strong>, be sure to put it in quotes. Also use quotes around a word to avoid stemming (where ‘horse’ would also match ‘horses’). <?php } echo '<form action="' . $ACTIONURL->generate() . '" method="post"> <input type="hidden" name="t" value="' . _htmlspecialchars(get_http_var('t')) . '"> <input type="hidden" name="submitted" value="1">'; if (!$details['pid'] && !$details['keyword'] || isset($errors['alertsearch'])) { if (isset($errors["alertsearch"])) { $PAGE->error_message($errors["alertsearch"]); } $text = $details['alertsearch']; if (!$text) { $text = $details['keyword']; } ?> <div class="row"> <input type="text" name="alertsearch" id="alertsearch" value="<?php if ($text) { echo _htmlentities($text); } ?> " maxlength="255" size="30" style="font-size:150%"> </div> <?php } if ($details['pid']) { echo '<input type="hidden" name="pid" value="' . _htmlspecialchars($details['pid']) . '">'; } if ($details['keyword']) { echo '<input type="hidden" name="keyword" value="' . _htmlspecialchars($details['keyword']) . '">'; } if (!$details['email_verified']) { if (isset($errors["email"]) && $details['submitted']) { $PAGE->error_message($errors["email"]); } ?> <div class="row"> <label for="email">Your email address:</label> <input type="text" name="email" id="email" value="<?php if (isset($details["email"])) { echo _htmlentities($details["email"]); } ?> " maxlength="255" size="30" class="form"> </div> <?php } ?> <div class="row"> <input type="submit" class="submit" value="<?php echo $details['pid'] || $details['keyword'] ? 'Subscribe' : 'Search'; ?> "> </div> <div class="row"> <?php if (!$details['email_verified']) { ?> <p>If you <a href="/user/?pg=join">join</a> or <a href="/user/login/?ret=%2Falert%2F">sign in</a>, you won't need to confirm your email address for every alert you set.<br><br> <?php } if (!$details['pid'] && !$details['keyword']) { ?> <p>Please note that you should only enter <strong>one term per alert</strong> – if you wish to receive alerts on more than one thing, or for more than one person, simply fill in this form as many times as you need, or use boolean OR.<br><br></p> <p>For example, if you wish to receive alerts whenever the words <i>horse</i> or <i>pony</i> are mentioned in Parliament, please fill in this form once with the word <i>horse</i> and then again with the word <i>pony</i> (or you can put <i>horse OR pony</i> with the OR in capitals as explained on the right). Do not put <i>horse, pony</i> as that will only sign you up for alerts where <strong>both</strong> horse and pony are mentioned.</p> <?php } ?> </div> <?php if (get_http_var('sign')) { echo '<input type="hidden" name="sign" value="' . _htmlspecialchars(get_http_var('sign')) . '">'; } if (get_http_var('site')) { echo '<input type="hidden" name="site" value="' . _htmlspecialchars(get_http_var('site')) . '">'; } echo '</form>'; }
if ($prettyquery == "") { $onload = "givefocus('query')"; $title = "Search"; } require_once "parliaments.inc"; require_once "constituencies.inc"; require_once "links.inc"; require_once "postcode.inc"; require_once "wiki.inc"; require_once "tablemake.inc"; require_once "tablepeop.inc"; $postcode = is_postcode($query); $header = false; if ($postcode) { $escaped_postcode = htmlentities(strtoupper($query)); $postcode_matches = postcode_to_constituencies($db, $query); if (!$postcode_matches or $postcode_matches['ERROR']) { $title = "Postcode Error"; pw_header(); print "<p>There was an error trying to look up the postcode"; if ($postcode_matches) { print ": " . htmlentities($postcode_matches['ERROR']) . "</p>"; } pw_footer(); exit; } $number_of_matches = count($postcode_matches); if ($number_of_matches == 1) { # If there's only one match for a postcode, that means # there's just a Westminster constituency, so redirect # straight to that page: