/upstream/macros/databarWork
Type: /type/macro
description: Work toolbar
plugin: upstream
macro: value: $def with (page)

$if page.type.key == '/type/work' and page.edition_count == 1:
    $ edit_url = page.editions[0].key + '?m=edit'
$elif page.type.key in ["/type/work", "/type/edition", "/type/author"]:
    $ edit_url = page.url(suffix="/edit")
$else:
    $ edit_url = page.key + "?m=edit"

$ booksense = "http://booksense.com/product/info.jsp?isbn=XXX"
$ amazon = "http://www.amazon.com/gp/product/XXX?tag=openlibr-20"
$ amazon_search = "http://www.amazon.com/s/?tag=openlibr-20&search-alias=aps&field-isbn=XXX"
$ abebooks = "http://gan.doubleclick.net/gan_click?lid=41000000024660875&pubid=21000000000318564&pvs1=XXX"
$ alibris = " http://click.linksynergy.com/fs-bin/click?id=eSmaWuUpnDY&offerid=99238.122856000&type=2&subid=0&tmpid=939&RD_PARM1=http%253A%252F%252Fwww.alibris.com/booksearch%253Fqsort%253Dp%2526isbn%253DXXX"
$ barnesandnoble = "http://search.barnesandnoble.com/booksearch/isbninquiry.asp?ISBN=XXXafsrc=1&lkid=J14933426&pubid=K172077"
$ bibliocom = "http://www.biblio.com/editions.php?isbn=XXX&aid=openlibrary"
$ powells = "http://www.powells.com/partner/33286/biblio/XXX"

$ worldcat = "http://worldcat.org/isbn/XXX"
$ worldcatoclc = "http://worldcat.org/oclc/XXX"
$ overdrive = "http://search.overdrive.com/SearchResults.aspx?ReserveID={XXX}"
$ bookmooch = "http://www.bookmooch.com/m/mooch_choice?asin=XXX"
$ titletrader = "http://www.titletrader.com/invinfo/XXX.html"

$ viewbook = "http://www.archive.org/stream/XXX"
$ detailbook = "http://www.archive.org/details/XXX"
$ downloadbook = "http://www.archive.org/download/XXX/XXX.pdf"
$ pdfbw = "http://www.archive.org/download/XXX/XXX_bw.pdf"
$ daisy = "http://www.archive.org/download/XXX/XXX_daisy.zip"
$ epub = "http://www.archive.org/download/XXX/XXX.epub"
$ mobi = "http://www.archive.org/download/XXX/XXX.mobi"
$ kindle = "https://www.amazon.com/gp/digital/fiona/web-to-kindle?clientid=IA&itemid=XXX&docid=XXX"

$ djvu = "http://www.archive.org/stream/XXX/XXX.djvu"
$ djvutxt = "http://www.archive.org/download/XXX/XXX_djvu.txt"
$ seeall = "http://www.archive.org/download/XXX"
$ google = ""

$ isbn = (page.isbn_10 and page.isbn_10[0]) or (page.isbn_13 and page.isbn_13[0]) or ""
$ isbn = isbn.replace("-", "")
$ oclc_numbers = (page.oclc_numbers and page.oclc_numbers[0]) or ""
$ isbn_13 = (page.isbn_13 and page.isbn_13[0]) or ""
$ isbn_10 = ""
$if page.isbn_10:
    $ isbn_10 = page.isbn_10[0]
$elif isbn_13:
    $ isbn_10 = isbn_13_to_isbn_10(isbn_13)

$ overdrive_id = None
$ asin = None
$ scribd = None
$if page.get('identifiers'):
    $if page.identifiers.get('scribd'):
        $ scribd = page.identifiers.scribd[0]
    $if page.identifiers.get('overdrive'):
        $ overdrive_id = page.identifiers.overdrive[0]
    $if page.identifiers.get('amazon'):
        $ asin = page.identifiers.amazon[0]

$if isbn_10 and not asin:
    $ asin = isbn_10

$ library = None

<div class="Tools">
$ collection = set()
$ meta_fields = page.get_ia_meta_fields()
$if meta_fields:
    $ collection = meta_fields.get('collection', [])
    $ contrib = meta_fields.get('contributor')
    $if 'inlibrary' in collection and 'inlibrary' in ctx.features:
        $ library = get_library()

$if page.volumes:
    <div>
    <h3 class="header">
        <span class="icon read"></span>
        <span class="head">Browse</span>
    </h3>
    $for v in page.volumes:
        $if v.ia_id != "-":
            <div class="panel">
                <p><a href="$viewbook.replace('XXX', v.ia_id)">View Volume $v.volume_number</a></p>
            </div>
    </div>
$elif 'printdisabled' in collection or 'lendinglibrary' in collection:
    <div>
        <h3 class="header">
            <span class="icon read"></span>
            <span class="head">Read</span>
        </h3>
        <div class="panel">
            <p><a href="$page.url('/daisy')" title="Encrypted DAISY" class="plain"><span class="underline">DAISY</span> <img src="/images/icons/icon-encrypto-sm.png" width="11" height="14" alt="Encrypted DAISY"/></a></p>
        </div>
    </div>
$elif page.get('ocaid'):
    <div>
        <h3 class="header">
            <span class="icon read"></span>
            <span class="head">Read</span>
        </h3>
        <div class="panel" id="readBooks">
            <p><a href="$viewbook.replace('XXX', page.ocaid)" title="Use BookReader to read online"><strong>Read online</strong></a><br/><br/></p>
            <p><a href="$downloadbook.replace('XXX', page.ocaid)" title="Download a PDF from Internet Archive">PDF</a></p>
            <p><a href="$djvutxt.replace('XXX', page.ocaid)" title="Download a text version from Internet Archive">Plain text</a></p>
            <p><a href="$page.url('/daisy')" title="Download a DAISY file from Internet Archive">DAISY</a></p>
            <p><a href="$epub.replace('XXX', page.ocaid)" title="Download an ePub from Internet Archive">ePub</a></p>
            <p><a href="$djvu.replace('XXX', page.ocaid)" title="Stream a DjVu from Internet Archive">DjVu</a> <span class="grey">streaming</span></p>
            <p><a href="$mobi.replace('XXX', page.ocaid)" title="Download a MOBI file from Internet Archive">MOBI</a> or <a href="$kindle.replace('XXX', page.ocaid)" title="Send to Kindle">send to Kindle</a></p>
        </div>
    </div>
$else:
    <div>
    <h3 class="closed">
        <span class="icon readinact"></span>
        <span class="head">Read</span>
    </h3>
    <div class="panel">
        <p class="smaller">No readable version available.</p>
    </div>
    </div>

$if isbn or oclc_numbers or overdrive_id or 'lendinglibrary' in collection or ('inlibrary' in collection and library is not None):
    $ current_and_available_loans = page.get_current_and_available_loans()
    $ user_loan = None
    $if ctx.user:
        $for current_loan in current_and_available_loans[0]:
            $if current_loan['user'] == ctx.user.key:
                $ user_loan = current_loan
                $ break
    <div>
    <h3 class="header">
        <span class="icon borrow"></span>
        <span class="head">Borrow</span>
    </h3>
    <div class="panel">
        $if  'lendinglibrary' in collection or user_loan or library:
            $:macros.LoanStatus(page, 'eBook', ctx.user, current_and_available_loans, contrib, '<br/>')
        $if overdrive_id:
            <p><a href="$overdrive.replace('XXX', overdrive_id)" title="Borrow from OverDrive">eBook, local library</a> <span class="smaller">OverDrive</span></p>
        $if isbn:
            <p><a href="$worldcat.replace('XXX', isbn)" title="Check WorldCat for an edition near you">Physical copy, local</a> <span class="smaller">WorldCat</span></p>
        $elif oclc_numbers:
            <p><a href="$worldcatoclc.replace('XXX', oclc_numbers)" title="Check WorldCat for an edition near you">Physical copy, local</a> <span class="smaller">WorldCat</span></p>
    </div>
    </div>
$else:
    <div>
    <h3 class="closed">
        <span class="icon borrowinact"></span>
        <span class="head">Borrow</span>
    </h3>
    <div class="panel">
        <p class="smaller">Try a <a href="http://www.worldcat.org/" title="Search WorldCat for an available edition">WorldCat</a> search?</p>
    </div>
    </div>

$if isbn or asin or scribd:
    <div>
        <h3 class="header">
            <span class="icon buy"></span>
            <span class="head">Buy</span>
        </h3>
        <div class="panel">
            $if isbn:
                <p><a href="$alibris.replace('XXX', isbn)" title="Check Alibris for availability">Alibris</a></p>
            $if asin:
                <p><a href="$amazon.replace('XXX', asin)" title="Check Amazon for availability">Amazon</a></p>
            $if isbn:
                <p><a href="$abebooks.replace('XXX', isbn)" title="Check AbeBooks for availability">AbeBooks</a></p>
                <p><a href="$bibliocom.replace('XXX', isbn)" title="Check Biblio.com for availability">Biblio.com</a></p>
                <p><a href="$powells.replace('XXX', isbn)" title="Check Powell's for availability">Powells</a></p>
            $if scribd:
                <p><a href="http://www.scribd.com/doc/$scribd" title="Available on Scribd!">Scribd</a> <span class="smaller grey">eBook in browser</span></p>
        </div>
    </div>
$else:
    <div>
    <h3 class="closed">
        <span class="icon buyinact"></span>
        <span class="head">Buy</span>
    </h3>
    <div class="panel">
        <p><a href="$edit_url">Add an ISBN</a> <span class="smaller">to link to booksellers</span></p>
    </div>
    </div>

</div>
<div class="clearfix"></div>
m: edit