@@ -9,7 +9,6 @@ class ReplyToPlugin extends Gdn_Plugin {
99
1010 const QUERY_PARAMETER_VIEW ='view ' ;
1111 const VIEW_FLAT = 'flat ' ;
12- const VIEW_TREE = 'tree ' ;
1312 const VIEW_THREADED = 'threaded ' ;
1413
1514 private $ replyToModel ;
@@ -67,12 +66,22 @@ public function postController_render_before($sender) {
6766 $ this ->prepareController ($ sender );
6867 }
6968
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+
7079 /**
7180 * Render View options for a discussion
7281 * @param $sender
7382 * @param $args
7483 */
75- public function base_Replies_handler ($ sender , $ args ){
84+ public function base_InlineDiscussionOptionsLeft_handler ($ sender , $ args ){
7685 $ discussion = $ sender ->data ('Discussion ' );
7786 if (!$ discussion ) {
7887 return ;
@@ -83,13 +92,13 @@ public function base_Replies_handler($sender, $args){
8392 }
8493
8594 $ discussionUrl = discussionUrl ($ discussion , '' , '/ ' );
86- $ viewMode = getIncomingValue ( self ::QUERY_PARAMETER_VIEW , self :: VIEW_FLAT );
95+ $ viewMode = self ::getViewMode ( );
8796
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 > ' ;
93102 }
94103
95104 /**
@@ -98,9 +107,9 @@ public function base_Replies_handler($sender, $args){
98107 * @param $sender
99108 */
100109 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 ) {
104113 $ sender ->orderBy (array ('TreeLeft asc ' , 'DateInserted asc ' ));
105114 }
106115 }
@@ -135,13 +144,8 @@ public function commentModel_deleteComment_handler(&$Sender) {
135144 }
136145
137146 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'];
145149 $ discussion = $ args ['Discussion ' ];
146150 $ offset = & $ args ['Offset ' ];
147151 $ limit = & $ args ['Limit ' ];
@@ -152,7 +156,6 @@ public function discussionController_BeforeCalculatingOffsetLimit_handler($sende
152156 }
153157
154158 if ($ viewMode === self ::VIEW_FLAT ) {
155- $ offset = 0 ;
156159 $ enableAutoOffset = false ;
157160 } else {
158161 // Show all comment on one offset for Tree/Threaded View
@@ -176,7 +179,7 @@ public function discussionController_beforeDiscussionRender_handler($sender, $ar
176179 return ;
177180 }
178181
179- $ viewMode = getIncomingValue ( self ::QUERY_PARAMETER_VIEW , self :: VIEW_FLAT );
182+ $ viewMode = self ::getViewMode ( );
180183 if ($ viewMode == self ::VIEW_FLAT ) {
181184 return ;
182185 }
@@ -196,32 +199,30 @@ public function base_commentOptions_handler($sender, $args) {
196199 return ;
197200 }
198201 $ discussion = $ sender ->data ('Discussion ' );
199- $ isClosed = ((int )$ discussion ->Closed ) == 1 ;
200- if ($ isClosed ) {
201- return ;
202- }
203202
204203 //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 ' );
210207
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 ) {
213210 return ;
214211 }
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+ // }
215216
216217 $ options = &$ args ['CommentOptions ' ];
217- $ comment = & $ args ['Comment ' ];
218+ $ comment = $ args ['Comment ' ];
218219 $ options ['ReplyToComment ' ] = [
219220 'Label ' => t ('Reply ' ),
220221 'Url ' => '/?ParentCommentID= ' .$ comment ->CommentID ,
221222 'Class ' => 'ReplyComment '
222223 ];
223224
224- $ viewMode = getIncomingValue ( self ::QUERY_PARAMETER_VIEW , self :: VIEW_FLAT );
225+ $ viewMode = self ::getViewMode ( );
225226 foreach ($ options as $ key => $ value ) {
226227 $ currentUrl = $ options [$ key ]['Url ' ];
227228 if (strpos ($ currentUrl , '? ' ) !== false ) {
@@ -236,26 +237,70 @@ public function base_commentOptions_handler($sender, $args) {
236237 }
237238 }
238239
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+
239272 /**
240273 * Insert the indentation classes into the comment.
274+ * All rendering options should be set before displaying comments
241275 * @param $sender
242276 * @param $args
243277 */
244278 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- }
251279 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+ }
254299 }
255300 $ comment = &$ args ['Comment ' ];
256301 $ 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;
259304 $ cssClass .= (!empty ($ comment ->ReplyToClass )? ' ' . $ comment ->ReplyToClass : '' );
260305 }
261306
@@ -330,6 +375,19 @@ private function buildCommentReplyToCssClasses(&$sender){
330375 }
331376 }
332377
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+
333391 public static function log ($ message , $ data ) {
334392 if (c ('Debug ' )) {
335393 Logger::event (
0 commit comments