package EPrints::Plugin::Screen::Container::NewFromContainer; use EPrints::Plugin::Screen; @ISA = ( 'EPrints::Plugin::Screen' ); use strict; sub new { my( $class, %params ) = @_; my $self = $class->SUPER::new(%params); $self->{actions} = [qw/ create /]; $self->{icon} = "action_use_as_template.png"; $self->{appears} = [ { place => "container_item_actions", position => 220, }, { place => "container_create_item_actions", position => 220, }, ]; return $self; } sub can_be_viewed { my( $self ) = @_; return $self->allow( "container/use" ); } sub allow_create { my ( $self ) = @_; if ($self->allow( "container/use" )) { # check that the user is either associated with a container or # they own an eprint in a container my $session = $self->{session}; my $user = $session->current_user; my $list = $session->get_repository->call( "get_allowed_containers", $user, $session ); if (defined $list && $list->count > 0) { return $self->allow( "container/use" ); } } return 0; } sub action_create { my( $self ) = @_; my $ds = $self->{processor}->{session}->get_repository->get_dataset( "inbox" ); my $container_ds = $self->{processor}->{session}->get_repository->get_dataset( "container" ); my $user_ds = $self->{processor}->{session}->get_repository->get_dataset( "user" ); my $user = $self->{session}->current_user; my $containerid = $self->{processor}->{containerid}; my $container = $container_ds->dataobj( $containerid ); my $new_item = $ds->create_object( $self->{session}, { userid => $user->get_value( "userid" ) } ); if( !$new_item ) { my $db_error = $self->{session}->get_database->error; $self->{processor}->{session}->get_repository->log( "Database Error: $db_error" ); $self->{processor}->add_message( "error", $self->html_phrase( "db_error" ) ); return; } # add the values from the container my $users = $container->get_value("users"); my $creators = (); foreach my $userid (@$users) { my $container_user = $user_ds->dataobj( $userid ); if (defined $container_user) { push @$creators, {name=>$container_user->get_value("name"), id=>$container_user->get_value("email")} ; } } if ( ! defined $creators || 0 == scalar @$creators) { push @$creators, {name=>$user->get_value("name"), id=>$user->get_value("email")} ; } $new_item->set_value("creators", $creators); my $subjects = $container->get_value("subjects"); $new_item->set_value("subjects", $subjects); my $funders = $container->get_value("funders"); $new_item->set_value("funders", $funders); my $divisions = $container->get_value("divisions"); $new_item->set_value("divisions", $divisions); my $category = $container->get_value("category"); $new_item->set_value("category", $category); $new_item->commit; # add the item to the container my $container_items = EPrints::Utils::clone( $container->get_value( "eprints" ) ); push @$container_items, $new_item->get_id; $container->set_value( "eprints", $container_items ); $container->commit; $self->{processor}->{eprint} = $new_item; $self->{processor}->{eprintid} = $new_item->get_id; $self->{processor}->{screenid} = "EPrint::Edit"; } sub render { my( $self ) = @_; my $session = $self->{session}; my $url = URI->new($self->{processor}->{url}); $url->query_form( screen => $self->{processor}->{screenid}, containerid => $self->{processor}->{containerid}, _action_create => 1 ); $session->redirect( $url ); $session->terminate(); exit(0); } sub properties_from { my( $self ) = @_; $self->{processor}->{containerid} = $self->{session}->param( "containerid" ); my $container_ds = $self->{session}->get_repository->get_dataset( "container" ); my $container = $container_ds->dataobj($self->{processor}->{containerid}); if( !defined $container ) { $self->{processor}->{screenid} = "Error"; $self->{processor}->add_message( "error", $self->{session}->html_phrase( "cgi/users/edit_eprint:cant_find_it", id=>$self->{session}->make_text( $self->{processor}->{containerid} ) ) ); return; } $self->SUPER::properties_from; } 1;