/upstream/templates/borrow.tmpl
Type: /type/template
body: value: $def with (page, loans, has_returned_recently)

$var title: $page.title

$ _x = ctx.setdefault('bodyid', 'plain')

$ available_loans = page.get_available_loans()
$ user_can_borrow_this = False
$ meta_fields = page.get_ia_meta_fields()
$if meta_fields.has_key('collection'):
    $ collection = meta_fields['collection']
$else:
    $ collection = None
$if meta_fields.has_key('contributor'):
    $ contrib = meta_fields['contributor']
$else:
    $ contrib = None

$ show_returned_message = False
$if has_returned_recently and not page.key in [loan['book'] for loan in loans]:
    $ show_returned_message = True

<div id="contentBody">

    <div class="navBorrow">
        <a href="http://openlibrary.org/" title="Return to home page"><img src="/images/logo_OL-sm.png" width="90" height="71" alt="Open Library logo" class="right"/></a>
        <a href="$page.url()"><strong>Back</strong></a>
    </div>

        <h1 class="sansserif">Borrow</h1>
        
        <div class="borrow">
            <table>
                <tbody>
                    <tr>
                        <td valign="top" style="padding-right:10px;">
                            $:render_template('covers/book_cover_small', page)
                        </td>
                        <td>
                            <span class="book">
                                <a class="borrowResults" href="$page.key">
                                <strong>$page.title</strong>
                                </a>
                            </span>
                            <span class="author">
                            $ authors = page.works[0].get_authors()
                            $:macros.AuthorList(authors)
                            </span>
                            <br/>
                            <span class="publisher">
                            $if page.publish_date or page.publishers:
                                $if page.publish_date:
                                    $page.publish_date,
                                $else:
                                    <em>Published date unknown,</em> 
                                $if page.publishers:
                                    $for p in page.publishers:
                                        $p$cond(loop.last, "", ", ")
                                $else:
                                    <em>publisher unknown</em>
                            </span>
                            $if contrib:
                                <span class="contributor small grey">
                                    from $contrib
                                </span>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

        $if page.key in [loan['book'] for loan in loans]:
            $# checked out by this user!
            
            $# find the loan
            $for candidate in loans:
                $if candidate['book'] == page.key:
                    $ active_loan = candidate
            
            $if can_return_resource_type(active_loan['resource_type']):
               <div class="message info">
                    <h2 class="sansserif">You have this book checked out. You can read it online or return the book.
                    <p>
                    $:macros.LoanReadForm(active_loan['book'])
                    $:macros.ReturnForm(active_loan)
                    </p>
                </div>
            $else:
                $# Currently this means ACS4
                $if active_loan['expiry'] is not None:
                    $# Has been fulfilled
                    <div class="message info">
                        <h2 class="sansserif">We show that you've borrowed this title.</h2>
                        <p>Open Adobe Digital Editions to read it or return to Open Library.</p>
                    </div>
                $else:
                    $# $$$ wording
                    <div class="message info">
                        <h2 class="sansserif">We show that you've borrowed this but haven't loaded it in Digital Editions yet.</h2>
                        <p>This loan is on hold for about 5 minutes. If you don't open it up within that time, someone else may borrow it. <a href="$active_loan['loan_link']"><strong>Download again</strong></a>?</p>
                       <p>Do you need to try <a href="http://www.adobe.com/products/digitaleditions/">downloading Adobe Digital Editions again</a>? The PDF or ePub loans will only open in that program.</p>
                    </div>

        $elif not can_borrow(page):
            $# Check for in-library
            $if 'In library' in page.works[0].get_subjects():
                $if available_loans:
                    <div class="message info">
                        $# george copy
                        $if show_returned_message:
                            <h2 class="sansserif">You've returned this book, but...</h2>
                            <p>It's part of the special "In Library" lending collection, which can only be accessed from within one of the <a href="/libraries">participating libraries</a>. (So, you'll need to go back into the library if you want to borrow it again.)
                            $ show_returned_message = False
                        $else:
                            <h2 class="sansserif">This book isn't available to borrow.</h2>
                            <p>Unless you find yourself literally sitting inside <a href="http://openlibrary.org/libraries">one of the libraries participating in the "In Library" lending program</a>. Look for something else in the <a href="/borrow">lending library</a>?</p>
                    </div>               
                $else:
                    <div class="message info">
                        <h2 class="sansserif">This title is checked out.</h2>
                        <p>It's special too, since it can only be borrowed from within a library that's participating in <a href="http://openlibrary.org/libraries">the "In Library" lending program</a>. Look for something else in the <a href="/borrow">lending library</a>?</p>
                    </div>               


            $else:
                $# not a borrowable book
                <div class="message info">
                    <h2 class="sansserif">This title is not available to borrow.</h2>
                    <p>Look for something else in the <a href="/borrow">lending library</a>?</p>
                </div>
            
        $elif not page.ocaid:
            $# overdrive-only - does not require login
            $# $$$ we could generate direct overdrive link since overdrive id is available
            <div class="message info">
                <h2 class="sansserif">An eBook may be available from your local library.</h2>
                <p><a href="this book link">Check overdrive.com</a> for availability.</p>
            </div>
            
        $elif not ctx.user:
            $# not logged in
            
            $if not available_loans:
                $# not logged in and book is checked-out
                <div class="message info">
                    <h2 class="sansserif">This title is checked out.</h2>
                    <p>But, you'll need to <a href="/account/login">log in</a> or <a href="/account/create">open a free account</a> to borrow eBooks from Open library.</p>
                </div>        

            $else:
                $# not logged in and book is available
                <div class="message alert">
                    <h2 class="sansserif">Only Open Library account holders can borrow this book.</h2>
                    <p>Log in below, or <a href="/account/create">open a new account</a>.</p>
                </div>
                <script type="text/javascript">
                \$().ready(validateForms);
                \$().ready(setup_account_create);
                \$().ready(function(){
                \$('#username').keyup(function(){
                     var value = \$(this).val();
                     \$('#userUrl').addClass('darkgreen').text(value).css('font-weight','700');
                    });
                });
                </script>
                <div class="panel">
                    <div class="head">
                        <h3>Log In</h3>
                    </div>
                    <form class="login olform" name="register" action="/account/login" method="post">
                    <div class="formElement">
                        <div class="label"><label for="username">$_("Username")</label> <span class="smaller lighter"></span></div>
                        <div class="input">
                            <input type="text" class="required" id="username" name="username" value=""/>
                        </div>
                    </div>
                    <div class="formElement">
                        <div class="label"><label for="password">$_("Password")</label> <span class="smaller lighter"></span></div>
                        <div class="input">
                            <input type="password" class="required"  id="password" name="password" value="" />
                        </div>
                    </div>
                    <div class="formElement">
                        <div class="input">
                            <input name="remember" type="checkbox" id="remember" />
                            <label for="remember" class="small">Remember me</label>
                        </div>
                    </div>
                    <input type="hidden" id="redirect" value="$page.url('/borrow')" name="redirect" />
                    <div class="clearfix"></div>
                    <div class="formElement bottom">
                        <br/>
                        <input value="$_('Log In')" type="submit" name="login" title="$_('Log In')"/>
                        &nbsp;
                        <a href="$page.key" class="smaller attn">$_("Cancel")</a>
                    </div>
                    <div class="formElement">
                        <div class="label"></div>
                        <div class="input smaller">$:_('Did you <a href="/account/password/forgot">forget your password</a>?')
                        <br/><br/>
                        $:_('Not a member of Open Library? <a href="/account/create">Sign up now</a>.')</div>
                    </div>
                    </form>            
                </div>

        $elif len(loans) >= 5:
            $# already loaned maximum number of books
            <div class="message stop">
                <h2 class="sansserif">Hang on. You've hit the lending limit of 5 concurrent titles.</h2>
                <p>You can free up space by returning one of <a href="/account/loans/" target="newWindow" title="Open link in a new browser window">your current loans</a> early.</p>
            </div>

        $elif not available_loans:
            $# book is not available for loan
            <div class="message info">
                <h2 class="sansserif">This book is checked out.</h2>
                <p>Please check back later, or browse <a href="/borrow">more books you can borrow</a>?</p>
            </div>
            $if ctx.user and ctx.user.is_admin():
                $# Stuff for adding to a list - NEED NEW WIDGET!
                <div class="Tools borrow" id="listsWork">
                    $:render_template("lists/widget", page, user_lists_only=True)
                    
                    <p id="borrowList" class="hidden">to keep an eye on it, or <a href="/subjects/lending_library">browse more titles</a>.</p>
                </div>
                <!-- 
                <p id="postBorrowList"><strong>OK!</strong> This title has been added to your <a href="LIST URL">LIST NAME</a> list.<br/>
                <a href="/borrow">Go back to browsing</a>?</p>
                -->
            
        $else:
            $# We can borrow it!
            $ user_can_borrow_this = True
            
        $if show_returned_message:
            $# May have returned this book and not currently borrowed
            <div class="message info">
                <h2 class="sansserif">You've returned this book.</h2>
                <p>Find something else to read in the <a href="/borrow">lending library</a>.</p>
            </div>
        
        $if user_can_borrow_this:
            <div class="borrow">
                <div class="postSubmit hidden center">
                    <p>Borrowing book...</p>
                    <img src="/images/ajax-loader-bar.gif" alt="Download in progress" width="220" height="19"/>
                </div>
                $:macros.LoanForm(page, available_loans)
            </div>        

        $if loans:
            $# user has outstanding loans
            <div class="clearfix"></div>
            <p class="help" style="margin-top:50px;border-top:1px dotted #615132;padding-top:10px;">You have $len(loans) of a possible 5 books out at the moment. Review <a href="/account/loans/" target="newWindow" title="Open link in a new browser window">your current loans</a>?<br/>
            <span class="smaller"><strong>Need help?</strong> Head for the <a href="/help/faq/borrow" target="newWindow" title="Open link in a new browser window">Borrowing FAQ</a>.</span></p>
        $else:
            <div class="clearfix"></div>
            <p class="help smaller" style="margin-top:50px;border-top:1px dotted #615132;padding-top:10px;"><strong>Need help?</strong> Head for the <a href="/help/faq/borrow" target="newWindow" title="Open link in a new browser window">Borrowing FAQ</a>.</p>

    $if ctx.user and ctx.user.is_admin():
        <div class="small sansserif clearfix"><br/><span class="adminOnly"><a href="borrow_admin">View loan debugging information</a></span></div>


</div>



<script type="text/javascript"> 
function update_borrow_message(){
    if (\$("#widget-lists").children().length > 0) {
        \$("#borrowList").hide();
    }
    else {
        \$("#borrowList").show();
    }
}    

\$(function(){
    \$('#widget-my-lists').children().insertBefore('#widget-my-lists').end().end().remove();
    \$('#widget-other-lists').children().insertBefore('#widget-other-lists').end().end().remove();
    if (\$('.dropit').length) {
        \$('#widget-add').addClass('contentQuarter').css('min-height','35px');
        \$('#listsWork .listLists').wrap('<div class="contentThreeQuarter" style="min-height:35px;">');
        \$('#listsWork').addClass('collapse');
    } else {
        \$('#listsWork').css('width','100%');
        \$('ul.listLists').css('width','100%')
    };
    
    update_borrow_message();
    \$(window).bind("ol.lists", update_borrow_message);
});
</script>
title: Borrow a Book
plugin: upstream