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

<meta>
  <description>Template für das BiblioMonde-System Zones 1.8</description>
  <variable name="baseurl"><description>Katalog-URL (inklusive .dll/, ohne Parameter)</description></variable>
  <variable name="style" default="Portal2"><description>Katalog-Style</description></variable>
</meta>


<action id="connect">
  <s>style := get('style', 'Portal2'),
     accountUrl := x"{$baseurl}/APS_ZONES?fn=MyZone"
   (:  accountUrl := x"{$baseurl}/APS_ACCOUNT?Style={$style}&SubStyle=&Theme=&Lang=GER&ResponseEncoding=utf-8&Parent=APS_OPAC&BrowseAsHloc=11" :)
(:     accountUrl := x"{$baseurl}/APS_ZONES?fn=MyLoans&Lang=GER" :)
  </s>
  <s>ordersListUrl := (), bookListUrl := (), bulkRenew := ()</s>
s  
<!--  <page url="{$baseurl}/APS_OPAC?Style={$style}">
    <template>
      <t:switch-prioritized>
        <a t:condition="matches(@href, 'APS_ACCOUNT|/APS_ZONES.*fn=Account')">{accountUrl := @href}</a>
        <h2>Das System ist gerade offline<t:s>vl:raise(.)</t:s></h2>
      </t:switch-prioritized>
    </template>
  </page>-->
</action>

<action id="search-connect">
 <!-- <page url="{$baseurl}/APS_OPAC?Style={get('style', 'Portal2')}">
    <template>
      <a t:condition="matches(@href, '/APS_QUICK_SEARCH')">{searchUrl := replace(@href, "QUICK_SEARCH", "ADVANCED_SEARCH")}</a>
    </template>
  </page>-->
  <s>style := get('style', 'Portal2'), 
     accountUrl := x"{$baseurl}/APS_ACCOUNT?fn=advancedsearch&Style={$style}&BrowseAsHloc=-2",
     $limit-map := {"libraryBranch": "Branch.", "libraryLocation": "scope.", "mediaType": "medium.", "language": ("publicationLang.", "Sprache.") }
     </s>

  <page url="{$baseurl}/APS_ZONES?fn=advancedsearch&Style={$style}"> 
    <template><body>
<!--      <a t:condition="matches(@href, 'APS_ACCOUNT|/APS_ZONES.*fn=Account')">{accountUrl := @href}</a>-->
      <FORM NAME="ExpertSearch">
        {$search-start:=form(.),
         let $limits := //input[starts-with(@name, "q.limits.limit")]
         return 
         search-params := ("title", "author", "free", "keywords", "year", "isbn", 
           for $key in jn:keys($limit-map) let $prefix := $limit-map($key) let $available := $limits[some $p in $prefix satisfies starts-with(@value, $p)]
           where exists($available)
           return {"name": $key, "options": ({"caption": "alle", "value": ""}, $available/{"caption": normalize-space(..), "value": @value}) }
         )}
      </form>
    </body></template>
  </page>
</action>

<action id="search"> 
  <variable name="search-keys">{"author": "au=", "title": "ti=", "free": "any=", "keywords": "su=", "isbn": "sb=", "year": "dp="}</variable>
  
  <s>
    i := 1,
    requestId := 0,
    request := uri-combine($search-start.url, 
      (for $key in jn:keys($book) where $book($key) and $search-keys($key) count $i 
       return {x"q.form.t{$i}&#46;expr": $book($key), x"q.form.t{$i}&#46;term": $search-keys($key)},
       for $key in jn:keys($limit-map) where $book($key) 
       return{"q.limits.limit": $book($key)}
      ))
  </s>
  
  <page url="{$request}" templateFile="searchList">
  
  </page>
  <s>search-next-page-available := $search-result-count > 0,
     requestId := 1,
     need-to-login := true()
     </s>  
</action>

<action id="search-next-page"> 
  <page url="{$baseurl}/{$obj}?Style={$style}&SubStyle=&Lang=GER&ResponseEncoding=utf-8&ResponseEncoding=utf-8&Method=FetchIncrementalBrowseDown&RequestId={$requestId}&" templateFile="searchList"/>
  <s>requestId := $requestId + 1</s>
</action>


<action id="search-details"> 
  <if test="contains($book.home-url, '&View=ISBD&')"><s>$book.home-url:=replace($book.home-url, "&View=ISBD&", "&View=Annotated&")</s></if> <!-- needed for cologne -->
 <if test="contains($book.home-url, 'hannover') and contains($book.home-url, 'APS_CAT_IDENTIFY')"><s>$book.home-url:= $book.home-url
  => replace("/APS_CAT_IDENTIFY", "/APS_PRESENT_BIB")
  => replace("Method=.*Key=", "Method=ShowStock&amp;no=")
 </s></if> 
  <page url="{$book.home-url}" templateFile="searchDetails"/>
</action>

<action id="internal-login">
  <page url="{$accountUrl}">
    <template>
      <t:switch-prioritized>
        <form name="LoginForm">  {form := form(., {"BRWR": $username, "PIN": $password})} </form>
        <form name="MainForm">   {form := form(., {"BRWR": $username, "PIN": $password})} </form>
        <h2>Das System ist gerade offline<t:s>vl:raise(.)</t:s></h2>
        <a t:condition="contains(@href, 'MyLoans')">{form := $accountUrl}</a> <!-- are already logged in-->
        <table>
          <a t:condition="contains(@href, 'ShowExpandedAccount')">{form := $accountUrl}</a> <!-- are already logged in, but nothing lend-->
          KEINE
        </table>
        <h2><font>{vl:raise(.)}</font></h2>
      </t:switch-prioritized>
    </template>
  </page>
  
  <page url="{$form}" templateFile="loggedIn"/>
</action>




<action id="order-single"> 
  <page url="{$book._order-url}" templateFile="orderConfirmation"/> 
  <if test="$need-to-login">
    <s>$accountUrl := $book._order-url, $need-to-login := false() </s>
    <call action="internal-login"/>
    <pattern href="orderConfirmation"/>
  </if>
</action> 	

<action id="internal-order-confirmed">
  <s>form := form-combine($confirm-form, {"MakeResTypeDef.Reservation.RecipientLocn": $choose-result, "Confirm": 1})</s>
  <page url="{$form}" test="$choose-result ne 0">
    <template>
      <td><t:meta text-matching="matches"/>Die Vormerkung wurde erfolgreich (durch|aus)geführt<t:s>book.statusId := "ordered"</t:s></td>
    </template>
  </page>
</action>


<action id="update-all">  
  <s>bFoundBottom := true()</s>
  <call action="internal-login"/>
    
  <s>requestId := 0</s>
  <page url="{$bookListUrl}" templateFile="list"/> 
  
  <s>requestId := 1</s>
  
  <loop test="not($bFoundBottom)">
    <page url="{$baseurl}/{$obj}?Style={$style}&SubStyle=&Lang=GER&ResponseEncoding=utf-8&ResponseEncoding=utf-8&Method=FetchIncrementalBrowseDown&RequestId={$requestId}&" templateFile="list"/> 
    <s>requestId := $requestId + 1</s>
  </loop>

  <s>requestId := 0</s>
  <page url="{$ordersListUrl}" templateFile="listOrders"/> 
  
  
  <s>requestId := 1</s>  
  <loop test="not($bFoundBottom)">
    <page url="{$baseurl}/{$obj}?Style={$style}&SubStyle=&Lang=GER&ResponseEncoding=utf-8&ResponseEncoding=utf-8&Method=FetchIncrementalBrowseDown&RequestId={$requestId}&" templateFile="listOrders"/>
    <s>requestId := $requestId + 1</s>
  </loop>
  
</action>

<action id="renew-list">  
  <if test="exists($renew-books?_renewLink)">
    <loop var="link" list="$renew-books?_renewLink">
      <page url="{$link}" templateFile="renewConfirm"/>
    </loop>
  </if>
  <else>
    <if test="not(get('bulkRenew', ()))">
      <s>vl:raise("Keinen Verlängerungslink gefunden. Vielleicht ist das Konto gesperrt, z.B.: wegen der Jahresgebühr der Bibliothek.")</s>
    </if>
  
    <s>renew-ids := for $book in $renew-books return ( x"item:IN={$book.id};AT={$book.author}/{$book.title}" ! 
            (if (contains(., "'")) then (., substring-before(., "'")) else . )
    )</s>
    
    <page url="{$bulkRenew}" templateFile="bulkRenew"/>
  
    <s>requestId := 1</s>  
    <loop test="not($bFoundBottom)">
      <page url="{$baseurl}/{$obj}?Style={$style}&SubStyle=&Lang=GER&ResponseEncoding=utf-8&BulkRenew=1&ResponseEncoding=utf-8&Method=FetchIncrementalBrowseDown&RequestId={$requestId}&" templateFile="bulkRenew2"/>
      <s>requestId := $requestId + 1</s>
    </loop>
  
  
    <s>renewConfirm := form-combine($baseForm, $options)</s>  
    <page url="{$renewConfirm}">
      <template><META NAME="ZonesObjName">{progressObj := @CONTENT}</META></template>
    </page>
    
    <s>i := 0</s>
    <loop test="$i < 10">
      <page url="{$baseurl}/{$progressObj}?Style={$style}&SubStyle=&Lang=GER&ResponseEncoding=utf-8&Method=BGTasks&Frame=Progress&BrowseAsHloc=-2&random={random()}&">
        <template><html>{
          if ( //script[matches(., 'finishedHere *= *[(] *"0" *== *"1" *[)]')]) then sleep(1000) else $i := 1000 
        }</html></template>
      </page>
      <s>$i := $i + 1</s>
    </loop> 
  </else>  
  
  <call action="update-all"/>  
</action>


<action id="cancel-list">
  <s>$cancel-url := $cancel-books[1]._cancel-url</s>
  <loop var="i" list="1 to count($cancel-books)">
    <s>b := $cancel-books[$i], nb := $cancel-books[$i+1], 
       if (not($cancel-url)) then vl:raise("Vormerkung nicht mehr löschbar") else ()
    </s>
    <page url="{$cancel-url}" templateFile="cancelConfirm"/>
    <page url="{$cancel-form}"> 
      <template>
        <t:if t:test="exists($nb)">
              <DIV class="inRoundBox1">
                <TABLE>
                  <!--  <TR><TD>Verfasser</TD><TD t:condition="normalize-space(.) eq $nb.author"/></TR> some books have no author => there is no table row
                    <TR><TD>Titel</TD><TD t:condition="normalize-space(.) eq $nb.title"/></TR>-->
                    <TR><TD>Signatur</TD><TD t:condition="normalize-space(.) eq $nb.id"/></TR>
                    <TR><TD></TD><TD><A t:condition="contains(@href, 'Cancel')">{$cancel-url := @href}</A></TD></TR>  
                </TABLE>
              </DIV>
        </t:if>
      </template>
    </page>
  </loop>
  
<!--    <template>
      <head>
       <META NAME="ZonesObjName">{obj:=@CONTENT, bFoundBottom := false()}</META>
      </head> 
    </template>-->
 

<!--  <s>requestId := 1</s>    
  <loop test="not($bFoundBottom)">
    <page url="{$baseurl}/{$obj}?Style={$style}&SubStyle=&Lang=GER&ResponseEncoding=utf-8&ResponseEncoding=utf-8&Method=FetchIncrementalBrowseDown&RequestId={$requestId}&"> 
    <template><body>{bFoundBottom := (./text())[1]/matches(., "bFoundBottom *= *1")}</body></template>
    </page>
    <s>requestId := $requestId + 1</s>
  </loop>-->
  <call action="update-all"/>  <!-- cancel urls change after cancelling. strangely, the old urls still work in firefox. but in videlibri we need to get new urls -->
</action>

<action id="catalogue">  
  <call action="connect"/>
  <s>url:=x"{$baseurl}/APS_OPAC?Style=Portal2"</s>
</action>

<!--http://katalog.stadtbibliothek.luebeck.de/alswww2.dll/Obj_577491377434134?
dateReq=1377434653896-->
</actions>
