refactoring

This commit is contained in:
2025-04-13 15:57:16 -06:00
parent 27bfe8e7b3
commit 8f2c241c3d
29 changed files with 867 additions and 841 deletions
+41
View File
@@ -0,0 +1,41 @@
<?php
/*
* Reads the markdown file for a given article and generates the HTML for it.
*/
function article_page_from_markdown($article_id)
{
$article = exec_stmt('SELECT * FROM article WHERE article_id = ?', 'i', $article_id)->fetch_assoc();
$author = exec_stmt('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?', 'i', $article['author_id'])->fetch_assoc();
$markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md');
$parsedown = new Parsedown();
$parsedown->setSafeMode(true);
$article_content = $parsedown->text($markdown);
$html = '
<div class="article">
<div class="row">
<h1>' . $article['title'] . '</h1>
' . (isset($_COOKIE['user_id']) && $_COOKIE['user_id'] == $article['author_id'] ? '<a href="articles.php?view=compose&article_id=' . $article_id . '"><i class="nf nf-fa-edit"></i></a>' : '') . '
</div>
<div class="row">
<div class="col_right">
<h5>Written by <a href="user.php?action=view&user_id=' . $author['user_id'] . '">' . $author['username'] . '</a></h5>
<p>Published on ' . $article['published_at'] . '<br>
Lasted edited on ' . $article['updated_at'] . '</p>
</div>
<div class="card_pic_box">
<img src="' . $_ENV['PROFILE_IMAGES_PQ_PATH'] . $author['profile_picture'] . '" class="card_profile_picture" />
</div>
</div>
<div class="line" style="margin-bottom: 15px;"></div>
<div class="article_content">
' . $article_content . '
</div>
</div>
';
return $html;
}
+49
View File
@@ -0,0 +1,49 @@
<?php
/*
* Generates cards for a given article.
*/
function article_card($article_id)
{
$article = exec_stmt('SELECT * FROM article WHERE article_id = ?', 'i', $article_id)->fetch_assoc();
$tags_results = exec_stmt('SELECT tag FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE article_id = ?', 'i', $article_id);
$tags_html = '';
if ($tags_results != null) {
$tags_html .= '<div class="tag_row">';
while ($row = $tags_results->fetch_assoc())
$tags_html .= '<a href="/articles.php?view=filter&tag=' . $row['tag'] . '" class="tag">#' . $row['tag'] . '</a>';
$tags_html .= '</div>';
}
$author = exec_stmt('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?', 'i', $article['author_id'])->fetch_assoc();
$card = '
<div class="article_card">
<div class="row">
<h4>' . $article['title'] . '</h4>
<div class="card_info_box">
<div class="col-right">
<a href="user.php?action=view&user_id=' . $author['user_id'] . '">Written by: ' . $author['username'] . '</a>
<small>Published: ' . $article['published_at'] . '</small>
<small>Updated: ' . $article['updated_at'] . '</small>
</div>
<div class="card_pic_box">
<img src="' . $_ENV['PROFILE_IMAGES_PQ_PATH'] . $author['profile_picture'] . '" class="card_profile_picture">
</div>
</div>
</div>
<div class="line"></div>
<p>' . $article['excerpt'] . '</p>
<div class="row">
<a href="/articles.php?view=read&article_id=' . $article['article_id'] . '" class="button">Read More</a>
<div class="tag_box">' . $tags_html . '</div>
</div>
</div>
';
return $card;
}
+12
View File
@@ -0,0 +1,12 @@
<?php
function preview()
{
$preview = '
<div class="full-height">
<div class="form_card" id="md_preview">
</div>
</div>
';
return $preview;
}
+36
View File
@@ -0,0 +1,36 @@
<?php
function compose_view($article_id = null)
{
if (!empty($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');
}
$view = '
<div class="grid-2x1">
' . (!empty($existing_markdown) ? form($article_id, $title, $excerpt, $existing_markdown, $tags) : form()) . '
' . preview() . '
';
$view .= preview() . '
</div>
';
return $view;
}
+46
View File
@@ -0,0 +1,46 @@
<?php
function form($article_id = null, $title = null, $excerpt = null, $existing_markdown = null, $tags = null)
{
$raw_tags = article_tags();
$tags_boxes = '
<div class="checkboxes">
<label>Tags</label>
';
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 .= '
</div>
';
$form = '
<div class="full-height">
<div class="form_card" id="composer">
<form method="post">
<input type="hidden" name="form_id" value="compose">
' . (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' . (isset($title) ? ' value="' . $title . '"' : '') . '>
<label for="markdown">Markdown</label>
<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"' . (isset($excerpt) ? 'value="' . $excerpt . '"' : '') . '>
' . $tags_boxes . '
<input class="button" type="submit" value="' . (isset($article_id) ? 'Update Article' : 'Create Article') . '">
</form>
</div>
</div>
';
return $form;
}
+55
View File
@@ -0,0 +1,55 @@
<?php
function article_list($list = null)
{
$content = '';
$filters = '
<form method="get" action="articles.php">
<input type="hidden" name="view" value="sort">
<div class="row_no_margin">
';
if (!isset($_GET['sort']) || $_GET['sort'] == 'newest') {
$ids = article_ids_by_newest();
$filters .= '
<p>Newest - Oldest</p>
<label for="recency_desc"><i class="nf nf-md-sort_clock_descending_outline"></i>
<input id="recency_desc" type="radio" name="sort" value="oldest" onclick="this.form.submit()">
</label>
';
} else if ($_GET['sort'] == 'oldest') {
$ids = article_ids_by_oldest();
$filters .= '
<p>Oldest - Newest</p>
<label for="recency_asc"><i class="nf nf-md-sort_clock_ascending_outline"></i>
<input id="recency_asc" type="radio" name="sort" value="newest" onclick="this.form.submit()">
</label>
';
}
$filters .= '
</div>
</form>
';
$content .= '
<div class="article_cards_grid">
<div class="article_filters">
' . $filters . '
</div>
<div class="article_cards">
';
if (!isset($list))
foreach ($ids as $id)
$content .= article_card($id);
else
foreach ($list as $id)
$content .= article_card($id);
$content .= '
</div>
</div>
';
return $content;
}