This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.php
287 lines (258 loc) · 12.8 KB
/
default.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php if (!defined('APPLICATION')) exit();
$PluginInfo['DiscussionAbout'] = array(
'Name' => 'DiscussionAbout',
'Description' => 'Have discussions about a non-Vanilla item in your database.',
'Version' => '1.2',
'Author' => "Jason Barnabe",
'AuthorEmail' => '[email protected]',
'MobileFriendly' => TRUE,
'License' => 'GNU GPL2'
);
require_once dirname(__FILE__).'/config.php';
class DiscussionAboutPlugin extends Gdn_Plugin {
public $Config = null;
public function __construct() {
parent::__construct();
$this->Config = DiscussionAboutPluginConfig();
}
# Load item name for discussion index and apply filters if parameters passed
public function DiscussionModel_BeforeGet_Handler($Sender, $JoinToUser = FALSE) {
$prefix = $Sender->SQL->Database->DatabasePrefix;
$Sender->SQL->Database->DatabasePrefix = '';
$Sender->SQL->Join($this->Config['ItemTable'].' discussionaboutitem', 'd.'.$this->Config['ForeignKey'].' = discussionaboutitem.'.$this->Config['ItemPrimaryKey'], 'left');
$Sender->SQL->Select('discussionaboutitem.'.$this->Config['ItemName'], '', 'DiscussionAboutName');
foreach ($this->Config['AdditionalFilters'] as $filter_param => $filter_options) {
if (isset($_REQUEST[$filter_param])) {
if (isset($filter_options['dont_respect_followed_categories']) && $filter_options['dont_respect_followed_categories']) {
unset($Sender->EventArguments['Wheres']['d.CategoryID']);
}
if (isset($filter_options['filter_column'])) {
$Sender->SQL->Where('discussionaboutitem.'.$filter_options['filter_column'], $_REQUEST[$filter_param]);
}
if (isset($filter_options['filter_sql'])) {
$filter_options['filter_sql']($Sender->SQL, $_REQUEST[$filter_param]);
}
}
}
if (isset($_REQUEST[$this->Config['ItemParameter']]) && is_numeric($_REQUEST[$this->Config['ItemParameter']])) {
$Sender->SQL->Where('discussionaboutitem.'.$this->Config['ItemPrimaryKey'], $_REQUEST[$this->Config['ItemParameter']]);
}
$Sender->SQL->Database->DatabasePrefix = $prefix;
}
# https://github.com/JasonBarnabe/DiscussionAbout/issues/1
# Same as above, for profile
public function DiscussionModel_BeforeGetByUser_Handler($Sender) {
$this->DiscussionModel_BeforeGet_Handler($Sender);
}
public function DiscussionModel_AfterAddColumns_Handler($Sender) {
# Remove announcements when we're filtering
if (!empty($this->GetFilterParamString())) {
$Sender->RemoveAnnouncements($Sender->EventArguments['Data']);
}
# Include related name in discussion name when rendering RSS
if (Gdn::Request()->OutputFormat() == 'rss') {
$Discussions = $Sender->EventArguments['Data'];
foreach ($Discussions->Result() as $Discussion) {
if (isset($Discussion->DiscussionAboutName) && $Discussion->DiscussionAboutName) {
$Discussion->Name.=' - '.$Discussion->DiscussionAboutName;
}
}
}
}
# Update discussion count and title for index
public function DiscussionsController_BeforeBuildPager_Handler($Sender) {
# Only do this if some of the parameters we care about are set
$FilterParams = $this->GetFilterParamString();
if (empty($FilterParams)) {
return;
}
# Add filter parameters to paginate links
$Sender->SetData('_PagerUrl', 'discussions/{Page}?'.$FilterParams);
$DiscussionModel = new DiscussionModel();
$prefix = $DiscussionModel->SQL->Database->DatabasePrefix;
$DiscussionModel->SQL->Database->DatabasePrefix = '';
$DiscussionModel->SQL
->Select('d.DiscussionID', 'count', 'CountDiscussions')
->Select('discussionaboutitem.'.$this->Config['ItemName'], '', 'DiscussionAboutItemName')
->From('GDN_Discussion d')
->Join($this->Config['ItemTable'].' discussionaboutitem', 'd.'.$this->Config['ForeignKey'].' = discussionaboutitem.'.$this->Config['ItemPrimaryKey'], 'left')
->GroupBy('DiscussionAboutItemName');
if (isset($_REQUEST[$this->Config['ItemParameter']]) && is_numeric($_REQUEST[$this->Config['ItemParameter']])) {
$DiscussionModel->SQL->Where('discussionaboutitem.'.$this->Config['ItemPrimaryKey'], $_REQUEST[$this->Config['ItemParameter']]);
}
$AdditionalFilterColumnNames = [];
foreach ($this->Config['AdditionalFilters'] as $filter_param => $filter_options) {
if (isset($_REQUEST[$filter_param])) {
if (isset($filter_options['filter_column'])) {
$DiscussionModel->SQL->Where('discussionaboutitem.'.$filter_options['filter_column'], $_REQUEST[$filter_param]);
}
if (isset($filter_options['filter_sql'])) {
$filter_options['filter_sql']($DiscussionModel->SQL, $_REQUEST[$filter_param]);
}
}
}
$DiscussionModel->SQL->Database->DatabasePrefix = $prefix;
$Row = $DiscussionModel->SQL->Get()->FirstRow();
$Sender->SetData('CountDiscussions', $Row->CountDiscussions);
$Title = 'Discussions';
# If there's no rows, we won't have the data necessary to do this
if ($Row->CountDiscussions > 0) {
$FilterNames = [];
if (isset($_REQUEST[$this->Config['ItemParameter']]) && is_numeric($_REQUEST[$this->Config['ItemParameter']])) {
$FilterNames[] = sprintf(T('DiscussionAbout.AboutItemHeader', 'about %s'), $Row->DiscussionAboutItemName);
}
foreach ($AdditionalFilterColumnNames as $ColumnName) {
$FilterNames[] = sprintf($ColumnName[1], $Row->{$ColumnName[0]});
}
$Title = 'Discussions '.implode(' ', $FilterNames);
}
$Sender->Head->AddRss(Url('/discussions/feed.rss?'.$FilterParams, TRUE), $Title);
$Sender->Head->Title($Title);
}
# Load item name for individual discussion
public function DiscussionModel_BeforeGetID_Handler($Sender) {
$prefix = $Sender->SQL->Database->DatabasePrefix;
$Sender->SQL->Database->DatabasePrefix = '';
$Sender->SQL->Join($this->Config['ItemTable'].' discussionaboutitem', 'd.'.$this->Config['ForeignKey'].' = discussionaboutitem.'.$this->Config['ItemPrimaryKey'], 'left');
$Sender->SQL->Select('discussionaboutitem.'.$this->Config['ItemName'], '', 'DiscussionAboutName');
$Sender->SQL->Database->DatabasePrefix = $prefix;
}
# Show item name after discussion name in discussion index
public function DiscussionsController_AfterDiscussionTitle_Handler($Sender) {
$Discussion = $Sender->EventArguments['Discussion'];
$Text = $Discussion->DiscussionAboutName;
$TextFunction = array_key_exists('DefaultName', $this->Config) ? $this->Config['DefaultName'] : null;
if (!isset($Text) && isset($TextFunction)) {
$Text = $TextFunction($Discussion->{$this->Config['ForeignKey']});
}
if (isset($Text)) {
if (is_numeric($Discussion->{$this->Config['ForeignKey']}) && $Discussion->{$this->Config['ForeignKey']} != 0) {
echo '<span class="DiscussionAboutListDiscussion"> - '.htmlspecialchars($Text).'</span>';
}
}
}
# Do it in the category discussion list too
public function CategoriesController_AfterDiscussionTitle_Handler($Sender) {
$this->DiscussionsController_AfterDiscussionTitle_Handler($Sender);
}
# And the profile discussion list
public function ProfileController_AfterDiscussionTitle_Handler($Sender) {
$this->DiscussionsController_AfterDiscussionTitle_Handler($Sender);
}
# Show item name in individual discussion
public function DiscussionController_AfterDiscussionTitle_Handler($Sender) {
if (isset($Sender->Discussion->{$this->Config['ForeignKey']}) && $Sender->Discussion->{$this->Config['ForeignKey']} != 0) {
$URLFunction = $this->Config['ItemIDToURL'];
$URL = null;
if (isset($URLFunction)) {
$URL = $URLFunction($Sender->Discussion->{$this->Config['ForeignKey']});
}
$Text = $Sender->Discussion->DiscussionAboutName;
$TextFunction = array_key_exists('DefaultName', $this->Config) ? $this->Config['DefaultName'] : null;
if (!isset($Text) && isset($TextFunction)) {
$Text = $TextFunction($Sender->Discussion->{$this->Config['ForeignKey']});
}
if (isset($Text)) {
if (isset($URL)) {
echo '<span class="DiscussionAboutShowDiscussion">'.sprintf(T('DiscussionAbout.AboutItemSubtitle', 'About: %s'), '<a href="'.htmlspecialchars($URL).'">'.htmlspecialchars($Text).'</a>').'</span>';
} else {
echo '<span class="DiscussionAboutShowDiscussion">'.sprintf(T('DiscussionAbout.AboutItemSubtitle', 'About: %s'), htmlspecialchars($Text)).'</span>';
}
}
}
}
# Hide category selector if we're forcing to a category
public function PostController_BeforeFormInputs_Handler($Sender) {
if ($this->GetItemID($Sender) && $this->Config['ForceToCategoryID'] != null) {
$Sender->ShowCategorySelector = false;
}
}
# Edit/new thread
public function PostController_DiscussionFormOptions_Handler($Sender) {
# Show name on new thread
if ($this->GetItemID($Sender)) {
$DiscussionAboutName = $this->GetDiscussionAboutName($Sender);
if (!isset($Sender->Discussion)) {
$URLFunction = $this->Config['ItemIDToURL'];
$URL = null;
if (isset($URLFunction)) {
$URL = $URLFunction($this->GetItemID($Sender));
}
# add item name to title
$ItemNameCode = null;
if (isset($URL)) {
$ItemNameCode = sprintf(T('DiscussionAbout.AboutItem', 'About: %s'), '<a href="'.htmlspecialchars($URL).'">'.htmlspecialchars($DiscussionAboutName).'</a>');
} else {
$ItemNameCode = sprintf(T('DiscussionAbout.AboutItem', 'About: %s'), htmlspecialchars($DiscussionAboutName));
}
$Sender->EventArguments['Options'] .= '<script>'.
'var span = document.createElement("span");'.
'span.innerHTML = '.json_encode($ItemNameCode).';'.
'var header = document.getElementById("DiscussionForm").getElementsByTagName("h1")[0];'.
'header.parentNode.insertBefore(span, header.nextSibling);'.
'</script>';
}
}
# Input to set the item ID
if (Gdn::Session()->CheckPermission('Vanilla.Discussions.Edit')) {
$Sender->EventArguments['Options'] .= "<p>".T('Item ID:')." <input type='text' name='".$this->Config['ForeignKey']."' value='".htmlspecialchars($this->GetItemID($Sender))."'></p>";
} else if ($this->GetItemID($Sender)) {
$Sender->EventArguments['Options'] .= "<input type='hidden' name='".$this->Config['ForeignKey']."' value='".htmlspecialchars($this->GetItemID($Sender))."'>";
} else if (isset($this->Config['UserEntryLabel'])) {
$Sender->EventArguments['Options'] .= "<label for='".$this->Config['ForeignKey']."'>".$this->Config['UserEntryLabel']."</label><input id='".$this->Config['ForeignKey']."' name='".$this->Config['ForeignKey']."-userentry' class='InputBox BigInput'>";
}
}
# Fix things up on saving a discussion
public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender) {
$UserEntryValue = $Sender->EventArguments['FormPostValues'][$this->Config['ForeignKey']."-userentry"];
$ItemIDToURLFunction = $this->Config['UserEntryToID'];
if ($UserEntryValue && $ItemIDToURLFunction) {
$Sender->EventArguments['FormPostValues'][$this->Config['ForeignKey']] = $ItemIDToURLFunction($UserEntryValue);
# Handle empty item id
} else if ($Sender->EventArguments['FormPostValues'][$this->Config['ForeignKey']] == '') {
$Sender->EventArguments['FormPostValues'][$this->Config['ForeignKey']] = null;
}
# Force to category
if ($Sender->EventArguments['FormPostValues'][$this->Config['ForeignKey']] != null && $this->Config['ForceToCategoryID'] != null) {
$Sender->EventArguments['FormPostValues']['CategoryID'] = $this->Config['ForceToCategoryID'];
}
}
# Return the ID of the item, whether from an existing discussion or from a request parameter
private function GetItemID($Sender) {
if (isset($Sender->Discussion) && is_numeric($Sender->Discussion->{$this->Config['ForeignKey']})) {
if ($Sender->Discussion->{$this->Config['ForeignKey']} == '0') {
return null;
}
return $Sender->Discussion->{$this->Config['ForeignKey']};
}
if (isset($_REQUEST[$this->Config['ItemParameter']]) && is_numeric($_REQUEST[$this->Config['ItemParameter']])) {
if ($_REQUEST[$this->Config['ItemParameter']] == '0') {
return null;
}
return $_REQUEST[$this->Config['ItemParameter']];
}
return null;
}
# Return the display name of the item, whether from an existing discussion or from a request parameter
private function GetDiscussionAboutName($Sender) {
$ItemID = $this->GetItemID($Sender);
$Results = $Sender->Database->Query('SELECT '.$this->Config['ItemName'].' DiscussionAboutName FROM '.$this->Config['ItemTable'].' WHERE '.$this->Config['ItemPrimaryKey'].' = '.$ItemID)->Result('DATASET_TYPE_ARRAY');
return $Results[0]->DiscussionAboutName;
}
# Returns a query string of all the filter parameters we're concerned with
private function GetFilterParamString() {
$FilterParams = '';
if (isset($_REQUEST[$this->Config['ItemParameter']]) && is_numeric($_REQUEST[$this->Config['ItemParameter']])) {
$FilterParams .= $this->Config['ItemParameter'].'='.$_REQUEST[$this->Config['ItemParameter']];
}
foreach ($this->Config['AdditionalFilters'] as $filter_param => $filter_options) {
if (isset($_REQUEST[$filter_param])) {
if (!empty($FilterParams)) {
$FilterParams .= '&';
}
$FilterParams .= $filter_param.'='.$_REQUEST[$filter_param];
}
}
return $FilterParams;
}
}