Skip to end of banner
Go to start of banner

Script - forum table

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

This is a groovy script that gets all the current blog posts in the space, sorts them according to the last comment, and outputs in a forum like table. (More forum like than the blogposts macro anyway). Only parameter is "maxLength". Would be fairly easy to add paging and categorization based on labels.
I wrote this as a script because I couldn't be bothered compiling it into a jar, and its also easier to customise to your needs.

import com.atlassian.confluence.util.*;
import com.atlassian.user.User;

class ForumRow {
  String thread;
  String author;
  int replies;
  String lastPost;
}


int maxL = 50;
if (maxLength!=null)
  maxL = Integer.parseInt(maxLength);

out.println("<style>.silverborder { border:1px solid silver;border-collapse:collapse }</style><table border=1 class='grid' style='width:100%;border:1px solid silver;border-collapse:collapse' cellpadding=2 ><tr><th>Thread</th><th>Author</th><th>Replies</th><th>Last Post</th></tr>");

List list = context.getEntity().getSpace().getCurrentBlogPosts(); 

Map map = new TreeMap();

for (b in list)
{
   ForumRow fr = new ForumRow();
   fr.thread = "<a href=\'" + contextPath + b.urlPath + "\' >" + GeneralUtil.htmlEncode(b.title ) + "</a>";
   fr.author = getUserFullName( b.lastModifierName );
   fr.replies = b.comments.size();
   fr.lastPost = formatDate(b.lastModificationDate) + "<br>Created by " + fr.author;
   Date d = b.lastModificationDate;
   
   for (comment in b.comments )
   {
      Date cd = comment.lastModificationDate; 
      if (cd.time > d.time )
      {
         d = cd;
         fr.lastPost = formatDate(d) + "<br>Reply by " + getUserFullName(comment.creatorName);
      }
   }
   map.put(-d.time,fr);
}

int i=0;

for (e in map )
{
   if (i%2==0)
     out.println("<tr>");
   else
     out.println("<tr style='background-color:#F9FAFC'>"); 

   out.println("<td class='silverborder' >" + e.value.thread+ "</td>");
   out.println("<td class='silverborder' >" + e.value.author+ "</td>");
   out.println("<td class='silverborder' >" + e.value.replies + "</td>");
   out.println("<td class='silverborder' >" + e.value.lastPost + "</td>");
   out.println("</tr>");

   i++;
   if (i>=maxL)
     break;
}

out.println("</table>");

    public String getUserFullName(String username)
    {
        if (username==null)
            return "Anonymous";
        User user = GeneralUtil.getUserAccessor().getUser(username);
        return (user != null) ? user.fullName : "Anonymous";
    }

    public String formatDate(Date date)
    {
        String ret = GeneralUtil.formatDateTime(date);
        if ( (new Date()).time - date.time < 86400000L )
            ret = "<span style=color:red >" + ret + "</span>";
        else if ( (new Date()).time - date.time < 86400000L *4 )
            ret = "<span style=color:blue >" + ret + "</span>";
        return ret;
    }

Best way to use this is to save this script as a Global Template called ForumScript, and then put this in your page:

  {groovy:script=ForumScript|maxLength=10}
  • No labels