Bug 14199: Database revision for marc21_orgcode.pl
[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;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26
27 use C4::Search;
28 use C4::Output;
29
30 use XML::LibXML;
31 use Koha::Util::FrameworkPlugin qw|date_entered|;
32
33 my $builder = sub {
34     my ( $params ) = @_;
35
36     my $lang = C4::Context->preference('DefaultLanguageField008' );
37     $lang = "eng" unless $lang;
38     $lang = pack("A3", $lang);
39
40     my $function_name = $params->{id};
41     my $dateentered = date_entered();
42     my $res           = "
43 <script type=\"text/javascript\">
44 //<![CDATA[
45
46 function Focus$function_name(event) {
47     if ( document.getElementById(event.data.id).value ) {
48         }
49         else {
50         document.getElementById(event.data.id).value='$dateentered' + 'b        xxu||||| |||| 00| 0 $lang d';
51         }
52     return 1;
53 }
54
55 function Click$function_name(event) {
56     defaultvalue=document.getElementById(event.data.id).value;
57     //Retrieve full leader string and pass it to the 008 tag editor
58     var leader_value = \$(\"input[id^='tag_000']\").val();
59     var leader_parameter = \"\";
60     if (leader_value){
61         //Only add the parameter to the URL if there is a value to add
62         leader_parameter = \"&leader=\"+leader_value;
63     }
64     newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=marc21_field_008.pl&index=\"+ event.data.id +\"&result=\"+defaultvalue+leader_parameter,\"tag_editor\",'width=1000,height=600,toolbar=false,scrollbars=yes');
65
66 }
67 //]]>
68 </script>
69 ";
70
71     return $res;
72 };
73
74 my $launcher = sub {
75     my ( $params ) = @_;
76     my $input = $params->{cgi};
77     my $lang = C4::Context->preference('DefaultLanguageField008' );
78     $lang = "eng" unless $lang;
79     $lang = pack("A3", $lang);
80
81     my $index   = $input->param('index');
82     my $result  = $input->param('result');
83     my $leader  = $input->param('leader');
84
85     my $material_configuration;
86     if ($leader && length($leader) == '24') {
87         #MARC 21 Material Type Configuration
88         #Field 008/18-34 Configuration
89         #If Leader/06 = a and Leader/07 = a, c, d, or m: Books
90         #If Leader/06 = a and Leader/07 = b, i, or s: Continuing Resources
91         #If Leader/06 = t: Books
92         #If Leader/06 = c, d, i, or j: Music
93         #If Leader/06 = e, or f: Maps
94         #If Leader/06 = g, k, o, or r: Visual Materials
95         #If Leader/06 = m: Computer Files
96         #If Leader/06 = p: Mixed Materials
97         #http://www.loc.gov/marc/bibliographic/bdleader.html
98         my $material_configuration_mapping = {
99             a => {
100                 a => 'BKS',
101                 c => 'BKS',
102                 d => 'BKS',
103                 m => 'BKS',
104                 b => 'CR',
105                 i => 'CR',
106                 s => 'CR',
107             },
108             t => 'BKS',
109             c => 'MU',
110             d => 'MU',
111             i => 'MU',
112             j => 'MU',
113             e => 'MP',
114             f => 'MP',
115             g => 'VM',
116             k => 'VM',
117             o => 'VM',
118             r => 'VM',
119             m => 'CF',
120             p => 'MX',
121         };
122         my $leader06 = substr($leader, 6, 1);
123         my $leader07 = substr($leader, 7, 1);
124         #Retrieve material type using leader06
125         $material_configuration = $material_configuration_mapping->{$leader06};
126         #If the value returned is a ref (i.e. leader06 is 'a'), then use leader07 to get the actual material type
127         if ( ($material_configuration) && (ref($material_configuration) eq 'HASH') ){
128             $material_configuration = $material_configuration->{$leader07};
129         }
130     }
131
132     my $dbh = C4::Context->dbh;
133
134     my ($template, $loggedinuser, $cookie) = get_template_and_user(
135         {   template_name   => "cataloguing/value_builder/marc21_field_008.tt",
136             query           => $input,
137             type            => "intranet",
138             authnotrequired => 0,
139             flagsrequired   => { editcatalogue => '*' },
140             debug           => 1,
141         }
142     );
143
144     my $dateentered = date_entered();
145     $result = "$dateentered" . "b        xxu||||| |||| 00| 0 $lang d" unless $result;
146     my $errorXml = '';
147     # Check if the xml, xsd exists and is validated
148     my $dir = C4::Context->config('intrahtdocs') . '/prog/' . $template->{lang} . '/data/';
149     if (-r $dir . 'marc21_field_008.xml') {
150         my $doc = XML::LibXML->new->parse_file($dir . 'marc21_field_008.xml');
151         if (-r $dir . 'marc21_field_CF.xsd') {
152             my $xmlschema = XML::LibXML::Schema->new(location => $dir . 'marc21_field_CF.xsd');
153             eval {
154                 $xmlschema->validate( $doc );
155             };
156             $errorXml = 'Can\'t validate the xml data from ' . $dir . 'marc21_field_008.xml' if ($@);
157         }
158     } else {
159         $errorXml = 'Can\'t read the xml file ' . $dir . 'marc21_field_008.xml';
160     }
161     $template->param(tagfield => '008',
162             index => $index,
163             result => $result,
164             errorXml => $errorXml,
165             material_configuration => $material_configuration,
166     );
167     output_html_with_http_headers $input, $cookie, $template->output;
168 };
169
170 return { builder => $builder, launcher => $launcher };