bib_profile web svc: report biblioitems.itemtype subfield as non-mandatory
[koha.git] / svc / bib_profile
1 #!/usr/bin/perl
2
3 # Copyright 2007 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19 #
20
21 use strict;
22 use CGI;
23 use C4::Auth qw/check_api_auth/;
24 use C4::Context;
25 use C4::Koha;
26 use XML::Simple;
27
28 my $query = new CGI;
29
30 my ($status, $cookie, $sessionID) = check_api_auth($query, { editcatalogue => 1} );
31
32 if ($status eq "ok") {
33     print $query->header(-type => 'text/xml', cookie => $cookie);
34 } else {
35     print $query->header(-type => 'text/xml', -status => '403 Forbidden');
36     print XMLout({ auth_status => $status }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
37     exit 0;
38 }
39
40 my $dbh = C4::Context->dbh;
41
42 # get list of required tags
43 my $result = {};
44 $result->{'auth_status'} = $status;
45 _get_mandatory_tags($result);
46 _get_mandatory_subfields($result);
47 _get_reserved_tags($result);
48 _get_bib_number_tag($result);
49 _get_biblioitem_itemtypes($result);
50 print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1,  
51         GroupTags => {mandatory_tags => 'tag', mandatory_subfields => 'subfield', reserved_tags => 'tag',
52                       valid_values => 'value'});
53
54 exit 0;
55
56 sub _get_mandatory_tags {
57     my $result = shift;
58     my $sth = $dbh->prepare_cached("SELECT tagfield FROM marc_tag_structure WHERE frameworkcode = '' AND mandatory = 1");
59     $sth->execute();
60     my @tags = ();
61     while (my $row = $sth->fetchrow_arrayref) {
62         push @tags, $row->[0];
63     }
64     $result->{'mandatory_tags'} = \@tags;
65 }
66
67 sub _get_mandatory_subfields {
68     my $result = shift;
69     my $sth = $dbh->prepare_cached("SELECT tagfield, tagsubfield 
70                                     FROM marc_subfield_structure 
71                                     WHERE frameworkcode = '' 
72                                     AND tagsubfield <> '\@' 
73                                     AND kohafield <> 'biblioitems.itemtype'
74                                     AND mandatory = 1");
75     $sth->execute();
76     my @subfields = ();
77     while (my $row = $sth->fetchrow_arrayref) {
78         push @subfields, { tag => $row->[0], subfield_label => $row->[1] };
79     }
80     $result->{'mandatory_subfields'} = \@subfields;
81 }
82
83 sub _get_reserved_tags {
84     my $result = shift;
85     my $sth = $dbh->prepare_cached("SELECT DISTINCT tagfield
86                                     FROM marc_subfield_structure 
87                                     WHERE frameworkcode = '' 
88                                     AND (kohafield = 'items.itemnumber' OR kohafield = 'biblioitems.itemtype' OR
89                                          kohafield = 'biblio.biblionumber')");
90     $sth->execute();
91     my @tags = ();
92     while (my $row = $sth->fetchrow_arrayref) {
93         push @tags, $row->[0];
94     }
95     $result->{'reserved_tags'} = \@tags;
96 }
97
98 sub _get_bib_number_tag {
99     my $result = shift;
100     my $sth = $dbh->prepare_cached("SELECT tagfield, tagsubfield
101                                     FROM marc_subfield_structure 
102                                     WHERE frameworkcode = '' 
103                                     AND kohafield = 'biblio.biblionumber'");
104     $sth->execute();
105     my @tags = ();
106     while (my $row = $sth->fetchrow_arrayref) {
107         push @tags, { tag => $row->[0], subfield => $row->[1] };
108     }
109     $result->{'bib_number'} = \@tags;
110 }
111
112 sub _get_biblioitem_itemtypes {
113     my $result = shift;
114     my $itemtypes = GetItemTypes;
115     my $sth = $dbh->prepare_cached("SELECT tagfield, tagsubfield
116                                     FROM marc_subfield_structure 
117                                     WHERE frameworkcode = '' 
118                                     AND kohafield = 'biblioitems.itemtype'");
119     $sth->execute();
120     my @tags = ();
121     while (my $row = $sth->fetchrow_arrayref) {
122         push @tags, { tag => $row->[0], subfield => $row->[1] };
123     }
124     my @valid_values = map { { code => $_,  description => $itemtypes->{$_}->{'description'} } } sort keys %$itemtypes;
125     $result->{'special_entry'} = { field => \@tags,  valid_values => \@valid_values };
126     
127 }