Bug 13437: Preliminary changes for marc21 plugins field 008
[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 =head1 DESCRIPTION
34
35 plugin_parameters : other parameters added when the plugin is called by the dopop function
36
37 =cut
38
39 sub plugin_javascript {
40     my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
41     my $function_name= $field_number;
42     my $dateentered = date_entered();
43     my $defaultval = substr( C4::Context->preference(PREF_008) || FIXLEN_DATA_ELTS, 0, 34 );
44     my $res="
45 <script type=\"text/javascript\">
46 //<![CDATA[
47
48 function Focus$function_name(subfield_managed) {
49     if (!document.getElementById(\"$field_number\").value) {
50     var authtype=document.forms['f'].elements['authtypecode'].value;
51     var fieldval='$dateentered$defaultval';
52     if(authtype && (authtype == 'TOPIC_TERM' || authtype == 'GENRE/FORM' || authtype == 'CHRON_TERM')) {
53       fieldval= fieldval.substr(0,14)+'b'+fieldval.substr(15);
54     }
55         document.getElementById(\"$field_number\").value=fieldval;
56     }
57     return 1;
58 }
59
60 function Clic$function_name(i) {
61     var authtype=document.forms['f'].elements['authtypecode'].value;
62     defaultvalue=document.getElementById(\"$field_number\").value;
63     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');
64
65 }
66 //]]>
67 </script>
68 ";
69
70     return ($function_name,$res);
71 }
72 sub plugin {
73     my ($input) = @_;
74     my $index= $input->param('index');
75     my $result= $input->param('result');
76     my $authtype= $input->param('authtypecode')||'';
77
78     my $defaultval = substr( C4::Context->preference(PREF_008) || FIXLEN_DATA_ELTS, 0, 34 );
79     substr($defaultval,14-6,1)='b' if $authtype=~ /TOPIC_TERM|GENRE.FORM|CHRON_TERM/;
80
81     my $dbh = C4::Context->dbh;
82
83     my ($template, $loggedinuser, $cookie)
84     = get_template_and_user({template_name => "cataloguing/value_builder/marc21_field_008_authorities.tt",
85                  query => $input,
86                  type => "intranet",
87                  authnotrequired => 0,
88                  flagsrequired => {editcatalogue => '*'},
89                  debug => 1,
90                  });
91     my $dateentered = date_entered();
92     $result = "$dateentered$defaultval" unless $result;
93     my @f;
94     for(0,6..17,28,29,31..33,38,39) {
95         $f[$_]=substr($result,$_,$_==0?6:1);
96     }
97     $template->param(index => $index);
98
99     $f[0]= $dateentered if !$f[0] || $f[0]=~/\s/;
100     $template->param(f1 => $f[0]);
101
102     for(6..17,28,29,31..33,38,39) {
103         $template->param(
104             "f$_" => $f[$_],
105             "f$_".($f[$_] eq '|'? 'pipe': $f[$_]) => $f[$_],
106         );
107     }
108     output_html_with_http_headers $input, $cookie, $template->output;
109 }