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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
<?php
namespace Forum;
use OpenGraph;
use SimpleORMap;
use Forum\Service\PostingNotification;
use User;
use Forum\DTO\ForumMember;
/**
* @property string $posting_id
* @property string $discussion_id
* @property string $range_id
* @property string $content
* @property boolean $anonymous
* @property int $mkdate
* @property int $chdate
*
* @property ForumDiscussion $discussion
* @property ForumPosting $posting
* @property ForumPostingReaction[] $reactions
* @property User $user
* @property ForumMember $author
*/
class ForumPosting extends SimpleORMap
{
protected static function configure($config = [])
{
$config['db_table'] = 'forum_postings';
$config['belongs_to']['discussion'] = [
'class_name' => ForumDiscussion::class,
'foreign_key' => 'discussion_id',
'assoc_foreign_key' => 'discussion_id'
];
$config['belongs_to']['posting'] = [
'class_name' => ForumPosting::class,
'foreign_key' => 'parent_id',
'assoc_foreign_key' => 'posting_id'
];
$config['belongs_to']['user'] = [
'class_name' => User::class,
'foreign_key' => 'user_id',
'assoc_foreign_key' => 'user_id'
];
$config['has_many']['reactions'] = [
'class_name' => ForumPostingReaction::class,
'foreign_key' => 'posting_id',
'assoc_foreign_key' => 'posting_id'
];
$config['additional_fields']['author']['get'] = 'getAuthor';
$config['registered_callbacks']['after_create'][] = 'onCreate';
$config['registered_callbacks']['after_delete'][] = 'onDelete';
parent::configure($config);
}
public function getAuthor(): ?ForumMember
{
if ($this->anonymous && $this->user_id !== User::findCurrent()->user_id) {
return ForumMember::fromArray();
}
$user = $this->user;
if ($user) {
return ForumMember::fromUser($user, $this->range_id);
}
return null;
}
public static function getRecentPosts($range_id, int $last_visit = 0): array
{
$query = [
"SELECT
forum_discussions.*,
COUNT(DISTINCT forum_postings.posting_id) AS 'posts'
FROM forum_topics
JOIN forum_discussions USING(topic_id)
JOIN forum_postings USING(discussion_id)
WHERE forum_topics.range_id = :range_id AND forum_postings.user_id != :user_id
",
[
'range_id' => $range_id,
'user_id' => User::findCurrent()->user_id
]
];
if ($last_visit) {
$query[0] .= " AND forum_postings.mkdate > :last_visit";
$query[1]["last_visit"] = $last_visit;
}
return \DBManager::get()->fetchAll(
$query[0]." GROUP BY discussion_id ORDER BY forum_postings.mkdate DESC",
$query[1]
);
}
public function getOpenGraphURLs(): array
{
$content = preg_replace("~<blockquote(.*?)>(.*)</blockquote>~si", '', $this['content']);
return array_filter(OpenGraph::extract($content)->toArray(), fn($og) => $og['is_opengraph']);
}
public function onCreate(): void
{
$postingNotification = new PostingNotification($this);
$postingNotification->notifySubscribers();
}
public function onDelete(): void
{
ForumPostingReaction::deleteBySQL("posting_id = ?", [$this->posting_id]);
}
}
|