Collections functions
General functions
Render functions
Theme permission functions
Resource functions

comments_show()

Description

Display all comments for a resource or collection

Parameters

ColumnTypeDefaultDescription
$ref integer The reference of the resource, collection or the comment (if called from itself recursively)
$bcollection_mode boolean false false == show comments for resources, true == show comments for collection
$bRecursive boolean true Recursively show comments, defaults to true, will be set to false if depth limit reached
$level integer 1 Used for recursion for display indentation etc.

Return

void

Location

include/comment_functions.php lines 208 to 482

Definition

 
function comments_show($ref$bcollection_mode false$bRecursive true$level 1)
    {
    if(!
is_numeric($ref))
        {
        return 
false;
        }

    global 
$baseurl_short$username$anonymous_login$lang$comments_max_characters$comments_flat_view$regex_email$comments_show_anonymous_email_address;

    
$anonymous_mode = (empty ($username) || $username == $anonymous_login);        // show extra fields if commenting anonymously

    
if ($comments_flat_view$bRecursive false;

    
$bRecursive $bRecursive && ($level $GLOBALS['comments_responses_max_level']);

    
// set 'name' to either user.fullname, comment.fullname or default 'Anonymous'

    
$sql =     "select c.ref thisref, c.ref_parent, c.hide, c.created, c.body, c.website_url, c.email, u.username, u.ref, u.profile_image, parent.created 'responseToDateTime', " .
            
"IFNULL(IFNULL(c.fullname, u.fullname), '" $lang['comments_anonymous-user'] . "') 'name' ," .
            
"IFNULL(IFNULL(parent.fullname, uparent.fullname), '" $lang['comments_anonymous-user'] . "') 'responseToName' " .
            
"from comment c left join (user u) on (c.user_ref = u.ref) left join (comment parent) on (c.ref_parent = parent.ref) left join (user uparent) on (parent.user_ref = uparent.ref) ";
    
$sql_values=array();
    
$collection_ref = ($bcollection_mode) ? $ref "";
    
$resource_ref = ($bcollection_mode) ? "" $ref;

    
$collection_mode $bcollection_mode "collection_mode=true" "";

    if (
$level == 1)
        {

        
// pass this JS function the "this" from the submit button in a form to post it via AJAX call, then refresh the "comments_container"

        
echo<<<EOT

        <script src="
${baseurl_short}lib/js/tagging.js"></script>
        <script type="text/javascript">

            var regexEmail = new RegExp ("
${regex_email}");

            function validateAnonymousComment(obj) {
                return (
                    regexEmail.test (String(obj.email.value).trim()) &&
                    String(obj.fullname.value).trim() != "" &&
                    validateComment(obj)
                )
            }

            function validateComment(obj) {
                return (String(obj.body.value).trim() != "");
            }

            function validateAnonymousFlag(obj) {
                return (
                    regexEmail.test (String(obj.email.value).trim()) &&
                    String(obj.fullname.value).trim() != "" &&
                    validateFlag(obj)
                )
            }

            function validateFlag(obj) {
                return (String(obj.comment_flag_reason.value).trim() != "");
            }

            function submitForm(obj) {
                jQuery.post(
                    '
${baseurl_short}pages/ajax/comments_handler.php?ref={$ref}&collection_mode={$collection_mode}',
                    jQuery(obj).serialize(),
                    function(data)
                    {
                    jQuery('#comments_container').replaceWith(data);
                    }
                );
            }
        </script>

        <div id="comments_container">
        <div id="comment_form" class="comment_form_container">
            <form class="comment_form" action="javascript:void(0);" method="">
EOT;
        
generateFormToken("comment_form");
        
hook("beforecommentbody");
        echo <<<EOT
                <input id="comment_form_collection_ref" type="hidden" name="collection_ref" value="${collection_ref}"></input>
                <input id="comment_form_resource_ref" type="hidden" name="resource_ref" value="
${resource_ref}"></input>
                <textarea class="CommentFormBody" id="comment_form_body" name="body" maxlength="
${comments_max_characters}" placeholder="${lang['comments_body-placeholder']}" onkeyup="TaggingProcess(this)"></textarea>

EOT;

        if (
$anonymous_mode)
            {
            echo <<<EOT
                <br />
                <input class="CommentFormFullname" id="comment_form_fullname" type="text" name="fullname" placeholder="
${lang['comments_fullname-placeholder']}"></input>
                <input class="CommentFormEmail" id="comment_form_email" type="text" name="email" placeholder="
${lang['comments_email-placeholder']}"></input>
                <input class="CommentFormWebsiteURL" id="comment_form_website_url" type="text" name="website_url" placeholder="
${lang['comments_website-url-placeholder']}"></input>

EOT;
            }

        
$validateFunction $anonymous_mode "if (validateAnonymousComment(this.parentNode))" "if (validateComment(this.parentNode))";

        echo<<<EOT
                <br />
                <input class="CommentFormSubmit" type="submit" value="
${lang['comments_submit-button-label']}" onClick="${validateFunction} { submitForm(this.parentNode) } else { alert ('${lang['comments_validation-fields-failed']}'); } ;"></input>
            </form>
        </div>     <!-- end of comment_form -->

EOT;

        
$sql .= $bcollection_mode "where c.collection_ref=?" "where c.resource_ref=?";  // first level will look for either collection or resource comments
        
$sql_values array_merge($sql_values,array("i",$ref));
        if (!
$comments_flat_view)
            {
            
$sql .= " and c.ref_parent is NULL";
            }
        }
    else
        {
        
$sql .= "where c.ref_parent=?";  // look for child comments, regardless of what type of comment
        
$sql_values array_merge($sql_values,array("i",$ref));
        }

    
$sql .= " order by c.created desc";
    
$found_comments ps_query($sql,$sql_values);

    foreach (
$found_comments as $comment)
        {

            
$thisRef $comment['thisref'];

            echo 
"<div class='CommentEntry' id='comment${thisRef}' style='margin-left: " . ($level-1)*50 "px;'>";    // indent for levels - this will always be zero if config $comments_flat_view=true

            # ----- Information line
            
hook("beforecommentinfo""all",array("ref"=>$comment["ref"]));

            echo 
"<div class='CommentEntryInfoContainer'>";
            echo 
"<div class='CommentEntryInfo'>";
            if (
$comment['profile_image'] != "" && $anonymous_mode != true)
                {
                echo 
"<div><img src='" get_profile_image("",$comment['profile_image']). "' id='CommentProfileImage'></div>";
                }
            echo 
"<div class='CommentEntryInfoCommenter'>";


            if (empty(
$comment['name'])) $comment['name'] = $comment['username'];
            if (!
hook("commentername""all",array("ref"=>$comment["ref"])))

            if (
$anonymous_mode == true)
                {
                echo 
"<div class='CommentEntryInfoCommenterName'>" htmlspecialchars($comment['name']) . "</div>";
                }
            else
                {
                echo 
"<a href='" $baseurl_short "pages/user/user_profile.php?username=" htmlspecialchars($comment['username']) . "'><div class='CommentEntryInfoCommenterName'>" htmlspecialchars($comment['name']) . "</div></a>";
                }

            if (
$comments_show_anonymous_email_address && !empty($comment['email']))
                {
                echo 
"<div class='CommentEntryInfoCommenterEmail'>" htmlspecialchars ($comment['email']) . "</div>";
                }
            if  (!empty (
$comment['website_url']))
                {
                echo 
"<div class='CommentEntryInfoCommenterWebsite'>" htmlspecialchars ($comment['website_url']) . "</div>";
                }

            echo 
"</div>";


            echo 
"<div class='CommentEntryInfoDetails'>" strftime('%a',strtotime($comment["created"])) . " " nicedate($comment["created"], truetruetrue). " ";
            echo 
"</div>";    // end of CommentEntryInfoDetails
            
echo "</div>";    // end of CommentEntryInfoLine
            
echo "</div>";    // end CommentEntryInfoContainer

            
echo "<div class='CommentBody'>";
            if (
$comment['hide'])
            {
            if (
text("comments_removal_message")!="")
                {
                    echo 
text("comments_removal_message");
                }
            else
                {
                    echo 
"[" $lang["deleted"] . "]";
                }
            }
            else
                {
                echo 
comments_tags_to_links(htmlspecialchars ($comment['body']));
                }
            echo 
"</div>";

            
# ----- Form area

            
$validateFunction $anonymous_mode "if (validateAnonymousFlag(this.parentNode))" "if (validateFlag(this.parentNode))";

            if (!
getval("comment${thisRef}flagged",""))
                {
                echo<<<EOT

                    <div id="CommentFlagContainer
${thisRef}" style="display: none;">
                        <form class="comment_form" action="javascript:void(0);" method="">
                            <input type="hidden" name="comment_flag_ref" value="
${thisRef}"></input>
                            <input type="hidden" name="comment_flag_url" value=""></input>

EOT;
                
hook("beforecommentflagreason");
                
generateFormToken("comment_form");
                echo <<<EOT
                    <textarea class="CommentFlagReason" maxlength="${comments_max_characters}" name="comment_flag_reason" placeholder="${lang['comments_flag-reason-placeholder']}"></textarea><br />
EOT;

                if (
$anonymous_mode) echo<<<EOT

                            <input class="CommentFlagFullname" id="comment_flag_fullname" type="text" name="fullname" placeholder="
${lang['comments_fullname-placeholder']}"></input>
                            <input class="CommentFlagEmail" id="comment_flag_email" type="text" name="email" placeholder="
${lang['comments_email-placeholder']}"></input><br />

EOT;
                echo<<<EOT
                            <input class="CommentFlagSubmit" type="submit" value="${lang['comments_submit-button-label']}" onClick="comment_flag_url.value=document.URL; ${validateFunction} { submitForm(this.parentNode); } else { alert ('${lang['comments_validation-fields-failed']}') }"></input>
                        </form>
                    </div>
EOT;

                }

            if (!
$comment['hide'])
                {
                
$respond_button_id "comment_respond_button_" $thisRef;
                
$respond_div_id "comment_respond_" $thisRef;

                echo 
"<div id='${respond_button_id}' class='CommentRespond'>";        // start respond div
                
echo "<a href='javascript:void(0)' onClick='
                    jQuery(\"#comment_form\").clone().attr(\"id\",\"
${respond_div_id}\").css(\"margin-left\",\"" . ($level 50) . 'px")' ".insertAfter(\"#comment$thisRef\");
                    jQuery(\"<input>\").attr({type: \"hidden\", name: \"ref_parent\", value: \"
$thisRef\"}).appendTo(\"#${respond_div_id} .comment_form\");
                    jQuery(\"#
{$respond_button_id} a\").removeAttr(\"onclick\");
                '>" 
'<i aria-hidden="true" class="fa fa-reply"></i>&nbsp;' $lang['comments_respond-to-this-comment'] . "</a>";
                echo 
"</div>";        // end respond

                
echo "<div class='CommentEntryInfoFlag'>";
                if (
getval("comment${thisRef}flagged",""))
                    {
                    echo 
"<div class='CommentFlagged'><i aria-hidden='true' class='fa fa-fw fa-flag'>&nbsp;</i>${lang['comments_flag-has-been-flagged']}</div>";
                    }
                else
                    {
                    echo<<<EOT
                    <div class="CommentFlag">
                        <a href="javascript:void(0)" onclick="jQuery('#CommentFlagContainer
${thisRef}').toggle('fast');" ><i aria-hidden="true" class="fa fa-fw fa-flag">&nbsp;</i>${lang['comments_flag-this-comment']}</a>
                    </div>
EOT;
                    }

                if(
checkPerm("o"))
                    {
                    
?>
                    <form class="comment_removal_form">
                         generateFormToken("comment_removal_form"); ?>
                        <input type="hidden" name="comment_to_hide" value=" echo htmlspecialchars($thisRef); ?>"></input>
                        <a href="javascript:void(0)" onclick="if (confirm (' echo htmlspecialchars($lang['comments_hide-comment-text-confirm']); ?>')) submitForm(this.parentNode);"> echo '<i aria-hidden="true" class="fa fa-trash-alt"></i>&nbsp;' $lang['comments_hide-comment-text-link']; ?></a>
                    </form>
                    
                    
}

                echo 
"</div>";        // end of CommentEntryInfoFlag

                
}

            echo 
"</div>";        // end of CommentEntry

            
if ($bRecursivecomments_show($thisRef$bcollection_modetrue$level+1);


        }
        if (
$level == 1)  echo "</div>";  // end of comments_container
    
}

This article was last updated 24th May 2022 18:35 Europe/London time based on the source file dated 14th April 2022 11:05 Europe/London time.