package EPrints::Plugin::Screen::BrowseContainers; use EPrints::Plugin::Screen; @ISA = ( 'EPrints::Plugin::Screen' ); use strict; sub new { my( $class, %params ) = @_; my $self = $class->SUPER::new(%params); $self->{appears} = [ { place => "key_tools", position => 121, } ]; return $self; } sub can_be_viewed { my( $self ) = @_; if ( $self->allow( "container/view" ) ) { # requested to limit the display to users who have a container # remove if performance is an issue! my $session = $self->{session}; my $user = $session->current_user; my $list = $session->get_repository->call( "get_allowed_containers", $user, $session ); return $list->count if defined $list; } return 0; } sub render { my( $self ) = @_; my $session = $self->{session}; my $user = $session->current_user; my $path = $session->get_repository->get_conf( "config_path" ); my $page = $session->make_doc_fragment; my $rel_path = $self->{session}->config( "rel_path" ); my $js = $self->{session}->make_element( "script", type=>"text/javascript", src=> "$rel_path/javascript/raphael-min.js" ); $page->appendChild($js); my $js_data = $session->make_javascript( $self->form_container_array ); $page->appendChild($js_data); my $js1 = $self->{session}->make_element( "script", type=>"text/javascript", src=> "$rel_path/javascript/z_browse_container.js" ); $page->appendChild($js1); my $js_div = $session->make_element( "div", id=>"canvas", style=>"height:780px; width:640px; border-style: solid; border-width: 2px; border-color: #F27F55;" ); $page->appendChild( $js_div ); # my $path_div = $session->make_element( "div", style=>"padding-bottom: 0.5em" ); # $path_div->appendChild( $session->html_phrase( "Plugin/Screen/BrowseContainers:root" )); # $page->appendChild( $path_div ); # $page->appendChild( $self->render_containers() ); return $page; } sub form_container_array { my( $self ) = @_; my $session = $self->{session}; my $ds = $session->get_repository->get_dataset( "container" ); my $count = 0; my $container_script; my $list = $ds->search; $list->map( sub { my( $session, $dataset, $container ) = @_; my $id = $container->get_value("containerid"); my $pid = $container->get_value("parentid"); my $name = $container->get_value("name"); my $items = $container->get_value("eprints"); my $tooltip = "Item count: ".scalar @$items; $pid = "root" if !defined $pid; $container_script .= "containers[$count] = new Array(3); "; $container_script .= "containers[$count][0] = '$id'; "; $container_script .= "containers[$count][1] = '$pid'; "; $container_script .= "containers[$count][2] = '$name'; "; $container_script .= "containers[$count][3] = '$tooltip'; "; $count++; } ); my $script = "var containers = new Array($count); "; $script .= $container_script; return $script; } sub render_containers { my( $self ) = @_; my $session = $self->{session}; my $ds = $session->get_repository->get_dataset( "container" ); my $containers = (); my $list = $ds->search; $list->map( sub { my( $session, $dataset, $container ) = @_; my $id = $container->get_value("containerid"); my $pid = $container->get_value("parentid"); my $name = $container->get_value("name"); $containers->{ $id }->{ "name" } = $name ; $containers->{ $id }->{ "parent" } = $pid; unless (defined $containers->{ $pid } ) { if (defined $pid) { $containers->{ $pid }->{ "name" } = "Not found" ; } } } ); my $parents = (); foreach my $id (keys %$containers) { unless (defined $containers->{ $id }->{ "parent" }) { $parents->{ $id }->{ "name" } = $containers->{ $id }->{ "name" } ; delete $containers->{$id}; } } #check we have at least one parent my $parentcount = scalar keys %$parents; my $childcount = scalar keys %$containers; if ( $parentcount < 1 ) { return $self->render_simple_list( $containers, "Plugin/Screen/BrowseContainers:no_parents" ); #return $self->{session}->html_phrase( "Plugin/Screen/BrowseContainers:no_parents" ); } my $frag = $self->{session}->make_element( "div"); my $div = $self->{session}->make_element( "div", style=>"margin-left: 3em; padding: 0.5em 0 0.5em 0; border-left: 1px solid blue" ); $frag->appendChild($div); foreach my $id ( sort keys %$parents ) { my $div_title = $self->{session}->make_element( "div", style=>"padding: 0.25em 0 0.25em 0;" ); $div->appendChild( $div_title ); $div_title->appendChild( $self->{session}->make_text( "-- " ) ); my $url = "?screen=Container::View&containerid=$id"; my $link = $self->{session}->render_link( $url ); $div_title->appendChild( $link ); $link->appendChild( $self->{session}->make_text( $parents->{$id}->{"name"} ) ); my $child_div = $self->render_child( $id, $containers ); $div->appendChild( $child_div) if defined $child_div; } if (scalar keys %$containers) { $frag->appendChild( $self->render_simple_list( $containers, "Plugin/Screen/BrowseContainers:orphaned_containers" ) ); } return $frag; } sub render_child { my( $self, $pid, $containers ) = @_; my $div; my $id = $self->get_next_child_id($pid, $containers); if ( $id > 0 ) { $div = $self->{session}->make_element( "div", style=>"margin-left: 3em; padding: 0.5em 0 0.5em 0; border-left: 1px solid blue" ); } while( $id > 0 ) { my $div_title = $self->{session}->make_element( "div", style=>"padding: 0.25em 0 0.25em 0;" ); $div->appendChild( $div_title ); $div_title->appendChild( $self->{session}->make_text( "-- " ) ); my $url = "?screen=Container::View&containerid=$id"; my $link = $self->{session}->render_link( $url ); $div_title->appendChild( $link ); $link->appendChild( $self->{session}->make_text( $containers->{$id}->{"name"} ) ); # remove this container now it has been processed #$containers->{$id}->{"seen"} = 1; delete $containers->{$id}; #render the children of this child my $child_div = $self->render_child($id, $containers); $div->appendChild($child_div) if defined $child_div; $id = $self->get_next_child_id($pid, $containers); } return $div; } sub get_next_child_id { my( $self, $pid, $containers ) = @_; foreach my $id (keys %$containers) { if ($pid == $containers->{$id}->{"parent"} && 1 != $containers->{$id}->{"seen"} ) { return $id; } } return -1; } sub render_simple_list { my ( $self, $containers, $reason ) = @_; my $div = $self->{session}->make_element( "div", style=>"margin-left: 3em; padding: 0.5em 0 0.5em 0; border-left: 1px solid blue" ); $div->appendChild( $self->{session}->html_phrase($reason) ); foreach my $id ( sort keys %$containers ) { my $div_title = $self->{session}->make_element( "div", style=>"padding: 0.25em 0 0.25em 0;" ); $div->appendChild( $div_title ); $div_title->appendChild( $self->{session}->make_text( "-- " ) ); my $url = "?screen=Container::View&containerid=$id"; my $link = $self->{session}->render_link( $url ); $div_title->appendChild( $link ); $link->appendChild( $self->{session}->make_text( $containers->{$id}->{"name"} ) ); } return $div; } 1;