Bug 13437: Trivial edits on marc21 plugins before conversion
[koha.git] / cataloguing / value_builder / marc21_field_008.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25
26 use C4::Search;
27 use C4::Output;
28
29 use XML::LibXML;
30 use Koha::Util::FrameworkPlugin qw|date_entered|;
31
32 sub plugin_javascript {
33     my ($dbh, $record, $tagslib, $field_number, $tabloop) = @_;
34
35     my $lang = C4::Context->preference('DefaultLanguageField008' );
36     $lang = "eng" unless $lang;
37     $lang = pack("A3", $lang);
38
39     my $function_name = $field_number;
40     my $dateentered = date_entered();
41     my $res           = "
42 <script type=\"text/javascript\">
43 //<![CDATA[
44
45 function Focus$function_name(subfield_managed) {
46         if ( document.getElementById(\"$field_number\").value ) {
47         }
48         else {
49         document.getElementById(\"$field_number\").value='$dateentered' + 'b        xxu||||| |||| 00| 0 $lang d';
50         }
51     return 1;
52 }
53
54 function Clic$function_name(i) {
55         defaultvalue=document.getElementById(\"$field_number\").value;
56     //Retrieve full leader string and pass it to the 008 tag editor
57     var leader_value = \$(\"input[id^='tag_000']\").val();
58     var leader_parameter = \"\";
59     if (leader_value){
60         //Only add the parameter to the URL if there is a value to add
61         leader_parameter = \"&leader=\"+leader_value;
62     }
63     newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=marc21_field_008.pl&index=$field_number&result=\"+defaultvalue+leader_parameter,\"tag_editor\",'width=1000,height=600,toolbar=false,scrollbars=yes');
64
65 }
66 //]]>
67 </script>
68 ";
69
70     return ($function_name, $res);
71 }
72
73 sub plugin {
74     my ($input) = @_;
75     my $lang = C4::Context->preference('DefaultLanguageField008' );
76     $lang = "eng" unless $lang;
77     $lang = pack("A3", $lang);
78
79     my $index   = $input->param('index');
80     my $result  = $input->param('result');
81     my $leader  = $input->param('leader');
82
83     my $material_configuration;
84     if ($leader && length($leader) == '24') {
85         #MARC 21 Material Type Configuration
86         #Field 008/18-34 Configuration
87         #If Leader/06 = a and Leader/07 = a, c, d, or m: Books
88         #If Leader/06 = a and Leader/07 = b, i, or s: Continuing Resources
89         #If Leader/06 = t: Books
90         #If Leader/06 = c, d, i, or j: Music
91         #If Leader/06 = e, or f: Maps
92         #If Leader/06 = g, k, o, or r: Visual Materials
93         #If Leader/06 = m: Computer Files
94         #If Leader/06 = p: Mixed Materials
95         #http://www.loc.gov/marc/bibliographic/bdleader.html
96         my $material_configuration_mapping = {
97             a => {
98                 a => 'BKS',
99                 c => 'BKS',
100                 d => 'BKS',
101                 m => 'BKS',
102                 b => 'CR',
103                 i => 'CR',
104                 s => 'CR',
105             },
106             t => 'BKS',
107             c => 'MU',
108             d => 'MU',
109             i => 'MU',
110             j => 'MU',
111             e => 'MP',
112             f => 'MP',
113             g => 'VM',
114             k => 'VM',
115             o => 'VM',
116             r => 'VM',
117             m => 'CF',
118             p => 'MX',
119         };
120         my $leader06 = substr($leader, 6, 1);
121         my $leader07 = substr($leader, 7, 1);
122         #Retrieve material type using leader06
123         $material_configuration = $material_configuration_mapping->{$leader06};
124         #If the value returned is a ref (i.e. leader06 is 'a'), then use leader07 to get the actual material type
125         if ( ($material_configuration) && (ref($material_configuration) eq 'HASH') ){
126             $material_configuration = $material_configuration->{$leader07};
127         }
128     }
129
130     my $dbh = C4::Context->dbh;
131
132     my ($template, $loggedinuser, $cookie) = get_template_and_user(
133         {   template_name   => "cataloguing/value_builder/marc21_field_008.tt",
134             query           => $input,
135             type            => "intranet",
136             authnotrequired => 0,
137             flagsrequired   => { editcatalogue => '*' },
138             debug           => 1,
139         }
140     );
141
142     my $dateentered = date_entered();
143     $result = "$dateentered" . "b        xxu||||| |||| 00| 0 $lang d" unless $result;
144     my $errorXml = '';
145     # Check if the xml, xsd exists and is validated
146     my $dir = C4::Context->config('intrahtdocs') . '/prog/' . $template->{lang} . '/data/';
147     if (-r $dir . 'marc21_field_008.xml') {
148         my $doc = XML::LibXML->new->parse_file($dir . 'marc21_field_008.xml');
149         if (-r $dir . 'marc21_field_CF.xsd') {
150             my $xmlschema = XML::LibXML::Schema->new(location => $dir . 'marc21_field_CF.xsd');
151             eval {
152                 $xmlschema->validate( $doc );
153             };
154             $errorXml = 'Can\'t validate the xml data from ' . $dir . 'marc21_field_008.xml' if ($@);
155         }
156     } else {
157         $errorXml = 'Can\'t read the xml file ' . $dir . 'marc21_field_008.xml';
158     }
159     $template->param(tagfield => '008',
160             index => $index,
161             result => $result,
162             errorXml => $errorXml,
163             material_configuration => $material_configuration,
164     );
165     output_html_with_http_headers $input, $cookie, $template->output;
166 }