Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video 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 112 to 363

Definition

 
function comments_show($ref$bcollection_mode false$bRecursive true$level 1
    {    
    if(!
is_numeric($ref))
        {
        return 
false;
        }
        
    global 
$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, 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) ";        
    
$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 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(
                    '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">
            <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']}"></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=${ref}"where c.resource_ref=${ref}";  // first level will look for either collection or resource comments        
        
if (!$comments_flat_view$sql .= " and c.ref_parent is null";                
        }            
    else 
        {
        
$sql .= "where c.ref_parent=${ref}";        // look for child comments, regardless of what type of comment
        
}
    
    
$sql .= " order by c.created desc";    
    
$found_comments sql_query($sql);
    
    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'>";
            echo 
"<div class='CommentEntryInfoCommenter'>";                        
            
            if (empty(
$comment['name'])) $comment['name'] = $comment['username'];
            if (!
hook("commentername""all",array("ref"=>$comment["ref"])))
            echo 
"<div class='CommentEntryInfoCommenterName'>" htmlspecialchars($comment['name']) . "</div>";        
            
            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). " ";            
            if (
$comment['responseToDateTime']!="")
                {
                
$responseToName htmlspecialchars ($comment['responseToName']);
                
$responseToDateTime =  strftime('%a',strtotime($comment["responseToDateTime"])) . " " nicedate($comment['responseToDateTime'], truetruetrue);                        
                
$jumpAnchorID "comment" $comment['ref_parent'];                                
                echo 
$lang['comments_in-response-to'] . "<br /><a class='.smoothscroll' rel='' href='#${jumpAnchorID}'>${responseToName} " $lang['comments_in-response-to-on'] . ${responseToDateTime}</a>";                
                }                        
            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["hidden"];
                }
            }
            else
                {
                echo 
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;

                }            
            
            
$respond_div_id "comment_respond_" $thisRef;
            
            echo 
"<div id='${respond_div_id}' class='CommentRespond'>";        // start respond div
            
echo "<a href='javascript:void(0)' onClick='
                jQuery(\"#
{$respond_div_id}\").replaceWith(jQuery(\"#comment_form\").clone().attr(\"id\",\"${respond_div_id}\")); 
                jQuery(\"<input>\").attr({type: \"hidden\", name: \"ref_parent\", value: \"
$thisRef\"}).appendTo(\"#${respond_div_id} .comment_form\");                
            '>" 
'<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","") || $comment['hide'])
                {
                    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
            
            
<