Skip to content

Commit

Permalink
fixup! optionally return alert parts from prettify and add parts to a…
Browse files Browse the repository at this point in the history
…lerts
  • Loading branch information
struan committed Dec 10, 2024
1 parent fc1ddf2 commit db420b4
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions classes/Utility/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@
*/

class Alert {
public static function sectionToTitle($section) {
$section_map = [
"uk" => gettext('All UK'),
"debates" => gettext('House of Commons debates'),
"whalls" => gettext('Westminster Hall debates'),
"lords" => gettext('House of Lords debates'),
"wrans" => gettext('Written answers'),
"wms" => gettext('Written ministerial statements'),
"standing" => gettext('Bill Committees'),
"future" => gettext('Future Business'),
"ni" => gettext('Northern Ireland Assembly Debates'),
"scotland" => gettext('All Scotland'),
"sp" => gettext('Scottish Parliament Debates'),
"spwrans" => gettext('Scottish Parliament Written answers'),
"wales" => gettext('Welsh parliament record'),
"lmqs" => gettext('Questions to the Mayor of London'),
];

return $section_map[$section];
}
public static function detailsToCriteria($details) {
$criteria = [];

Expand All @@ -20,6 +40,10 @@ public static function detailsToCriteria($details) {
$criteria[] = 'speaker:' . $details['pid'];
}

if (!empty($details['search_section'])) {
$criteria[] = 'section:' . $details['search_section'];
}

$criteria = join(' ', $criteria);
return $criteria;
}
Expand Down Expand Up @@ -51,6 +75,7 @@ public static function forUser($email) {
'raw' => $row['criteria'],
'keywords' => [],
'exclusions' => [],
'sections' => [],
];

$alert = array_merge($alert, $parts);
Expand All @@ -63,14 +88,42 @@ public static function forUser($email) {

public static function prettifyCriteria($alert_criteria, $as_parts = false) {
$text = '';
$parts = ['words' => [], 'sections' => [], 'exclusions' => [], 'match_all' => true];
if ($alert_criteria) {
$criteria = explode(' ', $alert_criteria);
$parts = [];
# check for phrases
if (strpos($alert_criteria, ' OR ') !== false) {
$parts['match_all'] = false;
}
$alert_criteria = str_replace(' OR ', ' ', $alert_criteria);
$alert_criteria = str_replace(['(', ')'], '', $alert_criteria);
if (strpos($alert_criteria, '"') !== false) {
# match phrases
preg_match_all('/"([^"]*)"/', $alert_criteria, $phrases);
# and then remove them from the criteria
$alert_criteria = trim(preg_replace('/ +/', ' ', str_replace($phrases[0], "", $alert_criteria)));

# and then create an array with the words and phrases
$criteria = [];
if ( $alert_criteria != "") {
$criteria = explode(' ', $alert_criteria);
}
$criteria = array_merge($criteria, $phrases[1]);
} else {
$criteria = explode(' ', $alert_criteria);
}
$words = [];
$exclusions = [];
$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 (strpos($c, '-') === 0) {
$exclusions[] = str_replace('-', '', $c);
} elseif (!preg_match('#^speaker:(\d+)#', $c, $m)) {
$words[] = $c;
}
}
Expand All @@ -85,6 +138,14 @@ 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;
}

$parts['exclusions'] = $exclusions;
}
if ($as_parts) {
return $parts;
Expand Down

0 comments on commit db420b4

Please sign in to comment.