From eec905b6b2d5cf707aa092d1ddb390edfa076caf Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Mon, 21 Oct 2024 17:35:54 +0100 Subject: [PATCH] enable alerts to be edited add an edit button to the alert page and update the new alert form to load and save an existing alert. --- classes/AlertView/NewAlert.php | 79 ++++++++++++------- classes/Utility/Alert.php | 21 ++++- www/includes/easyparliament/alert.php | 21 +++++ .../templates/html/alert/index.php | 51 ++++++++---- .../templates/html/alert/new.php | 2 +- 5 files changed, 126 insertions(+), 48 deletions(-) diff --git a/classes/AlertView/NewAlert.php b/classes/AlertView/NewAlert.php index 8496a0516a..321ec39cff 100644 --- a/classes/AlertView/NewAlert.php +++ b/classes/AlertView/NewAlert.php @@ -13,14 +13,10 @@ public function display() { global $this_page; $this_page = "alertnew"; - $this->processAction(); $this->getBasicData(); $this->checkInput(); $this->searchForConstituenciesAndMembers(); - - if ($this->data['step'] == 'confirm' && !sizeof($this->data['errors']) && ($this->data['keyword'] || $this->data['pid'])) { - $this->addAlert(); - } + $this->processAction(); $this->formatSearchTerms(); $this->checkForCommonMistakes(); @@ -31,10 +27,21 @@ public function display() { } private function processAction() { - $token = get_http_var('t'); - $alert = $this->alert->check_token($token); - $this->data['results'] = false; + if ($this->data['step'] == 'confirm') { + $success = true; + if ($this->data['alert']) { + $success = $this->updateAlert($this->data['alert']['id'], $this->data); + } else { + $success = $this->addAlert(); + } + if ($success) { + $this->data['results'] = 'alert-confirmed'; + } else { + $this->data['results'] = 'alert-fail'; + } + } + //$this->data['results'] = false; if ($action = get_http_var('action')) { $success = true; if ($action == 'Confirm') { @@ -43,33 +50,16 @@ private function processAction() { $this->data['results'] = 'alert-confirmed'; $this->data['criteria'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->alert->criteria); } - } elseif ($action == 'Suspend') { - $success = $this->suspendAlert($token); - if ($success) { - $this->data['results'] = 'alert-suspended'; - } - } elseif ($action == 'Resume') { - $success = $this->resumeAlert($token); - if ($success) { - $this->data['results'] = 'alert-resumed'; - } } elseif ($action == 'Delete') { $success = $this->deleteAlert($token); if ($success) { $this->data['results'] = 'alert-deleted'; } - } elseif ($action == 'Delete All') { - $success = $this->deleteAllAlerts($token); - if ($success) { - $this->data['results'] = 'all-alerts-deleted'; - } } if (!$success) { $this->data['results'] = 'alert-fail'; } } - - $this->data['alert'] = $alert; } protected function searchForConstituenciesAndMembers() { @@ -83,6 +73,8 @@ protected function searchForConstituenciesAndMembers() { } else { $this->data["errors"] = ["representative" => "No matching representative found"]; } + } else { + $this->data['pid'] = $this->data['members'][0]['person_id']; } } else { $this->data['members'] = []; @@ -103,6 +95,28 @@ private function wrap_phrase_in_quotes($phrase) { private function getBasicData() { global $this_page; + $this->data['token'] = get_http_var('t'); + $alert = $this->alert->check_token($this->data['token']); + + $criteria = ''; + if ($alert) { + $criteria = $alert['criteria']; + } + + $this->data['alert'] = $alert; + + $this->data['alert_parts'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($criteria, true); + + $existing_rep = ''; + if (isset($this->data['alert_parts']['spokenby'])) { + $existing_rep = $this->data['alert_parts']['spokenby'][0]; + } + + $existing_section = ''; + if (isset($this->data['alert_parts']['sections'])) { + $existing_section = $this->data['alert_parts']['sections'][0]; + } + if ($this->user->loggedin()) { $this->data['email'] = $this->user->email(); $this->data['email_verified'] = true; @@ -113,25 +127,25 @@ private function getBasicData() { $this->data["email"] = trim(get_http_var("email")); $this->data['email_verified'] = false; } - $words = get_http_var('words', [], true); + $words = get_http_var('words', $this->data['alert_parts']['words'], true); $this->data['words'] = []; $this->data['keywords'] = $words; foreach ($words as $word) { $this->data['words'][] = $this->wrap_phrase_in_quotes($word); } + $this->data['exclusions'] = trim(get_http_var("exclusions", implode('', $this->data['alert_parts']['exclusions']))); + $this->data['representative'] = trim(get_http_var("representative", $existing_rep)); $this->data['addword'] = trim(get_http_var("addword")); $this->data['step'] = trim(get_http_var("step")); - $this->data['exclusions'] = trim(get_http_var("exclusions")); - $this->data['representative'] = trim(get_http_var("representative")); - $this->data['search_section'] = trim(get_http_var("search_section")); + $this->data['search_section'] = trim(get_http_var("search_section", $existing_section)); $this->data['pid'] = trim(get_http_var("pid")); - $this->data['token'] = get_http_var('t'); $this->data['alertsearch'] = get_http_var('alertsearch'); $this->data['pc'] = get_http_var('pc'); $this->data['message'] = ''; + $this->data['results'] = ''; $this->data['keyword'] = implode(' ', $this->data['words']); if ($this->data['exclusions']) { @@ -180,6 +194,11 @@ private function getSearchSections() { } } + protected function updateAlert($token) { + $success = $this->alert->update($token, $this->data); + return $success; + } + protected function addAlert() { $external_auth = auth_verify_with_shared_secret($this->data['email'], OPTION_AUTH_SHARED_SECRET, get_http_var('sign')); if ($external_auth) { diff --git a/classes/Utility/Alert.php b/classes/Utility/Alert.php index 79f6d42f7d..77fb1a16f0 100644 --- a/classes/Utility/Alert.php +++ b/classes/Utility/Alert.php @@ -74,10 +74,23 @@ public static function forUser($email) { public static function prettifyCriteria($alert_criteria, $as_parts = false) { $text = ''; + $parts = ['words' => [], 'sections' => [], 'exclusions' => []]; if ($alert_criteria) { - $criteria = explode(' ', $alert_criteria); - $parts = []; + # check for phrases + 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 = 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)); @@ -86,6 +99,8 @@ public static function prettifyCriteria($alert_criteria, $as_parts = false) { 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; } @@ -107,6 +122,8 @@ public static function prettifyCriteria($alert_criteria, $as_parts = false) { $parts['sections'] = $sections; $parts['sections_verbose'] = $sections_verbose; } + + $parts['exclusions'] = $exclusions; } if ($as_parts) { return $parts; diff --git a/www/includes/easyparliament/alert.php b/www/includes/easyparliament/alert.php index 66493d36e8..d32680afbf 100644 --- a/www/includes/easyparliament/alert.php +++ b/www/includes/easyparliament/alert.php @@ -104,6 +104,27 @@ public function fetch($confirmed, $deleted) { return $data; } + public function update($id, $details) { + $criteria = \MySociety\TheyWorkForYou\Utility\Alert::detailsToCriteria($details); + + $q = $this->db->query("SELECT * FROM alerts + WHERE alert_id = :id", [ + ':id' => $id, + ])->first(); + if ($q) { + $q = $this->db->query("UPDATE alerts SET deleted = 0, criteria = :criteria, confirmed = 1 + WHERE alert_id = :id", [ + ":criteria" => $criteria, + ":id" => $id, + ]); + + if ($q->success()) { + return 1; + } + } + return -1; + } + public function add($details, $confirmation_email = false, $instantly_confirm = true) { // Adds a new alert's info into the database. diff --git a/www/includes/easyparliament/templates/html/alert/index.php b/www/includes/easyparliament/templates/html/alert/index.php index b8b5baa65a..0b6682b651 100644 --- a/www/includes/easyparliament/templates/html/alert/index.php +++ b/www/includes/easyparliament/templates/html/alert/index.php @@ -297,9 +297,9 @@