Bug 29407: Make the pickup locations dropdown JS reusable
[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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use C4::Output qw( output_html_with_http_headers );
23 use C4::Auth qw( get_template_and_user );
24 use CGI qw ( -utf8 );
25 use C4::Context;
26
27
28 my $input = CGI->new;
29
30 my ($template, $borrowernumber, $cookie)
31     = get_template_and_user({template_name => "admin/checkmarc.tt",
32                              query => $input,
33                              type => "intranet",
34                  flagsrequired => { parameters => 'manage_marc_frameworks' },
35                              });
36
37 my $dbh = C4::Context->dbh;
38 my $total = 0;
39 # checks itemnum field
40 my $sth = $dbh->prepare("select tab from marc_subfield_structure where kohafield=\"items.itemnumber\"");
41 $sth->execute;
42 while (my ($res) = $sth->fetchrow) {
43     if ($res==-1) {
44             $template->param(itemnum => 0);
45     } else {
46             $template->param(itemnum => 1);
47             $total++;
48         last;
49     }
50 }
51
52 # checks biblio.biblionumber and biblioitem.biblioitemnumber (same tag and tab=-1)
53 $sth = $dbh->prepare("select tagfield,tab,frameworkcode from marc_subfield_structure where kohafield=\"biblio.biblionumber\"");
54 $sth->execute;
55 my $first = 1;
56 my $bibliotag = '';
57 while (my ($res,$tab,$frameworkcode) = $sth->fetchrow) {
58     if ($first) {
59         $bibliotag = $res;
60         $first = 0;
61     } else {
62         if ($bibliotag != $res) {
63                 $template->param(biblionumber => 1);
64                 $total++;
65             last;
66         }
67     }
68     my $sth2 = $dbh->prepare("SELECT tagfield,tab 
69                               FROM marc_subfield_structure 
70                               WHERE kohafield=\"biblioitems.biblioitemnumber\"
71                               AND frameworkcode = ? ");
72     $sth2->execute($frameworkcode);
73     my ($res2,$tab2) = $sth2->fetchrow;
74     if ($res && $res2 && $tab==-1 && $tab2==-1) {
75             $template->param(biblionumber => 0);
76     } else {
77             $template->param(biblionumber => 1);
78             $total++;
79         last;
80     }
81 }
82
83 # checks all item fields are in the same tag and in tab 10
84
85 $sth = $dbh->prepare("select tagfield,tab,kohafield from marc_subfield_structure where kohafield like \"items.%\" and tab >=0");
86 $sth->execute;
87 my $field;
88 my $res;
89 my $res2;
90 my $tab;
91 ($res,$res2,$field) = $sth->fetchrow;
92 my $tagfield = $res;
93 $tab = $res2;
94 my $subtotal=0;
95 #warn "TAGF : $tagfield";
96 while (($res,$res2,$field) = $sth->fetchrow) {
97         # (ignore itemnumber, that must be in -1 tab)
98         if (($res ne $tagfield) or ($res2 ne $tab)) {
99                 $subtotal++;
100         }
101 }
102 $sth = $dbh->prepare("select kohafield from marc_subfield_structure where tagfield=?");
103 $sth->execute($tagfield);
104 while (($res2) = $sth->fetchrow) {
105         if (!$res2 || $res2 =~ /^items/) {
106         } else {
107                 $subtotal++;
108         }
109 }
110 if ($subtotal == 0) {
111         $template->param(itemfields => 0);
112 } else {
113         $template->param(itemfields => 1);
114         $total++;
115 }
116
117 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where tab = 10");
118 $sth->execute;
119 my $totaltags = 0;
120 my $list = "";
121 while (($res2) = $sth->fetchrow) {
122         $totaltags++;
123         $list.=$res2.",";
124 }
125 if ($totaltags > 1) {
126         $template->param(itemtags => $list);
127         $total++;
128 } else {
129         $template->param(itemtags => 0);
130 }
131
132
133 # checks biblioitems.itemtype must be mapped and use authorised_value=itemtype
134 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"biblioitems.itemtype\"");
135 $sth->execute;
136 while (($res,$res2,$field) = $sth->fetchrow) {
137     if ($res && $res2>=0 && $field eq "itemtypes") {
138             $template->param(itemtype => 0);
139     } else {
140             $template->param(itemtype => 1);
141             $total++;
142         last;
143     }
144 }
145
146 # checks items.homebranch must be mapped and use authorised_value=branches
147 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.homebranch\"");
148 $sth->execute;
149 while (($res,$res2,$field) = $sth->fetchrow) {
150     if ($res && $res2 eq 10 && $field eq "branches") {
151             $template->param(branch => 0);
152     } else {
153             $template->param(branch => 1);
154             $total++;
155         last;
156     }
157 }
158
159 # checks items.homebranch must be mapped and use authorised_value=branches
160 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.holdingbranch\"");
161 $sth->execute;
162 while (($res,$res2,$field) = $sth->fetchrow) {
163     if ($res && $res2 eq 10 && $field eq "branches") {
164             $template->param(holdingbranch => 0);
165     } else {
166             $template->param(holdingbranch => 1);
167             $total++;
168             last; #MR
169     }
170 }
171
172 # checks that itemtypes & branches tables are not empty
173 $sth = $dbh->prepare("select count(*) from itemtypes");
174 $sth->execute;
175 ($res) = $sth->fetchrow;
176 if ($res) {
177         $template->param(itemtypes_empty =>0);
178 } else {
179         $template->param(itemtypes_empty =>1);
180         $total++;
181 }
182
183
184 $sth = $dbh->prepare("select count(*) from branches");
185 $sth->execute;
186 ($res) = $sth->fetchrow;
187 if ($res) {
188         $template->param(branches_empty =>0);
189 } else {
190         $template->param(branches_empty =>1);
191         $total++;
192 }
193
194 $sth = $dbh->prepare("select count(*) from marc_subfield_structure where frameworkcode is NULL");
195 $sth->execute;
196 ($res) = $sth->fetchrow;
197 if ($res) {
198         $template->param(frameworknull =>1);
199         $total++;
200 }
201 $sth = $dbh->prepare("select count(*) from marc_tag_structure where frameworkcode is NULL");
202 $sth->execute;
203 ($res) = $sth->fetchrow;
204 if ($res) {
205         $template->param(frameworknull =>1);
206         $total++;
207 }
208
209 # verify that all of a field's subfields (except the ones explicitly ignored)
210 # are in the same tab
211 $sth = $dbh->prepare("SELECT tagfield, frameworkcode, frameworktext, GROUP_CONCAT(DISTINCT tab) AS tabs
212                       FROM marc_subfield_structure
213                       LEFT JOIN biblio_framework USING (frameworkcode)
214                       WHERE tab != -1
215                       GROUP BY tagfield, frameworkcode, frameworktext
216                       HAVING COUNT(DISTINCT tab) > 1");
217 $sth->execute;
218 my $inconsistent_tabs = $sth->fetchall_arrayref({});
219 if (scalar(@$inconsistent_tabs) > 0) {
220     $total++;
221     $template->param(inconsistent_tabs => 1);
222     $template->param(tab_info => $inconsistent_tabs);
223 }
224
225 # verify that authtypecodes used in the framework 
226 # are defined in auth_types
227 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield, authtypecode
228                       FROM marc_subfield_structure
229                       LEFT JOIN biblio_framework USING (frameworkcode)
230                       WHERE authtypecode IS NOT NULL
231                       AND authtypecode <> ''
232                       AND tab > '-1'
233                       AND authtypecode NOT IN (SELECT authtypecode FROM auth_types)
234                       ORDER BY frameworkcode, tagfield, tagsubfield");
235 $sth->execute;
236 my $invalid_authtypecodes = $sth->fetchall_arrayref({});
237 if (scalar(@$invalid_authtypecodes) > 0) {
238     $total++;
239     $template->param(invalid_authtypecodes => 1);
240     $template->param(authtypecode_info => $invalid_authtypecodes);
241 }
242
243 # checks items.permanent_location is not mapped
244 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield
245                       FROM marc_subfield_structure
246                       LEFT JOIN biblio_framework USING (frameworkcode)
247                       WHERE kohafield='permanent_location' OR
248                             kohafield='items.permanent_location'");
249 $sth->execute;
250 my $permanent_location_mapped = $sth->fetchall_arrayref({});
251 if (scalar(@$permanent_location_mapped) > 0) {
252     $total++;
253     $template->param(permanent_location_mapped => 1);
254     $template->param(mapped_permanent_location => $permanent_location_mapped);
255 }
256
257
258 $template->param(total => $total,
259                 );
260
261 output_html_with_http_headers $input, $cookie, $template->output;