<?xml version="1.0"?> 
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  version="1.0"
  xmlns:java="http://xml.apache.org/xslt/java"
  exclude-result-prefixes="java"
>
  <xsl:template match="forum.showForum">
    <xsl:param name="forumName"><xsl:value-of select="params/forumName"/></xsl:param>
    <span class="forum">
    <link type="text/css" rel="stylesheet" href="/css/forum.css"/>
    <table class="topic" cellpadding="0" border="0">
    <tr>
    <td><h3><xsl:value-of select="$forumName"/></h3>
    <td align="right"></td>
      <xsl:call-template name="control.showAnIcon">
        <xsl:with-param name="type">add</xsl:with-param>
        <xsl:with-param name="display">Create Topic</xsl:with-param>
        <xsl:with-param name="help">Create a new topic in the <xsl:value-of select="$forumName"/> forum.</xsl:with-param>
        <xsl:with-param name="url">/forum/<xsl:value-of select="$forumName"/>/?op=create</xsl:with-param>
        <xsl:with-param name="permission">org.benow.forum.ForumService.admin</xsl:with-param>
      </xsl:call-template>
    <xsl:if test="result/item">
      <xsl:call-template name="control.showAnIcon">
        <xsl:with-param name="type">rss_small</xsl:with-param>
        <xsl:with-param name="display">Subscribe</xsl:with-param>
        <xsl:with-param name="help">Subscribe to the <xsl:value-of select="$forumName"/> RSS feed.</xsl:with-param>
        <xsl:with-param name="url">/rss/<xsl:value-of select="$forumName"/>.rss</xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    </td>
    </tr>
    </table>
        
    <!-- 
    <xsl:value-of select="java:addStyle($render,'/css/forum.css')"/>
     -->
    
    <xsl:choose>
      <xsl:when test="result/item">
        <xsl:for-each select="result/item">
          <xsl:call-template name="forum.showTopic">
            <xsl:with-param name="brief">true</xsl:with-param>
            <xsl:with-param name="forumName"><xsl:value-of select="$forumName"/></xsl:with-param>
          </xsl:call-template>
          <p/>
        </xsl:for-each>
        <xsl:if test="result/@from&gt;0">
          <xsl:variable name="from"><xsl:value-of select="number(result/@from)"/></xsl:variable>
          <xsl:variable name="new"><xsl:value-of select="number($from)-10"/></xsl:variable>
          <a href="?starting={$new}">&lt; &lt; newer</a>
        </xsl:if>
        <xsl:if test="result/@super-count&gt;count(result/item)">
          <xsl:variable name="from"><xsl:value-of select="number(result/@from)"/></xsl:variable>
          <xsl:variable name="count"><xsl:value-of select="count(result/item)"/></xsl:variable>
          <xsl:variable name="new"><xsl:value-of select="number($from+$count)"/></xsl:variable>
          <a href="?starting={$new}">older &gt; &gt;</a>
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        No topics exist.<br/>
        <xsl:if test="java:hasPermission($user,'org.benow.forum.ForumService.admin')">
        <a href="/forum/topic.page?op=create&amp;forum={$forumName}&amp;redirect=/"><img border="0" src="/images/icons/edit_small.png"/> Create topic in: <xsl:value-of select="$forumName"/></a>.
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
    </span>
  </xsl:template>

  <xsl:template name="forum.showTopic" match="forum.showTopic">
    <xsl:param name="forumName"><xsl:value-of select="forum/name"/></xsl:param>
    <!-- set to not include comments -->
    <xsl:param name="brief"/>
    <xsl:if test="not($forumName) or $forumName=''">Developer: a forum name is required to forum.showTopic<br/></xsl:if>
    <xsl:value-of select="java:addStyle($render,'/css/forum.css')"/>

    <table border="0" cellspacing="0" style="border: 1px solid grey" class="topic">
      <tr>
        <td class="title">
          <a style="text-decoration: none" href="/forum/{$forumName}/{title-key}"><xsl:value-of select="title"/></a>
        </td>
      </tr>
      <tr>
        <td class="topic-body">
          <xsl:value-of select="java:xsl.XSLUtil.unescape(outline)"/>
          <xsl:choose>
          <xsl:when test="$brief and body"><p/>
          </xsl:when>
            <xsl:otherwise>
              <p/>
              <xsl:value-of select="java:xsl.XSLUtil.unescape(body)"/>
            </xsl:otherwise>
          </xsl:choose>
        </td>
      </tr>
      <tr>
            <xsl:if test="java:hasPermission($user,'org.benow.forum.ForumService.admin') or java:getName($user)=author/name">
        <script type="text/javascript">
          function confirmDelete<xsl:value-of select="@key"/>() {
            if (confirm("Are you sure you wish to delete topic\n'<xsl:value-of select="title"/>'?")) {
              document.location.href='/svc/org.benow.forum.ForumService?exec=deleteTopic(java.lang.Object)&amp;key=<xsl:value-of select="@key"/>&amp;redirect=/forum/<xsl:value-of select="$forumName"/>';
            }
          }
        </script>
            </xsl:if>
        <td class="topic-ops">
          <span class="op-item">
            <xsl:value-of select="author/name"/> -
            <xsl:value-of select="java:org.benow.xml.xsl.XSL.timeToString(creation-date,'hh:mm aa, dd MMM yyyy')"/>
          </span>
          <xsl:choose>
            <xsl:when test="$brief">
              <xsl:if test="body!=''">
                <span class="op-item"><a href="/forum/{$forumName}/{title-key}">Read more...</a> (<xsl:value-of select="string-length(body)"/> characters)</span>
              </xsl:if>
            </xsl:when>
          </xsl:choose>
          <xsl:if test="java:hasPermission($user,'org.benow.forum.ForumService.invoke')">
          <xsl:if test="java:hasPermission($user,'org.benow.forum.ForumService.admin') or java:getName($user)=author/name"> 
            <span class="op-item">              
              <a href="/forum/{$forumName}/{title-key}?op=edit">edit</a> -
              <a href="javascript:confirmDelete{@key}()">delete</a>
            </span>
          </xsl:if> 
          <span class="op-item"> 
          <xsl:choose>
            <xsl:when test="num-comments=0">No comments</xsl:when>
            <xsl:otherwise>
              <a href="/forum/topic.page?op=view&amp;key={@key}#comments">
                <xsl:value-of select="num-comments"/> comment<xsl:if test="num-comments&gt;1">s</xsl:if>
               </a>
            </xsl:otherwise></xsl:choose>
          - <a href="/forum/comment.page?op=create&amp;topic={@key}">reply</a>
          </span>
          </xsl:if>
        </td>
      </tr>
    </table>
    <xsl:if test="not($brief)">
      <a name="comments">
        <p/>
      </a>
      <xsl:if test="comments">
        <xsl:for-each select="comments/item">
          <xsl:call-template name="forum.showComment"/>
          <br/>
        </xsl:for-each>
      </xsl:if>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="forum.showComment">
    <a name="comment{@key}"><b></b></a>
          <table class="format" width="700">
            <tr>
              <td rowspan="2" width="25"><!-- save some space --></td>
              <td class="InfoTableTitle" style="font-size: 10px">
                <xsl:value-of select="title"/> - <xsl:value-of select="author/name"/> - 
                <xsl:value-of select="java:org.benow.xml.xsl.XSL.timeToString(creation-date,'hh:mm aa, dd MMM yyyy')"/>
              </td>
            </tr>
            <tr>
              <td>
                <xsl:value-of select="body"/>
              </td>
            </tr>
          </table>
  </xsl:template>
</xsl:stylesheet>

