/
class.quotemention.plugin.php
113 lines (94 loc) · 4.06 KB
/
class.quotemention.plugin.php
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
<?php
class QuoteMentionPlugin extends Gdn_Plugin {
public function assetModel_styleCss_handler($sender) {
$sender->addCssFile('tooltipster.css', 'plugins/quotemention');
$sender->addCssFile('quotemention.css', 'plugins/quotemention');
}
public function discussionController_initialize_handler($sender) {
$sender->addJsFile('jquery.tooltipster.min.js', 'plugins/quotemention');
$sender->addJsFile('quotemention.js', 'plugins/quotemention');
$sender->addDefinition('quoteMention.maxWidth', (int)Gdn::config('QuoteMention.MaxWidth', 350));
$sender->addDefinition('quoteMention.position', Gdn::config('QuoteMention.Position', 'bottom'));
$sender->addDefinition('quoteMention.showProgress', Gdn::config('QuoteMention.ShowProgress', true));
}
public function pluginController_quoteMention_create($sender, $discussionID, $commentID, $username) {
$sender->deliveryMethod(DELIVERY_METHOD_JSON);
$user = Gdn::userModel()->getByUsername($username);
$discussionModel = new DiscussionModel();
$discussion = $discussionModel->getID($discussionID);
if (!$user || !$discussion) {
throw notFoundException();
}
// Make sure this endpoint can't be used to snoop around.
$sender->permission(
'Vanilla.Discussions.View',
true,
'Category',
$discussion->PermissionCategoryID
);
// Find the previous comment of the mentioned user in this discussion.
$item = Gdn::sql()->getWhere('Comment', [
'DiscussionID' => $discussion->DiscussionID,
'InsertUserID' => $user->UserID,
'CommentID <' => $commentID
], 'CommentID', 'desc', 1)->firstRow();
// The items ID in the DOM used for highlighting.
if ($item) {
$target = '#Comment_'.$item->CommentID;
// The mentioned user might be the discussion creator.
} elseif ($discussion->InsertUserID == $user->UserID) {
$item = $discussion;
$target = '#Discussion_'.$item->DiscussionID;
}
if (!$item) {
// A success response code always means that a comment was found.
$sender->statusCode(404);
}
$sender->renderData($item ? [
'html' => nl2br(sliceString(
Gdn_Format::plainText($item->Body, $item->Format),
Gdn::config('QuoteMention.MaxLength', 400)
)),
'target' => $target
] : []);
}
// Settings page
public function settingsController_quoteMention_create($sender) {
$sender->permission('Garden.Settings.Manage');
$conf = new ConfigurationModule($sender);
$conf->initialize([
'QuoteMention.MaxWidth' => [
'Control' => 'textbox',
'LabelCode' => 'Maximum width (px) of the tooltip',
'Default' => 350
],
'QuoteMention.Position' => [
'Control' => 'dropdown',
'LabelCode' => 'Position of the tooltip',
'Items' => [
'bottom' => 'bottom',
'bottom-left' => 'bottom-left',
'left' => 'left',
'top-left' => 'top-left',
'top' => 'top',
'top-right' => 'top-right',
'right' => 'right',
'bottom-right' => 'bottom-right'
],
'Default' => 'bottom'
],
'QuoteMention.ShowProgress' => [
'Control' => 'CheckBox',
'LabelCode' => 'Show a progress indicator when loading contents.',
'Default' => true
],
'QuoteMention.MaxLength' => [
'Control' => 'textbox',
'LabelCode' => 'Maximum characters shown in the tooltip',
'Default' => 400
]
]);
$sender->title(sprintf(Gdn::translate('%s Settings'), 'Quote Mentions'));
$conf->renderAll();
}
}