implemented editing articles
This commit is contained in:
+51
-19
@@ -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 = '
|
||||
<div class="checkboxes">
|
||||
<label>Tags</label>
|
||||
';
|
||||
|
||||
foreach ($raw_tags as $row) {
|
||||
$tags .= '
|
||||
$tags_boxes .= '
|
||||
<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>
|
||||
</label>
|
||||
';
|
||||
}
|
||||
|
||||
$tags .= '
|
||||
$tags_boxes .= '
|
||||
</div>
|
||||
';
|
||||
|
||||
$form = '
|
||||
<div class="form_card">
|
||||
<form method="post">
|
||||
' . (isset($article_id) ? '<input type="hidden" name="article_id" value="' . $article_id . '">' : '') . '
|
||||
|
||||
<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>
|
||||
<textarea id="markdown" name="markdown" required>
|
||||
## Heading
|
||||
vintagecoding.net uses markdown for articles. This means:
|
||||
- Simple syntax for formatting
|
||||
- Pre-built styling
|
||||
- Markdown can be parsed into HTML.
|
||||
</textarea>
|
||||
<textarea id="markdown" name="markdown" required>' . (isset($existing_markdown) ? $existing_markdown : '') . '</textarea>
|
||||
|
||||
<label for="excerpt">Excerpt</label>
|
||||
<input id="excerpt" name="excerpt" placeholder="Article Excerpt">
|
||||
' . $tags . '
|
||||
<input class="button" type="submit" value="Create Article">
|
||||
<input id="excerpt" name="excerpt" placeholder="Article Excerpt"' . (isset($excerpt) ? 'value="' . $excerpt . '"' : '') . '>
|
||||
' . $tags_boxes . '
|
||||
<input class="button" type="submit" value="' . (isset($article_id) ? 'Update Article' : 'Create Article') . '">
|
||||
</form>
|
||||
</div>
|
||||
';
|
||||
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user