Bug 13437: Trivial edits on marc21 plugins before conversion
[koha.git] / cataloguing / value_builder / marc21_field_008_authorities.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 use Koha::Util::FrameworkPlugin qw|date_entered|;
29
30 use constant FIXLEN_DATA_ELTS => '|| aca||aabn           | a|a     d';
31 use constant PREF_008 => 'MARCAuthorityControlField008';
32
33 sub plugin_javascript {
34     my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
35     my $function_name= $field_number;
36     my $dateentered = date_entered();
37     my $defaultval = substr( C4::Context->preference(PREF_008) || FIXLEN_DATA_ELTS, 0, 34 );
38     my $res="
39 <script type=\"text/javascript\">
40 //<![CDATA[
41
42 function Focus$function_name(subfield_managed) {
43     if (!document.getElementById(\"$field_number\").value) {
44     var authtype=document.forms['f'].elements['authtypecode'].value;
45     var fieldval='$dateentered$defaultval';
46     if(authtype && (authtype == 'TOPIC_TERM' || authtype == 'GENRE/FORM' || authtype == 'CHRON_TERM')) {
47       fieldval= fieldval.substr(0,14)+'b'+fieldval.substr(15);
48     }
49         document.getElementById(\"$field_number\").value=fieldval;
50     }
51     return 1;
52 }
53
54 function Clic$function_name(i) {
55     var authtype=document.forms['f'].elements['authtypecode'].value;
56     defaultvalue=document.getElementById(\"$field_number\").value;
57     newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=marc21_field_008_authorities.pl&index=$field_number&result=\"+defaultvalue+\"&authtypecode=\"+authtype,\"tag_editor\",'width=1000,height=600,toolbar=false,scrollbars=yes');
58
59 }
60 //]]>
61 </script>
62 ";
63
64     return ($function_name,$res);
65 }
66
67 sub plugin {
68     my ($input) = @_;
69     my $index= $input->param('index');
70     my $result= $input->param('result');
71     my $authtype= $input->param('authtypecode')||'';
72
73     my $defaultval = substr( C4::Context->preference(PREF_008) || FIXLEN_DATA_ELTS, 0, 34 );
74     substr($defaultval,14-6,1)='b' if $authtype=~ /TOPIC_TERM|GENRE.FORM|CHRON_TERM/;
75
76     my $dbh = C4::Context->dbh;
77
78     my ($template, $loggedinuser, $cookie)
79     = get_template_and_user({template_name => "cataloguing/value_builder/marc21_field_008_authorities.tt",
80                  query => $input,
81                  type => "intranet",
82                  authnotrequired => 0,
83                  flagsrequired => {editcatalogue => '*'},
84                  debug => 1,
85                  });
86     my $dateentered = date_entered();
87     $result = "$dateentered$defaultval" unless $result;
88     my @f;
89     for(0,6..17,28,29,31..33,38,39) {
90         $f[$_]=substr($result,$_,$_==0?6:1);
91     }
92     $template->param(index => $index);
93
94     $f[0]= $dateentered if !$f[0] || $f[0]=~/\s/;
95     $template->param(f1 => $f[0]);
96
97     for(6..17,28,29,31..33,38,39) {
98         $template->param(
99             "f$_" => $f[$_],
100             "f$_".($f[$_] eq '|'? 'pipe': $f[$_]) => $f[$_],
101         );
102     }
103     output_html_with_http_headers $input, $cookie, $template->output;
104 }