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' },
39 my $dbh = C4::Context->dbh;
41 # checks itemnum field
42 my $sth = $dbh->prepare("select tab from marc_subfield_structure where kohafield=\"items.itemnumber\"");
44 while (my ($res) = $sth->fetchrow) {
46 $template->param(itemnum => 0);
48 $template->param(itemnum => 1);
54 # checks biblio.biblionumber and biblioitem.biblioitemnumber (same tag and tab=-1)
55 $sth = $dbh->prepare("select tagfield,tab,frameworkcode from marc_subfield_structure where kohafield=\"biblio.biblionumber\"");
59 while (my ($res,$tab,$frameworkcode) = $sth->fetchrow) {
64 if ($bibliotag != $res) {
65 $template->param(biblionumber => 1);
70 my $sth2 = $dbh->prepare("SELECT tagfield,tab
71 FROM marc_subfield_structure
72 WHERE kohafield=\"biblioitems.biblioitemnumber\"
73 AND frameworkcode = ? ");
74 $sth2->execute($frameworkcode);
75 my ($res2,$tab2) = $sth2->fetchrow;
76 if ($res && $res2 && $tab==-1 && $tab2==-1) {
77 $template->param(biblionumber => 0);
79 $template->param(biblionumber => 1);
85 # checks all item fields are in the same tag and in tab 10
87 $sth = $dbh->prepare("select tagfield,tab,kohafield from marc_subfield_structure where kohafield like \"items.%\" and tab >=0");
93 ($res,$res2,$field) = $sth->fetchrow;
97 #warn "TAGF : $tagfield";
98 while (($res,$res2,$field) = $sth->fetchrow) {
99 # (ignore itemnumber, that must be in -1 tab)
100 if (($res ne $tagfield) or ($res2 ne $tab)) {
104 $sth = $dbh->prepare("select kohafield from marc_subfield_structure where tagfield=?");
105 $sth->execute($tagfield);
106 while (($res2) = $sth->fetchrow) {
107 if (!$res2 || $res2 =~ /^items/) {
112 if ($subtotal == 0) {
113 $template->param(itemfields => 0);
115 $template->param(itemfields => 1);
119 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where tab = 10");
123 while (($res2) = $sth->fetchrow) {
127 if ($totaltags > 1) {
128 $template->param(itemtags => $list);
131 $template->param(itemtags => 0);
135 # checks biblioitems.itemtype must be mapped and use authorised_value=itemtype
136 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"biblioitems.itemtype\"");
138 while (($res,$res2,$field) = $sth->fetchrow) {
139 if ($res && $res2>=0 && $field eq "itemtypes") {
140 $template->param(itemtype => 0);
142 $template->param(itemtype => 1);
148 # checks items.homebranch must be mapped and use authorised_value=branches
149 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.homebranch\"");
151 while (($res,$res2,$field) = $sth->fetchrow) {
152 if ($res && $res2 eq 10 && $field eq "branches") {
153 $template->param(branch => 0);
155 $template->param(branch => 1);
161 # checks items.homebranch must be mapped and use authorised_value=branches
162 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.holdingbranch\"");
164 while (($res,$res2,$field) = $sth->fetchrow) {
165 if ($res && $res2 eq 10 && $field eq "branches") {
166 $template->param(holdingbranch => 0);
168 $template->param(holdingbranch => 1);
174 # checks that itemtypes & branches tables are not empty
175 $sth = $dbh->prepare("select count(*) from itemtypes");
177 ($res) = $sth->fetchrow;
179 $template->param(itemtypes_empty =>0);
181 $template->param(itemtypes_empty =>1);
186 $sth = $dbh->prepare("select count(*) from branches");
188 ($res) = $sth->fetchrow;
190 $template->param(branches_empty =>0);
192 $template->param(branches_empty =>1);
196 $sth = $dbh->prepare("select count(*) from marc_subfield_structure where frameworkcode is NULL");
198 ($res) = $sth->fetchrow;
200 $template->param(frameworknull =>1);
203 $sth = $dbh->prepare("select count(*) from marc_tag_structure where frameworkcode is NULL");
205 ($res) = $sth->fetchrow;
207 $template->param(frameworknull =>1);
211 # verify that all of a field's subfields (except the ones explicitly ignored)
212 # are in the same tab
213 $sth = $dbh->prepare("SELECT tagfield, frameworkcode, frameworktext, GROUP_CONCAT(DISTINCT tab) AS tabs
214 FROM marc_subfield_structure
215 LEFT JOIN biblio_framework USING (frameworkcode)
217 GROUP BY tagfield, frameworkcode, frameworktext
218 HAVING COUNT(DISTINCT tab) > 1");
220 my $inconsistent_tabs = $sth->fetchall_arrayref({});
221 if (scalar(@$inconsistent_tabs) > 0) {
223 $template->param(inconsistent_tabs => 1);
224 $template->param(tab_info => $inconsistent_tabs);
227 # verify that authtypecodes used in the framework
228 # are defined in auth_types
229 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield, authtypecode
230 FROM marc_subfield_structure
231 LEFT JOIN biblio_framework USING (frameworkcode)
232 WHERE authtypecode IS NOT NULL
233 AND authtypecode <> ''
235 AND authtypecode NOT IN (SELECT authtypecode FROM auth_types)
236 ORDER BY frameworkcode, tagfield, tagsubfield");
238 my $invalid_authtypecodes = $sth->fetchall_arrayref({});
239 if (scalar(@$invalid_authtypecodes) > 0) {
241 $template->param(invalid_authtypecodes => 1);
242 $template->param(authtypecode_info => $invalid_authtypecodes);
245 # checks items.permanent_location is not mapped
246 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield
247 FROM marc_subfield_structure
248 LEFT JOIN biblio_framework USING (frameworkcode)
249 WHERE kohafield='permanent_location' OR
250 kohafield='items.permanent_location'");
252 my $permanent_location_mapped = $sth->fetchall_arrayref({});
253 if (scalar(@$permanent_location_mapped) > 0) {
255 $template->param(permanent_location_mapped => 1);
256 $template->param(mapped_permanent_location => $permanent_location_mapped);
260 $template->param(total => $total,
263 output_html_with_http_headers $input, $cookie, $template->output;