<?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 (v004)                         -->
<!--  17.03.06 - kristian.syversen@usit.uio.no  -->
<!-- ========================================== -->


<!--
Withing this transformation document, preset texts are made to be translateable:

Hence adding text to be displayed in the transformed HTML, use this substitution

  <xsl:call-template name="person-document-phrase">
    <xsl:with-param name="identifier" select="'person.identifierName'"/>
  </xsl:call-template>

and write a corresponding text translation element within the 'person-translations.xsl' file!
-->


<!-- prefixes (text element definitions) -->
<xsl:import href="person-translations.xsl" />

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

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

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


<!-- GLOBAL VARIABLES -->

<!-- Values from PERSONSØK query (Requires element 'username' to be correctly set!) --> 
<!-- 
REMARK: 
Values from PERSONSØK are fetched even if user overrides value, as conditional 
statements cannot wrap global elements in this transformation file.
-->

<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-details" select="document($uri)" />    


<!-- XML-tree conversion on this form and similar does not work with XSLT 1 -->
<!-- (problem avoided by casting multiple entries to ';'-separated list strings -->
<!--
<xsl:variable name="person-details">
  <xsl:value-of select="document($uri)"/>
</xsl:variable> 
--> 

<xsl:variable name="override" select="//override-user-details/text()" />

<xsl:variable name="firstname">
  <xsl:choose>
    <xsl:when test="$override='Yes'">
      <xsl:value-of select="//override-ldap/givenname" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$person-details/person/givenname" />    
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="lastname">
  <xsl:choose>
    <xsl:when test="$override='Yes'">
      <xsl:value-of select="//override-ldap/surename" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$person-details/person/surename" />    
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="title">
  <xsl:choose>
    <xsl:when test="$override='Yes'">
      <xsl:value-of select="//override-ldap/title" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$person-details/person/title" />    
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- postal address has multiple lines -->
<xsl:variable name="postal-address">
  <xsl:choose>
    <xsl:when test="$override='Yes'">
      <!-- line paramters converted to a ';'-separated string -->
      <xsl:for-each select="//override-ldap/postal-address/line">
        <xsl:value-of select="." />
        <xsl:if test="not( position() = last() )">
          <xsl:text>;</xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
      <!-- line paramters converted to a ';'-separated string -->
      <xsl:for-each select="$person-details/person/postal-address/line">
        <xsl:value-of select="." />
        <xsl:if test="not( position() = last() )">
          <xsl:text>;</xsl:text>
        </xsl:if>
      </xsl:for-each>   
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- street address has multiple lines -->
<xsl:variable name="street-address">
  <xsl:choose>
    <xsl:when test="$override='Yes'">
      <!-- line paramters converted to a ';'-separated string -->
      <xsl:for-each select="//override-ldap/street-address/line">
        <xsl:value-of select="." />
        <xsl:if test="not( position() = last() )">
          <xsl:text>;</xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
      <!-- line paramters converted to a ';'-separated string -->
      <xsl:for-each select="$person-details/person/street-address/line">
        <xsl:value-of select="." />
        <xsl:if test="not( position() = last() )">
          <xsl:text>;</xsl:text>
        </xsl:if>
      </xsl:for-each>   
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- phones has multiple 'phone' number entries, hence generate comma-separated list -->   
<xsl:variable name="phones">
  <xsl:choose>
    <xsl:when test="$override='Yes'">
      <xsl:for-each select="//override-ldap/phones/phone">
        <xsl:value-of select="." />
        <xsl:if test="not( position() = last() )">
          <xsl:text>,&#160;</xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
      <xsl:for-each select="$person-details/person/phones/phone">
        <xsl:value-of select="." />
        <xsl:if test="not( position() = last() )">
          <xsl:text>,&#160;</xsl:text>
        </xsl:if>
      </xsl:for-each>   
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- faxes has multiple 'fax' number entries, hence generate comma-separated list -->   
<xsl:variable name="faxes">
  <xsl:choose>
    <xsl:when test="$override='Yes'">
      <xsl:for-each select="//override-ldap/faxes/fax">
        <xsl:value-of select="." />
        <xsl:if test="not( position() = last() )">
          <xsl:text>,&#160;</xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
      <xsl:for-each select="$person-details/person/faxes/fax">
        <xsl:value-of select="." />
        <xsl:if test="not( position() = last() )">
          <xsl:text>,&#160;</xsl:text>
        </xsl:if>
      </xsl:for-each>   
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- emails has multiple 'email' entries -->    
<xsl:variable name="emails">
  <xsl:choose>
    <xsl:when test="$override='Yes'">
      <xsl:for-each select="//emails/email">
        <xsl:value-of select="." />
        <xsl:if test="not( position() = last() )">
          <xsl:text>;</xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
      <xsl:for-each select="$person-details/person/emails/email">
        <xsl:value-of select="." />
        <xsl:if test="not( position() = last() )">
          <xsl:text>;</xsl:text>
        </xsl:if>
      </xsl:for-each>   
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="homepage">
  <xsl:choose>
    <xsl:when test="$override='Yes'">
      <xsl:value-of select="//override-ldap/homepage" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$person-details/person/homepage" />    
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>


<!-- MAIN ELEMENT MATCHING -->

<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="breadcrumb-and-mirrorlink" />
      <xsl:apply-templates /> 
    </body>
  </html>
</xsl:template>

  
<xsl:template name="breadcrumb-and-mirrorlink">
  <xsl:variable name="current-language">
    <xsl:choose>
      <xsl:when test="not( normalize-space($contentLanguage)='en' )">
        <xsl:text>no</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>en</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="filename-no" select="concat($username, '.xml')" />
  <xsl:variable name="filename-en" select="concat($username, '-eng.xml')" />
  <small>
    <span style="float: left;"> <xsl:call-template name="prefixed-breadcrumb" /> </span>
    <span style="float: right;">
      <xsl:choose>
        <xsl:when test="$current-language='no' 
                        and document(normalize-space($filename-en), self::node())//username/text()!=''
                        and not( contains($CURRENT-URL, '-eng.xml') )"> <!-- avoid self-reference if wrong language is set for english version -->
          <a href="{$filename-en}">English version</a>
          <img src="/vrtx/vortex-webdocuments/uio/person/english.png" alt="english-icon"/>
        </xsl:when>
        <xsl:when test="$current-language='en' 
                        and document(normalize-space($filename-no),self::node())//username/text()!='' 
                        and contains($CURRENT-URL, '-eng.xml')"> <!-- avoid self-reference -->
          <a href="{$filename-no}">Norsk versjon</a>
          <img src="/vrtx/vortex-webdocuments/uio/person/norsk.png" alt="norsk-ikon"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>&#160;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </span>
  </small>
  <br clear="all"/>
</xsl:template>  


<xsl:template match="person"> 
  <!--padding to ensure proper spacing in Internet Explorer -->
  <h3 style="padding-top: 15px">
    <!-- outer -->
    <xsl:choose>
      <xsl:when test="$override!='Yes'"> 
        <!-- inner -->
        <xsl:choose>
          <xsl:when test="$username!=''">    
            <xsl:choose>
              <!-- Error in result from Personsøk (e.g. service is down): -->
              <xsl:when test="not( $person-details/node() )">
                <i><span style="color:red;"> 
                  <xsl:call-template name="person-document-phrase">
                    <xsl:with-param name="identifier" select="'person.ldap_error'"/>
                  </xsl:call-template>
                </span></i>                  
              </xsl:when>
              <!-- User not found: -->
              <xsl:when test="not( $person-details/person/node() )">
                <i><span style="color:red;"> 
                  <xsl:call-template name="person-document-phrase">
                    <xsl:with-param name="identifier" select="'person.user_not_found'"/>
                    <xsl:with-param name="string" select="$username"/>
                  </xsl:call-template>
                </span></i>
              </xsl:when>                
              <!-- Expected behaviour: -->
              <xsl:otherwise>
                <!-- form LDAP -->
                <xsl:value-of select="$firstname" />&#160;<xsl:value-of select="$lastname" />
                <br/>
                <small><xsl:value-of select="$title" /></small>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <i><span style="color:red;"> 
              <xsl:call-template name="person-document-phrase">
                <xsl:with-param name="identifier" select="'person.username_missing'"/>
              </xsl:call-template>
            </span></i>
          </xsl:otherwise>
        </xsl:choose>
        <!-- end inner -->
      </xsl:when>
      <xsl:otherwise>
        <!-- form 'override-ldap' component -->
        <xsl:value-of select="$firstname" />&#160;<xsl:value-of select="$lastname" />
        <br/>
        <small><xsl:value-of select="$title" /></small>
      </xsl:otherwise>
    </xsl:choose>
    <!-- end outer -->
  </h3>
  
 
  <xsl:call-template name="person-info-table" />
          
  <xsl:apply-templates select="background" /> 
  <xsl:apply-templates select="vocation" /> 
  <xsl:apply-templates select="competence" /> 
  <xsl:apply-templates select="projects" /> 
  <xsl:apply-templates select="publications" />
  <xsl:apply-templates select="other" />
</xsl:template>


<xsl:template name="institute">
  <xsl:param name="code" />
  <xsl:choose>
    <xsl:when test="$code=''">
      <xsl:call-template name="person-document-phrase">
        <xsl:with-param name="identifier" select="'person.institute_code_missing'"/>
      </xsl:call-template>
    </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 høyde på bildet her (bredde blir da automatisk skalert riktig ift høyde): -->
  <xsl:variable name="image-height">200px</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" style="padding-right:20px;"> 
          <i>
            <xsl:variable name="alt-text">
              <xsl:choose>
                <xsl:when test="string-length($username)!=0">
                  [<xsl:value-of select="$username"/>.jpg]
                </xsl:when>
                <xsl:otherwise>
                  - no image -
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <img src="{$images-uri}{$username}.jpg" 
                 alt="{$alt-text}" 
                 style="height: {$image-height};"/>
          </i>
          <br/>
          <center style="font-size:75%;">
            <xsl:variable name="highres-image-link" select="//highres-image-link" />
            <xsl:choose>
              <xsl:when test="$highres-image-link!=''">
                (<a href="{$highres-image-link}">
                  <xsl:call-template name="person-document-phrase">
                    <xsl:with-param name="identifier" select="'person.press_image'"/>
                  </xsl:call-template>            
                </a>)
                <br/>
<!-- Lenke til side om bruk av pressebilder fra UiO -->
<!-- NB: P.d.d gjelder denne HF! Dette skal forandres til generell side for UiO -->              
                <i>
                  (<a href="http://www.hf.uio.no/om-fakultetet/opphavsrett.html">
                    <xsl:call-template name="person-document-phrase">
                      <xsl:with-param name="identifier" select="'person.press_image_usage'"/>
                    </xsl:call-template>            
                  </a>)
                </i>
              </xsl:when>
              <xsl:otherwise>&#160;</xsl:otherwise>
            </xsl:choose>
          </center>
        </td>
      </xsl:if>

      <td valign="top">
        &#160;<b>
          <xsl:call-template name="person-document-phrase">
            <xsl:with-param name="identifier" select="'person.postal_address'"/>
          </xsl:call-template>
        </b>&#160;
      </td>
      <td valign="top">
        <xsl:call-template name="print-string-lines">
          <xsl:with-param name="input" select="$postal-address"/>
        </xsl:call-template>
      </td>      
    </tr>

    <tr>     
      <td valign="top">
        &#160;<b>
          <xsl:call-template name="person-document-phrase">
            <xsl:with-param name="identifier" select="'person.visiting_address'"/>
          </xsl:call-template>
        </b>&#160;
      </td>
      <td valign="top">
        <xsl:call-template name="print-string-lines">
          <xsl:with-param name="input" select="$street-address"/>
        </xsl:call-template>
      </td>    
    </tr>
    
    <tr>
      <td valign="top">
        &#160;<b>
          <xsl:call-template name="person-document-phrase">
            <xsl:with-param name="identifier" select="'person.room'"/>
          </xsl:call-template>
        </b>&#160;
      </td>
      <td valign="top"> <xsl:apply-templates select="room" /> </td>
    </tr>

    <xsl:apply-templates select="office-hours" /> <!-- row is inserted if value is found -->
    
    <xsl:if test="$phones!=''">
      <tr>
        <td valign="top">
          &#160;<b>
            <xsl:call-template name="person-document-phrase">
              <xsl:with-param name="identifier" select="'person.phones'"/>
            </xsl:call-template>
          </b>&#160;
        </td>
        <td valign="top">
          <xsl:value-of select="$phones" />
        </td>
      </tr>
    </xsl:if>
    
    <xsl:if test="$faxes!=''">
      <tr>
        <td valign="top">
          &#160;<b>
            <xsl:call-template name="person-document-phrase">
              <xsl:with-param name="identifier" select="'person.faxes'"/>
            </xsl:call-template>
          </b>&#160;
        </td>
        <td valign="top">
          <xsl:value-of select="$faxes" />        
        </td>
      </tr>
    </xsl:if>

    <tr>
      <td valign="top">
        &#160;<b>
          <xsl:call-template name="person-document-phrase">
            <xsl:with-param name="identifier" select="'person.email'"/>
          </xsl:call-template>
        </b>&#160;
      </td>
      <td valign="top">
        <xsl:call-template name="print-emails">
          <xsl:with-param name="input" select="$emails"/>
        </xsl:call-template>
      </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 valign="top">
        &#160;<b>
          <xsl:call-template name="person-document-phrase">
            <xsl:with-param name="identifier" select="'person.office_hours'"/>
          </xsl:call-template>
        </b>&#160;
      </td>
      <td valign="top"> <xsl:value-of select="." /> </td>
    </tr>
  </xsl:if> 
</xsl:template>

<xsl:template name="homepage-row">
  <xsl:if test="not($homepage='') and //show-homepage-link!='No'">
    <tr>
      <td valign="top">
        &#160;<b>
          <xsl:call-template name="person-document-phrase">
            <xsl:with-param name="identifier" select="'person.homepage'"/>
          </xsl:call-template>
        </b>&#160;
      </td>
      <td valign="top">
        <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>   
<!-- end of table element templates -->


<!-- SUB-ELEMENT MATCHING -->

<xsl:template match="background">
  <xsl:if test="not(.='') and ./node()">
    <p>
      <b>
        <xsl:call-template name="person-document-phrase">
          <xsl:with-param name="identifier" select="'person.background'"/>
        </xsl:call-template>
      </b>
    </p>
    <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'">
        <p>
          <b>
            <xsl:call-template name="person-document-phrase">
              <xsl:with-param name="identifier" select="'person.vocation_technical_administrative'"/>
              <xsl:with-param name="string" select="//institute/text()"/>
            </xsl:call-template>
          </b>
        </p>
      </xsl:when>
      <xsl:otherwise>
        <p>
          <b>
            <xsl:call-template name="person-document-phrase">
              <xsl:with-param name="identifier" select="'person.vocation_scientific'"/>
              <xsl:with-param name="string" select="//institute/text()"/>
            </xsl:call-template>
          </b>
        </p>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:call-template name="list-structuredtext" />
  </xsl:if>
</xsl:template>
  
  
<xsl:template match="competence">
  <xsl:if test="not(.='') and ./node()">
    <p>
      <b>
        <xsl:call-template name="person-document-phrase">
          <xsl:with-param name="identifier" select="'person.competence'"/>
        </xsl:call-template>
      </b>
    </p>
    <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'">
        <p>
          <b>
            <xsl:call-template name="person-document-phrase">
              <xsl:with-param name="identifier" select="'person.projects_technical_administrative'"/>
              <xsl:with-param name="string" select="//institute/text()"/>
            </xsl:call-template>
          </b>
        </p>
      </xsl:when>
      <xsl:otherwise>
        <p>
          <b>
            <xsl:call-template name="person-document-phrase">
              <xsl:with-param name="identifier" select="'person.projects_scientific'"/>
              <xsl:with-param name="string" select="//institute/text()"/>
            </xsl:call-template>
          </b>
        </p>
      </xsl:otherwise>
    </xsl:choose>    
    <xsl:call-template name="list-structuredtext" />
  </xsl:if>
</xsl:template>


<xsl:template match="publications">
  <xsl:if test="(not(.='') and ./node()) or (//key-frida!='')">  
    <p>
      <b>
        <xsl:call-template name="person-document-phrase">
          <xsl:with-param name="identifier" select="'person.selected_publications'"/>
        </xsl:call-template>
      </b>
    </p>
  </xsl:if>
  <xsl:if test="not(.='') and ./node()">
    <xsl:call-template name="list-structuredtext" />
  </xsl:if>
  <xsl:if test="//key-frida!=''">
    <p>
      <xsl:apply-templates select="//key-frida"/>
    </p>
  </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="." />
    <xsl:variable name="search-and-sort">&#038;action=sok&#038;visParametre=1&#038;sort=ar</xsl:variable>
    <a href="{$frida}{$key}{$search-and-sort}">
      <xsl:call-template name="person-document-phrase">
        <xsl:with-param name="identifier" select="'person.publications'"/>
      </xsl:call-template>
    </a>
  </xsl:if>
</xsl:template>
  

<xsl:template match="other">
  <xsl:if test="not(.='') and ./node()">
    <xsl:call-template name="list-structuredtext" />
  </xsl:if>
</xsl:template>  
  
  
  
<!-- GENERIC TEMPLATES -->

<!-- General template to print a string containing ';'-separated lines as single lines -->
<xsl:template name="print-string-lines">
  <xsl:param name="input" />
  <xsl:if test="$input!=''">
    <xsl:choose>
      <xsl:when test="contains($input, ';')">
        <xsl:variable name="line" select="substring-before($input, ';')" /> 
        <xsl:variable name="rest" select="substring-after($input, ';')" />      

        <xsl:value-of select="$line" /> <br/>
        
        <xsl:call-template name="print-string-lines">
          <xsl:with-param name="input" select="$rest" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$input" />
      </xsl:otherwise> 
    </xsl:choose>
  </xsl:if>
</xsl:template>


<!-- template used to set email addresses in variable $emails as proper HTML -->
<xsl:template name="print-emails">
  <xsl:param name="input" />
  <xsl:if test="$input!=''">
    <xsl:choose>
      <xsl:when test="contains($input, ';')">
        <xsl:variable name="adr" select="substring-before($input, ';')" /> 
        <xsl:variable name="rest" select="substring-after($input, ';')" />      

        <a href="mailto:{$adr}"> <xsl:value-of select="$adr" /> </a>,&#160;
        
        <xsl:call-template name="print-emails">
          <xsl:with-param name="input" select="$rest" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <a href="mailto:{$input}"> <xsl:value-of select="$input" /> </a>
      </xsl:otherwise> 
    </xsl:choose>
  </xsl:if>
</xsl:template> 

  
  <!-- FIX DENNE slik at det vises uten bombepunkter: -->
  

<!-- Generic template to <li> each separate 'structuredtext' sub-element of calling node -->
<xsl:template name="list-structuredtext">
  <xsl:for-each select="./*">
    <xsl:apply-templates />     
  </xsl:for-each>
</xsl:template> 
  
</xsl:stylesheet>
