#!/usr/bin/perl # undmylibrary - MyLibrary syndicated to Facebook, a first cut # Eric Lease Morgan # January 9, 2008 - first cut; whew! # require use CGI; use strict; use WWW::Facebook::API; use MyLibrary::Core; use MyLibrary::Patron; # define use constant INSTANCE => 'facebook'; use constant GENERAL => 10247; use constant LOCATIONTYPE => 1; use constant DDW_ROOT => 'http://www.library.nd.edu/subjects/'; use constant SUBJECT_ID => 2; # initialize MyLibrary::Config->instance( INSTANCE ); my $cgi = CGI->new; my $fbml = ''; my $facebook = WWW::Facebook::API->new( api_key => "foo", secret => "bar", app_path => "http://mylibrary.library.nd.edu/demos/facebook/" ); # get the input my $params = $facebook->canvas->get_non_fb_params( $cgi ); my $subject_id = $$params{ 'subject_id' }; # no input if ( ! $subject_id ) { # list the general resources my $term = MyLibrary::Term->new( id => GENERAL ); $fbml .= $cgi->p( 'These are the recommended resources for ' . $term->term_name . ':' ); $fbml .= &get_recommended( GENERAL ); } # show resources for selected subject else { my $term = MyLibrary::Term->new( id => $subject_id ); $fbml .= $cgi->p( "These are the recommended resources for " . $term->term_name . ':' ); $fbml .= &get_recommended( $subject_id ); } # get the user id and update their profile page my $uid = $facebook->canvas->get_user( $cgi ); print $cgi->header; $facebook->profile->set_fbml( markup => $fbml, uid => $uid ); # list other subjects in a pop-up menu $fbml .= $cgi->p( 'You can also select a different subject:' ); my $facet = MyLibrary::Facet->new( id => SUBJECT_ID ); $fbml .= $cgi->start_form( -method => 'POST', -action => './' ); my @values = (); my %labels = {}; foreach my $term_id ( $facet->related_terms( sort => 'name' )) { my $term = MyLibrary::Term->new( id => $term_id ); push @values, $term->term_id; $labels{ $term->term_id } = $term->term_name; } $fbml .= $cgi->popup_menu( -name => 'subject_id', -values => \@values, -labels => \%labels ); $fbml .= $cgi->submit( -name=>'submit', -value=> 'Go' ); $fbml .= $cgi->endform; # give the mark-up a header and return it in the canvas $fbml = $cgi->h2( 'MyLibrary@Notre Dame' ) . $fbml; print $cgi->div({ style => 'margin: 3%' }, $fbml ); exit; ############# # subroutines sub get_recommended { my $term = MyLibrary::Term->new( id => shift ); 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({ style => 'margin-bottom: .25em' }, $cgi->a({ -href => $url }, $resource->name )); } # add the link to all resources my $leaf = lc( $term->term_name ); $leaf =~ s/ /_/g; $list .= $cgi->li({ style => 'margin-bottom: .5em' }, $cgi->a({ href => DDW_ROOT . $leaf } ,'All resources for ' . $term->term_name )); # done return $cgi->ul( $list ); }