diff --git a/classes/Search/Normal.php b/classes/Search/Normal.php index fdfc426fce..5c8f584d13 100644 --- a/classes/Search/Normal.php +++ b/classes/Search/Normal.php @@ -143,12 +143,7 @@ private function find_members() { $searchstring = trim(preg_replace('#-?[a-z]+:[a-z0-9]+#', '', $this->searchstring)); if (!$searchstring) return array(); - $members = array(); - - $q = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup($searchstring); - foreach ($q as $row) { - $members[] = $row['person_id']; - } + $members = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup($searchstring); $db = new \ParlDB; $q = $db->query("SELECT person FROM moffice WHERE position LIKE :pos ORDER BY from_date DESC, moffice_id", diff --git a/classes/Search/ParseArgs.php b/classes/Search/ParseArgs.php index 9e9755f616..0a561a4b38 100644 --- a/classes/Search/ParseArgs.php +++ b/classes/Search/ParseArgs.php @@ -78,12 +78,7 @@ private function parse_person_params() { # Searching from MP pages if ($searchspeaker = trim(get_http_var('person'))) { - $q = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup($searchspeaker); - $pids = array(); - foreach ($q as $row) { - $pids[$row['person_id']] = true; - } - $pids = array_keys($pids); + $pids = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup($searchspeaker); if (count($pids) > 0) { $searchstring .= ' speaker:' . join(' speaker:', $pids); } diff --git a/classes/Utility/Search.php b/classes/Utility/Search.php index e9870f15e5..38e36ca7ec 100644 --- a/classes/Utility/Search.php +++ b/classes/Utility/Search.php @@ -180,22 +180,30 @@ public static function searchMemberDbLookup($searchstring, $current_only=false) $db = new \ParlDB; $q = $db->query("SELECT person_id FROM person_names WHERE type='name' AND ($where)", $params); - return $q; + + # Check for redirects + $pids = array(); + foreach ($q as $row) { + $pid = $row['person_id']; + $redirect = $db->query("SELECT gid_to FROM gidredirect WHERE gid_from = :gid_from", + array(':gid_from' => "uk.org.publicwhip/person/$pid") + )->first(); + if ($redirect) { + $pid = str_replace('uk.org.publicwhip/person/', '', $redirect['gid_to']); + } + $pids[] = $pid; + } + + return array_unique($pids); } public static function searchMemberDbLookupWithNames($searchstring, $current_only=false) { - $q = self::searchMemberDbLookup($searchstring, $current_only); + $pids = self::searchMemberDbLookup($searchstring, $current_only); - if (!$q->rows()) { - return $q; + if (!count($pids)) { + return $pids; } - $person_ids = array(); - foreach ($q as $row) { - $pid = $row['person_id']; - $person_ids[$pid] = 1; - } - $pids = array_keys($person_ids); $pids_str = join(',', $pids); $where = ''; diff --git a/tests/SearchTest.php b/tests/SearchTest.php index 979cdaa617..5a476ca421 100644 --- a/tests/SearchTest.php +++ b/tests/SearchTest.php @@ -44,14 +44,14 @@ public function testSearchMemberDbLookupSingleName() // Test a single (first) name. $results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Joseph'); - $this->assertEquals(1, $results->rows()); - $this->assertEquals(2, $results->first()['person_id']); + $this->assertEquals(1, count($results)); + $this->assertEquals(2, $results[0]); // Test a single (last) name. $results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Bloggs'); - $this->assertEquals(1, $results->rows()); - $this->assertEquals(2, $results->first()['person_id']); + $this->assertEquals(1, cout($results)); + $this->assertEquals(2, $results[0]); } @@ -65,20 +65,20 @@ public function testSearchMemberDbLookupFullName() // Test a full name. $results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Mary Smith'); - $this->assertEquals(1, $results->rows()); - $this->assertEquals(3, $results->first()['person_id']); + $this->assertEquals(1, count($results)); + $this->assertEquals(3, $results[0]); // Test an inverse full name. $results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Smith Mary'); - $this->assertEquals(1, $results->rows()); - $this->assertEquals(3, $results->first()['person_id']); + $this->assertEquals(1, count($results)); + $this->assertEquals(3, $results[0]); // Test a name with title. $results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Mrs Smith'); - $this->assertEquals(1, $results->rows()); - $this->assertEquals(3, $results->first()['person_id']); + $this->assertEquals(1, count($results)); + $this->assertEquals(3, $results[0]); } diff --git a/www/includes/easyparliament/templates/html/alert/index.php b/www/includes/easyparliament/templates/html/alert/index.php index f44038c31d..6926a9531a 100644 --- a/www/includes/easyparliament/templates/html/alert/index.php +++ b/www/includes/easyparliament/templates/html/alert/index.php @@ -129,7 +129,7 @@ rows() > 0) || + (isset($members) && count($members)) || (isset($constituencies) && count($constituencies) > 0) || ($alertsearch) ) { @@ -139,7 +139,7 @@