[Koha-patches] [PATCH] Extending IndependantBranches to only show items at users branch in catalogue export

Michael Hafen mdhafen at washk12.org
Tue May 27 20:24:32 CEST 2008


---
 C4/Record.pm        |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 catalogue/export.pl |   13 ++++++++++++-
 2 files changed, 62 insertions(+), 1 deletions(-)

diff --git a/C4/Record.pm b/C4/Record.pm
index 2ac9eca..65f786b 100644
--- a/C4/Record.pm
+++ b/C4/Record.pm
@@ -30,6 +30,8 @@ use Unicode::Normalize; # _entity_encode
 use XML::LibXSLT;
 use XML::LibXML;
 
+use C4::Biblio;
+
 use vars qw($VERSION @ISA @EXPORT);
 
 # set the version for version checking
@@ -49,6 +51,7 @@ $VERSION = 3.00;
 
   &html2marcxml
   &html2marc
+  &marcCleanOtherBranches
   &changeEncoding
 );
 
@@ -501,6 +504,53 @@ sub html2marc {
     return $record;
 }
 
+=head2 marcCleanOtherBranches - Remove from a marc record items at another locations
+
+=over 4
+
+my ( $newrecord ) = marcCleanOtherBranches( $record, $branch );
+
+Removes items from a marc record that are at other locations
+
+=over 2
+
+C<$record> - The record itself
+
+=back
+
+=back
+
+=cut
+
+sub marcCleanOtherBranches {
+    my ( $record ) = @_;
+    my $userenv = C4::Context->userenv;
+    if ( C4::Context->preference("IndependantBranches") && $userenv ) {
+	my $branch = $userenv->{branch};
+	my $marc_record_obj;
+	my $error;
+	unless ( $record =~ /^MARC::Record/ ) { # it's not a MARC::Record object, make it one
+		eval { $marc_record_obj = MARC::Record->new_from_usmarc($record) }; # handle exceptions
+
+		# conversion to MARC::Record object failed, populate $error
+		if ($@) {
+			$error .="\nCreation of MARC::Record object failed: ".$MARC::File::ERROR;
+		}
+	}
+
+	my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.homebranch",'');
+	unless ( $error ) {
+	    $record = $marc_record_obj;
+	    foreach my $item_field ($record->field($item_tag)) {
+		unless ( $item_field->subfield($item_subfield) eq $branch ) {
+		    $record->delete_field($item_field);
+		}
+	    }
+	}
+    }
+    return $record;
+}
+
 =head2 changeEncoding - Change the encoding of a record
 
 =over 4
diff --git a/catalogue/export.pl b/catalogue/export.pl
index 758afd3..840a699 100755
--- a/catalogue/export.pl
+++ b/catalogue/export.pl
@@ -10,6 +10,17 @@ use CGI;
 use C4::Auth;
 
 my $query = new CGI;
+
+my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
+    {
+        template_name   => "catalogue/detail.tmpl",
+        query           => $query,
+        type            => "intranet",
+        authnotrequired => 0,
+        flagsrequired   => { catalogue => 1 },
+    }
+);
+
 my $op=$query->param("op");
 my $format=$query->param("format");
 if ($op eq "export") {
@@ -22,7 +33,7 @@ if ($op eq "export") {
 	}
 	while (my ($marc) = $sth->fetchrow) {
 		if ($marc){
-
+			$marc = marcCleanOtherBranches( $marc );
 			if ($format =~ /endnote/) {
 				$marc = marc2endnote($marc);
 				$format = 'endnote';
-- 
1.5.4.3




More information about the Koha-patches mailing list