@@ -10,6 +10,7 @@ class ReplyToPlugin extends Gdn_Plugin {
10
10
const QUERY_PARAMETER_VIEW ='view ' ;
11
11
const VIEW_FLAT = 'flat ' ;
12
12
const VIEW_THREADED = 'threaded ' ;
13
+ const VIEW_MODE = 'ReplyTo.ViewMode ' ;
13
14
14
15
private $ replyToModel ;
15
16
/**
@@ -56,32 +57,110 @@ public function assetModel_styleCss_handler($sender) {
56
57
// Set JS for this plugin.
57
58
protected function prepareController (&$ sender ) {
58
59
$ sender ->addJsFile ('replyto.js ' , 'plugins/ReplyTo ' );
60
+ }
61
+
62
+ /**
63
+ * Set a view mode for Discussion Controller
64
+ * View Mode is calculated from request url.
65
+ * Flat mode is used - '/discussion/{DiscussionID}/p{Page}
66
+ * Threaded mode is used by default
67
+ *
68
+ * @param $sender
69
+ * @param $args
70
+ */
71
+ public function discussionController_initialize_handler ($ sender , $ args ) {
72
+ $ viewMode = self ::getViewMode ();
73
+ $ sender ->setData (self ::VIEW_MODE , $ viewMode );
74
+
75
+ }
76
+
77
+ /**
78
+ * Set a view mode for Post Controller
79
+ * Replying to a comment and leaving a comment are processed by Post Controller.
80
+ * (the url 'post/comment/, 'post' method).
81
+ * Use HTTP_REFERER to get the current view mode
82
+ * @param $sender
83
+ * @param $args
84
+ */
85
+ public function postController_initialize_handler ($ sender , $ args ) {
86
+ if (isset ($ _SERVER ['HTTP_REFERER ' ])) {
87
+ $ url = $ _SERVER ['HTTP_REFERER ' ];
88
+ }
89
+ parse_str ( parse_url ( $ url , PHP_URL_QUERY ), $ array );
90
+ $ viewMode = $ array [self ::QUERY_PARAMETER_VIEW ];
91
+ if (!$ viewMode ) {
92
+ $ viewMode = self ::isPagingUrl ($ url )? self ::VIEW_FLAT : self ::VIEW_THREADED ;
93
+ }
94
+ $ sender ->setData (self ::VIEW_MODE , $ viewMode );
59
95
}
60
96
61
97
public function discussionController_render_before (&$ sender ) {
62
98
$ this ->prepareController ($ sender );
63
99
}
64
100
101
+ /**
102
+ * After deleting a comment in a threaded view, the comment tree should be re-rendered
103
+ * because tree left/right might be changed if a parent comment has been deleted.
104
+ * deliveryType is VIEW in a threaded view
105
+ * deliveryType is BOOL in a flat view. Don't re-render a view. Deleted comment
106
+ * is hidden on the client.
107
+ *
108
+ * @param $sender
109
+ * @param $args
110
+ */
111
+ public function discussionController_AfterCommentDeleted_handler ($ sender , $ args ) {
112
+ $ viewMode = $ sender ->data ('ReplyTo.ViewMode ' );
113
+ if ($ sender ->deliveryMethod () == DELIVERY_METHOD_JSON ) {
114
+ $ discussionID = $ args ['DiscussionID ' ];
115
+ $ sender ->json (self ::VIEW_MODE , $ viewMode );
116
+ if ($ viewMode == self ::VIEW_THREADED ) {
117
+ // Show all comments
118
+ $ commentModel = new CommentModel ();
119
+ $ CountComments = $ commentModel ->getCountByDiscussion ($ discussionID );
120
+ $ sender ->setData ('Comments ' , $ commentModel ->getByDiscussion ($ discussionID , $ CountComments , 0 ));
121
+ $ sender ->ClassName = 'DiscussionController ' ;
122
+ $ sender ->ControllerName = 'discussion ' ;
123
+ $ sender ->View = 'comments ' ;
124
+ }
125
+ }
126
+ }
127
+
65
128
public function postController_render_before ($ sender ) {
66
129
$ this ->prepareController ($ sender );
67
130
}
68
131
69
132
/**
70
- * Add View Mode before rendering comments
133
+ * The 'beforeCommentRender' are fired by DiscussionController and PostController.
134
+ * Re-render a comment tree if new comment is added in threaded view.
135
+ *
71
136
* @param $sender
72
137
* @param $args
73
138
*/
74
- public function base_beforeCommentsRender_handler ($ sender , $ args ) {
75
- $ viewMode = self ::getViewMode ();
76
- $ sender ->setData ('ViewMode ' , $ viewMode );
139
+ public function base_beforeCommentRender_handler ($ sender , $ args ) {
140
+ // Editing existing comment or new comment added
141
+ if ($ sender ->deliveryType () != DELIVERY_TYPE_DATA ) {
142
+ $ sender ->json ('ReplyTo.ViewMode ' , $ sender ->data (self ::VIEW_MODE ));
143
+ $ isNewComment = $ sender ->data ('NewComments ' );
144
+ if ($ isNewComment ) {
145
+ $ discussionID = val ('DiscussionID ' , $ args ['Discussion ' ]);
146
+ $ commentModel = new CommentModel ();
147
+ $ countComments = $ commentModel ->getCountByDiscussion ($ discussionID );
148
+ // FIX: https://github.com/topcoder-platform/forums/issues/511
149
+ // Render a full comment tree in threaded mode
150
+ if ($ sender ->data (self ::VIEW_MODE ) == self ::VIEW_THREADED ) {
151
+ // Show all comments
152
+ $ sender ->setData ('Comments ' , $ commentModel ->getByDiscussion ($ discussionID , $ countComments , 0 ));
153
+ }
154
+ }
155
+ }
77
156
}
78
157
79
158
/**
80
159
* Render View options for a discussion
81
160
* @param $sender
82
161
* @param $args
83
162
*/
84
- public function base_InlineDiscussionOptionsLeft_handler ($ sender , $ args ){
163
+ public function discussionController_InlineDiscussionOptionsLeft_handler ($ sender , $ args ){
85
164
$ discussion = $ sender ->data ('Discussion ' );
86
165
if (!$ discussion ) {
87
166
return ;
@@ -92,7 +171,7 @@ public function base_InlineDiscussionOptionsLeft_handler($sender, $args){
92
171
}
93
172
94
173
$ discussionUrl = discussionUrl ($ discussion , '' , '/ ' );
95
- $ viewMode = self ::getViewMode ( );
174
+ $ viewMode = $ sender -> data ( self ::VIEW_MODE );
96
175
97
176
echo '<span class="ReplyViewOptions"> ' ;
98
177
echo '<span class="MLabel">View: </span> ' ;
@@ -143,8 +222,16 @@ public function commentModel_deleteComment_handler(&$Sender) {
143
222
$ this ->replyToModel ->onDeleteComment ($ Comment );
144
223
}
145
224
225
+ /**
226
+ * Set offset and limit depends on view mode.
227
+ * In the threaded mode, all comments are displayed.
228
+ * In the flat mode, comments are displayed with pagination.
229
+ * The hook is used when rendering a discussion page with comments
230
+ * @param $sender
231
+ * @param $args
232
+ */
146
233
public function discussionController_BeforeCalculatingOffsetLimit_handler ($ sender , $ args ) {
147
- $ viewMode = self ::getViewMode ( );
234
+ $ viewMode = $ sender -> data ( self ::VIEW_MODE );
148
235
// $offsetProvided = $args['OffsetProvided'];
149
236
$ discussion = $ args ['Discussion ' ];
150
237
$ offset = & $ args ['Offset ' ];
@@ -179,7 +266,7 @@ public function discussionController_beforeDiscussionRender_handler($sender, $ar
179
266
return ;
180
267
}
181
268
182
- $ viewMode = self ::getViewMode ( );
269
+ $ viewMode = $ sender -> data ( self ::VIEW_MODE );
183
270
if ($ viewMode == self ::VIEW_FLAT ) {
184
271
return ;
185
272
}
@@ -222,19 +309,16 @@ public function base_commentOptions_handler($sender, $args) {
222
309
'Class ' => 'ReplyComment '
223
310
];
224
311
225
- $ viewMode = self ::getViewMode ();
312
+ $ viewMode = $ sender ->data (self ::VIEW_MODE );
313
+ $ deliveryType = $ viewMode == self ::VIEW_THREADED ? DELIVERY_TYPE_VIEW : DELIVERY_TYPE_BOOL ;
226
314
foreach ($ options as $ key => $ value ) {
227
- $ currentUrl = $ options [$ key ]['Url ' ];
228
- if (strpos ($ currentUrl , '? ' ) !== false ) {
229
- if (strpos ($ currentUrl , 'Target ' ) !== false ) {
230
- $ options [$ key ]['Url ' ] = $ currentUrl .urlencode ('?view= ' .$ viewMode );
231
- } else {
232
- $ options [$ key ]['Url ' ] = $ currentUrl . '&view= ' . $ viewMode ;
233
- }
234
- } else {
235
- $ options [$ key ]['Url ' ] = $ currentUrl .'?view= ' .$ viewMode ;
315
+ $ options [$ key ]['Url ' ] = strpos ($ options [$ key ]['Url ' ], '? ' ) !== false ? $ options [$ key ]['Url ' ]: $ options [$ key ]['Url ' ].'? ' ;
316
+ $ options [$ key ]['Url ' ] .= '&view= ' . $ viewMode ;
317
+ if ($ key == 'DeleteComment ' ) {
318
+ $ options [$ key ]['Url ' ] .='&deliveryType= ' .$ deliveryType ;
236
319
}
237
320
}
321
+
238
322
}
239
323
240
324
/**
@@ -276,7 +360,7 @@ public function base_inlineDiscussionOptions_handler($sender, $args) {
276
360
* @param $args
277
361
*/
278
362
public function base_beforeCommentDisplay_handler ($ sender , $ args ) {
279
- if ($ sender ->deliveryType () != DELIVERY_TYPE_ALL ) {
363
+ if ($ sender ->deliveryType () != DELIVERY_TYPE_ALL ) { // Editing a comment is processed by PostController
280
364
// Ajax request to post new comments or update comments
281
365
if (isset ($ _SERVER ['HTTP_REFERER ' ])) {
282
366
$ previous = $ _SERVER ['HTTP_REFERER ' ];
@@ -286,13 +370,13 @@ public function base_beforeCommentDisplay_handler($sender, $args) {
286
370
if (!$ viewMode ) {
287
371
$ viewMode = self ::isPagingUrl ($ previous ) ? self ::VIEW_FLAT : self ::VIEW_THREADED ;
288
372
}
289
- $ sender ->setData (' ViewMode ' , $ viewMode );
373
+ $ sender ->setData (self :: VIEW_MODE , $ viewMode );
290
374
if ($ viewMode == self ::VIEW_THREADED ) {
291
375
$ this ->buildCommentReplyToCssClasses ($ sender );
292
376
}
293
377
}
294
378
} else {
295
- $ viewMode = self ::getViewMode ( );
379
+ $ viewMode = $ sender -> data ( self ::VIEW_MODE );
296
380
if ($ viewMode == self ::VIEW_THREADED ) {
297
381
$ this ->buildCommentReplyToCssClasses ($ sender );
298
382
}
0 commit comments