Open Source Content Management Framework

Enabling user commenting on a page

  1. Using a Gravatar with comments
  2. Using a substyle
  3. Displaying comment count

First, we need a user account to use for postings. Preferably we should create a user group as well in name of security. In this we will call the group Commenters and create a guest account (username: guest, password: password). You of course won't be using this simple and predictable user account, right?

Second, we create a net.nemein.discussion topic. In this, we will give it URI name comments and we will it in the root folder of our website. Edit this topic so that the owner group is the one, which has our guest user as a member. Then edit component configuration and check the Allow thread creation by URI checkbox.

You might want to set the comments folder hidden from navigation in the process as well.

Third, we create (or edit) the component style we want to use the commenting feature with. Here it will be de.linkm.newsticker, but virtually any component will do - all you need to is to be able to get GUID of the current article.

In the component style (here: show-detail) we will add the following lines:

<?php
// Get the current page GUID via NAP
$nap = new midcom_helper_nav()
$leaf = $nap->get_leaf($nap->get_current_leaf());
$guid = $leaf[MIDCOM_NAV_GUID];
$anon_login = false;

if (!$_MIDGARD["user"])
{
    // Requestor is not logged in, log in as the guest account
    mgd_auth_midgard("guest", "password", 0);
    $GLOBALS["midgard"] = mgd_get_midgard();
    $anon_login = true;
}
if (!empty($guid))
{
    // Load the discussion forum in
    $_MIDCOM->dynamic_load("/comments/{$guid}");
}
if ($anon_login)
{
    // Clear the temporary login
    mgd_unsetuid();
}
?>

Case specific settings are written in italics.

Using a Gravatar with comments

Gravatar is an online service providing cross-site avatars linked via user's email address.

<?php
if ($view["poster_email"]) 
{
    $size = 50;
    $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id="
                .md5($view["poster_email"])
                ."&size=".$size;
    ?>
    <img src="&(grav_url);" class="avatar" alt="&(view["poster"]);" title="&(view["poster"]);" />
    <?php
} 
?>

Add this in style element <(view-reply-item)> of your discussion topic.

Using a substyle

To prevent the empty initial thread item from showing, you can use dynamic load.

<?php
$_MIDCOM->dynamic_load("/midcom-substyle-commentstyle/comments/{$guid}");
?>

Create style element called <(view-thread)> under the style you use for the net.nemein.discussion and leave it blank.

Displaying comment count

A typical feature is also to display the number of already posted comments in an article listing. To do this in de.linkm.newsticker news item listing, edit the show-index-item element and include the following:

<?php
// If we don't find any comments, then the count is zero
$comments_count = 0;

// Fetch the discussion forum topic
// Note: change the GUID to correspond to your topic structure
$comments_topic = mgd_get_object_by_guid("3fd26a198a0ffd907f52c629f55297a3");
if ($comments_topic)
{
    $comments_article = mgd_get_article_by_name($comments_topic->id, $view['_storage_guid']);
    if ($comments_article)
    {
        $comments = mgd_list_reply_articles($comments_article->id);
        if ($comments)
        {
            $comments_count = $comments->N;
        }
    }
}

echo "<a href=\"{$prefix}{$view_name}.html#comments\" rel=\"comments\">{$comments_count} comments</a>\n";
echo " | <a href=\"".$GLOBALS['midcom']->get_host_prefix()."midcom-permalink-{$view'_storage_guid'}\" rel=\"bookmark\" title=\"Permanent Link to post\">PermaLink</a>";
?>

This code checks the number of reply articles from the discussion forum and displays the link for commenting and also the permanent link of the article.

To make the article listing you need to find out the GUID of your discussion topic.

Designed by Nemein, hosted by Anykey