implemented preview of article markdown via AJAX
This commit is contained in:
+60
-38
@@ -4,77 +4,87 @@ require_once 'functions/functions.php';
|
||||
include_once 'functions/article_functions.php';
|
||||
|
||||
if (!empty($_POST)) {
|
||||
if (isset($_POST['article_id'])) {
|
||||
$tags = [];
|
||||
for ($i = 0; $i < count($_POST['tags']); $i++)
|
||||
$tags[] = $_POST['tags'][$i];
|
||||
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);
|
||||
}
|
||||
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)
|
||||
header('Location: articles.php');
|
||||
header('Location: articles.php');
|
||||
|
||||
include_once 'components/head.php';
|
||||
|
||||
if (isset($_GET['article_id'])) {
|
||||
$article_id = $_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();
|
||||
$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'];
|
||||
$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');
|
||||
$title = $article['title'];
|
||||
$excerpt = $article['excerpt'];
|
||||
$existing_markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md');
|
||||
}
|
||||
|
||||
echo '
|
||||
<div class="grid-2x1">
|
||||
';
|
||||
if (!empty($existing_markdown)) {
|
||||
echo form($article_id, $title, $excerpt, $existing_markdown, $tags);
|
||||
echo form($article_id, $title, $excerpt, $existing_markdown, $tags);
|
||||
} else {
|
||||
echo form();
|
||||
echo form();
|
||||
}
|
||||
|
||||
echo preview();
|
||||
|
||||
echo '
|
||||
</div>
|
||||
';
|
||||
|
||||
include_once 'components/foot.php';
|
||||
|
||||
function form($article_id = null, $title = null, $excerpt = null, $existing_markdown = null, $tags = null)
|
||||
{
|
||||
$raw_tags = article_tags();
|
||||
$tags_boxes = '
|
||||
$raw_tags = article_tags();
|
||||
$tags_boxes = '
|
||||
<div class="checkboxes">
|
||||
<label>Tags</label>
|
||||
';
|
||||
|
||||
foreach ($raw_tags as $row) {
|
||||
$tags_boxes .= '
|
||||
foreach ($raw_tags as $row) {
|
||||
$tags_boxes .= '
|
||||
<label class="form_checkbox_container">' . $row['tag'] . '
|
||||
<input name="tags[]" value="' . $row['tag_id'] . '" type="checkbox"' . (isset($tags) && in_array($row['tag_id'], $tags) ? ' checked' : '') . '>
|
||||
<span class="checkmark"></span>
|
||||
</label>
|
||||
';
|
||||
}
|
||||
}
|
||||
|
||||
$tags_boxes .= '
|
||||
$tags_boxes .= '
|
||||
</div>
|
||||
';
|
||||
|
||||
$form = '
|
||||
<div class="form_card">
|
||||
$form = '
|
||||
<div class="full-height">
|
||||
<div class="form_card" id="composer">
|
||||
<form method="post">
|
||||
' . (isset($article_id) ? '<input type="hidden" name="article_id" value="' . $article_id . '">' : '') . '
|
||||
|
||||
@@ -90,8 +100,20 @@ function form($article_id = null, $title = null, $excerpt = null, $existing_mark
|
||||
<input class="button" type="submit" value="' . (isset($article_id) ? 'Update Article' : 'Create Article') . '">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
';
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
return $form;
|
||||
function preview()
|
||||
{
|
||||
$preview = '
|
||||
<div class="full-height">
|
||||
<div class="form_card" id="md_preview">
|
||||
</div>
|
||||
</div>
|
||||
';
|
||||
|
||||
return $preview;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user