@@ -9,7 +9,6 @@ class ReplyToPlugin extends Gdn_Plugin {
9
9
10
10
const QUERY_PARAMETER_VIEW ='view ' ;
11
11
const VIEW_FLAT = 'flat ' ;
12
- const VIEW_TREE = 'tree ' ;
13
12
const VIEW_THREADED = 'threaded ' ;
14
13
15
14
private $ replyToModel ;
@@ -67,12 +66,22 @@ public function postController_render_before($sender) {
67
66
$ this ->prepareController ($ sender );
68
67
}
69
68
69
+ /**
70
+ * Add View Mode before rendering comments
71
+ * @param $sender
72
+ * @param $args
73
+ */
74
+ public function base_beforeCommentsRender_handler ($ sender , $ args ) {
75
+ $ viewMode = self ::getViewMode ();
76
+ $ sender ->setData ('ViewMode ' , $ viewMode );
77
+ }
78
+
70
79
/**
71
80
* Render View options for a discussion
72
81
* @param $sender
73
82
* @param $args
74
83
*/
75
- public function base_Replies_handler ($ sender , $ args ){
84
+ public function base_InlineDiscussionOptionsLeft_handler ($ sender , $ args ){
76
85
$ discussion = $ sender ->data ('Discussion ' );
77
86
if (!$ discussion ) {
78
87
return ;
@@ -83,13 +92,13 @@ public function base_Replies_handler($sender, $args){
83
92
}
84
93
85
94
$ discussionUrl = discussionUrl ($ discussion , '' , '/ ' );
86
- $ viewMode = getIncomingValue ( self ::QUERY_PARAMETER_VIEW , self :: VIEW_FLAT );
95
+ $ viewMode = self ::getViewMode ( );
87
96
88
- echo '<div class="ReplyViewOptions"><span class="MLabel">View: </span > ' ;
89
- echo anchor ( ' Flat ' , $ discussionUrl . ' ? ' . self :: QUERY_PARAMETER_VIEW . ' = ' . self :: VIEW_FLAT , $ viewMode == self :: VIEW_FLAT ? ' Active ' : '' ). ' | ' ;
90
- echo anchor ('Threaded ' , $ discussionUrl .'? ' .self ::QUERY_PARAMETER_VIEW .'= ' .self ::VIEW_THREADED , $ viewMode == self ::VIEW_THREADED ?'Active ' :'' ).' | ' ;
91
- echo anchor ('Tree ' , $ discussionUrl .'? ' .self ::QUERY_PARAMETER_VIEW .'= ' .self ::VIEW_TREE , $ viewMode == self ::VIEW_TREE ? ' Active ' :'' );
92
- echo '</div > ' ;
97
+ echo '<span class="ReplyViewOptions"> ' ;
98
+ echo ' <span class="MLabel">View:  </span> ' ;
99
+ echo anchor ('Threaded ' , $ discussionUrl .'? ' .self ::QUERY_PARAMETER_VIEW .'= ' .self ::VIEW_THREADED , $ viewMode == self ::VIEW_THREADED ?'ReplyViewOptionLink Active ' :'ReplyViewOptionLink ' ).' | ' ;
100
+ echo anchor ('Flat ' , $ discussionUrl .'? ' .self ::QUERY_PARAMETER_VIEW .'= ' .self ::VIEW_FLAT , $ viewMode == self ::VIEW_FLAT ? ' ReplyViewOptionLink Active ' :'ReplyViewOptionLink ' );
101
+ echo '</span > ' ;
93
102
}
94
103
95
104
/**
@@ -98,9 +107,9 @@ public function base_Replies_handler($sender, $args){
98
107
* @param $sender
99
108
*/
100
109
public function commentModel_afterConstruct_handler (&$ sender ) {
101
- self :: log ( ' commentModel_afterConstruct_handler ' , [ ' path ' => Gdn:: request ()-> pathAndQuery ()] );
102
- $ viewMode = getIncomingValue ( self :: QUERY_PARAMETER_VIEW , self :: VIEW_FLAT );
103
- if ($ viewMode == self ::VIEW_TREE || $ viewMode == self :: VIEW_THREADED ) {
110
+ $ viewMode = self :: getViewMode ( );
111
+
112
+ if ($ viewMode == self ::VIEW_THREADED ) {
104
113
$ sender ->orderBy (array ('TreeLeft asc ' , 'DateInserted asc ' ));
105
114
}
106
115
}
@@ -135,13 +144,8 @@ public function commentModel_deleteComment_handler(&$Sender) {
135
144
}
136
145
137
146
public function discussionController_BeforeCalculatingOffsetLimit_handler ($ sender , $ args ) {
138
- if (!Gdn::session ()->isValid ()) {
139
- return ;
140
- }
141
- $ viewMode = getIncomingValue (self ::QUERY_PARAMETER_VIEW );
142
- if (!$ viewMode ) {
143
- return ;
144
- }
147
+ $ viewMode = self ::getViewMode ();
148
+ // $offsetProvided = $args['OffsetProvided'];
145
149
$ discussion = $ args ['Discussion ' ];
146
150
$ offset = & $ args ['Offset ' ];
147
151
$ limit = & $ args ['Limit ' ];
@@ -152,7 +156,6 @@ public function discussionController_BeforeCalculatingOffsetLimit_handler($sende
152
156
}
153
157
154
158
if ($ viewMode === self ::VIEW_FLAT ) {
155
- $ offset = 0 ;
156
159
$ enableAutoOffset = false ;
157
160
} else {
158
161
// Show all comment on one offset for Tree/Threaded View
@@ -176,7 +179,7 @@ public function discussionController_beforeDiscussionRender_handler($sender, $ar
176
179
return ;
177
180
}
178
181
179
- $ viewMode = getIncomingValue ( self ::QUERY_PARAMETER_VIEW , self :: VIEW_FLAT );
182
+ $ viewMode = self ::getViewMode ( );
180
183
if ($ viewMode == self ::VIEW_FLAT ) {
181
184
return ;
182
185
}
@@ -196,32 +199,30 @@ public function base_commentOptions_handler($sender, $args) {
196
199
return ;
197
200
}
198
201
$ discussion = $ sender ->data ('Discussion ' );
199
- $ isClosed = ((int )$ discussion ->Closed ) == 1 ;
200
- if ($ isClosed ) {
201
- return ;
202
- }
203
202
204
203
//Check permission
205
- if (isset ($ discussion ->PermissionCategoryID )) {
206
- $ CategoryID = val ('PermissionCategoryID ' , $ discussion );
207
- } else {
208
- $ CategoryID = $ discussion ->CategoryID ;
209
- }
204
+ $ CategoryID = val ('PermissionCategoryID ' , $ discussion )? val ('PermissionCategoryID ' , $ discussion ):val ('CategoryID ' , $ discussion );
205
+ $ userCanClose = CategoryModel::checkPermission ($ CategoryID , 'Vanilla.Discussions.Close ' );
206
+ $ userCanComment = CategoryModel::checkPermission ($ CategoryID , 'Vanilla.Comments.Add ' );
210
207
211
- // Can the user comment on this category, and is the discussion open for comments?
212
- if (!Gdn:: Session ()-> CheckPermission ( ' Vanilla.Comments.Add ' , TRUE , ' Category ' , $ CategoryID ) ) {
208
+ $ canAddComment = ( $ discussion -> Closed == ' 1 ' && $ userCanClose ) || ( $ discussion-> Closed == ' 0 ' && $ userCanComment );
209
+ if (!$ canAddComment ) {
213
210
return ;
214
211
}
212
+ // Can the user comment on this category, and is the discussion open for comments?
213
+ // if (!Gdn::Session()->CheckPermission('Vanilla.Comments.Add', TRUE, 'Category', $CategoryID)) {
214
+ // return;
215
+ // }
215
216
216
217
$ options = &$ args ['CommentOptions ' ];
217
- $ comment = & $ args ['Comment ' ];
218
+ $ comment = $ args ['Comment ' ];
218
219
$ options ['ReplyToComment ' ] = [
219
220
'Label ' => t ('Reply ' ),
220
221
'Url ' => '/?ParentCommentID= ' .$ comment ->CommentID ,
221
222
'Class ' => 'ReplyComment '
222
223
];
223
224
224
- $ viewMode = getIncomingValue ( self ::QUERY_PARAMETER_VIEW , self :: VIEW_FLAT );
225
+ $ viewMode = self ::getViewMode ( );
225
226
foreach ($ options as $ key => $ value ) {
226
227
$ currentUrl = $ options [$ key ]['Url ' ];
227
228
if (strpos ($ currentUrl , '? ' ) !== false ) {
@@ -236,26 +237,70 @@ public function base_commentOptions_handler($sender, $args) {
236
237
}
237
238
}
238
239
240
+ /**
241
+ * Add 'Reply' option to discussion.
242
+ *
243
+ * @param Gdn_Controller $sender
244
+ * @param array $args
245
+ */
246
+ public function base_inlineDiscussionOptions_handler ($ sender , $ args ) {
247
+ $ discussion = $ args ['Discussion ' ];
248
+ if (!$ discussion ) {
249
+ return ;
250
+ }
251
+
252
+ if (!Gdn::session ()->UserID ) {
253
+ return ;
254
+ }
255
+
256
+ //Check permission
257
+ $ CategoryID = val ('PermissionCategoryID ' , $ discussion )? val ('PermissionCategoryID ' , $ discussion ):val ('CategoryID ' , $ discussion );
258
+ $ userCanClose = CategoryModel::checkPermission ($ CategoryID , 'Vanilla.Discussions.Close ' );
259
+ $ userCanComment = CategoryModel::checkPermission ($ CategoryID , 'Vanilla.Comments.Add ' );
260
+
261
+ // See the 'writeCommentForm' method vanilla/applications/vanilla/views/discussion/helper_functions.php
262
+ $ canAddComment = ($ discussion ->Closed == '1 ' && $ userCanClose ) || ($ discussion ->Closed == '0 ' && $ userCanComment );
263
+ if (!$ canAddComment ) {
264
+ return ;
265
+ }
266
+
267
+ // DropdownModule options
268
+ $ options = & $ args ['DiscussionOptions ' ];
269
+ $ options ->addLink ('Reply ' , url ("/ " , true ), 'reply ' , 'ReplyComment ' );
270
+ }
271
+
239
272
/**
240
273
* Insert the indentation classes into the comment.
274
+ * All rendering options should be set before displaying comments
241
275
* @param $sender
242
276
* @param $args
243
277
*/
244
278
public function base_beforeCommentDisplay_handler ($ sender , $ args ) {
245
- ReplyToPlugin::log ('base_beforeCommentDisplay_handler ' , []);
246
-
247
- $ viewMode = getIncomingValue (self ::QUERY_PARAMETER_VIEW , self ::VIEW_FLAT );
248
- if ($ viewMode == self ::VIEW_FLAT ) {
249
- return ;
250
- }
251
279
if ($ sender ->deliveryType () != DELIVERY_TYPE_ALL ) {
252
- ReplyToPlugin::log ('base_beforeCommentDisplay_handler ' , ['' ]);
253
- $ this ->buildCommentReplyToCssClasses ($ sender );
280
+ // Ajax request to post new comments or update comments
281
+ if (isset ($ _SERVER ['HTTP_REFERER ' ])) {
282
+ $ previous = $ _SERVER ['HTTP_REFERER ' ];
283
+ $ query = parse_url ($ previous , PHP_URL_QUERY );
284
+ parse_str ($ query , $ params );
285
+ $ viewMode = $ params ['view ' ];
286
+ if (!$ viewMode ) {
287
+ $ viewMode = self ::isPagingUrl ($ previous ) ? self ::VIEW_FLAT : self ::VIEW_THREADED ;
288
+ }
289
+ $ sender ->setData ('ViewMode ' , $ viewMode );
290
+ if ($ viewMode == self ::VIEW_THREADED ) {
291
+ $ this ->buildCommentReplyToCssClasses ($ sender );
292
+ }
293
+ }
294
+ } else {
295
+ $ viewMode = self ::getViewMode ();
296
+ if ($ viewMode == self ::VIEW_THREADED ) {
297
+ $ this ->buildCommentReplyToCssClasses ($ sender );
298
+ }
254
299
}
255
300
$ comment = &$ args ['Comment ' ];
256
301
$ cssClass = &$ args ['CssClass ' ];
257
- $ displayBody = &$ args ['DisplayBody ' ];
258
- $ displayBody = $ viewMode == self ::VIEW_FLAT || $ viewMode == self ::VIEW_THREADED ;
302
+ // $displayBody = &$args['DisplayBody'];
303
+ // $displayBody = $viewMode == self::VIEW_FLAT || $viewMode == self::VIEW_THREADED;
259
304
$ cssClass .= (!empty ($ comment ->ReplyToClass )? ' ' . $ comment ->ReplyToClass : '' );
260
305
}
261
306
@@ -330,6 +375,19 @@ private function buildCommentReplyToCssClasses(&$sender){
330
375
}
331
376
}
332
377
378
+ private static function isPagingUrl ($ url ) {
379
+ return preg_match ('/\/p\d+$/ ' , $ url );
380
+ }
381
+
382
+ private static function getViewMode (){
383
+ $ viewMode = getIncomingValue (self ::QUERY_PARAMETER_VIEW );
384
+ if (!$ viewMode ) {
385
+ $ viewMode = self ::isPagingUrl (Gdn::request ()->path ())? self ::VIEW_FLAT : self ::VIEW_THREADED ;
386
+ }
387
+
388
+ return $ viewMode ;
389
+ }
390
+
333
391
public static function log ($ message , $ data ) {
334
392
if (c ('Debug ' )) {
335
393
Logger::event (
0 commit comments