From 73d06c54a9057fbf746e7425c855691e9ea56924 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Mon, 9 Dec 2024 16:54:13 +0000 Subject: [PATCH] enable turning off votes in alerts This also tidies up editing MP alerts so it's mostly buttons rather than having to go through a form. --- classes/AlertView/Standard.php | 24 +++++++- classes/Utility/Alert.php | 11 +++- www/includes/easyparliament/alert.php | 2 +- .../templates/html/alert/_alert_form.php | 4 ++ .../templates/html/alert/_list_accordian.php | 56 ++++++++++++------- .../templates/html/alert/_mp_alert_form.php | 7 ++- .../templates/html/alert/_own_mp_alerts.php | 24 +++++++- .../templates/html/alert/index.php | 1 + 8 files changed, 103 insertions(+), 26 deletions(-) diff --git a/classes/AlertView/Standard.php b/classes/AlertView/Standard.php index 6c4feaaec9..6c6d711498 100644 --- a/classes/AlertView/Standard.php +++ b/classes/AlertView/Standard.php @@ -158,18 +158,24 @@ private function getBasicData() { $this->data['pid'] = trim(get_http_var("pid")); $this->data['pc'] = get_http_var('pc'); $this->data['submitted'] = get_http_var('submitted') || $this->data['pid'] || $this->data['keyword'] || $this->data['step']; + $this->data['ignore_speaker_votes'] = get_http_var('ignore_speaker_votes'); if ($this->data['addword'] || $this->data['step']) { $alert = $this->alert->check_token($this->data['token']); $criteria = ''; + $alert_ignore_speaker_votes = 0; if ($alert) { $criteria = $alert['criteria']; + $alert_ignore_speaker_votes = $alert['ignore_speaker_votes']; } + $ignore_speaker_votes = get_http_var('ignore_speaker_votes', $alert_ignore_speaker_votes); + $this->data['ignore_speaker_votes'] = ($ignore_speaker_votes == 'on' || $ignore_speaker_votes == 1); + $this->data['alert'] = $alert; - $this->data['alert_parts'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($criteria, $alert['ignore_speaker_votes'], true); + $this->data['alert_parts'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($criteria, $alert_ignore_speaker_votes, true); $existing_rep = ''; if (isset($this->data['alert_parts']['spokenby'])) { @@ -237,6 +243,22 @@ private function getBasicData() { $this->data['results'] = ''; $this->getSearchSections(); + } else if ($this->data['mp_step'] == 'mp_alert') { + $alert = $this->alert->check_token($this->data['token']); + if ($alert) { + $ignore_speaker_votes = get_http_var('ignore_speaker_votes', $alert['ignore_speaker_votes']); + $this->data['ignore_speaker_votes'] = ($ignore_speaker_votes == 'on' || $ignore_speaker_votes == 1); + + $existing_rep = ''; + if (isset($alert['criteria'])) { + $alert_parts = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($alert['criteria'], $alert['ignore_speaker_votes'], true); + $existing_rep = $alert_parts['spokenby'][0]; + $this->data['pid'] = $alert_parts['pid']; + } + $this->data['keyword'] = get_http_var('mp_search', $existing_rep); + } else { + $this->data['ignore_speaker_votes'] = get_http_var('ignore_speaker_votes'); + } } # XXX probably should do something here if $alertsearch is set $this->data['sign'] = get_http_var('sign'); diff --git a/classes/Utility/Alert.php b/classes/Utility/Alert.php index 33a518912e..a4fdcdfc5e 100644 --- a/classes/Utility/Alert.php +++ b/classes/Utility/Alert.php @@ -74,6 +74,7 @@ public static function forUser($email) { 'status' => $status, 'criteria' => $criteria, 'raw' => $row['criteria'], + 'ignore_speaker_votes' => $row['ignore_speaker_votes'], 'keywords' => [], 'exclusions' => [], 'sections' => [], @@ -89,7 +90,7 @@ public static function forUser($email) { public static function prettifyCriteria($alert_criteria, $ignore_speaker_votes = false, $as_parts = false) { $text = ''; - $parts = ['words' => [], 'sections' => [], 'exclusions' => [], 'match_all' => true]; + $parts = ['words' => [], 'sections' => [], 'exclusions' => [], 'match_all' => true, 'pid' => false]; if ($alert_criteria) { # check for phrases if (strpos($alert_criteria, ' OR ') !== false) { @@ -116,7 +117,13 @@ public static function prettifyCriteria($alert_criteria, $ignore_speaker_votes = $exclusions = []; $sections = []; $sections_verbose = []; - $spokenby = array_values(\MySociety\TheyWorkForYou\Utility\Search::speakerNamesForIDs($alert_criteria)); + $speaker_parts = \MySociety\TheyWorkForYou\Utility\Search::speakerNamesForIDs($alert_criteria); + $pids = array_keys($speaker_parts); + $spokenby = array_values($speaker_parts); + + if (count($pids) == 1) { + $parts['pid'] = $pids[0]; + } foreach ($criteria as $c) { if (preg_match('#^section:(\w+)#', $c, $m)) { diff --git a/www/includes/easyparliament/alert.php b/www/includes/easyparliament/alert.php index 72c29f3777..eaf6e3314e 100644 --- a/www/includes/easyparliament/alert.php +++ b/www/includes/easyparliament/alert.php @@ -167,7 +167,7 @@ public function add($details, $confirmation_email = false, $instantly_confirm = ':criteria' => $criteria, ])->first(); if ($q) { - if ($q['deleted']) { + if ($q['deleted'] || $q['ignore_speaker_votes'] != $ignore_speaker_votes) { $this->db->query("UPDATE alerts SET deleted=0, ignore_speaker_votes=:ignore_speaker_votes WHERE email = :email AND criteria = :criteria diff --git a/www/includes/easyparliament/templates/html/alert/_alert_form.php b/www/includes/easyparliament/templates/html/alert/_alert_form.php index 91a39f82a6..a3789351ec 100644 --- a/www/includes/easyparliament/templates/html/alert/_alert_form.php +++ b/www/includes/easyparliament/templates/html/alert/_alert_form.php @@ -105,6 +105,10 @@

+
+ > + +
diff --git a/www/includes/easyparliament/templates/html/alert/_list_accordian.php b/www/includes/easyparliament/templates/html/alert/_list_accordian.php index fb1065f292..f2e16b854e 100644 --- a/www/includes/easyparliament/templates/html/alert/_list_accordian.php +++ b/www/includes/easyparliament/templates/html/alert/_list_accordian.php @@ -186,27 +186,45 @@ - + - - + + +
- - - + + + + + + + + + + + + + +
@@ -214,7 +232,7 @@

Alert when is mentioned

- +
+ + - + + + + + + + + + + +
@@ -40,7 +59,8 @@

Alert when is mentioned

- + +