1 (edited by 2009-05-18 09:01:40)

Topic: [HACK] Threaded comments

Name: Nested comments
Author: FUNimations
CuteNews Compatibility: 1.3.6. - * (no incompatibility reported yet)
Description: Alow people to comment on comments.
Instructions:
before we start let me point you out the following
This forum splits long lines into 2 lines. So

//##############


becomes

//#####
####

It must be 1 line when you paste it so check this.
1. open shows.inc.php
find

$mail = trim($mail);

add below

//Nested Comments//
        if( (string)$_POST['replyID'] == "false" )
        {
            $post_id = (int) $_POST['id'];  // Yes it's stupid how I didn't thought about this :/
        }
        else
            $post_id = (int) $_POST['replyID'];
        //Nested Comments


find

$mail                 = replace_comment("add", preg_replace("/\n/", "",$mail));


add below

//Nested comments//
                                $replyID = $_POST['replyID'];
                                $subID = $_POST['subID'];
                                //Nested comments//


find

//----------------------------------
    // Add The Comment ... Go Go GO!
    //----------------------------------

add above<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>//################################################################################
####################################
//                         Add Nested Comment
//################################################################################
####################################
if(isset($_POST['replyID']) && (string)$_POST['replyID'] !== "false")
{
        // Add Blank Comment In The Active_Comments_File
        $all_comments = file($comm_file);
        $new_com_db = fopen($comm_file, "w");

        foreach($all_comments as $comment_line)
        {
            $comment_line = trim($comment_line);
            $comment_line_arr = explode("|>|", $comment_line);
            if((int)$_POST['subID'] == (int)$comment_line_arr[0])
            {
                $individual_comments = explode("||", $comment_line_arr[1]);
                unset($output);
                $output = $_POST['subID']."|>|";
                foreach($individual_comments as $comment)
                {

                    $comment_arr = explode("|", $comment);
                    if( !empty($comment_arr[0] ) && $comment_arr[5] == "empty" && (int)$comment_arr[0] == (int)$_POST['replyID'])
                    {
                        fwrite($new_com_db, $_POST['replyID']."|>|\n");
                        $comment_arr[5] = $_POST['replyID'];
                        $output .= implode( "|",$comment_arr )."||";
                    }
                    elseif( !empty($comment_arr[0] ) )
                        $output .= $comment."||";
                }
                fwrite($new_com_db, $output."\n");
            }
            else
            {
                fwrite($new_com_db, $comment_line."\n");
            }
        }
        fclose($new_com_db);
}</div>
find

//----------------------------------
    // Add The Comment ... Go Go GO!
    //----------------------------------

    $old_comments = file("$comm_file");
    $new_comments = fopen("$comm_file", "w");
    @flock ($new_comments,2);
    $found = FALSE;
    foreach($old_comments as $old_comments_line)
    {
     $old_comments_arr = explode("|>|", $old_comments_line);
     if($old_comments_arr[0] == $id)
     {
        $old_comments_arr[1] = trim($old_comments_arr[1]);
        fwrite($new_comments, "$old_comments_arr[0]|>|$old_comments_arr[1]$time|$name|$mail|$ip|$comments||\n");
        $found = TRUE;
     }else{
        fwrite($new_comments, $old_comments_line);
        //if we do not have the news ID in the comments.txt we are not doing anything (see comment below) (must make sure the news ID is valid)
     }
    }
    if(!$found){
            /* // do not add comment if News ID is not found \\ fwrite($new_comments, "$id|>|$time|$name|$mail|$ip|$comments||\n");*/

                echo("<div style=\"text-align: center;\">CuteNews did not added your comment because there is some problem with the comments database.
[url=]go back[/url]</div>");
                        $CN_HALT = TRUE;
                        break 1;
    }
    @flock ($new_comments,3);
    fclose($new_comments);

replace with

//----------------------------------
        // Add The Comment ... Go Go GO!
        //----------------------------------

        $old_comments = file("$comm_file");
        $new_comments = fopen("$comm_file", "w");
        @flock ($new_comments,2);
        $found = FALSE;
        foreach($old_comments as $old_comments_line)
        {
         $old_comments_arr = explode("|>|", $old_comments_line);
         //Nested Comments//
         if($old_comments_arr[0] == $post_id)
         {
         //Nested Commetns//
            $old_comments_arr[1] = trim($old_comments_arr[1]);
            //Nested Comments//
            fwrite($new_comments, "$old_comments_arr[0]|>|$old_comments_arr[1]$time|$name|$mail|$ip|$comments|empty||\n");
    //Nested Comments//
            $found = TRUE;
         }else{
            fwrite($new_comments, $old_comments_line);
            //if we do not have the news ID in the comments.txt we are not doing anything (see comment below) (must make sure the news ID is valid)
         }
        }

        if(!$found){
                /* // do not add comment if News ID is not found \\ fwrite($new_comments, "$id|>|$time|$name|$mail|$ip|$comments||\n");*/

                    echo("<div style=\"text-align: center;\">CuteNews did not added your comment because there is some problem with the comments database.
[url=]go back[/url]</div>");
                            $CN_HALT = TRUE;
                            break 1;
        }
        @flock ($new_comments,3);
    fclose($new_comments);

find

<input type=\"hidden\" name=\"ucat\" value=\"$ucat\" />
             $user_post_query
             <input type=\"submit\" /> \n 

replace with

<input type=\"hidden\" name=\"ucat\" value=\"$ucat\" />
             <input type=\"hidden\" name=\"replyID\" value=\"$replyID\" />
             <input type=\"hidden\" name=\"subID\" value=\"$subID\" />
             $user_post_query
             <input type=\"submit\" /> \n 

next find

//################################################################################
####################################
//                 Show Comments
//################################################################################
####################################
if($allow_comments){

and

//----------------------------------
    // Prepare the Comment Pagination
    //----------------------------------


and replace everything in between with

$comm_per_page = $config_comments_per_page;
        $total_comments = CountComments($id);
        $showed_comments = 0;
        $comment_number = 0;
        $showed = 0;
        commentsOutput( $id, $comm_file, $id);


find

echo"<form  $CN_remember_form  method=\"post\" name=\"comment\" id=\"comment\" action=\"\">".$template_form."<div><input type=\"hidden\" name=\"subaction\" value=\"addcomment\" /><input type=\"hidden\" name=\"ucat\" value=\"$ucat\" /><input type=\"hidden\" name=\"show\" value=\"$show\" /><input name=\"id\" type=\"hidden\" value=\"$_GET[id]\" />$user_post_query</div></form>
                    \n $CN_remember_include";

replace with

echo"<form  $CN_remember_form  method=\"post\" name=\"comment\" id=\"comment\" action=\"\">".$template_form."<div><input type=\"hidden\" name=\"subaction\" value=\"addcomment\" /><input type=\"hidden\" name=\"ucat\" value=\"$ucat\" /><input type=\"hidden\" name=\"show\" value=\"$show\" /><input name=\"id\" type=\"hidden\" value=\"$_GET[id]\" /><input type=\"hidden\" name=\"replyID\" id=\"replyID\" value=\"false\" /><input type=\"hidden\" name=\"subID\" id=\"subID\" value=\"false\" />$user_post_query</div></form>
                    \n $CN_remember_include";


That's it for shows.inc.php
2. open functions.inc.php
find the countComments function, and replace that function with this code.
<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>////////////////////////////////////////////////////////
// Function:         CountComments
// Description: Count How Many Comments Have a Specific Article
function CountComments($id, $archive = FALSE, $result = 0){

    global $cutepath;

    if($cutepath == ""){ $cutepath = "."; }
    if($archive and ($archive != "postponed" and $archive != "unapproved")){ $all_comments = file("$cutepath/data/archives/${archive}.comments.arch"); }
    else{ $all_comments = file("$cutepath/data/comments.txt"); }

    foreach($all_comments as $comment_line)
    {
        $news_id = explode("|>|", $comment_line);
        if($news_id[0] == $id)
        {
            $all_comments = explode("||", $news_id[1]);
            foreach($all_comments as $comment)
            {
                $arr = explode("|",$comment);
                if( !empty($arr[5]) )
                {
                    $result++;
                    if( $arr[5] != "empty" )
                        $result = CountComments( $arr[5], $archive, $result);
                }
            }

        }
    }

return $result;
}</div>
next find

?>


add above (note that the forum splits the word "javascript" into "java" & "script", this should be 1 word)<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>///////////////////////////////////////////////////////
// Function:         Comments output
// Description:      for nested comments

function commentsOutput( $id, $comm_file, $subID, $margin = 0 )
{
        global $total_comments, $showed_comments, $comment_number, $showed, $comm_per_page, $config_reverse_comments, $template_comment, $config_timestamp_comment, $comm_start_from;
$all_comments = file("$comm_file");
foreach($all_comments as $comment_line)
        {
                $comment_line = trim($comment_line);
                $comment_line_arr = explode("|>|", $comment_line);
                if($id == $comment_line_arr[0])
                {
                        $individual_comments = explode("||", $comment_line_arr[1]);

            //show the page with our new comment, if we just added one
            /* causes some problems, will be updated !!!
                        if($allow_add_comment and true){
                                $comm_start_from = $total_comments-1;
                                if($config_reverse_comments == "yes"){
                                        $comm_start_from = 0;
                                }
                        }
                        */

            $iteration = 0;
            if($config_reverse_comments == "yes"){$iteration = count($individual_comments)+1; $individual_comments = array_reverse($individual_comments); }

            foreach($individual_comments as $comment)
                        {

                                if($config_reverse_comments == "yes") { $iteration --; }
                                else{ $iteration ++; }



                                $comment_arr = explode("|", $comment);
                                if($comment_arr[0] != "")
                                {

                                if(isset($comm_start_from) and $comm_start_from != "")
                                {
                                    if($comment_number < $comm_start_from){ $comment_number++; commentsOutput( $comment_arr[5], $comm_file, $id, $margin + 20 ); continue; }
                                    elseif($showed_comments == $comm_per_page){  break; }
                                }

                                $comment_number ++;
                                if($comm_per_page != 0 and $comm_per_page == $showed_comments){ break; }

                                $comment_arr[4] = stripslashes(rtrim($comment_arr[4]));
                                if($comment_arr[2] != "none")
                                {
                                    if( preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $comment_arr[2])){ $url_target = "";$mail_or_url = "mailto:"; }
                                    else
                                    {
                                        $url_target = "target=\"_blank\"";
                                        $mail_or_url = "";
                                        if(substr($comment_arr[2],0,3) == "www"){ $mail_or_url = "http://"; }
                                    }
                                    $output = str_replace("{author}", "&quot;.stripslashes($comment_arr[1]).&quot;", $template_comment);
                                }
                                else{ $output = str_replace("{author}", $comment_arr[1], $template_comment); }

                                $comment_arr[4] = preg_replace("/\b((http(s?):\/\/)|(www\.))([\w\.]+)([&-~\%\/\w+\.-?]+)\b/i", "[url=]$2$4$5$6[/url]", $comment_arr[4]);
                                $comment_arr[4] = preg_replace("/([\w\.]+)(@)([-\w\.]+)/i", "[url=]$0[/url]", $comment_arr[4]);

                                //Nested Comments//
                                $output = preg_replace("'\[replyButton\](.*?)\[/replyButton\]'si", "<input type=\"button\" value=\"\\1\" id=\"CommentReply_{comment-id}\" name=\"CommentReply_{comment-id}\" onClick=\"java script:setReplyID('{comment-id}', 'CommentReply_{comment-id}', {subID})\">", $output);
                                $output = str_replace("{margin}", $margin, $output);
                                //Nested Comments//
                                $output = str_replace("{mail}", "$comment_arr[2]",$output);
                                $output = str_replace("{date}", date($config_timestamp_comment, $comment_arr[0]),$output);
                                $output = str_replace("{comment-id}", $comment_arr[0],$output);
                                $output = str_replace("{comment}", "<a name=\"".$comment_arr[0]."\"></a>$comment_arr[4]",$output);
                                $output = str_replace("{comment-iteration}", $iteration ,$output);
                                $output = str_replace("{subID}", $id ,$output);

                                $output = replace_comment("show", $output);


                                $showed_comments++;
                                echo $output;

                                //Nested comments//
                                if( $comment_arr[5] != "empty" )
                                    commentsOutput( $comment_arr[5], $comm_file, $id, $margin + 20 );
                                //Nested comments//
                    }
            }
                }
        }

}</div>



3. edit the templates do that the comment sub template contains a button.
use

[replyButton]text of the button[/replyButton]


Next there's also the {margin} tag. With this tag you can make comments on a comment jump in. example

style="margin-left:{margin}";

or use padding if you like that better.

4. now add this javascript to every page that has a CN Include.

<script type="text/javascript">
function setReplyID(id, obj, subid)
{
var subidinput = document.getElementById('subID');
var replyidinput = document.getElementById('replyID');
if( replyidinput.value != 'false' )
{
    var unsetter = document.getElementById('CommentReply_' + replyidinput.value);
    unsetter.value = unsetter.value.replace(/\*/gi, "");
}
if( replyidinput.value == id )
{
    replyidinput.value = 'false';
    subidinput.value = 'false';
}
else
{
    replyidinput.value = id;
    subidinput.value = subid;
    document.getElementById(obj).value = '*' + document.getElementById(obj).value + '*';
}
}
</script>