From df9e08d1761cd6f0b5766acbee1cb15327f1c65f Mon Sep 17 00:00:00 2001 From: Henri-Damien LAURENT Date: Tue, 25 Aug 2009 23:56:13 +0200 Subject: [PATCH] Adding Merge of two biblios from a virtualshelf Signed-off-by: Galen Charlton --- .../virtualshelves/merge/selection.tmpl | 18 +++ .../prog/en/modules/virtualshelves/merge.tmpl | 15 ++ .../en/modules/virtualshelves/shelves.tmpl | 10 +- virtualshelves/addbybiblionumber.pl | 47 +++++- virtualshelves/merge.pl | 137 ++++++++++++++++++ 5 files changed, 220 insertions(+), 7 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/virtualshelves/merge/selection.tmpl create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/merge.tmpl create mode 100755 virtualshelves/merge.pl diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/virtualshelves/merge/selection.tmpl b/koha-tmpl/intranet-tmpl/prog/en/includes/virtualshelves/merge/selection.tmpl new file mode 100644 index 0000000000..10d8b29dd8 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/virtualshelves/merge/selection.tmpl @@ -0,0 +1,18 @@ +
+ + + + + + + +
Merge
"> +
+ +
+
+ +
+
+ +
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/merge.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/merge.tmpl new file mode 100644 index 0000000000..a189a57a10 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/merge.tmpl @@ -0,0 +1,15 @@ + +Koha › <!-- TMPL_VAR NAME="shelf" --> merge + + + +

+ + + +

DOIT

+

ITEMS

+ + + + diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tmpl index 8b0a7d3055..5c9687cf45 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tmpl @@ -120,7 +120,11 @@ function placeHold () {
-
+ + " /> + +
+
" />
@@ -324,6 +328,10 @@ function placeHold () { + + " /> + +
" /> diff --git a/virtualshelves/addbybiblionumber.pl b/virtualshelves/addbybiblionumber.pl index d565493d62..ee199ece42 100755 --- a/virtualshelves/addbybiblionumber.pl +++ b/virtualshelves/addbybiblionumber.pl @@ -66,6 +66,18 @@ use C4::Auth; use CGI::Carp qw/fatalsToBrowser/; use warnings; +sub AddBibliosToShelf { + my ($shelfnumber,@biblionumber)=@_; + + # multiple bibs might come in as '/' delimited string (from where, i don't see), or as array. + if (scalar(@biblionumber) == 1) { + @biblionumber = (split /\//,$biblionumber[0]); + } + for my $bib (@biblionumber){ + AddToShelfFromBiblio($bib, $shelfnumber); + } +} + my $query = new CGI; # If set, then single item case. @@ -99,12 +111,35 @@ if ($biblionumbers) { $shelfnumber = AddShelf( $newvirtualshelf, $loggedinuser, $category, $sortfield ) if $newvirtualshelf; if ( $shelfnumber || ( $shelfnumber == -1 ) ) { # the shelf already exist. - foreach my $biblionumber (@biblionumbers) { - AddToShelfFromBiblio( $biblionumber, $shelfnumber ); + if ($confirmed == 1) { + AddBibliosToShelf($shelfnumber,@biblionumber); + print + "Content-Type: text/html\n\n"; + exit; + } else { + my ( $singleshelf, $singleshelfname, $singlecategory ) = GetShelf( $query->param('shelfnumber') ); + my @biblios; + for my $bib (@biblionumber) { + my $data = GetBiblioData( $bib ); + push(@biblios, + { biblionumber => $bib, + title => $data->{'title'}, + author => $data->{'author'}, + } ); + } + + $template->param + ( + biblionumber => \@biblionumber, + biblios => \@biblios, + multiple => (scalar(@biblionumber) > 1), + singleshelf => 1, + shelfname => $singleshelfname, + shelfnumber => $singleshelf, + total => scalar(@biblionumber), + confirm => 1, + ); } - print -"Content-Type: text/html\n\n"; - exit; } else { # this shelf doesn't already exist. my $limit = 10; @@ -163,5 +198,5 @@ else { # this shelf doesn't already exist. ); } - output_html_with_http_headers $query, $cookie, $template->output; } +output_html_with_http_headers $query, $cookie, $template->output; diff --git a/virtualshelves/merge.pl b/virtualshelves/merge.pl new file mode 100755 index 0000000000..bff5aa70a7 --- /dev/null +++ b/virtualshelves/merge.pl @@ -0,0 +1,137 @@ +#! /usr/bin/perl +# vim: enc=utf-8 fdm=marker fdn=1 sw=4 +use utf8; +use strict; +use warnings; +use Devel::SimpleTrace; +use Storable qw< freeze thaw >; +use C4::Auth; +use C4::Context; +use C4::Biblio; +use C4::VirtualShelves; +use YAML; +use CGI; + +sub debug { print STDERR @_,"\n" } + +# Global values {{{ +my $query = CGI->new; +my $sessionID = (checkauth($query))[2]; +my $session = C4::Auth::get_session( $sessionID ); + +my ( $template, $user, $cookie ) = get_template_and_user { + qw( + template_name virtualshelves/merge.tmpl + type intranet + authnotrequired 0 + ) + , query => $query + , flagsrequired => {} +}; +# }}} +# Functions {{{ + +sub render { + use C4::Output; + $template->param( session => $sessionID ); + output_html_with_http_headers $query, $cookie, $template->output; +} + +sub maybe_lost { + $template->param( maybe_lost => 1 ); + render; exit; +} + +sub fields_to_merge { $session->param('fields_to_merge') } + +# global values ... but only for nextid + +my $selection_id = 0; +sub nextid { $selection_id++ } + +sub prepare_subfield { { key => $$_[0], value => $$_[1] } } + +sub prepare_field { + my $ready = { + field => freeze($_) + , from => shift + , tag => $_->tag + , id => nextid + }; + + if ( $_->is_control_field ) { + $$ready{control} = 1; + } else { + $$ready{subfields} = [ map prepare_subfield, $_->subfields ] + }; + + $ready; +} + +sub fields_by_tag { $$a{tag} cmp $$b{tag} } + +sub build_records { + my $newbiblio = MARC::Record->new; + my $items = MARC::Record->new; + my ( $selected_fields ) = @_; + my $stored_fields = $session->param('fields'); + my $kept_biblio = $stored_fields->[0]->{id}; + my %biblio_to_delete; + + for ( @$stored_fields ) { + my $from = $$_{from}; + $biblio_to_delete{ $from } = 1; + + if ( $$_{tag} eq '995' ) { + if ( $from != $kept_biblio ) { + $items->append_fields( thaw $$_{field} ) + } + } else { + if ( exists $$selected_fields{ $$_{id} } ) { + $newbiblio->append_fields( thaw $$_{field} ) + } + } + } + delete $biblio_to_delete{ $kept_biblio }; + ( $newbiblio, $kept_biblio, $items, [ values %biblio_to_delete ] ); +} + +sub clear_session { (shift)->clear([qw< fields shelf >]) } + +# }}} +# the controller {{{ + +if ( my %field_selection = map { $_ => 1 } $query->param('selected_field') ) { + my ($record, $number, $items, $delete ) = build_records( \%field_selection ); + ModBiblio( $record, $number, GetFrameworkCode( $number )); + AddItems( $items, $number ); + for ( @$delete ) { + if ( my $error = DelBiblio($_) ) { + die $error + } + } +} else { + my $shelf = $query->param('shelf') or maybe_lost; + if ( my @records = C4::VirtualShelves::each_biblionumbers { + { id => $_, record => GetMarcBiblio($_) } + } $shelf + ) { + my @fields = sort {fields_by_tag} map { + my ( $id , $record ) = @$_{qw< id record >}; + map { prepare_field($id) } $record->fields; + } @records; + my @store = ( fields => \@fields ); + + for ($session) { + clear_session($_); + $_->param(@store, shelf => $shelf ); + } + $template->param(@store); + } else { + die "GET LOST ?"; + } +} + +render; + +# }}} -- 2.39.5