[pLog-svn] r1909 - in plugins/trunk: . nestedcomments
oscar at devel.plogworld.net
oscar at devel.plogworld.net
Wed Apr 27 18:30:28 GMT 2005
Author: oscar
Date: 2005-04-27 18:30:28 +0000 (Wed, 27 Apr 2005)
New Revision: 1909
Added:
plugins/trunk/nestedcomments/
plugins/trunk/nestedcomments/pluginnestedcomments.class.php
Log:
added a plugin to show post comments in a nested/threaded fashion
Added: plugins/trunk/nestedcomments/pluginnestedcomments.class.php
===================================================================
--- plugins/trunk/nestedcomments/pluginnestedcomments.class.php 2005-04-27 17:45:32 UTC (rev 1908)
+++ plugins/trunk/nestedcomments/pluginnestedcomments.class.php 2005-04-27 18:30:28 UTC (rev 1909)
@@ -0,0 +1,110 @@
+<?php
+
+ include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
+ include_once( PLOG_CLASS_PATH."class/dao/articlecomments.class.php" );
+
+ /**
+ * change this constant in case you want to redefine the limit for nested
+ * comments
+ */
+ define( "MAX_NESTED_COMMENTS", 6 );
+
+ class PluginNestedComments extends PluginBase
+ {
+
+ function PluginNestedComments()
+ {
+ $this->id = "nestedcomments";
+ $this->author = "The pLog Team";
+ $this->locales = Array();
+
+ // description
+ $this->desc = <<<EOD
+This plugin will generate a nested list of comments. In order to use it, edit your postandcomments.template file
+and where it says:
+<pre>
+ {foreach from=\$comments item=comment}
+</pre>
+with:
+<pre>
+ {assign var=comments value=\$nestedcomments->getNestedComments(\$post)}
+ {foreach from=\$comments item=comment}
+</pre>
+In order to nest the comments, you can check the value of the "depth" field in order to know how
+deeply a certain comment should be nested but keeping in mind that the array of comments is already
+sorted correctly:
+<pre>
+ {assign var=comments value=\$nestedcomments->getNestedComments(\$post)}
+ {foreach from=\$comments item=comment}
+ <div class="comment_{\$comment->getValue("depth")}">
+ {\$comment->getTopic()}<br/>
+ {\$comment->getText()}<br/>
+ </div>
+ {/foreach}
+</pre>
+In order to apply different graphical styles for different levels of nesting, define up to 6
+"comment_X" CSS classes (as in "comment_1", "comment_2", "comment_3", etc) in the CSS file used
+by your template set.
+<pre>
+ .comment_1 {
+ margin-left: 10px;
+ }
+ .comment_2 {
+ margin-left: 20px;
+ }
+ .comment_3 {
+ margin-left: 30px;
+ }
+ ...
+ .comment_6 {
+ margin-left: 60px;
+ }
+</pre>
+By default there can only
+be up to 6 levels of nesting but this limit is configurable by editing the plugin file.
+EOD;
+ }
+
+ /**
+ * @private
+ */
+ function _nestComments( $comments, $allComments, $depth = 0 )
+ {
+ $result = Array();
+ foreach( $comments as $comment ) {
+ if( $depth > MAX_NESTED_COMMENTS )
+ $depth = MAX_NESTED_COMMENTS;
+ $comment->setValue( "depth", $depth+1 );
+ $result[] = $comment;
+ if( isset($allComments[$comment->getId()]))
+ $result = array_merge( $result, $this->_nestComments( $allComments[$comment->getId()], $allComments, $depth+1 ));
+ }
+ return( $result );
+ }
+
+ /**
+ * returns an array with the comments already sorted in the same way as they should
+ * be nested. Use $comment->getValue( "depth" ) in order to know how deeply the comment
+ * should be nested.
+ *
+ * @param post
+ * @return An array of of UserComment objects
+ */
+ function getNestedComments( $post )
+ {
+ $nestedComments = Array();
+ $postComments = $post->getComments();
+ foreach( $postComments as $comment ) {
+ $key = $comment->getParentId();
+ if( $nestedComments["$key"] == "" )
+ $nestedComments["$key"] = Array();
+
+ $nestedComments["$key"][] = $comment;
+ }
+
+ $comments = $this->_nestComments( $nestedComments[0], $nestedComments );
+
+ return($comments);
+ }
+ }
+?>
\ No newline at end of file
More information about the pLog-svn
mailing list