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
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<?php
/** @var Post[] $posts */
/** @var ?User $currentUser */
/** @var \mystic\forum\Database $db */
use mystic\forum\orm\Attachment;
use mystic\forum\orm\Post;
use mystic\forum\orm\Subscription;
use mystic\forum\orm\TopicLogMessage;
use mystic\forum\orm\User;
$posts = $db->fetchCustom(Post::class, 'WHERE topic_id = $1 ORDER BY post_date', [ $topicId ]);
/** @var TopicLogMessage[] $logMessages */
$logMessages = $db->fetchCustom(TopicLogMessage::class, 'WHERE topic_id = $1 ORDER BY post_date', [ $topicId ]);
$userCache = [];
$topicAuthor = null;
if ($topic->createdBy !== null) {
$topicAuthor = new User();
$topicAuthor->id = $topic->createdBy;
if (!$db->fetch($topicAuthor)) {
$topicAuthor = null;
}
}
$allItems = [...$posts, ...$logMessages];
usort($allItems, fn(Post|TopicLogMessage $a, Post|TopicLogMessage $b): int => $a->postDate <=> $b->postDate);
$allItems = array_map(function(Post|TopicLogMessage $item) use (&$db, &$topicAuthor, &$topic): array {
/** @var ?User $postAuthor */
$postAuthor = null;
if ($item->authorId !== null && !isset($userCache[$item->authorId])) {
$usr = new User();
$usr->id = $item->authorId;
if ($db->fetch($usr))
$userCache[$item->authorId] = &$usr;
}
if (isset($userCache[$item->authorId]))
$postAuthor = &$userCache[$item->authorId];
if ($item instanceof Post) {
$attachments = $db->fetchCustom(Attachment::class, 'WHERE post_id = $1', [ $item->id ]);
return [
"type" => "post",
"post" => $item,
"postAuthor" => $postAuthor,
"topicAuthor" => $topicAuthor,
"attachments" => $attachments,
"topic" => $topic,
];
} else {
return [
"type" => "logMessage",
"logMessage" => $item,
"postAuthor" => $postAuthor,
"topicAuthor" => $topicAuthor,
"topic" => $topic,
];
}
}, $allItems);
$subscription = null;
if ($currentUser !== null) {
$subscription = new Subscription;
$subscription->userId = $currentUser->id;
$subscription->topicId = $topic->id;
if (!$db->fetchWhere($subscription, ["user_id", "topic_id"])) {
$subscription = null;
}
}
render("view_topic.twig", [
"topic" => $topic,
"topicAuthor" => $topicAuthor,
"allItems" => &$allItems,
"subscription" => $subscription,
"subscription_count" => count($db->fetchCustom(Subscription::class, "WHERE topic_id = $1", [ $topic->id ])),
]);
|