4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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>.
22 use C4::Output qw( output_html_with_http_headers );
23 use C4::Auth qw( get_template_and_user );
30 my ($template, $borrowernumber, $cookie)
31 = get_template_and_user({template_name => "admin/checkmarc.tt",
34 flagsrequired => { parameters => 'manage_marc_frameworks' },
37 my $dbh = C4::Context->dbh;
39 # checks itemnum field
40 my $sth = $dbh->prepare("select tab from marc_subfield_structure where kohafield=\"items.itemnumber\"");
42 while (my ($res) = $sth->fetchrow) {
44 $template->param(itemnum => 0);
46 $template->param(itemnum => 1);
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\"");
57 while (my ($res,$tab,$frameworkcode) = $sth->fetchrow) {
62 if ($bibliotag != $res) {
63 $template->param(biblionumber => 1);
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);
77 $template->param(biblionumber => 1);
83 # checks all item fields are in the same tag and in tab 10
85 $sth = $dbh->prepare("select tagfield,tab,kohafield from marc_subfield_structure where kohafield like \"items.%\" and tab >=0");
91 ($res,$res2,$field) = $sth->fetchrow;
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)) {
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/) {
110 if ($subtotal == 0) {
111 $template->param(itemfields => 0);
113 $template->param(itemfields => 1);
117 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where tab = 10");
121 while (($res2) = $sth->fetchrow) {
125 if ($totaltags > 1) {
126 $template->param(itemtags => $list);
129 $template->param(itemtags => 0);
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\"");
136 while (($res,$res2,$field) = $sth->fetchrow) {
137 if ($res && $res2>=0 && $field eq "itemtypes") {
138 $template->param(itemtype => 0);
140 $template->param(itemtype => 1);
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\"");
149 while (($res,$res2,$field) = $sth->fetchrow) {
150 if ($res && $res2 eq 10 && $field eq "branches") {
151 $template->param(branch => 0);
153 $template->param(branch => 1);
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\"");
162 while (($res,$res2,$field) = $sth->fetchrow) {
163 if ($res && $res2 eq 10 && $field eq "branches") {
164 $template->param(holdingbranch => 0);
166 $template->param(holdingbranch => 1);
172 # checks that itemtypes & branches tables are not empty
173 $sth = $dbh->prepare("select count(*) from itemtypes");
175 ($res) = $sth->fetchrow;
177 $template->param(itemtypes_empty =>0);
179 $template->param(itemtypes_empty =>1);
184 $sth = $dbh->prepare("select count(*) from branches");
186 ($res) = $sth->fetchrow;
188 $template->param(branches_empty =>0);
190 $template->param(branches_empty =>1);
194 $sth = $dbh->prepare("select count(*) from marc_subfield_structure where frameworkcode is NULL");
196 ($res) = $sth->fetchrow;
198 $template->param(frameworknull =>1);
201 $sth = $dbh->prepare("select count(*) from marc_tag_structure where frameworkcode is NULL");
203 ($res) = $sth->fetchrow;
205 $template->param(frameworknull =>1);
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)
215 GROUP BY tagfield, frameworkcode, frameworktext
216 HAVING COUNT(DISTINCT tab) > 1");
218 my $inconsistent_tabs = $sth->fetchall_arrayref({});
219 if (scalar(@$inconsistent_tabs) > 0) {
221 $template->param(inconsistent_tabs => 1);
222 $template->param(tab_info => $inconsistent_tabs);
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 <> ''
233 AND authtypecode NOT IN (SELECT authtypecode FROM auth_types)
234 ORDER BY frameworkcode, tagfield, tagsubfield");
236 my $invalid_authtypecodes = $sth->fetchall_arrayref({});
237 if (scalar(@$invalid_authtypecodes) > 0) {
239 $template->param(invalid_authtypecodes => 1);
240 $template->param(authtypecode_info => $invalid_authtypecodes);
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'");
250 my $permanent_location_mapped = $sth->fetchall_arrayref({});
251 if (scalar(@$permanent_location_mapped) > 0) {
253 $template->param(permanent_location_mapped => 1);
254 $template->param(mapped_permanent_location => $permanent_location_mapped);
258 $template->param(total => $total,
261 output_html_with_http_headers $input, $cookie, $template->output;