implemented editing articles

This commit is contained in:
2025-04-08 16:40:40 -06:00
parent e5b3e048b8
commit e829e2e566
5 changed files with 99 additions and 57 deletions
+49 -17
View File
@@ -4,8 +4,17 @@ require_once 'functions/functions.php';
include_once 'functions/article_functions.php'; include_once 'functions/article_functions.php';
if (!empty($_POST)) { if (!empty($_POST)) {
if (isset($_POST['article_id'])) {
$tags = [];
for ($i = 0; $i < count($_POST['tags']); $i++)
$tags[] = $_POST['tags'][$i];
update_article($_POST['article_id'], $_POST['title'], $_POST['excerpt'], $tags, $_POST['markdown']);
header('Location: articles.php?article_id=' . $_POST['article_id']);
} else {
$id = create_article($_COOKIE['user_id'], $_POST['title'], $_POST['excerpt'], (isset($_POST['tags']) ? $_POST['tags'] : []), $_POST['markdown']); $id = create_article($_COOKIE['user_id'], $_POST['title'], $_POST['excerpt'], (isset($_POST['tags']) ? $_POST['tags'] : []), $_POST['markdown']);
header('Location: articles.php?article_id=' . $id); header('Location: articles.php?article_id=' . $id);
}
} }
if (!isset($_COOKIE['role_id']) || $_COOKIE['role_id'] == 4) if (!isset($_COOKIE['role_id']) || $_COOKIE['role_id'] == 4)
@@ -13,53 +22,76 @@ if (!isset($_COOKIE['role_id']) || $_COOKIE['role_id'] == 4)
include_once 'components/head.php'; include_once 'components/head.php';
echo form(); if (isset($_GET['article_id'])) {
$article_id = $_GET['article_id'];
$conn = get_connection();
$stmt = $conn->prepare('SELECT * FROM article WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
$article = $stmt->get_result()->fetch_assoc();
$stmt = $conn->prepare('SELECT tag_id FROM article_tags WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
$result = $stmt->get_result();
$tags = [];
while ($row = $result->fetch_assoc())
$tags[] = $row['tag_id'];
$title = $article['title'];
$excerpt = $article['excerpt'];
$existing_markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md');
}
if (!empty($existing_markdown)) {
echo form($article_id, $title, $excerpt, $existing_markdown, $tags);
} else {
echo form();
}
include_once 'components/foot.php'; include_once 'components/foot.php';
function form() function form($article_id = null, $title = null, $excerpt = null, $existing_markdown = null, $tags = null)
{ {
$raw_tags = article_tags(); $raw_tags = article_tags();
$tags = ' $tags_boxes = '
<div class="checkboxes"> <div class="checkboxes">
<label>Tags</label> <label>Tags</label>
'; ';
foreach ($raw_tags as $row) { foreach ($raw_tags as $row) {
$tags .= ' $tags_boxes .= '
<label class="form_checkbox_container">' . $row['tag'] . ' <label class="form_checkbox_container">' . $row['tag'] . '
<input name="tags[]" value="' . $row['tag_id'] . '" type="checkbox"> <input name="tags[]" value="' . $row['tag_id'] . '" type="checkbox"' . (isset($tags) && in_array($row['tag_id'], $tags) ? ' checked' : '') . '>
<span class="checkmark"></span> <span class="checkmark"></span>
</label> </label>
'; ';
} }
$tags .= ' $tags_boxes .= '
</div> </div>
'; ';
$form = ' $form = '
<div class="form_card"> <div class="form_card">
<form method="post"> <form method="post">
' . (isset($article_id) ? '<input type="hidden" name="article_id" value="' . $article_id . '">' : '') . '
<label for="title">Title</label> <label for="title">Title</label>
<input id="title" name="title" placeholder="Article Title" required> <input id="title" name="title" placeholder="Article Title" required' . (isset($title) ? ' value="' . $title . '"' : '') . '>
<label for="markdown">Markdown</label> <label for="markdown">Markdown</label>
<textarea id="markdown" name="markdown" required> <textarea id="markdown" name="markdown" required>' . (isset($existing_markdown) ? $existing_markdown : '') . '</textarea>
## Heading
vintagecoding.net uses markdown for articles. This means:
- Simple syntax for formatting
- Pre-built styling
- Markdown can be parsed into HTML.
</textarea>
<label for="excerpt">Excerpt</label> <label for="excerpt">Excerpt</label>
<input id="excerpt" name="excerpt" placeholder="Article Excerpt"> <input id="excerpt" name="excerpt" placeholder="Article Excerpt"' . (isset($excerpt) ? 'value="' . $excerpt . '"' : '') . '>
' . $tags . ' ' . $tags_boxes . '
<input class="button" type="submit" value="Create Article"> <input class="button" type="submit" value="' . (isset($article_id) ? 'Update Article' : 'Create Article') . '">
</form> </form>
</div> </div>
'; ';
return $form; return $form;
} }
+9 -19
View File
@@ -291,32 +291,22 @@ table tr td label {
text-decoration-color: orange; text-decoration-color: orange;
} }
#theme-toggle, i {
label i {
width: 32px;
height: 32px;
border: none; border: none;
background: transparent;
font-size: 1.5vw;
transition-duration: 500ms;
}
#theme-toggle {
background: none; background: none;
font-size: 1.5vw; border: none;
} }
#theme-toggle *, i:hover {
label i {
transition-duration: 500ms;
}
#theme-toggle *:hover,
label i:hover {
color: orange; color: orange;
} }
label i {
width: 32px;
height: 32px;
font-size: 1.5vw;
transition-duration: 500ms;
}
.article { .article {
width: 60%; width: 60%;
margin: 0 auto; margin: 0 auto;
+37
View File
@@ -101,7 +101,11 @@ function article_page_from_markdown($article_id)
$html = ' $html = '
<div class="article"> <div class="article">
<div class="row">
<h3>' . $article['title'] . '</h3> <h3>' . $article['title'] . '</h3>
' . (isset($article_id) ? '<a href="compose.php?article_id=' . $article_id . '"><i class="nf nf-fa-edit"></i></a>' : '') . '
</div>
<div class="row"> <div class="row">
<div class="col_right"> <div class="col_right">
<h5>Written by <a href="user.php?action=view&user_id=' . $author['user_id'] . '">' . $author['username'] . '</a></h5> <h5>Written by <a href="user.php?action=view&user_id=' . $author['user_id'] . '">' . $author['username'] . '</a></h5>
@@ -232,6 +236,39 @@ function create_article($author_id, $title, $excerpt, $tags, $markdown_file_cont
return $id; return $id;
} }
function update_article($article_id, $title, $excerpt, $tags, $markdown_file_contents)
{
$conn = get_connection();
$stmt = $conn->prepare('UPDATE article SET title = ?, excerpt = ?, updated_at = CURRENT_TIMESTAMP WHERE article_id = ?');
$stmt->bind_param('ssi', $title, $excerpt, $article_id);
$stmt->execute();
$path = $_ENV['ARTICLES_FQ_PATH'] . $article_id . '/';
$fs = fopen($path . 'article.md', 'w');
fwrite($fs, $markdown_file_contents);
fclose($fs);
$stmt = $conn->prepare('DELETE from article_tags WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
if (count($tags) > 0) {
$tag_insert_stmt = 'INSERT INTO article_tags (article_id, tag_id) VALUES ';
$input = '';
for ($i = 0; $i < count($tags); $i++) {
$tag_insert_stmt .= '(' . $article_id . ', ?)';
if ($i < count($tags) - 1)
$tag_insert_stmt .= ', ';
$input .= 'i';
}
$stmt = $conn->prepare($tag_insert_stmt);
$stmt->bind_param($input, ...$tags);
$stmt->execute();
}
}
function delete_article($article_id) function delete_article($article_id)
{ {
-17
View File
@@ -1,17 +0,0 @@
<?php
require_once 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
use Postmark\PostmarkClient;
$client = new PostmarkClient($_ENV['POSTMARK_API_TOKEN']);
$send_result = $client->sendEmail(
"mailer@joshashton.dev",
"me@joshashton.dev",
"Postmark PHP Test",
"This is a test. Do not be alarmed."
);
+1 -1
View File
@@ -62,7 +62,7 @@
- [x] Delete Article by ID --- v0.1.0 - [x] Delete Article by ID --- v0.1.0
- [x] Delete Articles by Author (for account deletion) --- v0.1.0 - [x] Delete Articles by Author (for account deletion) --- v0.1.0
- [ ] Draft/unpublished status --- v0.2.0 - [ ] Draft/unpublished status --- v0.2.0
- [ ] Update Article by ID (required to be the author updating) --- v0.2.0 - [x] Update Article by ID (required to be the author updating) --- v0.2.0
- [x] Get Articles by Author --- v0.2.0 - [x] Get Articles by Author --- v0.2.0
- [x] Sort / filter system --- v0.2.0 - [x] Sort / filter system --- v0.2.0