summaryrefslogtreecommitdiff
path: root/includes/functions_thread.php
blob: 61b8e597a84bd8f34924d9115c818c5600bc7e1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
include_once './includes/Database.php';
include_once './includes/Session.php';

function get_all_threads(): array
{
	$sql = "SELECT thread_id FROM threads";
	$result = Database::get()->query($sql);

	$threads = array();

	foreach ($result as $row) {
		$thread = new Thread();
		$thread->get_from_database($row['thread_id']);
		array_push($threads, $thread);
	}

	return $threads;
}

function create_thread($subject, $category)
{
	if (!Session::get()->is_signed_in()) {
		trigger_error('You must be signed in to create a thread');
		return 0;
	}

	$user = Session::get()->get_current_user();

	// Insert the new thread into the database
	$sql = "INSERT INTO threads(thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);";
	Database::get()->query($sql, "sii", $subject, $category, $user->id);

	// Get the ID of the thread we just created
	$thread_id = Database::get()->get_last_id();

	// Increment the category's thread count
	$sql = "UPDATE categories SET `cat_thread_count` = `cat_thread_count` + '1' WHERE cat_id = ?;";
	Database::get()->query($sql, "i", $category);

	return $thread_id;
}

function delete_thread($thread)
{
	// User must be signed in
	if (!Session::get()->is_signed_in()) {
		trigger_error('You must be signed in to delete a thread.');
		return;
	}

	// User must be a moderator to delete a thread
	$current_user = Session::get()->get_current_user();
	if ($current_user->level != USER_LEVEL_MODERATOR) {
		trigger_error("You must be a moderator to delete this post.");
		return;
	}

	// TODO: The post must not be locked
	// TODO: The post must have not been around for a certain amount of time

	// Delete the thread from the database
	Database::get()->query("DELETE FROM threads WHERE thread_id = ?", "i", $thread->id);

	// Decrement the thread count of the category
	Database::get()->query("UPDATE categories SET `cat_thread_count` = `cat_thread_count` - '1' WHERE cat_id = ?", "i", $thread->category->id);
}