';
@@ -66,7 +79,7 @@ function article_card($article_id)
}
$author_query = 'SELECT username, profile_picture FROM user WHERE user_id = ' . $article['author_id'] . ';';
- $author = query_db_one_result($author_query);
+ $author = query_one_result($author_query);
$card = '
@@ -94,7 +107,44 @@ function article_card($article_id)
return $card;
}
-/*
- * Displays the article creation wizard and opens a new markdown file.
- */
-function create_article() {}
+function create_article($author_id, $title, $excerpt, $tags, $markdown_file_contents)
+{
+ $stmt = 'INSERT INTO article (author_id, title, excerpt, published_at) VALUES (' . $author_id . ', "' . $title . '", "' . $excerpt . '", CURRENT_TIMESTAMP);';
+ $id = exec_statement($stmt, 0);
+
+ $path = $_ENV['ARTICLES_PATH'] . $id . '/';
+ mkdir($path);
+ $fs = fopen($path . 'article.md', 'a');
+ fwrite($fs, $markdown_file_contents);
+ fclose($fs);
+
+ $stmt = 'INSERT INTO article_tags (article_id, tag_id) VALUES ';
+ for ($i = 0; $i < count($tags); $i++) {
+ $stmt .= '(' . $id . ', ' . $tags[$i] . ')';
+
+ if ($i < count($tags) - 1)
+ $stmt .= ', ';
+
+ if ($i == count($tags) - 1)
+ $stmt .= ';';
+ }
+
+ echo $stmt;
+ pretty_dump($tags);
+ if (count($tags) > 0)
+ exec_statement($stmt, 0);
+
+ return $id;
+}
+
+function delete_article($article_id)
+{
+ $stmt = 'DELETE FROM article WHERE article_id = ' . $article_id . ';';
+ return exec_statement($stmt, 1);
+}
+
+function delete_articles_by_author($author_id)
+{
+ $stmt = 'DELETE FROM article WHERE author_id = "' . $author_id . '";';
+ return exec_statement($stmt, 1);
+}
diff --git a/functions/db_functions.php b/functions/db_functions.php
index ae71f38..9b5d7e3 100644
--- a/functions/db_functions.php
+++ b/functions/db_functions.php
@@ -20,7 +20,7 @@ if ($_SERVER['HTTP_HOST'] == 'localhost') {
/*
* Handles any queries that should have only one row results.
*/
-function query_db_one_result($query_stmt)
+function query_one_result($query_stmt)
{
$conn = get_connection();
$results = mysqli_query($conn, $query_stmt);
@@ -32,7 +32,7 @@ function query_db_one_result($query_stmt)
return mysqli_fetch_assoc($results);
}
-function query_db_one_or_more_results($query_stmt)
+function query_one_or_more_results($query_stmt)
{
$conn = get_connection();
$results = mysqli_query($conn, $query_stmt);
@@ -44,6 +44,23 @@ function query_db_one_or_more_results($query_stmt)
return $results;
}
+/*
+ * Executes a statement. Type is typically either 0 or 1, where 0 is an INSERT or UPDATE,
+ * and 1 is DELETE. This is to determine whether a boolean value should be returned or
+ * the ID of the row(s).
+ */
+function exec_statement($stmt, $type)
+{
+ $conn = get_connection();
+ $results = mysqli_query($conn, $stmt);
+
+ if ($type == 0)
+ $results = mysqli_insert_id($conn);
+
+ mysqli_close($conn);
+ return $results;
+}
+
function get_connection()
{
// Connect to the DB
diff --git a/index.php b/index.php
index c9257f4..d214aec 100644
--- a/index.php
+++ b/index.php
@@ -1,11 +1,10 @@
Home';
+
include_once 'components/foot.php';
diff --git a/scripts/init.sql b/scripts/init.sql
index 73b1dac..1de4682 100644
--- a/scripts/init.sql
+++ b/scripts/init.sql
@@ -33,7 +33,7 @@ CREATE TABLE article (
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
published_at TIMESTAMP DEFAULT NULL,
- published BOOLEAN DEFAULT FALSE,
+ published BOOLEAN DEFAULT TRUE,
excerpt VARCHAR(256),
FOREIGN KEY (author_id) REFERENCES user(user_id)
);
diff --git a/user.php b/user.php
new file mode 100644
index 0000000..d1d47cc
--- /dev/null
+++ b/user.php
@@ -0,0 +1,32 @@
+