Bug 10480: Remove useless routines and irrelevant pod lines
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
31 =head1 DESCRIPTION
32
33 plugin_parameters : other parameters added when the plugin is called by the dopop function
34
35 =cut
36
37 # find today's date
38 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
39
40 $year += 1900;
41 $mon  += 1;
42 my $dateentered = substr($year, 2, 2) . sprintf("%0.2d", $mon) . sprintf("%0.2d", $mday);
43
44 sub plugin_javascript {
45     my $lang = C4::Context->preference('DefaultLanguageField008' );
46     $lang = "eng" unless $lang;
47     $lang = pack("A3", $lang);
48
49     my ($dbh, $record, $tagslib, $field_number, $tabloop) = @_;
50     my $function_name = $field_number;
51     my $res           = "
52 <script type=\"text/javascript\">
53 //<![CDATA[
54
55 function Focus$function_name(subfield_managed) {
56         if ( document.getElementById(\"$field_number\").value ) {
57         }
58         else {
59         document.getElementById(\"$field_number\").value='$dateentered' + 'b        xxu||||| |||| 00| 0 $lang d';
60         }
61     return 1;
62 }
63
64 function Clic$function_name(i) {
65         defaultvalue=document.getElementById(\"$field_number\").value;
66     //Retrieve full leader string and pass it to the 008 tag editor
67     var leader_value = \$(\"input[id^='tag_000']\").val();
68     var leader_parameter = \"\";
69     if (leader_value){
70         //Only add the parameter to the URL if there is a value to add
71         leader_parameter = \"&leader=\"+leader_value;
72     }
73     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');
74
75 }
76 //]]>
77 </script>
78 ";
79
80     return ($function_name, $res);
81 }
82
83 sub plugin {
84     my $lang = C4::Context->preference('DefaultLanguageField008' );
85     $lang = "eng" unless $lang;
86     $lang = pack("A3", $lang);
87
88     my ($input) = @_;
89     my $index   = $input->param('index');
90     my $result  = $input->param('result');
91     my $leader  = $input->param('leader');
92
93     my $material_configuration;
94     if ($leader && length($leader) == '24') {
95         #MARC 21 Material Type Configuration
96         #Field 008/18-34 Configuration
97         #If Leader/06 = a and Leader/07 = a, c, d, or m: Books
98         #If Leader/06 = a and Leader/07 = b, i, or s: Continuing Resources
99         #If Leader/06 = t: Books
100         #If Leader/06 = c, d, i, or j: Music
101         #If Leader/06 = e, or f: Maps
102         #If Leader/06 = g, k, o, or r: Visual Materials
103         #If Leader/06 = m: Computer Files
104         #If Leader/06 = p: Mixed Materials
105         #http://www.loc.gov/marc/bibliographic/bdleader.html
106         my $material_configuration_mapping = {
107             a => {
108                 a => 'BKS',
109                 c => 'BKS',
110                 d => 'BKS',
111                 m => 'BKS',
112                 b => 'CR',
113                 i => 'CR',
114                 s => 'CR',
115             },
116             t => 'BKS',
117             c => 'MU',
118             d => 'MU',
119             i => 'MU',
120             j => 'MU',
121             e => 'MP',
122             f => 'MP',
123             g => 'VM',
124             k => 'VM',
125             o => 'VM',
126             r => 'VM',
127             m => 'CF',
128             p => 'MX',
129         };
130         my $leader06 = substr($leader, 6, 1);
131         my $leader07 = substr($leader, 7, 1);
132         #Retrieve material type using leader06
133         $material_configuration = $material_configuration_mapping->{$leader06};
134         #If the value returned is a ref (i.e. leader06 is 'a'), then use leader07 to get the actual material type
135         if ( ($material_configuration) && (ref($material_configuration) eq 'HASH') ){
136             $material_configuration = $material_configuration->{$leader07};
137         }
138     }
139
140     my $dbh = C4::Context->dbh;
141
142     my ($template, $loggedinuser, $cookie) = get_template_and_user(
143         {   template_name   => "cataloguing/value_builder/marc21_field_008.tt",
144             query           => $input,
145             type            => "intranet",
146             authnotrequired => 0,
147             flagsrequired   => { editcatalogue => '*' },
148             debug           => 1,
149         }
150     );
151
152     $result = "$dateentered" . "b        xxu||||| |||| 00| 0 $lang d" unless $result;
153     my $errorXml = '';
154     # Check if the xml, xsd exists and is validated
155     my $dir = C4::Context->config('intrahtdocs') . '/prog/' . $template->{lang} . '/data/';
156     if (-r $dir . 'marc21_field_008.xml') {
157         my $doc = XML::LibXML->new->parse_file($dir . 'marc21_field_008.xml');
158         if (-r $dir . 'marc21_field_CF.xsd') {
159             my $xmlschema = XML::LibXML::Schema->new(location => $dir . 'marc21_field_CF.xsd');
160             eval {
161                 $xmlschema->validate( $doc );
162             };
163             $errorXml = 'Can\'t validate the xml data from ' . $dir . 'marc21_field_008.xml' if ($@);
164         }
165     } else {
166         $errorXml = 'Can\'t read the xml file ' . $dir . 'marc21_field_008.xml';
167     }
168     $template->param(tagfield => '008',
169             index => $index,
170             result => $result,
171             errorXml => $errorXml,
172             material_configuration => $material_configuration,
173     );
174     output_html_with_http_headers $input, $cookie, $template->output;
175 }