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
+55 -58
View File
@@ -6,68 +6,65 @@ include_once 'components/head.php';
include_once 'functions/article_functions.php';
// Default view, it'll show articles by recency
if (!isset($_GET['article_id']) && !isset($_GET['tag']) && !isset($_GET['author_id'])) {
$filters = '
<form method="get">
<div class="row_no_margin">
';
// UI Components
foreach (glob('components/article/*.php') as $file)
require_once $file;
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>
';
if (isset($_POST['form_id'])) {
switch ($_POST['form_id']) {
case 'compose':
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);
}
break;
}
$filters .= '
</div>
</form>
';
echo '
<div class="article_cards_grid">
<div class="article_filters">
' . $filters . '
</div>
<div class="article_cards">
';
foreach ($ids as $id)
echo article_card($id);
echo '
</div>
</div>
';
} else if (isset($_COOKIE['user_id']) && isset($_GET['article_id'])) {
increment_read_counter($_GET['article_id']);
echo article_page_from_markdown($_GET['article_id']);
} else if (isset($_GET['author_id'])) {
$ids = articles_ids_by_author($_GET['author_id']);
foreach ($ids as $id)
echo article_card($id);
} else if (isset($_GET['tag'])) {
$ids = article_ids_by_tag($_GET['tag']);
foreach ($ids as $id)
echo article_card($id);
} else {
$redirect = (isset($_GET['article_id']) ? 'articles.php?article_id=' . $_GET['article_id'] : '');
if ($redirect)
header('Location: user.php?action=login&redirect=' . urlencode($redirect));
if (isset($_GET['view']))
$view = $_GET['view'];
else
header('Location: articles.php');
$view = 'list';
switch ($view) {
case 'list':
echo article_list();
break;
case 'filter':
if (isset($_GET['tag']))
echo article_list(article_ids_by_tag($_GET['tag']));
else if (isset($_GET['author_id']))
echo article_list(articles_ids_by_author($_GET['author_id']));
break;
case 'sort':
if (isset($_GET['sort']) && $_GET['sort'] == 'oldest')
echo article_list(article_ids_by_oldest());
else if (isset($_GET['sort']) && $_GET['sort'] == 'newest')
echo article_list(article_ids_by_newest());
break;
case 'read':
if (isset($_COOKIE['user_id']) && isset($_GET['article_id'])) {
increment_read_counter($_GET['article_id']);
echo article_page_from_markdown($_GET['article_id']);
} else {
$redirect = (isset($_GET['article_id']) ? 'articles.php?view=read&article_id=' . $_GET['article_id'] : '');
if ($redirect)
header('Location: user.php?action=login&redirect=' . urlencode($redirect));
else
header('Location: articles.php');
}
break;
case 'compose':
echo compose_view((isset($_GET['article_id']) ? $_GET['article_id'] : ''));
break;
}
}
include_once 'components/foot.php';