#!/usr/bin/perl # mylibrary.cgi - recommend a set of resources based on a user's department # Eric Lease Morgan # Februry 2, 2007 - added ability to enter your bar code; can see book details # January 30, 2007 - changed encoding to utf-8; changed to my barcode # January 22, 2007 - added ability to see checked out books # January 15, 2007 - first cut # define use constant TEMPLATE => './etc/template.txt'; use constant HOME => './etc/home.txt'; use constant LOCATIONTYPE => 1; use constant BARCODE => '21006171820105'; use constant BORINFO => 'http://alephprod.library.nd.edu:8991/X?op=bor-info&library=ndu50&bor_id='; use constant FINDDOC => 'http://alephprod.library.nd.edu:8991/X?op=find-doc&base=ndu01&doc_num='; use constant BORINFO2HTML => './etc/books-checked-out.xsl'; use constant FINDDOC2HTML => './etc/finddoc2html.xsl'; use constant CLASSES => '17525'; # require use CGI; use CGI::Carp qw(fatalsToBrowser); use LWP::UserAgent; use MyLibrary::Core; use MyLibrary::Patron; use strict; use XML::LibXML; use XML::LibXSLT; use constant INSTANCE => 'soyp'; my $cgi = CGI->new; my $cmd = $cgi->param( 'cmd' ); MyLibrary::Config->instance( INSTANCE ); my $response = ''; if ( ! $cmd ) { # display the home page and quit my $html = &slurp( TEMPLATE ); $html =~ s/##CONTENT##/&slurp( HOME )/e; &gracefulExit ( $html ); } elsif ( $cmd eq 'sign-in' ) { # get the netid and check my $netid = $cgi->param( 'netid' ); if ( ! $netid ) { &error( 'You need to supply a University NetID in order for this command to work.' )}; # map university departments to subject term ids my %map = ( 'Biological Sciences' => 6068, 'Political Science' => 10851, 'Psychology' => 4614, 'University Libraries' => 10247, 'Philosophy and Theology' => 7476, 'History' => 1340, 'Architecture' => 8757, 'Mechanical Engineering' => 17686, 'Physics' => 3726, 'Mathematics' => 4044 ); # sample netids: # CSTECYK = biology # SSEAGRAV = political science # HMCGOVE1 = psychology # MPEDERS2 = philosophy and theology # AEDELBR1 = architecture # CBYRNE2 = mechanical engineering # LRUBINKO = physics # JPOELHUI = mathematics # get this patron and check my $patron = MyLibrary::Patron->new( username => $netid ); if (! $patron ) { &error( "The NetID ($netid) was not found. Call Eric." )} # initialize the response $response = $cgi->h1( 'Welcome, ' . $patron->patron_firstname ); # department and check my $department = $patron->patron_organization; if ( ! $map{ $department } ) { &error( "Your department ($department) has not yet be associated with a subject term. Call Eric." )} # recommended resources my $term = MyLibrary::Term->new( id => $map{ $department } ); $response .= $cgi->p( "Because you are associated with $department, you might be interested in these resources:" ); my $list = ''; foreach my $resource_id ( $term->suggested_resources( sort => 'name' )) { # add resource and link to a list my $resource = MyLibrary::Resource->new( id => $resource_id ); my @locations = $resource->resource_locations; my $url = ''; foreach my $location ( @locations ) { if ( $location->resource_location_type eq LOCATIONTYPE ) { $url = $location->location } } $list .= $cgi->li( $cgi->a({ -href => $url }, $resource->name )); } $response .= $cgi->ul( $list ); # librarian $response .= $cgi->p( 'If you have any questions, then feel free to call or email:' ); my @librarians = $term->librarians; $list = ''; foreach my $librarian ( @librarians ) { my $name = $librarian->name; $name =~ s/(.*, )(.*)/\2 \1/; $name =~ s/, $//; $list .= $cgi->li( $name . ' (' . $librarian->telephone . '; ' . $cgi->a({ -href => 'mailto:' . $librarian->email }, $librarian->email ) . ')' ); } $response .= $cgi->ul( $list ); # get their class recommendations my @term_ids = $patron->patron_terms; my $classes; foreach my $term_id ( @term_ids ) { my $term = MyLibrary::Term->new( id => $term_id ); if ( $term->facet_id == CLASSES ) { my @resource_ids = $term->related_resources; my $resources; foreach my $resource_id ( @resource_ids ) { my $resource = MyLibrary::Resource->new( id => $resource_id ); my $title = $resource->name; my $url; my @locations = $resource->resource_locations; foreach my $location ( @locations ) { if ( $location->resource_location_type eq LOCATIONTYPE ) { $url = $location->location } } $resources .= $cgi->li( $cgi->a({ href => $url }, $title )); } $resources = $cgi->ul( $resources ); $classes .= $cgi->li( $term->term_name . $resources ); } } $classes = $cgi->ul( $classes ); $response .= $cgi->h2( 'Classes' ); $response .= $cgi->p( 'Because you are enrolled in the following classes, you may want to use their associated resources.' ); $response .= $classes; # get what they have checked out my $barcode = ''; if ( $netid eq 'frankwright' || $netid eq 'leodavinci' || $netid eq 'cdarwin' || $netid eq 'kgodel' || $netid eq 'tacquinas' || $netid eq 'inewton' || $netid eq 'tjefferson' ) { $barcode = BARCODE } my $checked_out = ''; if ( $barcode ) { $response .= &checked_out( $barcode )} else { $response .= $cgi->p( 'Enter your barcode (the 15-digit number on the back of your University ID card), and I will try to figure out what you have currently checked out of the library.' ); $response .= $cgi->start_form( -method => 'get', -action => './' ); $response .= $cgi->hidden( -name => 'cmd', -value => 'checked_out', -override => 1 ); $response .= 'bar code: '; $response .= $cgi->textfield( -name => 'barcode' ); $response .= $cgi->submit( -value=>'Go' ); $response .= $cgi->endform; } # salutations $response .= $cgi->p( 'Have a nice day.' ); # create the html, fill it with the response, and quit my $html = &slurp( TEMPLATE ); $html =~ s/##CONTENT##/$response/e; &gracefulExit ( $html ); } elsif ( $cmd eq 'checked_out' ) { # get the barcode my $barcode = $cgi->param( 'barcode' ); # create the html, fill it with the response, and quit my $html = &slurp( TEMPLATE ); $html =~ s/##CONTENT##/&checked_out( $barcode )/e; &gracefulExit ( $html ); } elsif ( $cmd eq 'details' ) { # get the id of the book my $id = $cgi->param( 'id' ); # create the html, fill it with the response, and quit my $html = &slurp( TEMPLATE ); $html =~ s/##CONTENT##/&get_details( $id )/e; &gracefulExit ( $html ); } ############# # subroutines sub slurp { # open a file named by the input and return its contents my $f = @_[0]; my $r; open (F, "< $f"); while () { $r .= $_ } close F; return $r; } sub error { # get the error message my $msg = shift; $msg = $cgi->p( $msg ); # create the html my $html = &slurp( TEMPLATE ); $html =~ s/##CONTENT##/$msg/e; # done &gracefulExit ( $html ); } sub gracefulExit { # get the html, send a header, send the html, and quit my $html = shift; print $cgi->header(-type=>'text/html', -charset => 'UTF-8'); print $html; exit; } sub checked_out { my $barcode = shift; my $result = ''; # create the x-server request my $url = BORINFO . $barcode; # create a user agent, create a request, send it, and get a response my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new( GET => $url ); my $response = $ua->request( $request ); # transform the response my $parser = XML::LibXML->new; my $xslt = XML::LibXSLT->new; my $source = $parser->parse_string( $response->content ) or croak $!; my $style = $parser->parse_file( BORINFO2HTML ) or croak $!; my $stylesheet = $xslt->parse_stylesheet($style) or croak $!; my $results = $stylesheet->transform($source) or croak $!; # build response $result .= $cgi->h2( 'Checked out' ); $result .= $cgi->p( 'According to our records, you have the following books checked-out:' ); $result .= $stylesheet->output_string( $results ); return $result; } sub get_details { my $id = shift; my $result = ''; # create the x-server request my $url = FINDDOC . $id; # create a user agent, create a request, send it, and get a response my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new( GET => $url ); my $response = $ua->request( $request ); # transform the response my $parser = XML::LibXML->new; my $xslt = XML::LibXSLT->new; my $source = $parser->parse_string( $response->content ) or croak $!; my $style = $parser->parse_file( FINDDOC2HTML ) or croak $!; my $stylesheet = $xslt->parse_stylesheet($style) or croak $!; my $results = $stylesheet->transform($source) or croak $!; # build response $result .= $cgi->h2( 'Details' ); $result .= $cgi->p( 'This is what I know about this book:' ); $result .= $stylesheet->output_string( $results ); return $result; }