Skip to content

Commit

Permalink
enable alerts to be edited
Browse files Browse the repository at this point in the history
add an edit button to the alert page and update the new alert form to
load and save an existing alert.
  • Loading branch information
struan committed Oct 21, 2024
1 parent 35ca246 commit eec905b
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 48 deletions.
79 changes: 49 additions & 30 deletions classes/AlertView/NewAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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') {
Expand All @@ -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() {
Expand All @@ -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'] = [];
Expand All @@ -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;
Expand All @@ -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']) {
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 19 additions & 2 deletions classes/Utility/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions www/includes/easyparliament/alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 36 additions & 15 deletions www/includes/easyparliament/templates/html/alert/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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">
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion www/includes/easyparliament/templates/html/alert/new.php
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit eec905b

Please sign in to comment.