Article editor supports adress editing

This commit is contained in:
Zdenek Borovec 2024-07-20 16:28:02 +02:00
parent 47de0245d8
commit c3ecf326c7

View file

@ -68,14 +68,16 @@ function remove_blogpost_tags($conn, $blogpost_id) {
/** /**
* Publish a new blogpost and add the specified tags to it.. * Publish a new blogpost and add the specified tags to it..
* @param $conn Active Mysql connection. * @param $conn Active Mysql connection.
* @param $title Title for the blogpost. * @param $blogpost_addr Human-readable address of the edited blogpost.
* @param $tagStr String with all the tags for the blogpost * @param $title Title for the blogpost.
* (space-separated). * @param $tagStr String with all the tags for the blogpost
* @param $abstract Abstract for the article. * (space-separated).
* @param $content Content of the article. * @param $abstract Abstract for the article.
* @param $content Content of the article.
*/ */
function publish_blogpost($conn, $title, $tagStr, $abstract, $content) { function publish_blogpost($conn, $blogpost_addr, $title, $tagStr, $abstract,
$content) {
// Get an ID for the blogpost // Get an ID for the blogpost
$stmt = $conn->prepare("SELECT UUID()"); $stmt = $conn->prepare("SELECT UUID()");
$stmt->execute(); $stmt->execute();
@ -83,9 +85,11 @@ function publish_blogpost($conn, $title, $tagStr, $abstract, $content) {
$blogpost_id = $result["UUID()"]; $blogpost_id = $result["UUID()"];
// Prepare, bind and execute the insert statement // Prepare, bind and execute the insert statement
$stmt = $conn->prepare("INSERT INTO blogposts (blogpost_id, title, abstract, $stmt = $conn->prepare("INSERT INTO blogposts (blogpost_id,
content) VALUES (:blogpost_id, :title, :abstract, :content);"); readable_address, title, abstract, content) VALUES
(:blogpost_id, :address, :title, :abstract, :content);");
$stmt->bindParam(":blogpost_id", $blogpost_id); $stmt->bindParam(":blogpost_id", $blogpost_id);
$stmt->bindParam(":address", $blogpost_addr);
$stmt->bindParam(":title", $title); $stmt->bindParam(":title", $title);
$stmt->bindParam(":abstract", $abstract); $stmt->bindParam(":abstract", $abstract);
$stmt->bindParam(":content", $content); $stmt->bindParam(":content", $content);
@ -98,20 +102,22 @@ function publish_blogpost($conn, $title, $tagStr, $abstract, $content) {
/** /**
* Update the blogpost content, title, abstract and date edited. * Update the blogpost content, title, abstract and date edited.
* Then update the tags. * Then update the tags.
* @param $conn Active Mysql connection. * @param $conn Active Mysql connection.
* @param $blogpost_id GUID of the edited blogpost. * @param $blogpost_id GUID of the edited blogpost.
* @param $title Title for the blogpost. * @param $blogpost_addr Human-readable address of the edited blogpost.
* @param $tagStr String with all the tags for the blogpost * @param $title Title for the blogpost.
* (space-separated). * @param $tagStr String with all the tags for the blogpost
* @param $abstract Abstract for the article. * (space-separated).
* @param $content Content of the article. * @param $abstract Abstract for the article.
* @param $content Content of the article.
*/ */
function update_blogpost($conn, $blogpost_id, $title, $tagStr, $abstract, function update_blogpost($conn, $blogpost_id, $blogpost_addr, $title,
$content) { $tagStr, $abstract, $content) {
// Prepare, bind and execute the update statement // Prepare, bind and execute the update statement
$stmt = $conn->prepare("UPDATE blogposts SET title = :title, $stmt = $conn->prepare("UPDATE blogposts SET readable_address = :address,
abstract = :abstract, content = :content, date_edited = DEFAULT title = :title, abstract = :abstract, content = :content,
WHERE blogpost_id = :blogpost_id;"); date_edited = DEFAULT WHERE blogpost_id = :blogpost_id;");
$stmt->bindParam(":address", $blogpost_addr);
$stmt->bindParam(":title", $title); $stmt->bindParam(":title", $title);
$stmt->bindParam(":abstract", $abstract); $stmt->bindParam(":abstract", $abstract);
$stmt->bindParam(":content", $content); $stmt->bindParam(":content", $content);
@ -140,16 +146,24 @@ if(isset($_POST["submit"])) {
// over the content here and only trusted users should have access // over the content here and only trusted users should have access
// to this section // to this section
$title = $_POST["blogpost_title"]; $title = $_POST["blogpost_title"];
$address = $_POST["blogpost_address"];
$tagsStr = $_POST["blogpost_tags"]; $tagsStr = $_POST["blogpost_tags"];
$abstract = $_POST["article_abstract"]; $abstract = $_POST["article_abstract"];
$content = $_POST["article_content"]; $content = $_POST["article_content"];
// If adress is empty, set it to null
if(strcmp($address, "") == 0) {
$address = null;
}
if($_POST["blogpost_id"]) { if($_POST["blogpost_id"]) {
$blogpostId = $_POST["blogpost_id"]; $blogpostId = $_POST["blogpost_id"];
update_blogpost($conn, $blogpostId, $title, $tagsStr, $abstract, $content); update_blogpost($conn, $blogpostId, $address, $title, $tagsStr,
$abstract, $content);
} }
else { else {
publish_blogpost($conn, $title, $tagsStr, $abstract, $content); publish_blogpost($conn, $address, $title, $tagsStr, $abstract,
$content);
} }
header("Location: "."http://www.zdenekborovec-dev.cz/blog"); header("Location: "."http://www.zdenekborovec-dev.cz/blog");
@ -159,7 +173,7 @@ if(isset($_GET["guid"])) {
$blogId = sanitize_input($_GET["guid"]); $blogId = sanitize_input($_GET["guid"]);
// select article title, abstract and content from the database // select article title, abstract and content from the database
$stmt = $conn->prepare("SELECT title, abstract, content $stmt = $conn->prepare("SELECT readable_address, title, abstract, content
FROM blogposts WHERE blogpost_id = :blogpost_id"); FROM blogposts WHERE blogpost_id = :blogpost_id");
$stmt->bindParam(":blogpost_id", $blogId); $stmt->bindParam(":blogpost_id", $blogId);
$stmt->execute(); $stmt->execute();
@ -169,6 +183,7 @@ if(isset($_GET["guid"])) {
// Set prefill values for the form // Set prefill values for the form
$blogId_prefill = $blogId; $blogId_prefill = $blogId;
$title_prefill = $result["title"]; $title_prefill = $result["title"];
$address_prefill = $result["readable_address"];
$abstract_prefill = $result["abstract"]; $abstract_prefill = $result["abstract"];
$content_prefill = $result["content"]; $content_prefill = $result["content"];
@ -194,15 +209,30 @@ printf("
<form method=\"post\" action=\"\"> <form method=\"post\" action=\"\">
<input type=\"hidden\" name=\"blogpost_id\" value=\"%s\"> <input type=\"hidden\" name=\"blogpost_id\" value=\"%s\">
<label for=\"blogpost_title\">Post title:</label> <table class=\"noborder-table\">
<input type=\"text\" name=\"blogpost_title\" value=\"%s\"><br> <tr><td style=\"padding: 0px 4px;\">
<label for=\"blogpost_title\">Post title:</label>
<label for=\"blogpost_tags\">Post tags:</label> </td><td style=\"padding: 0px 4px;\">
<input type=\"text\" name=\"blogpost_tags\" value=\"%s\"> <input type=\"text\" name=\"blogpost_title\" value=\"%s\">
</td><td style=\"padding: 0px 4px;\">
Tags should be separated by spaces, use dash-case, use the </td></tr>
<a href=\"http://www.zdenekborovec-dev.cz/blog/tageditor\"> <tr><td style=\"padding: 0px 4px;\">
tageditor</a> page to add new tags. <br> <label for=\"blogpost_address\">Address:</label>
</td><td style=\"padding: 0px 4px;\">
<input type=\"text\" name=\"blogpost_address\" value=\"%s\">
</td><td style=\"padding: 0px 4px;\">
Leave empty to use GUID addressing
</td></tr>
<tr><td style=\"padding: 0px 4px;\">
<label for=\"blogpost_tags\">Post tags:</label>
</td><td style=\"padding: 0px 4px;\">
<input type=\"text\" name=\"blogpost_tags\" value=\"%s\">
</td><td style=\"padding: 0px 4px;\">
Tags should be separated by spaces, use dash-case, use the
<a href=\"http://www.zdenekborovec-dev.cz/blog/tageditor\">
tageditor</a> page to add new tags.
</td></tr>
</table>
<div class=\"centered-container\"> <div class=\"centered-container\">
<textarea name=\"article_abstract\" style=\"width: 100%%; <textarea name=\"article_abstract\" style=\"width: 100%%;
@ -215,7 +245,7 @@ printf("
<input name=\"submit\" type=\"submit\" value=\"Send File\"> <input name=\"submit\" type=\"submit\" value=\"Send File\">
</form> </form>
</article> </article>
", $blogId_prefill, $title_prefill, $tagStr_prefill, $abstract_prefill, ", $blogId_prefill, $title_prefill, $address_prefill, $tagStr_prefill, $abstract_prefill,
$content_prefill); $content_prefill);
include_once($COMMONS."/footer.php"); include_once($COMMONS."/footer.php");
?> ?>