From fba24b92229e0c63cb535b49ff44a80ff93ff9c8 Mon Sep 17 00:00:00 2001
From: Struan Donald
Date: Wed, 4 Dec 2024 17:47:42 +0000
Subject: [PATCH] show all reps if using postcode to signup to MP alert
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.
---
classes/AlertView/Standard.php | 6 +-
classes/Utility/Search.php | 15 ++-
.../templates/html/alert/_mp_alert_form.php | 120 ++++++++++--------
3 files changed, 83 insertions(+), 58 deletions(-)
diff --git a/classes/AlertView/Standard.php b/classes/AlertView/Standard.php
index 1c383db28c..011d91fc1f 100644
--- a/classes/AlertView/Standard.php
+++ b/classes/AlertView/Standard.php
@@ -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'] = [[
@@ -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']);
@@ -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
diff --git a/classes/Utility/Search.php b/classes/Utility/Search.php
index e9cd84a7bd..8f2420cb6d 100644
--- a/classes/Utility/Search.php
+++ b/classes/Utility/Search.php
@@ -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 ];
+ }
}
}
diff --git a/www/includes/easyparliament/templates/html/alert/_mp_alert_form.php b/www/includes/easyparliament/templates/html/alert/_mp_alert_form.php
index 3d351299a2..5746c14aed 100644
--- a/www/includes/easyparliament/templates/html/alert/_mp_alert_form.php
+++ b/www/includes/easyparliament/templates/html/alert/_mp_alert_form.php
@@ -1,58 +1,76 @@
-