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