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>.
31 my ($template, $borrowernumber, $cookie)
32 = get_template_and_user({template_name => "admin/checkmarc.tt",
35 flagsrequired => { parameters => 'manage_marc_frameworks' },
38 my $dbh = C4::Context->dbh;
40 # checks itemnum field
41 my $sth = $dbh->prepare("select tab from marc_subfield_structure where kohafield=\"items.itemnumber\"");
43 while (my ($res) = $sth->fetchrow) {
45 $template->param(itemnum => 0);
47 $template->param(itemnum => 1);
53 # checks biblio.biblionumber and biblioitem.biblioitemnumber (same tag and tab=-1)
54 $sth = $dbh->prepare("select tagfield,tab,frameworkcode from marc_subfield_structure where kohafield=\"biblio.biblionumber\"");
58 while (my ($res,$tab,$frameworkcode) = $sth->fetchrow) {
63 if ($bibliotag != $res) {
64 $template->param(biblionumber => 1);
69 my $sth2 = $dbh->prepare("SELECT tagfield,tab
70 FROM marc_subfield_structure
71 WHERE kohafield=\"biblioitems.biblioitemnumber\"
72 AND frameworkcode = ? ");
73 $sth2->execute($frameworkcode);
74 my ($res2,$tab2) = $sth2->fetchrow;
75 if ($res && $res2 && $tab==-1 && $tab2==-1) {
76 $template->param(biblionumber => 0);
78 $template->param(biblionumber => 1);
84 # checks all item fields are in the same tag and in tab 10
86 $sth = $dbh->prepare("select tagfield,tab,kohafield from marc_subfield_structure where kohafield like \"items.%\" and tab >=0");
92 ($res,$res2,$field) = $sth->fetchrow;
96 #warn "TAGF : $tagfield";
97 while (($res,$res2,$field) = $sth->fetchrow) {
98 # (ignore itemnumber, that must be in -1 tab)
99 if (($res ne $tagfield) or ($res2 ne $tab)) {
103 $sth = $dbh->prepare("select kohafield from marc_subfield_structure where tagfield=?");
104 $sth->execute($tagfield);
105 while (($res2) = $sth->fetchrow) {
106 if (!$res2 || $res2 =~ /^items/) {
111 if ($subtotal == 0) {
112 $template->param(itemfields => 0);
114 $template->param(itemfields => 1);
118 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where tab = 10");
122 while (($res2) = $sth->fetchrow) {
126 if ($totaltags > 1) {
127 $template->param(itemtags => $list);
130 $template->param(itemtags => 0);
134 # checks biblioitems.itemtype must be mapped and use authorised_value=itemtype
135 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"biblioitems.itemtype\"");
137 while (($res,$res2,$field) = $sth->fetchrow) {
138 if ($res && $res2>=0 && $field eq "itemtypes") {
139 $template->param(itemtype => 0);
141 $template->param(itemtype => 1);
147 # checks items.homebranch must be mapped and use authorised_value=branches
148 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.homebranch\"");
150 while (($res,$res2,$field) = $sth->fetchrow) {
151 if ($res && $res2 eq 10 && $field eq "branches") {
152 $template->param(branch => 0);
154 $template->param(branch => 1);
160 # checks items.homebranch must be mapped and use authorised_value=branches
161 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.holdingbranch\"");
163 while (($res,$res2,$field) = $sth->fetchrow) {
164 if ($res && $res2 eq 10 && $field eq "branches") {
165 $template->param(holdingbranch => 0);
167 $template->param(holdingbranch => 1);
173 # checks that itemtypes & branches tables are not empty
174 $sth = $dbh->prepare("select count(*) from itemtypes");
176 ($res) = $sth->fetchrow;
178 $template->param(itemtypes_empty =>0);
180 $template->param(itemtypes_empty =>1);
185 $sth = $dbh->prepare("select count(*) from branches");
187 ($res) = $sth->fetchrow;
189 $template->param(branches_empty =>0);
191 $template->param(branches_empty =>1);
195 $sth = $dbh->prepare("select count(*) from marc_subfield_structure where frameworkcode is NULL");
197 ($res) = $sth->fetchrow;
199 $template->param(frameworknull =>1);
202 $sth = $dbh->prepare("select count(*) from marc_tag_structure where frameworkcode is NULL");
204 ($res) = $sth->fetchrow;
206 $template->param(frameworknull =>1);
210 # verify that all of a field's subfields (except the ones explicitly ignored)
211 # are in the same tab
212 $sth = $dbh->prepare("SELECT tagfield, frameworkcode, frameworktext, GROUP_CONCAT(DISTINCT tab) AS tabs
213 FROM marc_subfield_structure
214 LEFT JOIN biblio_framework USING (frameworkcode)
216 GROUP BY tagfield, frameworkcode, frameworktext
217 HAVING COUNT(DISTINCT tab) > 1");
219 my $inconsistent_tabs = $sth->fetchall_arrayref({});
220 if (scalar(@$inconsistent_tabs) > 0) {
222 $template->param(inconsistent_tabs => 1);
223 $template->param(tab_info => $inconsistent_tabs);
226 # verify that authtypecodes used in the framework
227 # are defined in auth_types
228 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield, authtypecode
229 FROM marc_subfield_structure
230 LEFT JOIN biblio_framework USING (frameworkcode)
231 WHERE authtypecode IS NOT NULL
232 AND authtypecode <> ''
234 AND authtypecode NOT IN (SELECT authtypecode FROM auth_types)
235 ORDER BY frameworkcode, tagfield, tagsubfield");
237 my $invalid_authtypecodes = $sth->fetchall_arrayref({});
238 if (scalar(@$invalid_authtypecodes) > 0) {
240 $template->param(invalid_authtypecodes => 1);
241 $template->param(authtypecode_info => $invalid_authtypecodes);
244 # checks items.permanent_location is not mapped
245 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield
246 FROM marc_subfield_structure
247 LEFT JOIN biblio_framework USING (frameworkcode)
248 WHERE kohafield='permanent_location' OR
249 kohafield='items.permanent_location'");
251 my $permanent_location_mapped = $sth->fetchall_arrayref({});
252 if (scalar(@$permanent_location_mapped) > 0) {
254 $template->param(permanent_location_mapped => 1);
255 $template->param(mapped_permanent_location => $permanent_location_mapped);
259 $template->param(total => $total,
262 output_html_with_http_headers $input, $cookie, $template->output;