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