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