<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/TR/REC-html40">

<!-- ========================================== -->
<!--  person.xsl (v001)                         -->
<!--  02.06.05 - kristian.syversen@usit.uio.no  -->
<!-- ========================================== -->

<xsl:import href="../../../system/api-v001.xsl" />

<!-- components -->
<xsl:import href="../../../common/components/structuredtext/v002/structuredtext.xsl" />

<!-- common -->
<xsl:include href="../../../felles/ressurser/breadcrumb-v001.xsl" />
<xsl:include href="../../../felles/ressurser/output-method-html-v001.xsl" />


<!-- Global variables -->
<!-- Values from PERSONSØK query (Requires element 'username' to be correctly set!) -->    
<xsl:variable name="username" select="//username/text()" />
<xsl:variable name="uri">  <!-- &#038; = "&" -->
  http://www.uio.no/sok?person=<xsl:value-of select="$username"/>&#038;format=xml
</xsl:variable>
    
<xsl:variable name="person-search-result" select="document($uri)" />

<xsl:variable name="firstname" select="$person-search-result/person/givenname" /> 
<xsl:variable name="lastname" select="$person-search-result/person/surename" /> 
<xsl:variable name="title" select="$person-search-result/person/title" />
<!-- postal address has multiple lines -->
<xsl:variable name="postal-address" select="$person-search-result/person/postal-address" />
<!-- street address has multiple lines -->
<xsl:variable name="street-address" select="$person-search-result/person/street-address" /> 
    
<xsl:variable name="phones" select="$person-search-result/person/phones" />      
<xsl:variable name="faxes" select="$person-search-result/person/faxes" />  
<xsl:variable name="emails" select="$person-search-result/person/emails" />  

<xsl:variable name="homepage" select="$person-search-result/person/homepage" /> 
    

<xsl:strip-space elements="*"/>

<xsl:template match="/">

  <html>
    <head>
      <title>
        <xsl:value-of select="$firstname"/>&#160;<xsl:value-of select="$lastname"/> - <xsl:value-of select="$title"/>
      </title>
    </head>

    <body>
      <xsl:call-template name="prefixed-breadcrumb"/>
      <xsl:apply-templates /> 
    </body>
  </html>
</xsl:template>


<xsl:template match="person">
  <h3>
    <xsl:if test="$username!=''">
      <xsl:choose>
        <!-- Error in result from Personsøk (e.g. service is down): -->
        <xsl:when test="not( $person-search-result/node() )">
          <i><span style="color:red;"> Feil ved tjenesten for henting av persondata! </span></i>                  
        </xsl:when>
        <!-- User not found: -->
        <xsl:when test="not( $person-search-result/person/node() )">
          <i><span style="color:red;"> Persondata for bruker <big>'<xsl:value-of select="$username"/>'</big> ikke funnet i søketjenesten </span></i>
        </xsl:when>                
        <!-- Expected behaviour: -->
        <xsl:otherwise>
          <xsl:value-of select="$firstname" />&#160;<xsl:value-of select="$lastname" />, <xsl:value-of select="$title" />:
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
    &#160; <!-- if empty, to close <H3> tag -->
  </h3>
 
  <p> <xsl:call-template name="person-info-table" /> </p>
          
  <p> <xsl:apply-templates select="background" /> </p> 
  <p> <xsl:apply-templates select="vocation" /> </p> 
  <p> <xsl:apply-templates select="competence" /> </p> 
  <p> <xsl:apply-templates select="projects" /> </p> 
  <p> <xsl:apply-templates select="publications" /> </p> 
</xsl:template>


<xsl:template name="institute">
  <xsl:param name="code" />
  <xsl:choose>
    <xsl:when test="$code=''">
      Institutt-kode mangler
    </xsl:when>
    <!-- default -->
    <xsl:otherwise>
      <xsl:value-of select="//institute" />
    </xsl:otherwise>
  </xsl:choose>    
</xsl:template>


<xsl:template name="person-info-table">

  <!-- Sett inn ønsket størrelse på bildet her: -->
  <xsl:variable name="image-width">170</xsl:variable>
  <xsl:variable name="image-height">200</xsl:variable>
  <!-- Sette relativ lenke til bildet -->
  <xsl:variable name="images-uri">./bilder/</xsl:variable> <!-- end uri with '/' -->
  
  
  <table style="padding-top:20px; padding-bottom:20px" border="0">
    <tr>
      <!-- collapse cell if chosen not to display image -->
      <xsl:if test="not(//user-image='No')">      
        <td rowspan="8" valign="top" align="left"> 
          <i>
            <img src="{$images-uri}{$username}.jpg" 
                 alt="[{$username}.jpg]" 
                 height="{$image-height}" 
                 width="{$image-width}" 
                 style="padding-right:20px;"/> 
          </i>                                 
        </td>
      </xsl:if>
      
      <td valign="top">&#160;<b>Adresse:</b>&#160;</td>
      <td>
        <xsl:for-each select="$postal-address/line">
          <xsl:value-of select="." /> <br/>
        </xsl:for-each>
      </td>
    </tr>
    
    <tr>
      <td valign="top">&#160;<b>Besøksadresse:</b>&#160;</td>
      <td>
        <xsl:for-each select="$street-address/line">
          <xsl:value-of select="." /> <br/>
        </xsl:for-each>
      </td>
    </tr>
    <tr>
      <td>&#160;<b>Rom:</b>&#160;</td>
      <td> <xsl:apply-templates select="room" /> </td>
    </tr>

    <xsl:apply-templates select="office-hours" /> <!-- row is inserted if value is found -->

    <tr>
      <!-- For listing of phones and faxes some string checking and 
           concatenation is made to ensure proper output -->
      <td>&#160;<b>Telefon / fax:</b>&#160;</td>
      <td>     
        <xsl:for-each select="$phones/phone">
          <xsl:choose>
            <xsl:when test="not( position() = last() )">  <!-- if not last node, ', ' is added to the string -->
              <xsl:value-of select="concat(.,',&#160;')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="."/>            
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
        <big>&#160;/&#160;</big>
        <xsl:for-each select="$faxes/fax">
          <xsl:choose>
            <xsl:when test="not( position() = last() )">  <!-- if not last node, ', ' is added to the string -->
              <xsl:value-of select="concat(.,',&#160;')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="."/>            
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </td>
    </tr>
    <tr>
      <td>&#160;<b>E-post:</b>&#160;</td>
      <td> 
        <xsl:for-each select="$emails/email">
          <xsl:variable name="adr" select="." />
          <xsl:choose>
            <xsl:when test="not( position() = last() )"> 
              <a href="mailto:{$adr}"> <xsl:value-of select="$adr" /> </a> <br />
            </xsl:when>
            <xsl:otherwise>
              <a href="mailto:{$adr}"> <xsl:value-of select="$adr" /> </a>      
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </td>
    </tr>

    <xsl:call-template name="homepage-row" />

    <xsl:apply-templates select="key-frida" />
  </table>
</xsl:template>


<!-- TABLE ELEMENTS CONDITIONAL INSERTION -->
<!-- Table elements: Row is only inserted into person-info-table if value is found -->
<xsl:template match="office-hours">
  <xsl:if test="not(.='')">
    <tr>
      <td>&#160;<b>Treffetider:</b>&#160;</td>
      <td> <xsl:value-of select="." /> </td>
    </tr>
  </xsl:if> 
</xsl:template>

<xsl:template name="homepage-row">
  <xsl:if test="not($homepage='')">
    <tr>
      <td>&#160;<b>Hjemmeside:</b>&#160;</td>
      <td>
        <xsl:choose>
          <xsl:when test="not(contains($homepage,'http://'))">
            <a href="{concat('http://', $homepage)}"><xsl:value-of select="$homepage" /></a>
          </xsl:when>
          <xsl:otherwise>
            <a href="{$homepage}"><xsl:value-of select="$homepage" /></a>
          </xsl:otherwise>
        </xsl:choose>
      </td>
    </tr>
  </xsl:if> 
</xsl:template>

<!-- &#038; = "&" -->
<xsl:template match="key-frida">
  <xsl:if test=".!=''"> 
    <xsl:variable name="frida">http://wo.uio.no/as/WebObjects/frida.woa/wa/fres?inst=185&#038;pnr=</xsl:variable>
    <xsl:variable name="key" select="." /> 
    <tr>
      <td>&#160;<b>Publikasjoner:</b>&#160;</td>
      <td> <a href="{$frida}{$key}&#038;action=sok">FRIDA</a> </td>
    </tr>   
  </xsl:if>
</xsl:template>
<!-- END OF TABLE ELEMENT TEMLATES -->


<xsl:template match="background">
  <xsl:if test="not(.='') and ./node()">
    <b>Bakgrunn&#160; </b>
    <xsl:apply-templates />
  </xsl:if>
</xsl:template>
  

<xsl:template match="vocation">
  <xsl:if test="not(.='') and ./node()">  
    <xsl:choose>
      <xsl:when test="//category/text()='technical-administrative'">
        <b>Ansvarsområder&#160; </b> 
      </xsl:when>
      <xsl:otherwise>
        <b>Undervisnings- og veiledningsområder&#160; </b>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:call-template name="list-structuredtext" />
  </xsl:if>
</xsl:template>
  
  
<xsl:template match="competence">
  <xsl:if test="not(.='') and ./node()">
    <b>Faglige kompetanseområder&#160; </b> 
    <xsl:call-template name="list-structuredtext" />
  </xsl:if>
</xsl:template>  
  

<xsl:template match="projects">
  <xsl:if test="not(.='') and ./node()">
    <xsl:variable name="code" select="//institute" />
    <xsl:choose>
      <xsl:when test="//category/text()='technical-administrative'">
        <b>
          Pågående og planlagte prosjekter ved 
          <xsl:if test="not($code='')">
            ved&#160;<xsl:value-of select="$code"/>&#160;
          </xsl:if>
        </b>
      </xsl:when>
      <xsl:otherwise>
        <b>
          Pågående og planlagte forskningsprosjekter 
          <xsl:if test="not($code='')">
            ved&#160;<xsl:value-of select="$code"/>&#160;
          </xsl:if>
        </b>
      </xsl:otherwise>
    </xsl:choose>    
    <xsl:call-template name="list-structuredtext" />
  </xsl:if>
</xsl:template>


<xsl:template match="publications">
  <xsl:if test="not(.='') and ./node()">  
    <b>Utvalgte publikasjoner&#160; </b> 
    <xsl:call-template name="list-structuredtext" />
  </xsl:if>
</xsl:template>
  

  
<!-- Generic template to <li> each separate 'structuredtext' sub-element of calling node -->
<xsl:template name="list-structuredtext">
  <xsl:if test="not(current()='')">  <!-- TEST OM TOM NODE, IKKE TOM TEKST (hvordan?) -->
    <ul>
      <xsl:for-each select="./*">
        <li><xsl:apply-templates /></li>     
      </xsl:for-each>
    </ul>
  </xsl:if>
</xsl:template> 
  
</xsl:stylesheet>
