From db0494d05b7accb2481d736726729c1c72d65d5e Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Thu, 10 Oct 2024 11:44:50 +0100 Subject: [PATCH] handle sections for alerts prettyCriteria Also split them out and include as key in alert object so we can use them in alerts interface --- classes/Utility/Alert.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/classes/Utility/Alert.php b/classes/Utility/Alert.php index 1173bc6c11..6edfba202e 100644 --- a/classes/Utility/Alert.php +++ b/classes/Utility/Alert.php @@ -9,6 +9,16 @@ */ class Alert { + #XXX don't calculate this every time + private static function sectionToTitle($section) { + global $hansardmajors; + $section_map = []; + foreach ($hansardmajors as $major => $details) { + $section_map[$details["page_all"]] = $details["title"]; + } + + return $section_map[$section]; + } public static function detailsToCriteria($details) { $criteria = []; @@ -67,10 +77,15 @@ public static function prettifyCriteria($alert_criteria, $as_parts = false) { $criteria = explode(' ', $alert_criteria); $parts = []; $words = []; + $sections = []; + $sections_verbose = []; $spokenby = array_values(\MySociety\TheyWorkForYou\Utility\Search::speakerNamesForIDs($alert_criteria)); foreach ($criteria as $c) { - if (!preg_match('#^speaker:(\d+)#', $c, $m)) { + if (preg_match('#^section:(\w+)#', $c, $m)) { + $sections[] = $m[1]; + $sections_verbose[] = self::sectionToTitle($m[1]); + } elseif (!preg_match('#^speaker:(\d+)#', $c, $m)) { $words[] = $c; } } @@ -85,6 +100,12 @@ public static function prettifyCriteria($alert_criteria, $as_parts = false) { $text = implode(' or ', $spokenby) . " speaks"; $parts['spokenby'] = $spokenby; } + + if ($sections) { + $text = $text . " in " . implode(' or ', $sections_verbose); + $parts['sections'] = $sections; + $parts['sections_verbose'] = $sections_verbose; + } } if ($as_parts) { return $parts;