Skip to content

Commit

Permalink
optionally enable matching all terms in an alert
Browse files Browse the repository at this point in the history
Add checkbox to match all terms rather than any term as is the default
  • Loading branch information
struan committed Oct 28, 2024
1 parent 97dfb4c commit 617d3b6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
21 changes: 19 additions & 2 deletions classes/AlertView/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private function getBasicData() {
$this->data['addword'] = trim(get_http_var("addword"));
$this->data['this_step'] = trim(get_http_var("this_step"));
$this->data['shown_related'] = get_http_var('shown_related');
$this->data['match_all'] = get_http_var('match_all') == 'on';

if ($this->data['addword'] || $this->data['step']) {
$alert = $this->alert->check_token($this->data['token']);
Expand All @@ -159,6 +160,10 @@ private function getBasicData() {
$existing_section = $this->data['alert_parts']['sections'][0];
}

if ($this->data['alert_parts']['match_all']) {
$this->data['match_all'] = true;
}

$words = get_http_var('words', $this->data['alert_parts']['words'], true);

$this->data['words'] = [];
Expand Down Expand Up @@ -197,9 +202,13 @@ private function getBasicData() {

$this->data['search_section'] = trim(get_http_var("search_section", $existing_section));

$this->data['keyword'] = implode(' OR ', $this->data['words']);
$separator = ' OR ';
if ($this->data['match_all']) {
$separator = ' ';
}
$this->data['keyword'] = implode($separator, $this->data['words']);
if ($this->data['exclusions']) {
$this->data['keyword'] .= " -" . $this->data["exclusions"];
$this->data['keyword'] = '(' . $this->data['keyword'] . ') -' . $this->data["exclusions"];
}

$this->data['results'] = '';
Expand Down Expand Up @@ -483,6 +492,14 @@ protected function formatSearchMemberData() {
protected function setUserData() {
if (!isset($this->data['criteria'])) {
$criteria = $this->data['keyword'];
if (!$this->data['match_all']) {
$has_or = strpos($criteria, ' OR ') !== false;
$missing_braces = strpos($criteria, '(') === false;

if ($has_or && $missing_braces) {
$criteria = "($criteria)";
}
}
if ($this->data['search_section']) {
$criteria .= " section:" . $this->data['search_section'];
}
Expand Down
6 changes: 5 additions & 1 deletion classes/Utility/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ public static function forUser($email) {

public static function prettifyCriteria($alert_criteria, $as_parts = false) {
$text = '';
$parts = ['words' => [], 'sections' => [], 'exclusions' => []];
$parts = ['words' => [], 'sections' => [], 'exclusions' => [], 'match_all' => true];
if ($alert_criteria) {
# 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);
Expand Down
13 changes: 12 additions & 1 deletion www/includes/easyparliament/templates/html/alert/_alert_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
</button>
</div>

<div class="alert-page-subsection">
<label for="match_all"><?= gettext('Only alert if all words present, default is if any word is present') ?></label>
<input type="checkbox" id="match_all" name="match_all"<?= $match_all ? ' checked' : ''?>>
</div>

<div class="alert-page-subsection">
<label for="exclusions"><?= gettext('Is there anything you would not like to recieve alerts about? (optional)') ?></label>
<input type="text" id="exclusions" name="exclusions" aria-required="true" value="<?= _htmlspecialchars($exclusions) ?>" placeholder="Eg. 'Freedom of Information', 'FOI'">
Expand Down Expand Up @@ -117,6 +122,7 @@
<input type="hidden" name="representative" value="<?= _htmlspecialchars($representative) ?>">
<input type="hidden" name="search_section" value="<?= _htmlspecialchars($search_section) ?>">
<input type="hidden" name="email" id="email" value="<?= _htmlentities($email) ?>">
<input type="hidden" name="match_all" value="<?= $match_all ? 'on' : ''?>">
<div class="alert-step" id="step2" role="region" aria-labelledby="step2-header">
<h2 id="step2-header"><?= gettext('Adding some extras') ?></h2>
<div class="keyword-list alert-page-subsection">
Expand Down Expand Up @@ -182,12 +188,17 @@
<input type="hidden" name="representative" value="<?= _htmlspecialchars($representative) ?>">
<input type="hidden" name="search_section" value="<?= _htmlspecialchars($search_section) ?>">
<input type="hidden" name="email" id="email" value="<?= _htmlentities($email) ?>">
<input type="hidden" name="match_all" value="<?= $match_all ? 'on' : ''?>">
<!-- Step 4 (Review) -->
<div class="alert-step" id="step3" role="region" aria-labelledby="step3-header">
<h2 id="step3-header"><?= gettext('Review Your Alert') ?></h2>

<div class="keyword-list alert-page-subsection">
<h3 class="heading-with-bold-word"><?= gettext('You will get an alert if any of these words are in a speech') ?>:</h3>
<?php if ($match_all) { ?>
<h3 class="heading-with-bold-word"><?= gettext('You will get an alert if all of these words are in a speech') ?>:</h3>
<?php } else { ?>
<h3 class="heading-with-bold-word"><?= gettext('You will get an alert if any of these words are in a speech') ?>:</h3>
<?php } ?>
<ul>
<?php foreach ($keywords as $word) { ?>
<li class="label label--primary-light"><?= _htmlspecialchars($word) ?>
Expand Down

0 comments on commit 617d3b6

Please sign in to comment.