biblios integration: added bib_profile API
authorGalen Charlton <galen.charlton@liblime.com>
Tue, 20 Nov 2007 23:08:57 +0000 (17:08 -0600)
committerJoshua Ferraro <jmf@liblime.com>
Wed, 21 Nov 2007 02:37:46 +0000 (20:37 -0600)
Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
biblios/bib_profile [new file with mode: 0755]

diff --git a/biblios/bib_profile b/biblios/bib_profile
new file mode 100755 (executable)
index 0000000..41ea042
--- /dev/null
@@ -0,0 +1,126 @@
+#!/usr/bin/perl
+
+# Copyright 2007 LibLime
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
+#
+
+use strict;
+use CGI;
+use C4::Auth qw/check_api_auth/;
+use C4::Context;
+use C4::Koha;
+use XML::Simple;
+
+my $query = new CGI;
+
+my ($status, $cookie, $sessionID) = check_api_auth($query, { editcatalogue => 1} );
+
+if ($status eq "ok") {
+    print $query->header(-type => 'text/xml', cookie => $cookie);
+} else {
+    print $query->header(-type => 'text/xml', -status => '403 Forbidden');
+    print XMLout({ auth_status => $status }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
+    exit 0;
+}
+
+my $dbh = C4::Context->dbh;
+
+# get list of required tags
+my $result = {};
+$result->{'auth_status'} = $status;
+_get_mandatory_tags($result);
+_get_mandatory_subfields($result);
+_get_reserved_tags($result);
+_get_bib_number_tag($result);
+_get_biblioitem_itemtypes($result);
+print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1,  
+        GroupTags => {mandatory_tags => 'tag', mandatory_subfields => 'subfield', reserved_tags => 'tag',
+                      valid_values => 'value'});
+
+exit 0;
+
+sub _get_mandatory_tags {
+    my $result = shift;
+    my $sth = $dbh->prepare_cached("SELECT tagfield FROM marc_tag_structure WHERE frameworkcode = '' AND mandatory = 1");
+    $sth->execute();
+    my @tags = ();
+    while (my $row = $sth->fetchrow_arrayref) {
+        push @tags, $row->[0];
+    }
+    $result->{'mandatory_tags'} = \@tags;
+}
+
+sub _get_mandatory_subfields {
+    my $result = shift;
+    my $sth = $dbh->prepare_cached("SELECT tagfield, tagsubfield 
+                                    FROM marc_subfield_structure 
+                                    WHERE frameworkcode = '' 
+                                    AND tagsubfield <> '\@' 
+                                    AND mandatory = 1");
+    $sth->execute();
+    my @subfields = ();
+    while (my $row = $sth->fetchrow_arrayref) {
+        push @subfields, { tag => $row->[0], subfield_label => $row->[1] };
+    }
+    $result->{'mandatory_subfields'} = \@subfields;
+}
+
+sub _get_reserved_tags {
+    my $result = shift;
+    my $sth = $dbh->prepare_cached("SELECT DISTINCT tagfield
+                                    FROM marc_subfield_structure 
+                                    WHERE frameworkcode = '' 
+                                    AND (kohafield = 'items.itemnumber' OR kohafield = 'biblioitems.itemtype' OR
+                                         kohafield = 'biblio.biblionumber')");
+    $sth->execute();
+    my @tags = ();
+    while (my $row = $sth->fetchrow_arrayref) {
+        push @tags, $row->[0];
+    }
+    $result->{'reserved_tags'} = \@tags;
+}
+
+sub _get_bib_number_tag {
+    my $result = shift;
+    my $sth = $dbh->prepare_cached("SELECT tagfield, tagsubfield
+                                    FROM marc_subfield_structure 
+                                    WHERE frameworkcode = '' 
+                                    AND kohafield = 'biblio.biblionumber'");
+    $sth->execute();
+    my @tags = ();
+    while (my $row = $sth->fetchrow_arrayref) {
+        push @tags, { tag => $row->[0], subfield => $row->[1] };
+    }
+    $result->{'bib_number'} = \@tags;
+}
+
+sub _get_biblioitem_itemtypes {
+    my $result = shift;
+    my $itemtypes = GetItemTypes;
+    my $sth = $dbh->prepare_cached("SELECT tagfield, tagsubfield
+                                    FROM marc_subfield_structure 
+                                    WHERE frameworkcode = '' 
+                                    AND kohafield = 'biblioitems.itemtype'");
+    $sth->execute();
+    my @tags = ();
+    while (my $row = $sth->fetchrow_arrayref) {
+        push @tags, { tag => $row->[0], subfield => $row->[1] };
+    }
+    my @valid_values = map { { code => $_,  description => $itemtypes->{$_}->{'description'} } } sort keys %$itemtypes;
+    $result->{'special_entry'} = { field => \@tags,  valid_values => \@valid_values };
+    
+}