Turn off foreign key check for the installation
[koha.git] / admin / checkmarc.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use C4::Output;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use C4::Biblio;
27
28
29 my $input = new CGI;
30
31 my ($template, $borrowernumber, $cookie)
32     = get_template_and_user({template_name => "admin/checkmarc.tmpl",
33                              query => $input,
34                              type => "intranet",
35                              authnotrequired => 0,
36                              flagsrequired => {parameters => 1},
37                              debug => 1,
38                              });
39
40 my $dbh = C4::Context->dbh;
41 my $total;
42 # checks itemnum field
43 my $sth = $dbh->prepare("select tab from marc_subfield_structure where kohafield=\"items.itemnumber\"");
44 $sth->execute;
45 my ($res) = $sth->fetchrow;
46 if ($res==-1) {
47         $template->param(itemnum => 0);
48 } else {
49         $template->param(itemnum => 1);
50         $total++;
51 }
52
53 # checks biblio.biblionumber and biblioitem.biblioitemnumber (same tag and tab=-1)
54 $sth = $dbh->prepare("select tagfield,tab from marc_subfield_structure where kohafield=\"biblio.biblionumber\"");
55 $sth->execute;
56 my $tab;
57 ($res,$tab) = $sth->fetchrow;
58 $sth = $dbh->prepare("select tagfield,tab from marc_subfield_structure where kohafield=\"biblioitems.biblioitemnumber\"");
59 $sth->execute;
60 my ($res2,$tab2) = $sth->fetchrow;
61 if ($res && $res2 && $tab==-1 && $tab2==-1) {
62         $template->param(biblionumber => 0);
63 } else {
64         $template->param(biblionumber => 1);
65         $total++;
66 }
67
68 # checks all item fields are in the same tag and in tab 10
69
70 $sth = $dbh->prepare("select tagfield,tab,kohafield from marc_subfield_structure where kohafield like \"items.%\" and tab >=0");
71 $sth->execute;
72 my $field;
73 ($res,$res2,$field) = $sth->fetchrow;
74 my $tagfield = $res;
75 my $tab = $res2;
76 my $subtotal=0;
77 #warn "TAGF : $tagfield";
78 while (($res,$res2,$field) = $sth->fetchrow) {
79         # (ignore itemnumber, that must be in -1 tab)
80         if (($res ne $tagfield) or ($res2 ne $tab)) {
81                 $subtotal++;
82         }
83 }
84 $sth = $dbh->prepare("select kohafield from marc_subfield_structure where tagfield=?");
85 $sth->execute($tagfield);
86 while (($res2) = $sth->fetchrow) {
87         if (!$res2 || $res2 =~ /^items/) {
88         } else {
89                 $subtotal++;
90         }
91 }
92 if ($subtotal == 0) {
93         $template->param(itemfields => 0);
94 } else {
95         $template->param(itemfields => 1);
96         $total++;
97 }
98
99 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where tab = 10");
100 $sth->execute;
101 my $totaltags = 0;
102 my $list = "";
103 while (($res2) = $sth->fetchrow) {
104         $totaltags++;
105         $list.=$res2.",";
106 }
107 if ($totaltags > 1) {
108         $template->param(itemtags => $list);
109         $total++;
110 } else {
111         $template->param(itemtags => 0);
112 }
113
114
115 # checks biblioitems.itemtype must be mapped and use authorised_value=itemtype
116 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"biblioitems.itemtype\"");
117 $sth->execute;
118 ($res,$res2,$field) = $sth->fetchrow;
119 if ($res && $res2>=0 && $field eq "itemtypes") {
120         $template->param(itemtype => 0);
121 } else {
122         $template->param(itemtype => 1);
123         $total++;
124 }
125
126 # checks items.homebranch must be mapped and use authorised_value=branches
127 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.homebranch\"");
128 $sth->execute;
129 ($res,$res2,$field) = $sth->fetchrow;
130 if ($res && $res2 eq 10 && $field eq "branches") {
131         $template->param(branch => 0);
132 } else {
133         $template->param(branch => 1);
134         $total++;
135 }
136 # checks items.homebranch must be mapped and use authorised_value=branches
137 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.holdingbranch\"");
138 $sth->execute;
139 ($res,$res2,$field) = $sth->fetchrow;
140 if ($res && $res2 eq 10 && $field eq "branches") {
141         $template->param(holdingbranch => 0);
142 } else {
143         $template->param(holdingbranch => 1);
144         $total++;
145 }
146
147 # checks that itemtypes & branches tables are not empty
148 $sth = $dbh->prepare("select count(*) from itemtypes");
149 $sth->execute;
150 ($res) = $sth->fetchrow;
151 unless ($res) {
152         $template->param(itemtypes_empty =>1);
153         $total++;
154 }
155
156 $sth = $dbh->prepare("select count(*) from branches");
157 $sth->execute;
158 ($res) = $sth->fetchrow;
159 unless ($res) {
160         $template->param(branches_empty =>1);
161         $total++;
162 }
163
164 $sth = $dbh->prepare("select count(*) from marc_biblio where frameworkcode is NULL");
165 $sth->execute;
166 ($res) = $sth->fetchrow;
167 if ($res) {
168         $template->param(frameworknull =>1);
169         $total++;
170 }
171 $sth = $dbh->prepare("select count(*) from marc_subfield_structure where frameworkcode is NULL");
172 $sth->execute;
173 ($res) = $sth->fetchrow;
174 if ($res) {
175         $template->param(frameworknull =>1);
176         $total++;
177 }
178 $sth = $dbh->prepare("select count(*) from marc_tag_structure where frameworkcode is NULL");
179 $sth->execute;
180 ($res) = $sth->fetchrow;
181 if ($res) {
182         $template->param(frameworknull =>1);
183         $total++;
184 }
185
186 $template->param(total => $total,
187                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
188                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
189                 IntranetNav => C4::Context->preference("IntranetNav"),
190                 );
191 output_html_with_http_headers $input, $cookie, $template->output;