<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:udt="DotNetNuke/UserDefinedTable" exclude-result-prefixes="udt">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
  <!--
  This prefix is used to generate module specific query strings
  Each querystring or form value that starts with udt_{ModuleId}_param 
  will be added as parameter starting with param
  -->
  <xsl:variable name="prefix_param">udt_<xsl:value-of select="//udt:Context/udt:ModuleId" />_param</xsl:variable>
  <xsl:param name="param_page" select="1" />
  <xsl:variable name="paging" select="//udt:Context/udt:Paging" />
  <xsl:param name="param_orderby" select="//udt:Fields[udt:UserDefinedFieldId=//udt:Context/udt:OrderBy]/udt:SortColumn" />
  <xsl:param name="param_direction" select="//udt:Context/udt:OrderDirection" />
  <!--wrong string would break stylesheet, so fallback to ascending if userinput is wrong-->
  <xsl:variable name="orderDirection">
    <xsl:choose>
      <xsl:when test="$param_direction='descending'">
        <xsl:text>descending</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>ascending</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="orderType">
    <xsl:variable name="DataType" select="//udt:Fields[udt:SortColumn=$param_orderby]/udt:FieldType" />
    <xsl:choose>
      <xsl:when test="$DataType='Int32' or $DataType='Decimal' or $DataType='Currency'">number</xsl:when>
      <xsl:otherwise>text</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:template match="udt:Data" mode="list">
    <xsl:param name="from" select="1" />
    <xsl:param name="to" select="count(*)" />
    <xsl:if test="position() &gt; $from and position() &lt; $to">
      <tr>
        <td width="1%">
          <xsl:call-template name="EditLink" />
        </td>
        <td class="Graduates_img" width="1%">
          <xsl:value-of select="udt:Img" disable-output-escaping="yes" />
        </td>
        <td class="Graduates_fio" width="1%">
          <xsl:value-of select="udt:ФИО" disable-output-escaping="yes" />
        </td>
        <td class="Graduates_year" width="1%">
          <xsl:value-of select="udt:Год_x0020_окончания" disable-output-escaping="yes" />
        </td>
        <td class="Graduates_dis" width="96%">
          <xsl:value-of select="udt:Описание" disable-output-escaping="yes" />
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <xsl:template match="/udt:UserDefinedTable">
    <xsl:variable name="currentData" select="udt:Data" />
    <xsl:variable name="from">
      <xsl:choose>
        <xsl:when test="$paging">
          <xsl:value-of select="$paging * number($param_page) - $paging" />
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="to">
      <xsl:choose>
        <xsl:when test="$paging">
          <xsl:value-of select="$paging * number($param_page) +1" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="count($currentData)+1" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:if test="$currentData">
      <table>
        <!-- DEFINE ANY HEADERS HERE, EXAMPLE IS FOR TABLE TYPE LISTING -->
        <!-- Parameter header is optional! -->
        <!--
      <tr>
        <td/>
        <td class="NormalBold UDT_Table_Head">
          <xsl:apply-templates select ="udt:Fields[udt:FieldTitle='NameOfColumn']">
            <xsl:with-param name ="header" select ="NewHeaderName"/>
          </xsl:apply-templates>
        </td>...
      </tr>
      -->
        <tr>
          <td width="1%">
          </td>
          <td width="1%">
          </td>
          <td width="1%">
            <xsl:apply-templates select="udt:Fields[udt:FieldTitle='ФИО']" />
          </td>
          <td width="1%">
            <xsl:apply-templates select="udt:Fields[udt:FieldTitle='Год окончания']" />
          </td>
          <td width="96%">
          </td>
        </tr>
        <xsl:apply-templates select="$currentData" mode="list">
          <xsl:sort select="*[name()=$param_orderby]" order="{$orderDirection}" data-type="{$orderType}" />
          <xsl:with-param name="from" select="$from" />
          <xsl:with-param name="to" select="$to" />
        </xsl:apply-templates>
      </table>
    </xsl:if>
    <xsl:if test="$paging">
      <xsl:call-template name="renderPaging">
        <xsl:with-param name="maxPages" select="ceiling(count($currentData) div $paging)" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="udt:Fields" name="SortingHeader">
    <xsl:param name="header" select="udt:FieldTitle" />
    <xsl:if test="udt:Visible='true' or udt:Visible='True'">
      <a>
        <xsl:attribute name="href">
          <xsl:choose>
            <xsl:when test="udt:ValueColumn=$param_orderby">
              <xsl:variable name="flippedDirection">
                <xsl:choose>
                  <xsl:when test="$orderDirection='ascending'">
                    <xsl:text>descending</xsl:text>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:text>ascending</xsl:text>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
      ?<xsl:value-of select="$prefix_param" />_orderby=<xsl:value-of select="udt:ValueColumn" />&amp;<xsl:value-of select="$prefix_param" />_direction=<xsl:value-of select="$flippedDirection" /></xsl:when>
            <xsl:otherwise>
              ?<xsl:value-of select="$prefix_param" />_orderby=<xsl:value-of select="udt:ValueColumn" />&amp;<xsl:value-of select="$prefix_param" />_direction=<xsl:value-of select="$orderDirection" /></xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
        <!--flipped order direction-->
        <xsl:value-of select="$header" />
        <xsl:if test="udt:ValueColumn=$param_orderby">
          <img src="{//udt:Context/udt:ApplicationPath}/images/sort{$orderDirection}.gif" border="0" />
        </xsl:if>
      </a>
    </xsl:if>
  </xsl:template>

  <xsl:template name="pagingSinglePages">
    <!--renders paging links-->
    <xsl:param name="pageNumber" select="1" />
    <xsl:param name="maxPages" select="ceiling(count(//udt:Data) div $paging)" />
    <xsl:choose>
      <xsl:when test="number($param_page)=$pageNumber">
        <span class="NormalDisabled">[<xsl:value-of select="$pageNumber" />]</span>
      </xsl:when>
      <xsl:otherwise>
        <a href="?{$prefix_param}_page={$pageNumber}" class="CommandButton">
          <xsl:value-of select="$pageNumber" />
        </a>
      </xsl:otherwise>
    </xsl:choose> 
  <xsl:if test="$pageNumber &lt; $maxPages"><xsl:call-template name="pagingSinglePages"><xsl:with-param name="pageNumber" select="$pageNumber +1" /><xsl:with-param name="maxPages" select="$maxPages" /></xsl:call-template></xsl:if></xsl:template>

  <xsl:template name="renderPaging">
    <xsl:param name="maxPages" select="ceiling(count(//udt:Data) div $paging)" />
    <xsl:variable name="previous" select="number($param_page) - 1" />
    <xsl:variable name="next" select="number($param_page) + 1" />
    <table class="PagingTable" bordercolor="Gray" border="0" style="border-color:Gray;border-width:1px;border-style:Solid;width:100%;">
      <tr>
        <td class="Normal" align="Left">
          <xsl:value-of select="//udt:Context/udt:LocalizedString_Page" />&#160;<xsl:value-of select="number($param_page)" />&#160;<xsl:value-of select="//udt:Context/udt:LocalizedString_Of" />&#160;<xsl:value-of select="$maxPages" /></td>
        <td class="Normal" align="Right">
          <xsl:choose>
            <xsl:when test="number($param_page)&gt;1">
              <a href="?" class="CommandButton">
                <xsl:value-of select="//udt:Context/udt:LocalizedString_First" />
              </a>
            </xsl:when>
            <xsl:otherwise>
              <span class="NormalDisabled">
                <xsl:value-of select="//udt:Context/udt:LocalizedString_First" />
              </span>
            </xsl:otherwise>
          </xsl:choose>  
  <xsl:choose><xsl:when test="number($param_page)&gt;1"><a href="?{$prefix_param}_page={$previous}" class="CommandButton"><xsl:value-of select="//udt:Context/udt:LocalizedString_Previous" /></a></xsl:when><xsl:otherwise><span class="NormalDisabled"><xsl:value-of select="//udt:Context/udt:LocalizedString_Previous" /></span></xsl:otherwise></xsl:choose>  
  <xsl:variable name="startpage"><xsl:choose><xsl:when test="number($param_page)&gt;5"><xsl:value-of select="number($param_page) -4" /></xsl:when><xsl:otherwise>1</xsl:otherwise></xsl:choose></xsl:variable><xsl:variable name="endpage"><xsl:choose><xsl:when test="$startpage+9&gt;$maxPages"><xsl:value-of select="$maxPages" /></xsl:when><xsl:otherwise><xsl:value-of select="$startpage +9" /></xsl:otherwise></xsl:choose></xsl:variable><xsl:call-template name="pagingSinglePages"><xsl:with-param name="pageNumber" select="$startpage" /><xsl:with-param name="maxPages" select="$endpage" /></xsl:call-template><xsl:choose><xsl:when test="number($param_page)&lt;$maxPages"><a href="?{$prefix_param}_page={$next}" class="CommandButton"><xsl:value-of select="//udt:Context/udt:LocalizedString_Next" /></a></xsl:when><xsl:otherwise><span class="NormalDisabled"><xsl:value-of select="//udt:Context/udt:LocalizedString_Next" /></span></xsl:otherwise></xsl:choose>  
  <xsl:choose><xsl:when test="number($param_page)&lt;$maxPages"><a href="?{$prefix_param}_page={$maxPages}" class="CommandButton"><xsl:value-of select="//udt:Context/udt:LocalizedString_Last" /></a></xsl:when><xsl:otherwise><span class="NormalDisabled"><xsl:value-of select="//udt:Context/udt:LocalizedString_Last" /></span></xsl:otherwise></xsl:choose>  
  </td>
      </tr>
    </table>
  </xsl:template>

  <xsl:template name="EditLink">
    <xsl:if test="udt:EditLink">
      <a href="{udt:EditLink}">
        <img border="0" alt="edit" src="{//udt:Context/udt:ApplicationPath}/images/edit.gif" />
      </a>
    </xsl:if>
  </xsl:template>
<udt:template listType="table" delimiter=";" listView="&lt;tr&gt;&#xD;&#xA;  &lt;td width=&quot;1%&quot;&gt;[UDT:EditLink]&lt;/td&gt;&#xD;&#xA;  &lt;td class=&quot;Graduates_img&quot; width=&quot;1%&quot;&gt;[Img]&lt;/td&gt;&#xD;&#xA;  &lt;td class=&quot;Graduates_fio&quot; width=&quot;1%&quot;&gt;[ФИО]&lt;/td&gt;&#xD;&#xA;  &lt;td class=&quot;Graduates_year&quot; width=&quot;1%&quot;&gt;[Год окончания]&lt;/td&gt;&#xD;&#xA;  &lt;td class=&quot;Graduates_dis&quot; width=&quot;96%&quot;&gt;[Описание]&lt;/td&gt;&#xD;&#xA;&lt;/tr&gt;" headerView="&lt;tr&gt;&#xD;&#xA;  &lt;td width=&quot;1%&quot;&gt;&lt;/td&gt;&#xD;&#xA;  &lt;td width=&quot;1%&quot;&gt;&lt;/td&gt;&#xD;&#xA;  &lt;td width=&quot;1%&quot;&gt;[ФИО]&lt;/td&gt;&#xD;&#xA;  &lt;td width=&quot;1%&quot;&gt;[Год окончания]&lt;/td&gt;&#xD;&#xA;  &lt;td width=&quot;96%&quot;&gt;&lt;/td&gt;&#xD;&#xA;&lt;/tr&gt;" detailView="&lt;table&gt;&#xD;&#xA;  &lt;tr&gt;&#xD;&#xA;    &lt;td  width=&quot;100%&quot;&gt;[Подробнее]&lt;/td&gt;&#xD;&#xA;  &lt;/tr&gt;&#xD;&#xA;&lt;/table&gt;&#xD;&#xA;[UDT:ListView]" trackingEmail="" paging="true" sorting="true" />
</xsl:stylesheet>