Skip to content

Commit

Permalink
optionally return alert parts from prettify and add parts to alerts
Browse files Browse the repository at this point in the history
To make it a bit easier to pick out the different parts of an alert
update prettifyCritera to return the parts as a hash instead of turning
it into a string.
  • Loading branch information
struan committed Oct 9, 2024
1 parent 00e6ef6 commit f4a6752
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions classes/Utility/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static function forUser($email) {
$alerts = [];
foreach ($q as $row) {
$criteria = self::prettifyCriteria($row['criteria']);
$parts = self::prettifyCriteria($row['criteria'], true);
$token = $row['alert_id'] . '-' . $row['registrationtoken'];

$status = 'confirmed';
Expand All @@ -43,21 +44,28 @@ public static function forUser($email) {
$status = 'suspended';
}

$alerts[] = [
$alert = [
'token' => $token,
'status' => $status,
'criteria' => $criteria,
'raw' => $row['criteria'],
'keywords' => [],
'exclusions' => [],
];

$alert = array_merge($alert, $parts);

$alerts[] = $alert;
}

return $alerts;
}

public static function prettifyCriteria($alert_criteria) {
public static function prettifyCriteria($alert_criteria, $as_parts = false) {
$text = '';
if ($alert_criteria) {
$criteria = explode(' ', $alert_criteria);
$parts = [];
$words = [];
$spokenby = array_values(\MySociety\TheyWorkForYou\Utility\Search::speakerNamesForIDs($alert_criteria));

Expand All @@ -68,12 +76,19 @@ public static function prettifyCriteria($alert_criteria) {
}
if ($spokenby && count($words)) {
$text = implode(' or ', $spokenby) . ' mentions [' . implode(' ', $words) . ']';
$parts['spokenby'] = $spokenby;
$parts['words'] = $words;
} elseif (count($words)) {
$text = '[' . implode(' ', $words) . ']' . ' is mentioned';
$parts['words'] = $words;
} elseif ($spokenby) {
$text = implode(' or ', $spokenby) . " speaks";
$parts['spokenby'] = $spokenby;
}
}
if ($as_parts) {
return $parts;
}
return $text;
}

Expand Down

0 comments on commit f4a6752

Please sign in to comment.