BootstrapSQLiteBlog/pages/post.php

183 lines
5.7 KiB
PHP

<?php
global $c;
UserAuth::require("can_post");
$varUser = UserAuth::getUser();
$strId = Request::getArg(0);
$strContent = "";
$strLocation = Request::getParam("to") ?? "";
$strVisibility = "";
$strVerb = "Create";
if (strlen($strId) > 0)
{
$strVerb = "Edit";
$varRows = $c->query("SELECT * from posts where id = ?", $strId);
if (count($varRows) !== 1)
{
BootstrapRender::message("Zero or more than one row returned", "danger");
Respond::redirect("/post");
}
$varRow = $varRows[0];
$strContent = $varRow["content"];
$strLocation = $varRow["location"];
$strVisibility = $varRow["visibility"];
}
if (Request::posts("location", "content", "visibility"))
{
$strLocation = Request::getPosted("location");
$strContent = Request::getPosted("content");
$strVisibility = Request::getPosted("visibility");
if ($strId == null || strlen($strId) < 1)
{
$c->query(
"INSERT into posts (username, content, location, visibility)
values (?, ?, ?, ?)",
$varUser["username"],
$strContent,
$strLocation,
$strVisibility);
$strId = $c->query("get_last_post.sql")[0]["id"];
}
if (strlen($strContent) < 1)
{
$c->query("DELETE from posts where id = ?", $strId);
BootstrapRender::message("Post deleted successfully.", "success");
Respond::redirect("/post");
}
$c->query(
"UPDATE posts
set
content = ?,
location = ?,
visibility = ?,
updated = current_timestamp
where
id = ?",
$strContent,
$strLocation,
$strVisibility,
$strId);
BootstrapRender::message("Post saved.", "success");
Respond::redirect("/post/{$strId}");
}
?>
<style>
textarea {
font-family: monospace;
}
</style>
<form method="post">
<div class="navbar navbar-expand bg-body-tertiary d-flex px-3 sticky-top">
<div class="container justify-content-between">
<div class="navbar-nav d-inline-flex align-items-center">
<span class="navbar-brand"><?= $strVerb; ?> Post</span>
</div>
<div class="navbar-nav d-inline-flex">
</div>
</div>
</div>
<div class="container my-5">
<div class="row">
<div class="col-lg-3">
<?php BootstrapRender::message(); ?>
</div>
<div class="col-lg-12">
<div class="mb-3">
<label class="form-label">Content</label>
<textarea
class="form-control"
name="content"
placeholder="Enter markdown content here..."
oninput="fnResize(this);"
><?= $strContent; ?></textarea>
<small class="text-muted">Test</small>
</div>
</div>
<div class="col-lg-3">
<div class="mb-3">
<label class="form-label">Location</label>
<input
type="text"
class="form-control"
name="location"
placeholder="/"
value="<?= $strLocation; ?>"
fv-regex="^\/"
fv-warning="Location must start with a forward slash" />
<small class="text-muted">e.g. /home or /info</small>
</div>
</div>
<div class="col-lg-3">
<div class="mb-3">
<label class="form-label">Visible To</label>
<input
type="text"
class="form-control"
name="visibility"
placeholder="everyone"
value="<?= $strVisibility; ?>"
fv-regex="^(|everyone|users|admins)$"
fv-warning="Visibility must be empty, everyone, users, or admins" />
<small class="text-muted">e.g. everyone, users, admins</small>
</div>
</div>
<div class="col-lg-3">
<div class="mb-3">
<label class="form-label">Actions</label>
<div>
<?php if ($strId == null || strlen($strId) < 1): ?>
<a class="btn btn-primary" onclick="fnSave();"><i class="fa fa-fw fa-paper-plane"></i> Submit</a>
<?php else: ?>
<a class="btn btn-success" onclick="fnSave();"><i class="fa fa-fw fa-save"></i> Save</a>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
</form>
<script>
$(function() {
fnSave = function() {
FormValidator.onError = function(str)
{
BootstrapRender.message(str, "danger");
};
FormValidator.validate(function() {
$("form").first().submit();
});
};
fnResize = function(x) {
x.style.minHeight = "2in";
x.style.height = "auto";
x.style.height = x.scrollHeight + "px";
};
fnResize($("textarea").first()[0]);
});
</script>