summaryrefslogtreecommitdiff
path: root/src/application/actions/newtopic
diff options
context:
space:
mode:
authorJonas Kohl2024-10-10 17:33:13 +0200
committerJonas Kohl2024-10-10 17:33:13 +0200
commit64b1ec0fabbf7328a79a20ff58502ebfa80fad8b (patch)
tree88f2281295b347bdd3beee5bc45f68314f2051dc /src/application/actions/newtopic
parent4ffc399a847ce4f328d4f14adebb48d06ad033f9 (diff)
Break up actions into individual files
Diffstat (limited to 'src/application/actions/newtopic')
-rw-r--r--src/application/actions/newtopic/_common.php7
-rw-r--r--src/application/actions/newtopic/get.php10
-rw-r--r--src/application/actions/newtopic/post.php68
3 files changed, 85 insertions, 0 deletions
diff --git a/src/application/actions/newtopic/_common.php b/src/application/actions/newtopic/_common.php
new file mode 100644
index 0000000..b3b709b
--- /dev/null
+++ b/src/application/actions/newtopic/_common.php
@@ -0,0 +1,7 @@
+<?php
+
+if (!$currentUser) {
+ http_response_code(403);
+ msg_error("You need to be logged in to create new topics!");
+ exit;
+}
diff --git a/src/application/actions/newtopic/get.php b/src/application/actions/newtopic/get.php
new file mode 100644
index 0000000..366caac
--- /dev/null
+++ b/src/application/actions/newtopic/get.php
@@ -0,0 +1,10 @@
+<?php
+
+use mystic\forum\utils\RequestUtils;
+
+_view("template_start", ["_title" => __("New topic")]);
+_view("template_navigation_start");
+_view("template_navigation", ["user" => RequestUtils::getAuthorizedUser($db)]);
+_view("template_navigation_end");
+_view("form_newtopic");
+_view("template_end", [...getThemeAndLangInfo()]);
diff --git a/src/application/actions/newtopic/post.php b/src/application/actions/newtopic/post.php
new file mode 100644
index 0000000..ca79599
--- /dev/null
+++ b/src/application/actions/newtopic/post.php
@@ -0,0 +1,68 @@
+<?php
+
+use mystic\forum\orm\Attachment;
+use mystic\forum\orm\Post;
+use mystic\forum\orm\Topic;
+use mystic\forum\utils\RequestUtils;
+
+$formId = "newtopic";
+$title = trim(RequestUtils::getRequiredField("title", $formId));
+$message = trim(RequestUtils::getRequiredField("message", $formId));
+
+$attachments = reArrayFiles($_FILES["files"]);
+
+if (count($attachments) > MAX_ATTACHMENT_COUNT)
+ RequestUtils::triggerFormError(__("Too many attachments"), $formId);
+
+// check all attachments before saving one
+foreach ($attachments as $att) {
+ if ($att["size"] > MAX_ATTACHMENT_SIZE) {
+ RequestUtils::triggerFormError(__("Individual file size exceeded"), $formId);
+ }
+}
+
+if (strlen($title) < 1 || strlen($title) > 255) {
+ RequestUtils::triggerFormError(__("Title too short or too long!"), $formId);
+}
+
+if (strlen($message) < 1 || strlen($message) > 0x8000) {
+ RequestUtils::triggerFormError(__("Message too short or too long!"), $formId);
+}
+
+$topic = new Topic();
+$topic->createdBy = $currentUser->id;
+$topic->id = $db->generateId();
+$topic->title = $title;
+$topic->creationDate = new DateTimeImmutable();
+$topic->isLocked = false;
+
+$db->insert($topic);
+
+$item = new Post();
+$item->id = $db->generateId();
+$item->authorId = $currentUser->id;
+$item->topicId = $topic->id;
+$item->content = $message;
+$item->postDate = $topic->creationDate;
+$item->deleted = false;
+$item->edited = false;
+
+$db->insert($item);
+
+foreach ($attachments as $att) {
+ [
+ "name" => $name,
+ "type" => $type,
+ "tmp_name" => $tmpName,
+ ] = $att;
+ $attachment = new Attachment();
+ $attachment->id = $db->generateId();
+ $attachment->name = $name;
+ $attachment->mimeType = $type;
+ $attachment->postId = $item->id;
+ $attachment->contents = file_get_contents($tmpName);
+
+ $db->insert($attachment);
+}
+
+header("Location: ?_action=viewtopic&topic=" . urlencode($topic->id));