diff --git a/compose.php b/compose.php
index 950d78c..2c5a370 100644
--- a/compose.php
+++ b/compose.php
@@ -4,8 +4,17 @@ require_once 'functions/functions.php';
include_once 'functions/article_functions.php';
if (!empty($_POST)) {
- $id = create_article($_COOKIE['user_id'], $_POST['title'], $_POST['excerpt'], (isset($_POST['tags']) ? $_POST['tags'] : []), $_POST['markdown']);
- header('Location: articles.php?article_id=' . $id);
+ 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']);
+ header('Location: articles.php?article_id=' . $id);
+ }
}
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';
-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';
-function form()
+function form($article_id = null, $title = null, $excerpt = null, $existing_markdown = null, $tags = null)
{
$raw_tags = article_tags();
- $tags = '
+ $tags_boxes = '
';
foreach ($raw_tags as $row) {
- $tags .= '
+ $tags_boxes .= '
';
}
- $tags .= '
+ $tags_boxes .= '
';
$form = '
';
+
return $form;
}
diff --git a/css/base.css b/css/base.css
index 707a042..3309273 100644
--- a/css/base.css
+++ b/css/base.css
@@ -291,32 +291,22 @@ table tr td label {
text-decoration-color: orange;
}
-#theme-toggle,
-label i {
- width: 32px;
- height: 32px;
+i {
border: none;
+ background: transparent;
+ font-size: 1.5vw;
+ transition-duration: 500ms;
+}
+
+#theme-toggle {
background: none;
- font-size: 1.5vw;
+ border: none;
}
-#theme-toggle *,
-label i {
- transition-duration: 500ms;
-}
-
-#theme-toggle *:hover,
-label i:hover {
+i:hover {
color: orange;
}
-label i {
- width: 32px;
- height: 32px;
- font-size: 1.5vw;
- transition-duration: 500ms;
-}
-
.article {
width: 60%;
margin: 0 auto;
diff --git a/functions/article_functions.php b/functions/article_functions.php
index 12b2db0..012d08e 100644
--- a/functions/article_functions.php
+++ b/functions/article_functions.php
@@ -101,7 +101,11 @@ function article_page_from_markdown($article_id)
$html = '
-
' . $article['title'] . '
+
+
+
' . $article['title'] . '
+ ' . (isset($article_id) ? '
' : '') . '
+
@@ -232,6 +236,39 @@ function create_article($author_id, $title, $excerpt, $tags, $markdown_file_cont
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)
{
diff --git a/test.php b/test.php
deleted file mode 100644
index a0e8ca3..0000000
--- a/test.php
+++ /dev/null
@@ -1,17 +0,0 @@
-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."
-);
diff --git a/vintagecoding.net.md b/vintagecoding.net.md
index 15b3ba2..9826fc4 100644
--- a/vintagecoding.net.md
+++ b/vintagecoding.net.md
@@ -62,7 +62,7 @@
- [x] Delete Article by ID --- v0.1.0
- [x] Delete Articles by Author (for account deletion) --- v0.1.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] Sort / filter system --- v0.2.0