Skip to content

Commit

Permalink
handle sections for alerts prettyCriteria
Browse files Browse the repository at this point in the history
Also split them out and include as key in alert object so we can use
them in alerts interface
  • Loading branch information
struan committed Oct 10, 2024
1 parent bdd0a6f commit 62c413b
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion classes/Utility/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
Expand Down

0 comments on commit 62c413b

Please sign in to comment.