/upstream/macros/LoanStatus
Type: /type/macro
description: Link to edition/borrow page showing loan status
plugin: upstream
macro: value: $def with (page, ebook_text, user, current_and_available_loans, contributor, line_break = '')

$# TODO construct list of available formats from available_loans

$if current_and_available_loans:
  $ current_loans = current_and_available_loans[0]
  $ available_loans = current_and_available_loans[1]
$else:
  $ current_loans = []
  $ available_loans = []

$ user_loan = None
$if user:
    $for current_loan in current_loans:
        $if current_loan['user'] == user.key:
            $ user_loan = current_loan
            $ break

$ borrow_link = page.url() + '/borrow'

$if user_loan:
    $if user_loan['resource_type'] == 'bookreader':
        $# "do_return" is a fake "title" for the book that can be used for stats
        $ return_url = user_loan['book'] + '/do_return/borrow'
        $ form_id = 'return_' + user_loan['store_key']
        $ read_form_id = 'read_' + user_loan['store_key']

        <form id='$form_id' action="$return_url" method="post" style='display:hidden'>
        <input type="hidden" name="action" value="return" />
        </form>
        <form id='$read_form_id' method="POST" action="$page.key/_doread/borrow" style='display:hidden' id><input type="hidden" name="action" value="read"/></form>
        <p style="margin-bottom:10px;"><a href="$borrow_link" title="Borrow from $contributor">$ebook_text</a> <span class="smaller">You have this book checked out. 
        <a href="$user_loan['loan_link']" onclick="javascript:jQuery('#$read_form_id').submit(); return false">Read it</a> or <a href="$return_url" onclick="javascript:jQuery('#$form_id').submit(); return false;">return now</a>?</span></p>
    $else:
        <p style="margin-bottom:10px;"><a href="$borrow_link" title="Borrow from $contributor">$ebook_text</a> <span class="smaller">You have this book checked out.</span></p>    
        
$elif available_loans:
            <p style="margin-bottom:10px;"><a href="$borrow_link" title="Borrow from $contributor">$ebook_text</a> <span class="smaller">PDF, ePub or in browser $:{line_break}from $contributor</span></p>
$else:
            <p style="margin-bottom:10px;"><strike><a href="$borrow_link" title="Borrow from $contributor">$ebook_text</a></strike> <span class="smaller">This title is <i class="red">checked out</i>.</span></p>
m: edit