<?xml version="1.0" encoding="UTF-8"?>
<actions>

<meta>
  <description>Template des aDISWeb-Systems, üblicherweise in Bayern verwendet</description>
  <variable name="server"><description>reiner Servername, (z.B.: ssl.muenchen.de) </description></variable>
  <variable name="startparams" default=""><description>Parameter in der URL nach aDISWeb/app für Server, auf denen mehrere Bibliothekssysteme laufen</description></variable>    
  <variable name="multiple-district-ordering" default=""><description>Spezielle Option für VOEBB (oder ähnliche Verbünde). Für Einzelbibliotheken leerlassen</description></variable>    
</meta>


<action id="connect"></action>

<action id="internal-login"> 
  <if test="get('idpSystem')">
    <s>i := 1</s>
    <!-- follow links till we reach the login page -->
    <loop test="$i < 10">
        <page url="{$loginFormPost}">
          <template><t:switch-prioritized>  
            <div class="notify">Benutzernummer oder Passwort falsch<t:s>vl:raise-login(.)</t:s></div> <!-- blb karlsruhe -->
          
            <!-- seems we already are on the correct page, send request again (not tested with any library) -->
            <td>Keine Ausleihen<t:s>i:=1000</t:s></td>
            <a>Ausleihen zeigen oder verlängern<t:s>i:=1000</t:s></a> 
            <a>Ausleihe zeigen oder verlängern<t:s>i:=1000</t:s></a>
          
            <!-- we were logged out ? -->
            <a>Mein Konto<t:s>loginFormPost := .</t:s></a>
    
            <!-- redirection -->
            <form>{loginFormPost := form(.)}</form>
            <a>{$loginFormPost}</a>
            <meta http-equiv="refresh">{$loginFormPost}</meta>
          </t:switch-prioritized></template>
        </page>
        <s>$i := $i + 1</s>
    </loop>
    
  
  </if>  


  <page test="not(get('idpSystem'))" url="{$loginFormPost}">
    <post name="textButton" value="Anmeldung abschicken"/>
    
    <template>
      <template:switch-prioritized>
        <form>{loginFormPost:=form(.)}
        <h1>Sie sind angemeldet.</h1>
        </form>

        <td>Keine Ausleihen</td>
        <a>Ausleihen zeigen oder verlängern</a> <!-- seems we already are on the correct page, send request again (not tested with any library) -->
        <a>Ausleihe zeigen oder verlängern</a>
        
        <h1>Bitte auch das Passwort angeben<t:s>vl:raise-login(.)</t:s></h1>
        <h1>Sie sind nicht mit Ihren Angaben registriert<t:s>vl:raise-login(.)</t:s></h1>
        <li>Benutzernummer oder Passwort falsch?<t:s>vl:raise-login(.)</t:s></li>
        <h1>Das System kann Sie derzeit nicht anmelden! Angaben sind fehlerhaft!<t:s>vl:raise-login(.)</t:s></h1>
        <h1>Anmeldung fehlgeschlagen. Bitte überprüfen Sie Ihre Eingabe.<t:s>vl:raise-login(.)</t:s></h1>
        
         
        <span class="bnzeig" t:condition="contains(text(), 'Sie sind angemeldet')" /> <!-- sind schon angemeldet. (neuess, ordering!) -->
        
        <div class="message"><div id="R01"><h1>{vl:raise(.)}</h1></div></div>
         
      </template:switch-prioritized>
    </template>
  </page>  
</action>

<action id="update-all">
  <variable name="connected">false()</variable>
  <try>
    <page url="https://{$server}/aDISWeb/app{get('startparams', '')}" templateFile="connected"/>
    <catch errors="http:503"><s>vl:raise("Der Bibliothekskatalog ist momentan temporär nicht erreichbar. Vermutlich werden Wartungsarbeiten durchgeführt (insbesondere bei den VÖBB)")</s></catch>
  </try>
  
  <if test="$connected"><variable name="connectPage">$searchPage</variable></if>

  <page url="{$connectPage}">
    <template><template:switch>
      <a><span>Benutzerkonto</span>{loginForm:=@href}</a>
      <a><span>Mein Konto</span>{loginForm:=@href}</a>
      <a><span>Anmeldung<!--Mein Konto--></span>{loginForm:=@href}</a>
    </template:switch></template>
  </page>
  
  <loop test="exists($loginForm)">
    <s>lf := $loginForm, loginForm := ()</s>
    <page url="{$lf}">
      <template><t:switch prioritized="true">
        <input name="$Textfield">{ loginFormPost := form((ancestor::form, //form)[1], {"$Textfield": $username, "$Textfield$0": $password}) } <!-- Aalen has invalid html where the form is not an ancestor of the input-->  </input> 
        <input name="j_username">{ loginFormPost := form((ancestor::form, //form)[1], {"j_username": $username, "j_password": $password}),
                idpSystem := true() }</input> 
        <form><div class="message">Sind Sie Angehörige/r unserer Hochschule?
          <t:s> if (starts-with($username, "E")) then loginForm := form(ancestor::form[1], (.//input)[2])
            else if (starts-with($username, "I")) then (loginForm := form(ancestor::form[1], (.//input)[1]), idpSystem := true() )
            else vl:raise("Diese Bibliothek unterscheidet zwischen internen und externen Benutzern. In VideLibri muss dafür vor der Kontonummer jeweils der Buchstabe I oder E eingefügt werden. ")
          </t:s>
        </div></form>
        <form><div class="message">vl:raise(.)</div></form>
      </t:switch></template>
    </page>
  </loop>
  <call action="internal-login"/>
  
  <variable name="first-login">true()</variable>
  
  
  <page url="{$loginFormPost}" templateFile="accountOverview"/>

  <s>nextSplitTable := false(), $splitIndex := 1, $splitCount := 0</s>
  <if test="$kontoPage">
    <page url="{$kontoPage}" templateFile="list"/>
    <loop test="$nextSplitTable">
      <page url="{$nextSplitTable}" templateFile="list"/>
      <s>$splitIndex := $splitIndex + 1, $nextSplitTable := ()</s>
      <if test="$splitIndex <= $splitCount">
        <page url="{$goBackPage}" templateFile="accountOverview"/>
        <page url="{$kontoPage}" templateFile="nextSplitTable"/>
      </if>
    </loop>
    <page url="{$goBackPage}" templateFile="accountOverview"/>
  </if>
  
  <if test="$orderedMagazinePage">
    <page url="{$orderedMagazinePage}" templateFile="list"/>
    <page url="{$goBackPage}" templateFile="accountOverview"/>
  </if>
  
  <if test="$providedPage">
    <page url="{$providedPage}" templateFile="list"/>
    <page url="{$goBackPage}" templateFile="accountOverview"/>
  </if>

  <if test="$orderedPage">
    <page url="{$orderedPage}" templateFile="list"/>
    <page url="{$goBackPage}" templateFile="accountOverview"/>
  </if>
  
  <if test="$requestedPage">
    <page url="{$requestedPage}" templateFile="list"/>
    <page url="{$goBackPage}" templateFile="accountOverview"/>
  </if>

</action>

<action id="renew-list">
  <if test="not($splitCount)">
    <page url="{$kontoPage}" templateFile="list"/>
    <variable name="book-list">string-join(for $book in $renew-books return x"{$book.extendid}=on", "&amp;")</variable> 
    <page url="{$extendFormPost}"> <post value="{$book-list}"/> </page>    
  </if>
  <if test="$splitCount">
    <s>$renew-splits := distinct-values($renew-books ! (.)._splitIndex)</s>
    <loop var="splitIndex" list="$renew-splits">
      <page url="{$kontoPage}" templateFile="list"/>
      <page url="{$nextSplitTable}" templateFile="list"/>
      <variable name="book-list">string-join(for $book in $renew-books[(.)._splitIndex = $splitIndex] return x"{$book.extendid}=on", "&amp;")</variable> 
      <page url="{$extendFormPost}" templateFile="renewed"> <post value="{$book-list}"/> </page>    
      <page url="{$goBackPage}" templateFile="accountOverview"/>
    </loop>
  </if>
  <call action="update-all"/>
</action>



<action id="cancel-list">
  <loop var="mode" list="distinct-values($cancel-books ! (.)._mode)">
    <s>
    page := switch ($mode)
      case 'ordered' return $orderedPage
      case 'requested' return $requestedPage
      case 'orderedMagazine' return $orderedMagazinePage
      case 'provided' return $providedPage
      default return concat("unknown page ", $mode, "NEU AKTUALISIEREN")
    </s>
    <page url="{$page}">
      <template><form>{
        cancelFormPost := form(., jn:object((.//input[@type = "submit" and contains(@value, "arkierte") and contains(@value, "schen")])[1]/{string(@name): @value})),
        cancelFormPost.post := replace($cancelFormPost.post, "[a-zA-Z0-9]+=on&", "")
      }</form></template>
    </page> 

  
    <variable name="book-list">string-join(for $book in $cancel-books[(.)._mode = $mode] return  x"{$book.cancelid}=on", "&amp;")</variable> 
    
    <page url="{$cancelFormPost}" templateFile="renewed">
      <post value="{$book-list}"/>
    </page>
    
    <page url="{$goBackPage}" templateFile="accountOverview"/>
  </loop>
  
  <call action="update-all"/>
</action>









<action id="gotoindex">
  <loop test="$firstIndex > $targetIndex">
    <page url="{$form.url}" templateFile="searchBasicForm">
      <post value="{$form.post}&amp;%24Toolbar_4.x=0&amp;%24Toolbar_4.y=0"/>
    </page>
  </loop>

  <loop test="$lastIndex &lt; $targetIndex and $targetIndex &lt; $search-result-count">
    <page url="{$form.url}" templateFile="search">
      <post value="{$form.post}&amp;%24Toolbar_5.x=0&amp;%24Toolbar_5.y=0"/>
    </page>
  </loop>
  
</action>

<action id="search">
  <variable name="connected">false()</variable>
  
  <page url="https://{$server}/aDISWeb/app{get('startparams', '')}" templateFile="connected"/> <!-- some libraries log in directly, some redirect -->
  <page url="{$connectPage}" test="not($connected)" templateFile="connected"/>
  
  <page url="{$searchPage}" templateFile="searchInputForm"/>

  <variable>lastSearchedIndex := 0, search-next-page-available := (), gotDetails_GoBackPage := ()</variable>

  <page url="{$startSearch}" templateFile="search"/>

  <page test="exists($gotDetails_GoBackPage)" url="{$gotDetails_GoBackPage}" templateFile="search"/>
</action>

<action id="search-next-page">
  <variable>targetIndex := $lastSearchedIndex + 1, $goback := get('goback', ())</variable>
  <choose>
    <when test="$goback and $firstIndex &lt;= $targetIndex and $targetIndex &lt;= $lastIndex">
      <page url="{$goback}" templateFile="search"/>
    </when>
    <otherwise>
      <call action="gotoindex"/>
    </otherwise>
  </choose>
</action>


<action id="search-details">
  <variable name="targetIndex">$book._index * 1</variable> <call action="gotoindex"/>

  <page url="{$form.url}" templateFile="searchDetails">
    <post>{replace($form.post, "selected=", concat("selected=", $book._searchId))}</post>
  </page>
  
  <page url="{$goback}" templateFile="searchBasicForm"/>
  
</action>




<action id="order-single">
  <variable name="targetIndex">$book._index * 1</variable> <call action="gotoindex"/>

  <s>form := form-combine($form, concat("selected=", $book._searchId))</s>
  <call action="internal-order-single"/>
</action>

<action id="internal-order-single">
  <page url="{$form}">
    <template><form>{form:=form(., (.//input[@type = "submit" and contains(@value, "Bestellen")])[1]/{string(@name): @value})}</form></template>
  </page>
  
  <s>inCollection:=false(), subBookOnly := false(), error := ()</s>
  
  <page test="get('multiple-district-ordering', false())" url="{$form}" templateFile="orderMultipleDistrict"/>
  
  <if test="$inCollection">
    <page url="{$inCollection}">
      <template>
        <div id="R06"><div class="aDISListe"><table>
          <tr><th>In</th><td><a>{collection:=.}</a></td></tr> 
        </table></div></div>
      </template>
    </page>
    
    <page url="{$collection}">
      <template><form>{form:=form(., (.//input[@type = "submit" and contains(@value, "Bestellen")])[1]/{string(@name): @value})}</form></template>
    </page>
    <page test="get('multiple-district-ordering', false())" url="{$form}" templateFile="orderMultipleDistrict"/>
  </if>
  <call action="internal-order-check-volume"/>
  <call action="internal-go-back-with-error"/>
  
  <if test="not(get('multiple-district-ordering', false())) and not($subBookOnly)">
    <s>$choose-result := "ok"</s>
    <call action="internal-order-continue"/>
  </if>
</action>

<action id="internal-go-back-with-error">
  <if test="$error">
    <page url="{$goBackPage}">
      <template><a title="Trefferliste">{goBackPage:=@href, vl:raise($error)}</a></template>
    </page>
    <page url="{$goBackPage}" templateFile="searchBasicForm"/>
  </if>
</action>

<action id="internal-go-back2">
  <if test="$choose-result instance of xs:decimal">
    <page url="{$goBackPage}">
      <template><a title="Trefferliste">{goBackPage:=@href}</a></template>
    </page>
    <page url="{$goBackPage}" templateFile="searchBasicForm"/>
  </if>
</action>

<!-- when $subBookOnly is set, go to that page and ask which volume -->
<action id="internal-order-check-volume">
  <if test="$subBookOnly"> 
    <page url="{$subBookOnly}">
      <template>
        <form>
          <t:switch-prioritized>
            <input title="Zurück zur vorherigen Seite">{backButtonName := uri-encode(@name)}</input>
            <input t:condition="contains(@title, 'zur vorherigen Seite')">{backButtonName := uri-encode(@name)}</input>
          </t:switch-prioritized> 
          {goBackPage := form(., x"{$backButtonName}.x=0&{$backButtonName}.y=0")}
          <div id="R06"><div class="aDISListe"><table>
            <tr><th>Band</th><td>{vl:choose("internal-order-volume", "Welcher Band soll bestellt werden?", a/substring(normalize-space(.), 1, 75), a/@href)}</td></tr> 
          </table></div></div>
        </form>
      </template>            
    </page>
  </if>
</action>


 
<action id="internal-order-volume">
  <if test="$choose-result instance of xs:decimal">
    <page url="{$goBackPage}" templateFile="searchBasicForm"/>
  </if>
  <if test="$choose-result instance of xs:string">
    <s>form := $choose-result</s>
    <call action="internal-order-single"/>
  </if>
</action>

<action id="internal-order-continue">
  <call action="internal-go-back2"/>
  <if test="$choose-result instance of xs:string">
    <if test="get('multiple-district-ordering', false())"> 
      <page url="{$choose-result}">
        <template><form>{form:=form(., (.//input[@type = "submit" and contains(@value, "Bestellung")])[1])}</form></template>
      </page>
    </if>
    
    <s>internal-loggedin := true(), subBookOnly := false()</s>
    <page url="{$form}" templateFile="orderConfirmation"/>    
    <call action="internal-order-check-volume"/>
    <if test="not(get('internal-loggedin', false())) and not($subBookOnly)">
      <call action="internal-login"/>
      <variable name="internal-loggedin">true()</variable>
      <variable name="form">$loginFormPost</variable>
      <page url="{$form}" templateFile="orderConfirmation"/>
    </if>
  

    <call action="internal-go-back-with-error"/>
  </if>
</action>


<action id="internal-order-confirmed">
  <call action="internal-go-back2"/>

  <if test="$choose-result instance of xs:string">
    <variable name="form">
    {
      "url": $form.url,
      "method": $form.method,
      "post": replace($form.post, "_DESTINATIONFILLER_", $choose-result)
    }
    </variable>  

    <page url="{$form}" templateFile="orderConfirmed"/>
    <page url="{$form}" templateFile="orderConfirmed"/>
    <page url="{$final-form}">
      <template>
      <t:s>book.statusId := "ordered"</t:s>
  <!-- geht ohne das???   <a title="Trefferliste">{goback:=@href}</a>--><!--inside main in munich, outside in nürnberg-->
      </template>
    </page>

    <!-- works without this?? -->    
    <page url="{$goBackPage}">
      <template>
        <t:switch prioritized="true">
          <a title="Trefferliste">{goBackPage:=@href}</a>
          <span class="toolbar_txt">Vollanzeige<!--we are already back. safety. case never observed--></span>
        </t:switch>
      </template>
    </page>
    <page url="{$goBackPage}" templateFile="searchBasicForm"/>

  </if>
</action>

<action id="catalogue">
  <variable name="url" value="https://{$server}/aDISWeb/app{get('startparams', '')}"/>
</action>



</actions>