Skip to content

Commit

Permalink
show all reps if using postcode to signup to MP alert
Browse files Browse the repository at this point in the history
If the user is somewhere with multiple representatives and they use a
postcode to sign up to an MP alert then offer them a choice. If they
only have one rep then just sign them up.
  • Loading branch information
struan committed Dec 4, 2024
1 parent d306bbf commit fba24b9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 58 deletions.
6 changes: 3 additions & 3 deletions classes/AlertView/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ protected function searchForConstituenciesAndMembers() {
}
$members_from_words = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookupWithNames($text, true);
$this->data['members'] = array_merge($members_from_words, $members_from_names);
[$this->data['constituencies'], $this->data['valid_postcode']] = \MySociety\TheyWorkForYou\Utility\Search::searchConstituenciesByQuery($text);
[$this->data['constituencies'], $this->data['valid_postcode']] = \MySociety\TheyWorkForYou\Utility\Search::searchConstituenciesByQuery($text, false);
} elseif ($this->data['pid']) {
$MEMBER = new \MEMBER(['person_id' => $this->data['pid']]);
$this->data['members'] = [[
Expand Down Expand Up @@ -384,7 +384,7 @@ protected function searchForConstituenciesAndMembers() {
# If the above search returned one result for constituency
# search by postcode, use it immediately
if (isset($this->data['constituencies']) && count($this->data['constituencies']) == 1 && $this->data['valid_postcode']) {
$MEMBER = new \MEMBER(['constituency' => $this->data['constituencies'][0], 'house' => 1]);
$MEMBER = new \MEMBER(['constituency' => array_values($this->data['constituencies'])[0], 'house' => 1]);
$this->data['pid'] = $MEMBER->person_id();
$this->data['pc'] = $text;
unset($this->data['constituencies']);
Expand All @@ -394,7 +394,7 @@ protected function searchForConstituenciesAndMembers() {
$cons = [];
foreach ($this->data['constituencies'] as $constituency) {
try {
$MEMBER = new \MEMBER(['constituency' => $constituency, 'house' => 1]);
$MEMBER = new \MEMBER(['constituency' => $constituency]);
$cons[$constituency] = $MEMBER;
} catch (\MySociety\TheyWorkForYou\MemberException $e) {
// do nothing
Expand Down
15 changes: 11 additions & 4 deletions classes/Utility/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,19 @@ public static function searchMemberDbLookupWithNames($searchstring, $current_onl
* saying whether it was a postcode used.
*/

public static function searchConstituenciesByQuery($searchterm) {
public static function searchConstituenciesByQuery($searchterm, $mp_only=true) {
if (validate_postcode($searchterm)) {
// Looks like a postcode - can we find the constituency?
$constituency = Postcode::postcodeToConstituency($searchterm);
if ($constituency) {
return [ [$constituency], true ];
if ($mp_only) {
$constituency = Postcode::postcodeToConstituency($searchterm);
if ($constituency) {
return [ [$constituency], true ];
}
} else {
$constituencies = Postcode::postcodeToConstituencies($searchterm);
if ($constituencies) {
return [ $constituencies, true ];
}
}
}

Expand Down
120 changes: 69 additions & 51 deletions www/includes/easyparliament/templates/html/alert/_mp_alert_form.php
Original file line number Diff line number Diff line change
@@ -1,58 +1,76 @@
<form action="<?= $actionurl ?>" method="post" class="" id="create-alert-form">
<?php if (!$email_verified) { ?>
<p>
<?php if (isset($errors["email"]) && $submitted) { ?>
<span class="alert-page-error"><?= $errors["email"] ?></span>
<?php } ?>
<input type="email" class="form-control" placeholder="<?= gettext('Your email address') ?>" name="email" id="email" value="<?= _htmlentities($email) ?>">
</p>
<?php } ?>

<p>
<?php if ($pid) { ?>
<input type="text" class="form-control" name="mp_search" id="mp_search" disabled="disabled"
value="<?= $pid_member->full_name() ?><?php if ($pid_member->constituency()) { ?> (<?= _htmlspecialchars($pid_member->constituency()) ?>)<?php } ?>">
<?php } elseif ($keyword) { ?>
<input type="text" class="form-control" name="mp_search" id="mp_search" disabled="disabled" value="<?= _htmlspecialchars($display_keyword) ?>">
<?php } else { ?>
<label for="mp-postcode">Search postcode, or MP name</label>
<input id="mp-postcode" type="text" class="form-control" placeholder="<?= gettext('e.g. ‘B2 4QA’ or ‘John Doe’') ?>" name="mp_search" id="mp_search" value="<?= _htmlentities($search_text) ?>" style="min-width:300px;">
<?php } ?>
</p>
<?php if (isset($constituencies) && count($constituencies) > 0) {
$member_options = true; ?>
<h3><?= sprintf(gettext('Sign up for alerts when Representatives for constituencies matching <i>%s</i> speaks'), _htmlspecialchars($search_term)) ?></h3>
<ul>
<?php foreach ($constituencies as $constituency => $member) { ?>
<li>
<form action="<?= $actionurl ?>" method="post">
<input type="hidden" name="t" value="<?= _htmlspecialchars($token) ?>">
<input type="hidden" name="email" value="<?= _htmlspecialchars($email) ?>">
<input type="hidden" name="pid" value="<?= $member->person_id() ?>">
<?= $member->full_name() ?>
(<?= _htmlspecialchars($constituency) ?>) in <?= _htmlspecialchars($member->house_text($member->house_disp)) ?>
<input type="submit" class="button small" value="<?= gettext('Subscribe') ?>"></form>
</li>
<?php } ?>
</ul>
<?php } else { ?>
<form action="<?= $actionurl ?>" method="post" class="" id="create-alert-form">
<?php if (!$email_verified) { ?>
<p>
<?php if (isset($errors["email"]) && $submitted) { ?>
<span class="alert-page-error"><?= $errors["email"] ?></span>
<?php } ?>
<input type="email" class="form-control" placeholder="<?= gettext('Your email address') ?>" name="email" id="email" value="<?= _htmlentities($email) ?>">
</p>
<?php } ?>

<p>
<?php if ($pid || $keyword) { ?>
<button type="submit" class="button" name="mp_step" value="mp_confirm">
<span><?= gettext('Subscribe') ?></span>
<i aria-hidden="true" class="fi-megaphone"></i>
</button>
<p>
<?php if ($pid) { ?>
<input type="text" class="form-control" name="mp_search" id="mp_search" disabled="disabled"
value="<?= $pid_member->full_name() ?><?php if ($pid_member->constituency()) { ?> (<?= _htmlspecialchars($pid_member->constituency()) ?>)<?php } ?>">
<?php } elseif ($keyword) { ?>
<input type="text" class="form-control" name="mp_search" id="mp_search" disabled="disabled" value="<?= _htmlspecialchars($display_keyword) ?>">
<?php } else { ?>
<button type="submit" class="button" name="mp_step" value="mp_search">
<span><?= gettext('Search') ?></span>
<i aria-hidden="true" class="fi-magnifying-glass"></i>
</button>
<label for="mp-postcode">Search postcode, or MP name</label>
<input id="mp-postcode" type="text" class="form-control" placeholder="<?= gettext('e.g. ‘B2 4QA’ or ‘John Doe’') ?>" name="mp_search" id="mp_search" value="<?= _htmlentities($search_text) ?>" style="min-width:300px;">
<?php } ?>
<button type="submit" class="button button--red" name="action" value="Abandon">
<i aria-hidden="true" class="fi-trash"></i>
<span><?= gettext('Abandon changes') ?></span>
</button>
</p>
</p>

<p>
<?php if ($pid || $keyword) { ?>
<button type="submit" class="button" name="mp_step" value="mp_confirm">
<span><?= gettext('Subscribe') ?></span>
<i aria-hidden="true" class="fi-megaphone"></i>
</button>
<?php } else { ?>
<button type="submit" class="button" name="mp_step" value="mp_search">
<span><?= gettext('Search') ?></span>
<i aria-hidden="true" class="fi-magnifying-glass"></i>
</button>
<?php } ?>
<button type="submit" class="button button--red" name="action" value="Abandon">
<i aria-hidden="true" class="fi-trash"></i>
<span><?= gettext('Abandon changes') ?></span>
</button>
</p>

<input type="hidden" name="t" value="<?= _htmlspecialchars($token) ?>">
<input type="hidden" name="submitted" value="1">
<input type="hidden" name="t" value="<?= _htmlspecialchars($token) ?>">
<input type="hidden" name="submitted" value="1">

<?php if ($pid) { ?>
<input type="hidden" name="pid" value="<?= _htmlspecialchars($pid) ?>">
<?php } ?>
<?php if ($keyword) { ?>
<input type="hidden" name="keyword" value="<?= _htmlspecialchars($keyword) ?>">
<?php } ?>
<?php if ($pid) { ?>
<input type="hidden" name="pid" value="<?= _htmlspecialchars($pid) ?>">
<?php } ?>
<?php if ($keyword) { ?>
<input type="hidden" name="keyword" value="<?= _htmlspecialchars($keyword) ?>">
<?php } ?>

<?php if ($sign) { ?>
<input type="hidden" name="sign" value="<?= _htmlspecialchars($sign) ?>">
<?php } ?>
<?php if ($sign) { ?>
<input type="hidden" name="sign" value="<?= _htmlspecialchars($sign) ?>">
<?php } ?>

<?php if ($site) { ?>
<input type="hidden" name="site" value="<?= _htmlspecialchars($site) ?>">
<?php } ?>
</form>
<?php if ($site) { ?>
<input type="hidden" name="site" value="<?= _htmlspecialchars($site) ?>">
<?php } ?>
</form>
<?php } ?>

0 comments on commit fba24b9

Please sign in to comment.