implemented database, article cards, and parsing from markdown.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
require 'db_functions.php';
|
||||
|
||||
/*
|
||||
* Reads the markdown file for a given article and generates the HTML for it.
|
||||
*/
|
||||
function article_page_from_markdown($article_id)
|
||||
{
|
||||
$query = 'SELECT * FROM articles WHERE article_id = ' . $article_id . ';';
|
||||
$article = query_db_one_result($query);
|
||||
$markdown = read_file_one_string($_ENV['ARTICLES_PATH'] . $article_id . '/' . $article['markdown_file_path']);
|
||||
|
||||
// Instantiate Parsedown
|
||||
$parsedown = new Parsedown();
|
||||
|
||||
// Example usage
|
||||
$html = $parsedown->text($markdown);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generates cards for a given article.
|
||||
*/
|
||||
function article_card($article_id)
|
||||
{
|
||||
$article_query = 'SELECT * FROM articles WHERE article_id = ' . $article_id . ';';
|
||||
$article = query_db_one_result($article_query);
|
||||
|
||||
$article_tags_query = 'SELECT tag FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE article_id = ' . $article_id . ';';
|
||||
$tags_results = query_db_one_or_more_results($article_tags_query);
|
||||
$tags_html = '<div class="tag_row">';
|
||||
|
||||
while ($row = mysqli_fetch_array($tags_results))
|
||||
$tags_html .= '<a href="/article.php?tag=' . $row['tag'] . '" class="tag">#' . $row['tag'] . '</a>';
|
||||
|
||||
$tags_html .= '</div>';
|
||||
|
||||
$author_query = 'SELECT username, profile_picture FROM user WHERE user_id = ' . $article['author_id'] . ';';
|
||||
$author = query_db_one_result($author_query);
|
||||
|
||||
$card = '
|
||||
<div class="article_card">
|
||||
<div class="row">
|
||||
<h4>' . $article['title'] . '</h4>
|
||||
<div class="card_info_box">
|
||||
<div class="col-right">
|
||||
<small>Written by: ' . $author['username'] . '</small>
|
||||
<small>Published: ' . $article['published_at'] . '</small>
|
||||
</div>
|
||||
<div class="card_pic_box">
|
||||
<img src="/profile_pictures/' . $author['profile_picture'] . '" class="card_profile_picture">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="line"></div>
|
||||
<p>' . $article['excerpt'] . '</p>
|
||||
<div class="row">
|
||||
<a href="/article.php?article_id=' . $article['article_id'] . '" class="button">Read More</a>
|
||||
<div class="tag_box">' . $tags_html . '</div>
|
||||
</div>
|
||||
</div>
|
||||
';
|
||||
|
||||
return $card;
|
||||
}
|
||||
|
||||
/*
|
||||
* Displays the article creation wizard and opens a new markdown file.
|
||||
*/
|
||||
function create_article() {}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
// Use environment variables for the database password and IP address.
|
||||
$db_user = $_ENV['DATABASE_USER'];
|
||||
$db_pass = $_ENV['DATABASE_PASSWORD'];
|
||||
$ip_addr = $_ENV['IP_ADDRESS'];
|
||||
|
||||
// Make some constants
|
||||
if ($_SERVER['HTTP_HOST'] == 'localhost') {
|
||||
define('HOST', 'localhost');
|
||||
define('USER', $db_user);
|
||||
define('PASS', $db_pass);
|
||||
define('DB', 'vintagecoding');
|
||||
} else {
|
||||
define('HOST', $ip_addr);
|
||||
define('USER', $db_user);
|
||||
define('PASS', $db_pass);
|
||||
define('DB', 'vintagecoding');
|
||||
}
|
||||
|
||||
/*
|
||||
* Handles any queries that should have only one row results.
|
||||
*/
|
||||
function query_db_one_result($query_stmt)
|
||||
{
|
||||
$conn = get_connection();
|
||||
$results = mysqli_query($conn, $query_stmt);
|
||||
mysqli_close($conn);
|
||||
|
||||
if (mysqli_num_rows($results) == 0 || mysqli_num_rows($results) > 1)
|
||||
return null;
|
||||
else
|
||||
return mysqli_fetch_assoc($results);
|
||||
}
|
||||
|
||||
function query_db_one_or_more_results($query_stmt)
|
||||
{
|
||||
$conn = get_connection();
|
||||
$results = mysqli_query($conn, $query_stmt);
|
||||
mysqli_close($conn);
|
||||
|
||||
if (mysqli_num_rows($results) == 0)
|
||||
return null;
|
||||
else
|
||||
return $results;
|
||||
}
|
||||
|
||||
function get_connection()
|
||||
{
|
||||
// Connect to the DB
|
||||
$conn = mysqli_connect(HOST, USER, PASS, DB);
|
||||
|
||||
return $conn;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Reads in file contents and stores as a single string.
|
||||
*/
|
||||
function read_file_one_string($file_path)
|
||||
{
|
||||
$fs = fopen($file_path, 'r');
|
||||
$string = fread($fs, filesize($file_path));
|
||||
return $string;
|
||||
}
|
||||
|
||||
function pretty_dump($arr)
|
||||
{
|
||||
return '
|
||||
<pre>
|
||||
' . print_r($arr) . '
|
||||
</pre>
|
||||
';
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
// Initialize Composer.
|
||||
require_once 'vendor/autoload.php';
|
||||
|
||||
// Load environment variables.
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
||||
$dotenv->safeLoad();
|
||||
|
||||
// Set SESSION variable.
|
||||
$_SESSION['initialized'] = true;
|
||||
Reference in New Issue
Block a user