summaryrefslogtreecommitdiff
path: root/create_thread.php
blob: 11b278cad92eb4054587e97b830691d86fbfc3b0 (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
<?php session_start() ?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Create a thread - cflip.net forum</title>
<?php include_once 'includes/templates/head.php'; ?>
</head>
<body>
<?php include_once 'includes/templates/header.php' ?>
	<h2>Create a new thread</h2>
<?php
include_once 'includes/Session.php';
include_once 'includes/error.php';
if (!Session::get()->is_signed_in()) {
	trigger_error('You must be <a href="signin.php">signed in</a> to create a thread.');
	exit();
}
?>
	<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
		<label for="thread_subject">Subject: </label><br>
		<input type="text" name="thread_subject"><br>
		<label for="thread_cat">Category: </label><br>
		<?php
		include_once './includes/model/Category.php';

		$categories = Category::get_all_categories();

		if (count($categories) == 0) {
			trigger_error('There are no categories to post to!');
		} else {
			echo '<select name="thread_cat">';

			foreach ($categories as $category) {
				echo '<option value="' . $category->id . '">' . $category->name . '</option>';
			}

			echo '</select><br>';
		}
		?>
		<label for="post_content">Write your post: </label><br>
		<textarea name="post_content"></textarea><br>
		<input type="submit" name="submit">
	</form>
<?php
include_once './includes/form/CreateThreadForm.php';
include_once './includes/model/Post.php';
include_once './includes/model/Thread.php';
include_once './includes/error.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $form = new CreateThreadForm();
    $post_content = $form->validate_post_content($_POST['post_content']);
    $thread_subject = $form->validate_thread_subject($_POST['thread_subject']);
    $thread_category = $form->validate_thread_category($_POST['thread_cat']);

	$form->on_success(function () use ($post_content, $thread_subject, $thread_category) {
        $thread_id = Thread::create($thread_subject, $thread_category);
        Post::create($post_content, $thread_id, $thread_category);

        header("Location: /viewthread.php?id=" . $thread_id);
    });
}
?>
</body>
</html>