Repository navigation
Expand file tree
/
Copy pathapi_alert.php
More file actions
43 lines (42 loc) · 1.63 KB
/
Copy pathapi_alert.php
File metadata and controls
43 lines (42 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
// Add or delete reorder alerts. Form-encoded POST; redirects back to the
// Order Report (reports/order_report/).
require_once __DIR__ . '/common.php';
require_once __DIR__ . '/auth.php';
requireLogin();
$db = getDB();
$action = $_POST['action'] ?? '';
if ($action === 'add') {
$name = trim((string)($_POST['generic_name'] ?? ''));
$lt = max(1, (int)($_POST['lead_time_days'] ?? 0));
$email = !empty($_POST['email']) ? 1 : 0;
// The Ignore Unknown Items placeholder never gets an Order Report row, so an
// alert on it could never fire anyway (op_report_alerts skips alerts with no
// row). Refuse it outright rather than storing a reminder that sits enabled
// and silent forever. The Order Report's own picker can't offer it — this
// covers a stale form or a hand-posted request.
if ($name === UNIDENTIFIED_NAME) $name = '';
if ($name !== '' && $lt > 0) {
$ins = $db->prepare(
"INSERT INTO alerts (generic_name, lead_time_days, enabled, email_enabled) VALUES (?, ?, 1, ?)"
);
$ins->execute([$name, $lt, $email]);
}
}
if ($action === 'set_email') {
// Toggle whether this alert's reorder reminder is emailed by the cron job.
$id = (int)($_POST['id'] ?? 0);
$email = !empty($_POST['email']) ? 1 : 0;
if ($id) {
$u = $db->prepare("UPDATE alerts SET email_enabled = ? WHERE id = ?");
$u->execute([$email, $id]);
}
}
if ($action === 'delete') {
$id = (int)($_POST['id'] ?? 0);
if ($id) {
$d = $db->prepare("DELETE FROM alerts WHERE id = ?");
$d->execute([$id]);
}
}
header('Location: reports/order_report/');