Ejemplo n.º 1
0
function api_getConstituency_postcode($pc)
{
    $pc = preg_replace('#[^a-z0-9 ]#i', '', $pc);
    if (!validate_postcode($pc)) {
        api_error('Invalid postcode');
        return;
    }
    $constituency = MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituency($pc);
    if ($constituency == 'CONNECTION_TIMED_OUT') {
        api_error('Connection timed out');
        return;
    }
    if (!$constituency) {
        api_error('Unknown postcode');
        return;
    }
    return _api_getConstituency_name($constituency);
}
Ejemplo n.º 2
0
<?php

include_once '../../includes/easyparliament/init.php';
include_once INCLUDESPATH . "easyparliament/member.php";
$pc = get_http_var('pc');
$pc = preg_replace('#[^a-z0-9 ]#i', '', $pc);
if (!$pc) {
    exit;
}
if (validate_postcode($pc)) {
    $constituency = strtolower(MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituency($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);
        }
        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.");
}
Ejemplo n.º 3
0
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');
    }
}
Ejemplo n.º 4
0
 /**
  * Test testing for NI postcode
  */
 public function testPostcodeIsNi()
 {
     $this->assertEquals(true, MySociety\TheyWorkForYou\Utility\Postcode::postcodeIsNi('BT1 0AA'));
     $this->assertEquals(false, MySociety\TheyWorkForYou\Utility\Postcode::postcodeIsNi('SW1A 1AA'));
 }
Ejemplo n.º 5
0
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&rsquo;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&rsquo;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);
}
Ejemplo n.º 6
0
<?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
Ejemplo n.º 7
0
 public 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(MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituency($pc));
         if ($constituency == strtolower($this->constituency())) {
             $this->the_users_mp = true;
         }
     }
 }
Ejemplo n.º 8
0
$errors = array();
$email = trim(get_http_var('email'));
if (!$email) {
    $errors[] = 'Please enter your e-mail address';
} elseif (!validate_email($email)) {
    $errors[] = 'Please enter a valid e-mail address';
}
$postcode = trim(get_http_var('postcode'));
$postcode = preg_replace('#[^A-Z0-9]#i', '', $postcode);
if (!$postcode) {
    $errors[] = 'Please enter your postcode';
} elseif (!validate_postcode($postcode)) {
    $errors[] = 'Please enter a valid postcode';
}
if (!$errors) {
    $constituency = MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituency($postcode);
    if ($constituency != "connection_timed_out" && $constituency != "") {
        $token = auth_random_token();
        if (send_subscribe_email($email, $token)) {
            $q = $db->query("INSERT INTO campaigners (email, postcode, token, signup_date, constituency) VALUES (:email, :postcode, :token, now(), :constituency)", array(':email' => $email, ':postcode' => $postcode, ':token' => $token, ':constituency' => $constituency));
            print "<html><head><title>Check your email! - Free Our Bills - TheyWorkForYou</title></head><body>";
            freeourbills_styles();
            ?>
        <h1 class="free_our_bills_confirm">Nearly Done! Now check your email...</h1>
        <h2 class="free_our_bills_confirm">The confirmation email <strong>may</strong> take a few minutes to arrive &mdash; <em>please</em> be patient.</h2>
        <p class="free_our_bills_confirm">If you use web-based email or have 'junk mail' filters, you may wish to check your bulk&#47;spam mail folders: sometimes, our messages are marked that way.</p>
        <p class="free_our_bills_confirm">You must now click on the link within the email we've just sent you -<br>if you do not, you will not have joined the Free Our Bills campaign.</p>

        <?php 
            exit;
        }