# EPrints Services/Shelves # adds all the EPrintIds of a Search to a Shelf (given a Search cache_id) use EPrints; use strict; use warnings; our $GLOBAL_ERROR; #set by side-effect! my $session = EPrints::Session->new(); exit(0) unless( defined $session ); my $user = $session->current_user; unless( defined $user ) { $GLOBAL_ERROR = '
You must login to use the Shelves
'; terminate_with_error($session); return; } my $shelfid = $session->param( 'shelfid' ); my $cacheid = $session->param( 'cacheid' ); my $search_url = $session->param( 'searchurl'); my $shelf = get_shelf($session, $user, $shelfid); unless ( defined $shelf || defined $GLOBAL_ERROR ) { terminate_with_error($session); return; } my $ids; if (defined $cacheid) { $ids = list_ids_from_cache_id($session, $user, $cacheid); } elsif (defined $search_url) { $ids = list_ids_from_search_url($session, $search_url); } if ( (!defined $ids) || (defined $GLOBAL_ERROR) ) { terminate_with_error($session); return; } $shelf->add_items( @{$ids} ); $session->terminate; sub list_ids_from_cache_id { my ($session, $user, $cacheid) = @_; # sanity checks first my $cachemap = $session->dataset( 'cachemap' )->dataobj( $cacheid ); unless( defined $cachemap ) { # perhaps it timed-out $GLOBAL_ERROR = '
Search expired. Try starting another search.
'; return; } my $cache_owner_id = $cachemap->get_value( "userid" ); my $user_id = $user->get_id; unless( "$cache_owner_id" eq "$user_id" ) { $GLOBAL_ERROR = '
You do not own this Search.
'; return; } my $list = EPrints::List->new( session => $session, dataset => $session->dataset( 'eprint' ), cache_id => $cacheid ); return $list->ids; } sub list_ids_from_search_url { } sub get_shelf { my ($session, $user, $shelfid) = @_; return unless $shelfid; # instanciate shelf, check current user has the perms to write to it my $shelf = $session->dataset( 'shelf' )->dataobj( $shelfid ); unless( defined $shelf ) { $GLOBAL_ERROR = '
Shelf #'.$shelfid.' does not exist.
'; return; } unless( $shelf->has_editor( $user ) ) { $GLOBAL_ERROR = '
You may not modify this Shelf.
'; return; } return $shelf; } sub terminate_with_error { my ($session) = @_; if ($GLOBAL_ERROR) { $session->send_http_header( content_type => "application/json" ); print STDOUT JSON::encode_json( { error => $GLOBAL_ERROR } ); } $session->terminate; return; }