Sito realizzato con DelightCMS. Contattami per informazioni

Disqus comment system helper

Pubblicato in Cakephp, 9 mesi fa | Lascia un commento

L'integrazione del sistema di gestione commenti disqus è un lavoro piuttosto semplice e veloce. Per renderlo ancora più semplice, e più in stile cake, ho creato un piccolo helper che si occupa di fare tutto il lavoro sporco.

Innanzitutto è necessario un account disqus. Puoi prenderne uno qua:

http://disqus.com/comments/register/

Poi, copia-incolla il codice sottostante nel file ./app/views/helpers/disqus.php

Helper Class:

<?php 
// ./app/views/helpers/disqus.php
    /**
     * Disqus comment system integration Helper
     *
     * @author ToX - http://emanuele.itoscano.com - toss82 - at - gmail.com
     *
     *
     *
     * @help:
     *        In function makeRepliable, I set the projectName somewhere in my configuration files, 
     *        remember to change this variable to something that suits your needs. 
     *         
     */

class DisqusHelper extends Helper {
    var 
$helpers = array('Html''Javascript');

    function 
repliesLinkCounter() {
        
$projectName Configure::read('Disqus.projectName');
        
$createJS "
            (function() {
                var links = document.getElementsByTagName('a');
                var query = '?';
                for(var i = 0; i < links.length; i++) {
                if(links.href.indexOf('#disqus_thread') >= 0) {
                    query += 'url' + i + '=' + encodeURIComponent(links.href) + '&amp;';
                }
                }
                document.write('<script charset=\"utf-8\" type=\"text/javascript\" src=\"http://disqus.com/forums/{$projectName}/get_num_replies.js' + query + '\"></' + 'script>');
            })();
        "
;
        
$return $this->Javascript->codeBlock($createJS);
        return 
$return;
    }

    function 
makeRepliable() {
        
$projectName Configure::read('Disqus.projectName');
        
$return '<div id="disqus_thread"></div>';
        
$return .= $this->Javascript->link("http://disqus.com/forums/{$projectName}/embed.js");
        return 
$return;
    }

    function 
recentComments($num_items 5$hide_avatars 0$avatar_size 32$excerpt_lenght 200) {
        
$projectName Configure::read('Disqus.projectName');
        
$return =  "<div id='recentcomments' class='dsq-widget'>";
            
$return .=  "<h2 class='dsq-widget-title'>"__('Recent Comments'true) ."</h2>";
            
$return .= $this->Javascript->link("http://disqus.com/forums/{$projectName}/recent_comments_widget.js?num_items={$num_items}&hide_avatars={$hide_avatars}&avatar_size={$avatar_size}&excerpt_length={$excerpt_lenght}");
        
$return .=  "</div>";
        
return $return;
    }
}
?>

Come è scritto nell'header, "In function makeRepliable, I set the projectName somewhere in my configuration files, remember to change this variable to something that suits your needs.". È l'unica configurazione da fare...

Aggiungi l'helper Disqus in app_controller o nel controller che preferisci. A questo punto abbiamo quasi finito: inserisci questa stringa dove vuoi visualizzare i commenti ed il form:

echo $disqus->makeRepliable();

et voilà...volendo, è possibile visualizzare gli ultimi commenti inseriti in una sidebar, o dovunque tu voglia con questo codice, puoi naturalmente modificare le variabili in base alle tue necessità

echo $disqus->recentComments($num_items = 5, $hide_avatars = 0, $avatar_size = 32, $excerpt_lenght = 200);

aggiungendo invece questo link dopo ogni permalink che punta ad un post commentabile è possibile avere un contatore di commenti dopo i permalink che portano ad un post con i commenti disqus, aggiungendo questo link:

echo $html->link("View comments", $yourPermaLink . "#disqus_thread");

E questo codice al fondo dell'elenco dei permalink

echo $disqus->repliesLinkCounter();