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 @@ </button> <div id="accordion-content-<?= $index ?>" class="accordion-content" aria-hidden="true" role="img"> <div class="accordion-content-header"> - <form action="<?= $actionurl ?>" method="POST"> - <input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>"> - <div class="alert-controller-wrapper"> + <div class="alert-controller-wrapper"> + <form action="<?= $actionurl ?>" method="POST"> + <input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>"> <button class="button small display-none">Discard changes</button> <?php if ($alert['status'] == 'unconfirmed') { ?> <button type="submit" class="button small" name="action" value="Confirm"> @@ -320,9 +320,16 @@ <span><?= gettext('Delete alert') ?></span> <i aria-hidden="true" class="fi-trash"></i> </button> + </form> + <form action="/alert/new/" method="POST"> + <input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>"> + <button type="submit" class="button small" value="Edit"> + <span><?= gettext('Edit alert') ?></span> + <i aria-hidden="true" class="fi-page-edit"></i> + </button> <?php } ?> - </div> - </form> + </form> + </div> <dl class="alert-meta-info"> <?php if (array_key_exists("mentions", $alert)) { ?> <div class="content-header-item"> @@ -443,9 +450,9 @@ <h3 class="alert-page-subsection--heading"><?= gettext('Your MP') ?> ﹒ XXX</h3> <p class="alert-page-subsection--subtitle"><?= _htmlspecialchars($alert['criteria']) ?></p> - <form action="<?= $actionurl ?>" method="POST"> - <input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>"> - <div> + <div> + <form action="<?= $actionurl ?>" method="POST"> + <input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>"> <?php if ($alert['status'] == 'unconfirmed') { ?> <button type="submit" class="button small" name="action" value="Confirm"> <span><?= gettext('Confirm alert') ?></span> @@ -465,9 +472,16 @@ <span><?= gettext('Delete alert') ?></span> <i aria-hidden="true" class="fi-trash"></i> </button> + </form> + <form action="/alert/new/" method="POST"> + <input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>"> + <button type="submit" class="button small" value="Edit"> + <span><?= gettext('Edit alert') ?></span> + <i aria-hidden="true" class="fi-page-edit"></i> + </button> + </form> <?php } ?> - </div> - </form> + </div> <?php if (!in_array(implode('', $alert['spokenby']), $all_keywords)) { ?> <p class="alert-page-subsection--subtitle">Alert when <?= _htmlspecialchars(implode(', ', $alert['spokenby'])) ?> is <strong>mentioned</strong></p> @@ -489,9 +503,9 @@ <h3 class="alert-page-subsection--heading"><?= _htmlspecialchars(implode(', ', $alert['spokenby'])) ?></h3> <p class="alert-page-subsection--subtitle"><?= _htmlspecialchars($alert['criteria']) ?> - <form action="<?= $actionurl ?>" method="POST"> - <input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>"> - <div> + <div> + <form action="<?= $actionurl ?>" method="POST"> + <input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>"> <?php if ($alert['status'] == 'unconfirmed') { ?> <button type="submit" class="button small" name="action" value="Confirm"> <span><?= gettext('Confirm alert') ?></span> @@ -511,9 +525,16 @@ <span><?= gettext('Delete alert') ?></span> <i aria-hidden="true" class="fi-trash"></i> </button> + </form> + <form action="/alert/new/" method="POST"> + <input type="hidden" name="t" value="<?= _htmlspecialchars($alert['token']) ?>"> + <button type="submit" class="button small" value="Edit"> + <span><?= gettext('Edit alert') ?></span> + <i aria-hidden="true" class="fi-page-edit"></i> + </button> <?php } ?> - </div> - </form> + </form> + </div> <?php if (!in_array(implode('', $alert['spokenby']), $all_keywords)) { ?> <p class="alert-page-subsection--subtitle">Alert when <?= _htmlspecialchars(implode(', ', $alert['spokenby'])) ?> is <strong>mentioned</strong></p> diff --git a/www/includes/easyparliament/templates/html/alert/new.php b/www/includes/easyparliament/templates/html/alert/new.php index da2952d6ca..78ac69147a 100644 --- a/www/includes/easyparliament/templates/html/alert/new.php +++ b/www/includes/easyparliament/templates/html/alert/new.php @@ -130,7 +130,7 @@ <h1><?= gettext('Create Alert') ?></h1> <form action="" method="POST" id="create-alert-form"> - <input type="hidden" name="token" value="<?= _htmlspecialchars($token) ?>"> + <input type="hidden" name="t" value="<?= _htmlspecialchars($token) ?>"> <?php if (!$step or $step == "define") { ?> <input type="hidden" name="this_step" value="define">