tlm/index.php
2025-03-24 12:57:50 -04:00

148 lines
4.5 KiB
PHP

<?php
include "config.php";
include "helper.php";
$user = is_logged_in();
if ($user === false) {
include "boilerplate/head.php";
echo "<a href=\"login.php\">{$lang['account']['log_in']}</a>";
$q = "SELECT count(*) FROM users LIMIT 1;";
if ($signups !== false || ($signups === false && pg_fetch_array(pg_query($db, $q))["count"] !== 0)) {
echo " - <a href=\"signup.php\">{$lang['account']['sign_up']}</a>";
}
include "boilerplate/foot.php";
exit();
} else if ($_SERVER["REQUEST_METHOD"] === "POST") {
$intent = $_POST["intent"];
if ($intent === "create-item") {
$row_id = $_POST["row"];
$date = strtotime($_POST["date"]);
$description = $_POST["description"];
if (strlen($row_id) !== 64 || !preg_match("/[a-f0-9]{64}/", $row_id)) {
$err = lang["add"]["errors"]["invalid_id"];
} else if ($date === false) {
$err = lang["add"]["errors"]["date"];
} else if (strlen($description) === 0 || strlen($description) > 256) {
$err = lang["add"]["errors"]["description_length"];
} else {
pg_insert(
$db, "item", array(
"id" => generate_id(),
"row" => $row_id,
"description" => $description,
"date" => date("Y-m-d", $date)
)
);
}
}
} else if ($_GET["del"] && strlen($_GET["del"]) === 64 && preg_match("/[a-f0-9]{64}/", $_GET["del"])) {
pg_query($db, "DELETE FROM item WHERE id='{$_GET['del']}';");
}
include "boilerplate/head.php";
$select_options = "";
$todo_list = "";
$q = "SELECT * FROM section WHERE users='{$user['id']}' ORDER BY LOWER(name) ASC;";
$sections = pg_fetch_all(pg_query($db, $q));
foreach ($sections as $section) {
$q = "SELECT * FROM row WHERE section='{$section['id']}' ORDER BY LOWER(name) ASC;";
$rows = pg_fetch_all(pg_query($db, $q));
$show_subtitle = $section["show_subtitle"] === "t";
$enable_optgroup = $show_subtitle || count($rows) !== 1;
$todo_list .= "<h3>" . htmlspecialchars($section["name"]) . "</h3>";
if ($enable_optgroup) {
$select_options .= "<optgroup label=\"" . htmlspecialchars($section["name"]) . "\">";
}
$first = true;
foreach ($rows as $row) {
$select_options .= "<option value=\"{$row['id']}\">";
if ($first && !$show_subtitle) {
$select_options .= htmlspecialchars($section["name"]);
} else {
$select_options .= htmlspecialchars($row["name"]);
$todo_list .= "<strong>" . htmlspecialchars($row["name"]) . "</strong>";
}
$q = "SELECT * FROM item WHERE row='{$row['id']}' ORDER BY date ASC, LOWER(description) ASC;";
$items = pg_fetch_all(pg_query($db, $q));
$todo_list .= "<ul>";
if (count($items) === 0) {
$todo_list .= "<li><i>{$lang['list']['none']}</i></li>";
} else {
foreach ($items as $item) {
$color = "red";
$todo_list .= "<li>" . str_replace(
"%d", htmlspecialchars($item["description"]), str_replace(
"%c", "</span>", str_replace(
"%C", "<span class=\"$color\">", str_replace(
"%t", date("d M, Y", strtotime($item["date"])), $row["display_format"]
)
)
)
) . " <a class=\"plain\" href=\"index.php?del={$item['id']}\"><button tabindex=\"-1\">{$lang['list']['remove']}</button></a></li>";
}
}
$todo_list .= "</ul>";
$select_options .= "</option>";
$first = false;
}
if ($enable_optgroup) {
$select_options .= "</optgroup>";
}
}
?>
<div id="container">
<h2><?php echo $lang["add"]["title"]; ?></h2>
<form method="POST">
<input type="hidden" name="intent" value="create-item">
<table>
<tr>
<td class="right"><label for="row"><?php echo $lang["add"]["section"]; ?></label></td>
<td>
<select required id="row" name="row">
<?php echo $select_options; ?>
</select>
</td>
</tr>
<tr>
<td class="right"><label for="date"><?php echo $lang["add"]["date"]; ?></label></td>
<td><input type="date" name="date" id="date" required></td>
</tr>
<tr>
<td class="right"><label for="description"><?php echo $lang["add"]["description"]; ?></label></td>
<td><input autofocus maxlength="256" placeholder="<?php echo $lang["add"]["description_placeholder"]; ?>" name="description" id="description" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="<?php echo $lang["add"]["button"]; ?>"></td>
</tr>
</table>
</form>
<h2><?php echo $lang["list"]["title"]; ?></h2>
<?php echo $todo_list; ?>
</div>
<?php
include "boilerplate/foot.php";
?>